jdk/src/share/native/sun/nio/ch/genSocketOptionRegistry.c
changeset 1152 29d6145d1097
child 1247 b4c26443dee5
equal deleted inserted replaced
1151:4070cecdb99d 1152:29d6145d1097
       
     1 /*
       
     2  * Copyright 2007 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Sun designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Sun in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    23  * have any questions.
       
    24  */
       
    25 
       
    26 #include <stdio.h>
       
    27 #ifdef _WIN32
       
    28 #include <winsock2.h>
       
    29 #include <ws2tcpip.h>
       
    30 #else
       
    31 #include <sys/socket.h>
       
    32 #include <netinet/in.h>
       
    33 #include <netinet/tcp.h>
       
    34 #endif
       
    35 
       
    36 /**
       
    37  * Generates sun.nio.ch.SocketOptionRegistry, a class that maps Java-level
       
    38  * socket options to the platform specific level and option.
       
    39  */
       
    40 
       
    41 static void out(char* s) {
       
    42     printf("%s\n", s);
       
    43 }
       
    44 
       
    45 static void emit(const char *name, char * family, int level, int optname) {
       
    46     printf("            map.put(new RegistryKey(%s, %s),", name, family);
       
    47     printf(" new OptionKey(%d, %d));\n", level, optname);
       
    48 }
       
    49 
       
    50 static void emit_unspec(const char *name, int level, int optname) {
       
    51     emit(name, "Net.UNSPEC", level, optname);
       
    52 }
       
    53 
       
    54 static  void emit_inet(const char *name, int level, int optname) {
       
    55     emit(name, "StandardProtocolFamily.INET", level, optname);
       
    56 }
       
    57 
       
    58 static void emit_inet6(const char *name, int level, int optname) {
       
    59     emit(name, "StandardProtocolFamily.INET6", level, optname);
       
    60 }
       
    61 
       
    62 int main(int argc, const char* argv[]) {
       
    63     out("// AUTOMATICALLY GENERATED FILE - DO NOT EDIT                                  ");
       
    64     out("package sun.nio.ch;                                                            ");
       
    65     out("import java.net.SocketOption;                                                  ");
       
    66     out("import java.net.StandardSocketOption;                                          ");
       
    67     out("import java.net.ProtocolFamily;                                                ");
       
    68     out("import java.net.StandardProtocolFamily;                                        ");
       
    69     out("import java.util.Map;                                                          ");
       
    70     out("import java.util.HashMap;                                                      ");
       
    71     out("class SocketOptionRegistry {                                                   ");
       
    72     out("    private SocketOptionRegistry() { }                                         ");
       
    73     out("    private static class RegistryKey {                                         ");
       
    74     out("        private final SocketOption name;                                       ");
       
    75     out("        private final ProtocolFamily family;                                   ");
       
    76     out("        RegistryKey(SocketOption name, ProtocolFamily family) {                ");
       
    77     out("            this.name = name;                                                  ");
       
    78     out("            this.family = family;                                              ");
       
    79     out("        }                                                                      ");
       
    80     out("        public int hashCode() {                                                ");
       
    81     out("            return name.hashCode() + family.hashCode();                        ");
       
    82     out("        }                                                                      ");
       
    83     out("        public boolean equals(Object ob) {                                     ");
       
    84     out("            if (ob == null) return false;                                      ");
       
    85     out("            if (!(ob instanceof RegistryKey)) return false;                    ");
       
    86     out("            RegistryKey other = (RegistryKey)ob;                               ");
       
    87     out("            if (this.name != other.name) return false;                         ");
       
    88     out("            if (this.family != other.family) return false;                     ");
       
    89     out("            return true;                                                       ");
       
    90     out("        }                                                                      ");
       
    91     out("    }                                                                          ");
       
    92     out("    private static class LazyInitialization {                                  ");
       
    93     out("        static final Map<RegistryKey,OptionKey> options = options();           ");
       
    94     out("        private static Map<RegistryKey,OptionKey> options() {                  ");
       
    95     out("            Map<RegistryKey,OptionKey> map =                                   ");
       
    96     out("                new HashMap<RegistryKey,OptionKey>();                          ");
       
    97 
       
    98     emit_unspec("StandardSocketOption.SO_BROADCAST", SOL_SOCKET, SO_BROADCAST);
       
    99     emit_unspec("StandardSocketOption.SO_KEEPALIVE", SOL_SOCKET, SO_KEEPALIVE);
       
   100     emit_unspec("StandardSocketOption.SO_LINGER",    SOL_SOCKET, SO_LINGER);
       
   101     emit_unspec("StandardSocketOption.SO_SNDBUF",    SOL_SOCKET, SO_SNDBUF);
       
   102     emit_unspec("StandardSocketOption.SO_RCVBUF",    SOL_SOCKET, SO_RCVBUF);
       
   103     emit_unspec("StandardSocketOption.SO_REUSEADDR", SOL_SOCKET, SO_REUSEADDR);
       
   104     emit_unspec("StandardSocketOption.TCP_NODELAY",  IPPROTO_TCP, TCP_NODELAY);
       
   105 
       
   106     emit_inet("StandardSocketOption.IP_TOS",            IPPROTO_IP,     IP_TOS);
       
   107     emit_inet("StandardSocketOption.IP_MULTICAST_IF",   IPPROTO_IP,     IP_MULTICAST_IF);
       
   108     emit_inet("StandardSocketOption.IP_MULTICAST_TTL",  IPPROTO_IP,     IP_MULTICAST_TTL);
       
   109     emit_inet("StandardSocketOption.IP_MULTICAST_LOOP", IPPROTO_IP,     IP_MULTICAST_LOOP);
       
   110 
       
   111 #ifdef AF_INET6
       
   112     emit_inet6("StandardSocketOption.IP_MULTICAST_IF",   IPPROTO_IPV6,  IPV6_MULTICAST_IF);
       
   113     emit_inet6("StandardSocketOption.IP_MULTICAST_TTL",  IPPROTO_IPV6,  IPV6_MULTICAST_HOPS);
       
   114     emit_inet6("StandardSocketOption.IP_MULTICAST_LOOP", IPPROTO_IPV6,  IPV6_MULTICAST_LOOP);
       
   115 #endif
       
   116 
       
   117     emit_unspec("ExtendedSocketOption.SO_OOBINLINE", SOL_SOCKET, SO_OOBINLINE);
       
   118 
       
   119     out("            return map;                                                        ");
       
   120     out("        }                                                                      ");
       
   121     out("    }                                                                          ");
       
   122     out("    public static OptionKey findOption(SocketOption name, ProtocolFamily family) { ");
       
   123     out("        RegistryKey key = new RegistryKey(name, family);                       ");
       
   124     out("        return LazyInitialization.options.get(key);                            ");
       
   125     out("    }                                                                          ");
       
   126     out("}                                                                              ");
       
   127 
       
   128     return 0;
       
   129 }