
/**
 * ServiceViewer.java
 *
 *
 * Created: Thu Apr 19 04:54:57 2001
 *
 * @author <a href="mailto: "Jan Newmarch</a>
 * @version
 */

package client;

import servicefinder.ServiceFinder;

import java.rmi.RMISecurityManager;
import net.jini.discovery.LookupDiscovery;
import net.jini.core.lookup.ServiceTemplate;
import net.jini.core.lookup.ServiceItem;
import net.jini.core.discovery.LookupLocator;
import net.jini.discovery.LookupDiscoveryManager;
import net.jini.lookup.ServiceDiscoveryManager;
import net.jini.lease.LeaseRenewalManager;
import net.jini.core.lookup.ServiceRegistrar;
import net.jini.lookup.LookupCache;
import net.jini.discovery.LookupLocatorDiscovery;

public class ServiceViewer implements net.jini.discovery.DiscoveryListener {

    private static final long WAITFOR = 10000L;
    public static final String ROOTHOST = "jannote.csse.monash.edu.au";

    protected ServiceFinder finder;
    protected LookupCache cache;

    public static void main(String argv[]) {
        new ServiceViewer();

        // stay around long enough to receive replies
        try {
            Thread.currentThread().sleep(2*WAITFOR);
        } catch(java.lang.InterruptedException e) {
            // do nothing
        }
	System.err.println("Client timed out");
	System.exit(0);
    }

    public ServiceViewer () {

        LookupLocator lookup = null;
	ServiceRegistrar registrar = null;
        ServiceDiscoveryManager clientMgr = null;

        // set the codebase before installing the security manager
        System.setProperty("java.rmi.server.codebase", 
                           "http://" + ROOTHOST + "/classes/");
        System.setSecurityManager(new RMISecurityManager());


        Class [] classes = new Class[] {ServiceFinder.class};
        ServiceTemplate template = new ServiceTemplate(null, classes, 
                                                       null);
	try {
            LookupLocatorDiscovery discovery =
                new LookupLocatorDiscovery(new LookupLocator[] {
		    new LookupLocator("jini://" + ROOTHOST)});
	    discovery.addDiscoveryListener(this);
            clientMgr = new ServiceDiscoveryManager(discovery, 
						    new LeaseRenewalManager());
        } catch(Exception e) {
	    System.out.println(e.toString() + "</body> </html>"); 
        }
	


        try {
            cache = clientMgr.createLookupCache(template, 
                                                null,
                                                null);
        } catch(Exception e) {
            e.printStackTrace();
            System.exit(1);
        }

        // Get the service
	int count = 10;
	while ((count-- > 0) && (cache.lookup(null) == null)) {
	    try {
		Thread.currentThread().sleep(1000);
	    } catch(java.lang.InterruptedException e) {
	    }
	}
	if (cache.lookup(null) == null) {
            System.out.println("Jini service finder is not running");
	} else {
            System.out.println("Jini service finder is running");
	}
        ServiceItem item = cache.lookup(null);
	if (item == null) {
	    System.out.println("<br><em>Service finder item null</em></br>");
	    return;
	}
	/*

        // Try to find the service, blocking till timeout if necessary
	ServiceItem item = null;
        try {
            item = clientMgr.lookup(template, 
                                    null, 
                                    WAITFOR);
        } catch(Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
	*/
	finder = (ServiceFinder) item.service;


        // Get the service
        // finder = (ServiceFinder) service;
	printLocators();
	printServices();
	printServicesByLocators();

	System.exit(0);
    }

    void printLocators() {
	System.out.println("Finding locators");
	LookupLocator[] locators = null;
	try {
	    locators = finder.getLocators();
	} catch(java.rmi.RemoteException e) {
	    System.err.println(e.toString());
	    return;
	}
	for (int n = 0; n < locators.length; n++) {
	    System.out.println(locators[n].getHost());
	}
    }

    void printServices() {
	System.out.println("Finding services");
	ServiceItem[] services = null;
	try {
	    services = finder.getServices();
	} catch(java.rmi.RemoteException e) {
	    System.err.println(e.toString());
	    return;
	}
	for (int n = 0; n < services.length; n++) {
	    ServiceItem serviceItem = services[n];
	    System.out.println(serviceItem.service.toString());
	}
    }

    void printServicesByLocators() {
	LookupLocator[] locators = null;
	try {
	    locators = finder.getLocators();
	} catch(java.rmi.RemoteException e) {
	    System.err.println(e.toString());
	    return;
	}
	String host = null;
	for (int n = 0; n < locators.length; n++) {
	    host = locators[n].getHost();
	    printServices(host);
	}
    }

    void printServices(String url) {
	System.out.println("Finding services for " + url);
	ServiceItem[] services = null;
	try {
	    services = finder.getServices(url);
	} catch(java.rmi.RemoteException e) {
	    System.err.println(e.toString());
	    return;
	}
	for (int n = 0; n < services.length; n++) {
	    ServiceItem serviceItem = services[n];
	    System.out.println(serviceItem.service.toString());
	}
    }

    public void discovered(net.jini.discovery.DiscoveryEvent e) {
        System.out.println("discovered");
         ServiceRegistrar[] registrars = e.getRegistrars();
         for (int n = 0; n < registrars.length; n++) {
             ServiceRegistrar reg = registrars[n];
             try {
                 System.out.println("  discovered " +
                                reg.getLocator().getHost());
             } catch(java.rmi.RemoteException ex) {
             }
         }
    }

    public void discarded(net.jini.discovery.DiscoveryEvent e) {
        System.out.println("discarded");
         ServiceRegistrar[] registrars = e.getRegistrars();
         for (int n = 0; n < registrars.length; n++) {
             ServiceRegistrar reg = registrars[n];
             try {
                 System.out.println("  discarded " +
                                reg.getLocator().getHost());
             } catch(java.rmi.RemoteException ex) {
             }
         }
    }
}// ServiceViewer
