CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Threaded View

  1. #1
    Join Date
    Sep 2007
    Posts
    18

    Passing a remote client interface to a remote server

    Here is the environment:

    I have a remote coordinator interface and a remote client interface which implement Remote interface:
    Code:
    public interface CoordinatorInterface extends Remote {
    
    	public void test() throws java.rmi.RemoteException;
    	/**
    	 * remote function used by client to connect 
    	 */
    	public void connectClient(ClientInterface clientInterface) throws java.rmi.RemoteException;
    }
    Code:
    public interface ClientInterface extends Remote {
    	
    	/**
    	 * create a client interface
    	 */
    	public void updateClientList() throws java.rmi.RemoteException;
    	
    	/**
    	 * update client's whiteboard
    	 */
    	public void updateClient() throws java.rmi.RemoteException;
    
    }
    And a NetworkCoordinator class implements the CoordinatorInterface; a NetworkClient class implements that ClientInterface:
    Code:
    public class NetworkCoordinator implements CoordinatorInterface{
    
    	private static final long serialVersionUID = 1L;
    	/**contains client id and client interface*/
    	private Hashtable clients;
    	/**contains the request the server processed*/
    	private Hashtable requests;
    	/**clientId counted by Server*/
    	private long clientIdServer;
    	/**sequenceId counted by Server*/
    	private long seqIdServer;
    	
    
    	public NetworkCoordinator() {
    		super();
    		clients = new Hashtable();
    		requests = new Hashtable();
    		clientIdServer = 1000;
    		seqIdServer = 1100;
    	}
    
    	public void test() throws java.rmi.RemoteException
    	{
    		System.out.println("test");
    	}
    	/**
    	 * remote function used by client to connect 
    	 */
    	public void connectClient(ClientInterface clientInterface) throws java.rmi.RemoteException
    	{
    		clientIdServer++;
    		clients.put(clientIdServer, clientInterface);
    		//put keys into enumeration
    		Enumeration clientsEnum = clients.keys();
    		//update each client's clientlist
    		while(clientsEnum.hasMoreElements())
    		{
    			int clientId = (Integer)clientsEnum.nextElement();
    			ClientInterface ci = (ClientInterface)clients.get(clientId);
    			ci.updateClientList();
    		}
    		System.out.println("Client connected");
    	}
    	
    	public static void main(String args[])//this is going to be run()
    	{
    		if (System.getSecurityManager() == null) {
    			System.setSecurityManager(new SecurityManager());
    		}
    		try {
    			//the name for client to look for in registry
    			String name = "Coordinator";
    			NetworkCoordinator testServer = new NetworkCoordinator();
    			CoordinatorInterface stub = (CoordinatorInterface) UnicastRemoteObject.exportObject(testServer, 0);
    			Registry registry = LocateRegistry.getRegistry();
    			registry.rebind(name, stub);
    			System.out.println("Coordinator bound");
    		} catch (Exception e) {
    			System.err.println("Coordinator exception:");
    			e.printStackTrace();
    		}
    	}
    	
    }
    Code:
    public class NetworkClient implements ClientInterface {
    
    	/**
    	 * create a client interface
    	 */
    	public NetworkClient() {
    		super();
    	}
    	
    	/**
    	 * update client's list of nodes
    	 */
    	public void updateClientList() throws java.rmi.RemoteException
    	{
    		System.out.println("updateClientList called by server.");
    	}
    	
    	/**
    	 * update client's whiteboard
    	 */
    	public void updateClient()throws java.rmi.RemoteException
    	{
    		
    	}
    
    	/**
    	 * for test using
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		if (System.getSecurityManager() == null) {
    			System.setSecurityManager(new SecurityManager());
    		}
    		try {
    			String name = "Coordinator";
    			Registry registry = LocateRegistry.getRegistry(args[0]);
    			CoordinatorInterface serverinterface = (CoordinatorInterface) registry.lookup(name);
    			NetworkClient client = new NetworkClient();
    			ClientInterface stub = (ClientInterface)UnicastRemoteObject.exportObject(client,1);
    			serverinterface.connectClient(stub);
    			//UnicastRemoteObject.exportObject(client);
    			//serverinterface.connectClient(client);
    			serverinterface.test();
    
    		} catch (Exception e) {
    			System.err.println("Client exception:");
    			e.printStackTrace();
    		}
    	}
    }
    My goal is to start the coordiantor and client, and then the client will use serverinterface.connectClient(stub);
    to pass its interface to the coordinator, and the coordinator will save it into a hashtable and call client's updateClientList() method.

    The Coordinator class runs fine, I can get to the "Coordinator bound" system output. And the serverinterface.test() call works too. But my problem is how to pass the clientinterface to the coordinator, by using serverinterface.connectClient(clientinterface); In the above code I would get a Client exception:
    Java.rmi.StubNotFoundException
    What did I do wrong?

    Thanks a lot
    J.
    Last edited by tajepe; March 22nd, 2008 at 04:33 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured