import java.util.*; import java.net.*; import java.io.*; // This is a simple registry server. // It does not do any error checking or bound checking. // It uses the ROR as specified in RemoteObjectRef.java. // Requests processed: // (1) lookup (from SimpleRegistry.java) --> returns ROR (one // component at a time to the lookup requesting client. // (2) rebind (from SimpleRegistry.java) --> binds ROR. // (3) "who are you?" (from LocateSimpleResgistry.java) --> // "I am simple registry server." // This program does no checking. // It should be run with one argument specifying the port // it will be listening on, e.g., // java SimpleRegistryServer 2222 public class SimpleRegistryServer { public static void main(String args[]) throws IOException { int port = Integer.parseInt(args[0]); // Create a server socket to receive requests. ServerSocket serverSoc = new ServerSocket(port); System.out.println("Server socket created.\n"); // Create a table to store (service name, ROR) pairs. Hashtable table = new Hashtable(); // Loop: accept, receive request, reply, close. // It does not reuse connection. // Moreover, there is no concurrency, poor programming practice :-) while (true) { // Create a new socket connection. Socket newsoc = serverSoc.accept(); System.out.println("Accepted the request."); // Create input/output streams on the socket (TCP is bidirectional). BufferedReader in = new BufferedReader(new InputStreamReader (newsoc.getInputStream())); PrintWriter out = new PrintWriter(newsoc.getOutputStream(), true); // Get a line, which should be one of the following: // (1) lookup servicename --> ["found", ROR] or ["not found"] // (2) rebind servicename ROR --> ["bound"] // (3) "who are you?" --> "I am a simple registry server." String command = in.readLine(); // Branch on command (=request) if (command.equals("lookup")) { System.out.println("It's a lookup request."); String serviceName = in.readLine(); System.out.println("Service name is "+serviceName+"."); // Test if serviceName is in the hash table. // If it is, get the corresponding ROR. if (table.containsKey(serviceName)) { System.out.println("Service name found."); RemoteObjectRef ror= (RemoteObjectRef) table.get(serviceName); System.out.println ("ROR is "+ ror.IP_adr+","+ ror.Port+","+ ror.Obj_Key+","+ ror.Remote_Interface_Name+"."); out.println("found"); out.println(ror.IP_adr); out.println(Integer.toString(ror.Port)); out.println(Integer.toString(ror.Obj_Key)); out.println(ror.Remote_Interface_Name); System.out.println("ROR was sent.\n"); } else { System.out.println("Service not found.\n"); out.println("not found"); } } else if (command.equals("rebind")) { System.out.println("It's a rebind request."); // No error check here. String serviceName = in.readLine(); System.out.println("Service name is "+serviceName+"."); // Receive ROR data from client. // Serialisation not used. System.out.println("SimpleRegistryServer received the following ror:"); String IP_adr = in.readLine(); int Port = Integer.parseInt(in.readLine()); int Obj_Key = Integer.parseInt(in.readLine()); String Remote_Interface_Name = in.readLine(); System.out.println("IP address= "+IP_adr); System.out.println("port num= "+Port); System.out.println("object key= "+Obj_Key); System.out.println("Interface Name= "+Remote_Interface_Name); // Construct an ROR to be bound. RemoteObjectRef ror= new RemoteObjectRef(IP_adr, Port, Obj_Key, Remote_Interface_Name); // put it in the table. table.remove(serviceName); Object res = table.put(serviceName, ror); System.out.println("ROR is put in the hash table.\n"); // ack. out.println("bound"); } else if (command.equals("who are you?")) { out.println("I am a simple registry server."); System.out.println("I was asked who I was, so I answered.\n"); } else { System.out.println("I got an imcomprehensive message.\n"); } // close the socket. newsoc.close(); } } }