import net.jini.core.lookup.ServiceID;
import com.sun.jini.lookup.ServiceIDListener;
import com.sun.jini.lookup.JoinManager;
import com.sun.jini.lease.LeaseRenewalManager;
import net.jini.core.discovery.LookupLocator;
import java.net.InetAddress;
import java.rmi.RMISecurityManager;
import java.io.*;

public class KYSS_jini implements ServiceIDListener{
    private String hostName;
    private String hostIP;
    private ServiceID myID;
    
    //register a KYSS_jini services to the jini lookup service
    public KYSS_jini() {

System.out.println("Connect to "+lookupHost);
System.out.println("Hostname "+hostName);
System.out.println("host IP "+hostIP);
	
	this.hostName = hostName;
	this.hostIP = hostIP;

	//set rmi security manager;
	System.setSecurityManager (new RMISecurityManager ());
	
	try {
	    // Find the lookup service
	    LookupLocator locator = new LookupLocator ("jini://" + lookupHost);
	    LookupLocator[] locators = new LookupLocator[1];
	    locators[0] = locator;
	    
	    // register(join) the file server to the lookup service
	    JoinManager myManager = new JoinManager(this, null, null, locators, this, new LeaseRenewalManager());
	}
	catch(IOException e) {
	    System.out.println("KYSS_jini register error " + e);
	    e.printStackTrace();
	}
	System.out.println ("Server has been Joined!");
    }

    public void serviceIDNotify (ServiceID uniqueID) {
	myID = uniqueID;
	System.out.println("server: ID set: " + myID );
    }

    public String getHostName() {
	return hostName;
    }

    public String getIP() {
	return hostIP;
    }

       public static void main(String args[]) {
                try {
			InetAddress host = InetAddress.getLocalHost();
                        String hostname = host.getHostName();
        //set rmi security manager;
        System.setSecurityManager (new RMISecurityManager ());

        try {
            // Find the lookup service
            LookupLocator locator = new LookupLocator ("jini://sunserver1.cs.umbc.edu:6544" );
            LookupLocator[] locators = new LookupLocator[1];
            locators[0] = locator;

            // register(join) the file server to the lookup service
            JoinManager myManager = new JoinManager(this, null, null, locators,
this, new LeaseRenewalManager());
        }
        catch(IOException e) {
            System.out.println("KYSS_jini register error " + e);
            e.printStackTrace();
        }

                        KYSS_jini obj = new KYSS_jini("sunserver1.cs.umbc.edu:6544",hostname,host.getHostAddress());
                        System.out.println("KYSS_jini bound in registry");
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }

}
