import net.jini.core.entry.*;
import net.jini.core.event.*;
import net.jini.core.lookup.*; 
import net.jini.core.discovery.*;    
import net.jini.lookup.entry.*; 
import com.sun.jini.lookup.*;
import java.rmi.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

/***************************************************************************

    MyClient -- The Jini Client Class

 ***************************************************************************/

    class MyClient
      {
      public static void main (String[] args)
        {
        int i, j;
        int iPort;
        String sHost;
        MyClientFrame frame;
        Entry[] aeAttributes;
        LookupLocator lookup;  
        ServiceID serviceID;
        ServiceMatches matches;
        ServiceRegistrar registrar;
        ServiceTemplate template;
        Applet applet;
        Object object;

        try 
          {


/*        Setting the security manager to allow the RMI class loader
          to go to the codebase for classes that are not available
          locally.
          ==========================================================  */

          System.setSecurityManager (new RMISecurityManager ());


/*        Find the Jini lookup service (reggie) and print its location.
          =============================================================  */

          lookup = new LookupLocator ("jini://localhost");
          sHost = lookup.getHost ();
          iPort = lookup.getPort ();
          System.out.println ();
          System.out.println ("client: LookupLocator      = " + lookup);
          System.out.println ("client: LookupLocator.host = " + sHost);
          System.out.println ("client: LookupLocator.port = " + iPort);


/*        Get the lookup service's ServiceRegistrar (the class by which
          interaction with the lookup service is possible).
          =============================================================  */

          registrar  = lookup.getRegistrar ();
          serviceID  = registrar.getServiceID ();
          System.out.println ("client: ServiceRegistrar   = " + registrar);
          System.out.println ("client: ServiceID          = " + serviceID);


/*        Get the services that have registered with this lookup service.
          ===============================================================  */

          matches = registrar.lookup 
            (
            new ServiceTemplate (null, null, null),
            300
            );
          System.out.println ("client: ServiceMatches     = " + matches);
          System.out.println ("client: Number of matches  = " 
            + matches.totalMatches);


/*        Each cycle of this loop processes one service that is 
          registered with this lookup service.
          =====================================================  */

          for (i = 0; i < matches.totalMatches; i++)
            {
            System.out.println ("client: " + i + ":   svc item:   " 
              + matches.items[i]);
            System.out.println ("client: " + i + ":   svc ID:     " 
              + matches.items[i].serviceID);
            serviceID = matches.items[i].serviceID;
            System.out.println ("client: " + i + ":   svc object: " 
              + matches.items[i].service);


/*          Each cycle of this loop prints one attribute set with which
            the service was registered.
            ===========================================================  */
            
            for (j = 0; j < matches.items[i].attributeSets.length; j++)
              {
              object = matches.items[i].attributeSets[j];
              System.out.println ("client: " + i + ":" + j + ": attrib Set: " 
                + object);


/*            If the class of the attribute set descends from Applet, 
              then launch a frame and put place it into the frame.
              =======================================================  */

              if (object instanceof Applet)
                {
                System.out.println ("client: Initg an applet attribute...");
                applet = (Applet) object;
                applet.init ();

                System.out.println ("client: Launching an applet...");
                frame = new MyClientFrame ();
                frame.addWindowListener (frame);
                frame.setSize (300, 200);
                frame.add ("Center", applet);
                frame.show ();
                }
              }
            }
          } 

        catch (Exception e) 
          {
          System.out.println ("client: MyClient.main() exception: " + e);
          }
        }
      }



/***************************************************************************

    MyClientFrame -- The Independent Window to Hold an Applet

 ***************************************************************************/

    class MyClientFrame extends Frame implements WindowListener
      {
      public void windowOpened (WindowEvent e)         // WindowListener
        {
        }

      public void windowClosing (WindowEvent e)        // WindowListener
        {
        System.out.println ("client: Applet closing.");
        System.exit (0);
        }

      public void windowClosed (WindowEvent e)         // WindowListener
        {
        }

      public void windowIconified (WindowEvent e)      // WindowListener
        {
        }

      public void windowDeiconified (WindowEvent e)    // WindowListener
        {
        }

      public void windowActivated (WindowEvent e)      // WindowListener
        {
        }

      public void windowDeactivated (WindowEvent e)    // WindowListener
        {
        }
      }

