import net.jini.core.entry.*; import net.jini.core.lookup.*; import net.jini.core.discovery.*; import net.jini.lookup.entry.*; import com.sun.jini.lookup.*; import com.sun.jini.lease.*; import java.net.*; import java.io.*; /*************************************************************************** MyServer -- The Jini Server Class ***************************************************************************/ public class MyServer implements ServiceIDListener, Serializable, Runnable { int iPort; public MyServer (int iPortIn) { Thread th; iPort = iPortIn; th = new Thread (this); th.start (); } public void serviceIDNotify (ServiceID sidIn) // ServiceIDListener { System.out.println ("serv: Received ServiceID = " + sidIn); } public void run () { String s; byte[] ab; InputStream is; OutputStream os; Socket socket; ServerSocket ssocket; try { System.out.println ("serv: Listening on port " + iPort); ssocket = new ServerSocket (iPort); while (true) { System.out.println ("serv: Waiting for a connection..."); socket = ssocket.accept (); System.out.println ("*-----------------------------------*"); System.out.println ("serv: Connected to " + socket.getInetAddress () + "/" + socket.getPort ()); is = socket.getInputStream (); os = socket.getOutputStream (); s = "Hello from " + this; // all socket requests recv a string ab = s.getBytes (); System.out.println ("serv: Sending --->" + s + "<---"); os.write (ab); socket.close (); } } catch (Exception e) { } } public static void main (String[] args) { int i; MyServer myServer; MyServerProxy myServerProxy; LookupLocator lookup; Entry[] aeAttributes; JoinManager joinmanager; ServiceMatches matches; ServiceRegistrar registrar; try { /* Create the service object and proxy object and register them with the lookup service. Pass the IP and port number of the service to the proxy so it will know where to make requests. ============================================================ */ System.out.println (); System.out.println ("main: Creating the service..."); myServer = new MyServer (7777); myServerProxy = new MyServerProxy ( InetAddress.getLocalHost().getHostAddress(), 7777 ); System.out.println ("main: Registering with lookup service..."); aeAttributes = new Entry[1]; aeAttributes[0] = new Name("MyServerProxy"); joinmanager = new JoinManager ( myServerProxy, aeAttributes, myServer, new LeaseRenewalManager () ); /* Itemize the current Jini services. Note: Sometimes this is executed too quickly for the service that was registered above to show up so I pause for a bit first. ============================================================== */ System.out.println ("main: Pausing 5 seconds..."); try {Thread.sleep (5000);} catch (Exception e) {} System.out.println ("main: Finding lookup service..."); lookup = new LookupLocator ("jini://localhost"); registrar = lookup.getRegistrar (); System.out.println ("main: Itemizing services..."); matches = registrar.lookup ( new ServiceTemplate (null, null, null), 50 ); System.out.println ("main: ServiceMatches = " + matches); System.out.println ("main: num matches = " + matches.totalMatches); for (i = 0; i < matches.totalMatches; i++) { System.out.println ("main: svc item " + i + ": " + matches.items[i]); System.out.println ("main: svc object " + i + ": " + matches.items[i].service); } } catch (Exception e) { System.out.println("main: MyServer.main(): Exception " + e); } } }