import org.omg.CosNaming.*; import org.omg.CosNaming.NamingContextPackage.*; import org.omg.CORBA.*; import org.omg.PortableServer.*; public class hrserver extends horseracePOA { private horse[] H; private int HN = 3; // number of horses private int clients = 0; private int countup = 0; private int countdown = 0; public int LW = 60; // lane width public int TW = 800; // track length; private boolean stoprace = false; public hrserver(int x) { super(); HN = x; H = new horse[HN]; } public synchronized horse[] joinrace(String name, ByteHolder id) { try { if (clients >= HN) return null; id.value = (byte)clients; H[clients] = new horse(true,0,(clients*LW)+LW,name); clients++; if (clients==HN) notifyAll(); else wait(); } catch (Exception e) {e.printStackTrace();} return H; } public synchronized horse[] quitrace (byte id) { H[id].inrace = false; return H; } public synchronized horse[] moveForward(byte id, int step) { try { while (countdown > 0) wait(); // round not yet finished countup++; if (countup < clients) wait(); else { countdown = clients; notifyAll(); } if (stoprace) { H[id].inrace = false; return H; } H[id].x += step; if (H[id].x > TW-68) stoprace = true; countdown--; if (countdown == 0) { countup = 0; notifyAll(); } } catch (Exception e) {e.printStackTrace();} return H; } public static void main(String[] args) // args define HN and orbd host { try { // initialize local ORB String[] options = new String[4]; options[0] = "-ORBInitialHost"; options[1] = args[1]; options[2] = "-ORBInitialPort"; options[3] = "10000"; ORB myorb = ORB.init(options,null); // initialize portable Object Adapter, glue to ORB POA rootpoa = (POA)myorb.resolve_initial_references("RootPOA"); rootpoa.the_POAManager().activate(); // create instance of server object, call POA to convert to objref int HN = Integer.parseInt(args[0]); hrserver theserver = new hrserver(HN); // HN used here org.omg.CORBA.Object objref = rootpoa.servant_to_reference(theserver); System.out.println(myorb.object_to_string(objref)); // at this point, objref is CORBA reference to server object // Register objref with name service (similar to registry) org.omg.CORBA.Object nsobjref = myorb.resolve_initial_references("NameService"); //narrowing = CORBA type casting: NamingContext ncref = NamingContextHelper.narrow(nsobjref); NameComponent[] path = { new NameComponent("hrserver","Object") }; ncref.rebind(path,objref); // actual registration // now wait for client invocation System.out.println("waiting for client call..."); myorb.run(); } catch (Exception e) {e.printStackTrace();} } // main } // hrserver