import org.omg.CORBA.*; import org.omg.PortableServer.*; import org.omg.CosNaming.*; import org.omg.CosNaming.NamingContextPackage.*; import java.io.*; import dbg.*; public class Server { public static final String SERVER_NAME = "Bondesjakk-server"; public static String serverName = SERVER_NAME; static NameComponent[] aName = null; static NamingContext nc = null; public static void main(String args[]) { System.out.println("Starting server..."); java.util.Properties props = System.getProperties(); props.put("org.omg.CORBA.ORBClass", "com.ooc.CORBA.ORB"); props.put("org.omg.CORBA.ORBSingletonClass", "com.ooc.CORBA.ORBSingleton"); int status = 0; ORB orb = null; // Add a shutdown hook to catch Ctrl-C (SIGALRM) signals try{ Runtime.getRuntime().addShutdownHook( new Thread(){ public void run(){ System.out.println("\nShutting down."); unbindName(); } }); }catch(Exception e){ System.err.println("Error adding shutdown hook to catch Ctrl-C"); } try { // // Create ORB // orb = ORB.init(args, props); status = run(orb, args); } catch(RuntimeException ex) { ex.printStackTrace(); status = 1; } if(orb != null) { try { ((com.ooc.CORBA.ORB)orb).destroy(); } catch(Exception ex) { System.err.println(ex.getMessage()); ex.printStackTrace(); status = 1; } } System.exit(status); } static void unbindName(){ System.out.println("Unregistering server \""+ aName[0].id + "\" from name service."); try{ nc.unbind(aName); }catch (UserException ue){ System.out.println("Error unregistering from nameservice: "); System.out.println(ue.getMessage()); } } static void printUsage(){ System.out.println("Usage: serverstart "+ "[-Nnamingservice] "+ "[-Sgameservername] "); } static int run(ORB orb, String args[]) { // // Parse command line arguments // String namingFile = null; int argc = args.length; for (int i=0; i < argc; i++){ // System.out.println("argv["+i+"] = "+args[i]); if (args[i].startsWith("-S")){ if (args[i].length() == 2 ){ // "-S servername" i++; serverName = args[i].trim(); }else{ // "-Sservername serverName = args[i].trim().substring(2); } } else if (args[i].startsWith("-h") || args[i].startsWith("--h") || args[i].startsWith("-H") ){ printUsage(); System.exit(0); } } // // Resolve Root POA // org.omg.CORBA.Object poaObj = null; try { poaObj = orb.resolve_initial_references("RootPOA"); } catch(org.omg.CORBA.ORBPackage.InvalidName ex) { throw new RuntimeException(); } POA rootPOA = POAHelper.narrow(poaObj); // // Get a reference to the POA manager // POAManager manager = rootPOA.the_POAManager(); com.ooc.OBPortableServer.POAManager obManager = com.ooc.OBPortableServer.POAManagerHelper.narrow(manager); // Create new instance of servant GameserverImpl gameserverImpl = new GameserverImpl(orb,"Gameserver working!!!"); // Perform implicit activation. Gameserver gameserver = gameserverImpl._this(orb); // // Get naming service // org.omg.CORBA.Object obj = null; //System.out.println("here is naming file :"+ namingFile); //if(namingFile != null) //{ // try // { // FileReader r = new FileReader(namingFile); // BufferedReader in = new BufferedReader(r); // String ref = in.readLine(); // r.close(); // obj = orb.string_to_object(ref); // } // catch(IOException e) // { // e.printStackTrace(); // return 1; // } // } //else // { try { obj = orb.resolve_initial_references("NameService"); } catch(org.omg.CORBA.ORBPackage.InvalidName ex) { System.out.println("Can't resolve `NameService'"); return 1; } //} if(obj == null) { System.out.println("`NameService' is a nil object reference"); return 1; } nc = null; try { nc = NamingContextHelper.narrow(obj); } catch(org.omg.CORBA.BAD_PARAM ex) { System.out.println("`NameService' is not " + "a NamingContext object reference"); return 1; } try { // // Bind names with the Naming Service // aName = new NameComponent[1]; aName[0] = new NameComponent(); aName[0].id = serverName; aName[0].kind = ""; nc.bind(aName, gameserver); // // Run implementation // try { // Listen for requests manager.activate(); } catch(org.omg.PortableServer.POAManagerPackage.AdapterInactive ex) { throw new RuntimeException(); } orb.run(); // // Unregister names with the Naming Service // nc.unbind(aName); } catch(NotFound ex) { System.err.print("Got a `NotFound' exception ("); switch(ex.why.value()) { case NotFoundReason._missing_node: System.err.print("missing node"); break; case NotFoundReason._not_context: System.err.print("not context"); break; case NotFoundReason._not_object: System.err.print("not object"); break; } System.err.println(")"); ex.printStackTrace(); return 1; } catch(CannotProceed ex) { System.err.println("Got a `CannotProceed' exception"); ex.printStackTrace(); return 1; } catch(InvalidName ex) { System.err.println("Got an `InvalidName' exception"); ex.printStackTrace(); return 1; } catch(AlreadyBound ex) { System.err.println("Got an `AlreadyBound' exception"); ex.printStackTrace(); return 1; } return 0; } private static void usage() { System.out.println( "Usage: java naming.Server [options]\n\n" + "Options:\n" + "-f FILE Read the NamingService IOR from file FILE.\n" + "-h, --help Display this help message."); System.exit(1); } }