00001
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 package gov.lanl.isr.sensix.discovery;
00036
00037 import java.util.*;
00038 import org.omg.CORBA.*;
00039 import com.sun.corba.se.impl.orbutil.*;
00040 import gov.lanl.isr.sensix.Logger;
00041
00042
00043 public class DiscoveryServer
00044 {
00045 public static final String DISCOVERY_SERVER_ID =
00046 Integer.toString(Integer.parseInt(ORBConstants.NAME_SERVICE_SERVER_ID) +
00047 DiscoveryService.DISCOVERY_PORT);
00048 public static final String appname = "DiscoveryService";
00049
00050
00051 private class shutdownHook extends Thread
00052 {
00053 private DiscoveryService discover;
00054
00055 public shutdownHook(DiscoveryService ds) {
00056 super();
00057 discover = ds;
00058 }
00059
00060 public void run() {
00061 discover.shutdown();
00062 System.err.println(DiscoveryServer.appname + ": Shutdown");
00063 System.exit(0);
00064 }
00065 }
00066
00067 public DiscoveryServer(ORB orb, int id, byte level, byte debug_level,
00068 int service_port) {
00069 DiscoveryService ds = null;
00070 Logger logger = new Logger(DiscoveryServer.appname, debug_level);
00071 logger.log(Logger.LOG.INFO, "Discovery Service started: node ID " +
00072 id + " at level " + level);
00073 logger.log(Logger.LOG.DEBUG, "ORB object from init: " + orb);
00074
00075 try {
00076 ds = new DiscoveryService(orb, id, level, DiscoveryServer.appname,
00077 debug_level, service_port);
00078
00079 org.omg.CORBA.Object serv = (org.omg.CORBA.Object)ds;
00080 com.sun.corba.se.org.omg.CORBA.ORB sun_orb =
00081 (com.sun.corba.se.org.omg.CORBA.ORB)orb;
00082 sun_orb.register_initial_reference("DiscoveryService", serv);
00083 }
00084 catch (org.omg.CORBA.ORBPackage.InvalidName e) {
00085 logger.log(Logger.LOG.ERROR,
00086 "DiscoveryService is unknown to the ORB");
00087 System.exit(1);
00088 }
00089 catch (org.omg.CORBA.SystemException e) {
00090 logger.log(Logger.LOG.ERROR, "Discovery server system exception");
00091 e.printStackTrace();
00092 System.exit(1);
00093 }
00094 catch (org.omg.CORBA.UserException e) {
00095 logger.log(Logger.LOG.ERROR, "Discovery server user exception");
00096 e.printStackTrace();
00097 System.exit(1);
00098 }
00099
00100 Thread sd_thread = new shutdownHook(ds);
00101 Runtime.getRuntime().addShutdownHook(sd_thread);
00102
00103 java.lang.Object sync = new java.lang.Object();
00104 try {
00105 synchronized (sync) { sync.wait(); }
00106 }
00107 catch (InterruptedException e) {}
00108 }
00109
00110
00111 public static void usage() {
00112 System.err.println(DiscoveryServer.appname +
00113 " usage: java -jar discovery.jar " +
00114 "[-level h_level] [-id node_id] " +
00115 "<-debug> <-info> <-help>");
00116 System.err.println(" where:");
00117 System.err.println(" 'h_level' (required) represents this " +
00118 "node's position in the network hierarchy " +
00119 "in [0 - 255]");
00120 System.err.println(" node_id (required) is a unique unsigned " +
00121 "integer identifier for this node " +
00122 "in [0 - 4294967295]");
00123 System.err.println(" '-debug' increases the verbosity of output");
00124 System.err.println(" '-info' slightly increases the output");
00125 System.err.println(" '-help' produces this output and exits");
00126 }
00127
00128
00129
00130 public static void main(String args[])
00131 {
00132 int service_port = 0;
00133 byte level = 0;
00134 boolean level_set = false;
00135 int id = 0;
00136 boolean id_set = false;
00137 byte debug_level = 0;
00138
00139 for (int i = 0; i < args.length; i++) {
00140 String argument = args[i];
00141 int idx = args[i].lastIndexOf("-");
00142 if (idx > 0)
00143 argument = args[i].substring(idx);
00144
00145 if (argument.equalsIgnoreCase("debug"))
00146 debug_level = 2;
00147 else if (argument.equalsIgnoreCase("info"))
00148 debug_level = 1;
00149 else if (argument.equalsIgnoreCase("level")) {
00150 i++;
00151 if (i < args.length) {
00152 try {
00153 level = (new Integer(args[i])).byteValue();
00154 level_set = true;
00155 } catch (NumberFormatException e) {
00156 System.err.println(DiscoveryServer.appname +
00157 ": Bad level argument: " + args[i]);
00158 level_set = false;
00159 }
00160 }
00161 }
00162 else if (argument.equalsIgnoreCase("id")) {
00163 i++;
00164 if (i < args.length) {
00165 try {
00166 id = Integer.parseInt(args[i]);
00167 id_set = true;
00168 } catch (NumberFormatException e) {
00169 System.err.println(DiscoveryServer.appname +
00170 ": Bad id argument: " + args[i]);
00171 id_set = false;
00172 }
00173 }
00174 }
00175 else if (argument.equalsIgnoreCase("help")) {
00176 usage();
00177 System.exit(0);
00178 }
00179 }
00180 if (level_set == false || id_set == false) {
00181 usage();
00182 System.exit(1);
00183 }
00184
00185 Properties props = System.getProperties();
00186 props.put(ORBConstants.SERVER_ID_PROPERTY, DISCOVERY_SERVER_ID);
00187 props.put("org.omg.CORBA.ORBClass",
00188 "com.sun.corba.se.impl.orb.ORBImpl");
00189
00190 try {
00191 String port =
00192 System.getProperty(ORBConstants.INITIAL_PORT_PROPERTY);
00193 if (port != null && port.length() > 0)
00194 service_port = Integer.parseInt(port);
00195
00196 for (int i = 0; i < args.length; i++) {
00197 if (args[i].equals("-ORBInitialPort")) {
00198 i++;
00199 if (i < args.length)
00200 service_port = Integer.parseInt(args[i]);
00201 }
00202 }
00203 }
00204 catch (NumberFormatException e) {}
00205
00206 if (service_port == 0) {
00207 service_port = DiscoveryService.DISCOVERY_PORT;
00208 System.err.println(DiscoveryServer.appname +
00209 ": using default discovery port " +
00210 service_port);
00211 }
00212
00213 props.put(ORBConstants.INITIAL_PORT_PROPERTY,
00214 Integer.toString(service_port));
00215 props.put(ORBConstants.PERSISTENT_SERVER_PORT_PROPERTY,
00216 Integer.toString(service_port));
00217
00218 ORB orb = ORB.init(args, props);
00219 DiscoveryServer serv = new DiscoveryServer(orb, id, level, debug_level,
00220 service_port);
00221 }
00222 }