RMI Help
I am recently working on a project which deals with remote login and file transfer between clients. I have discovered that i can not use socket programming as i will not be able to achive some importants features of the system such as the ability to log in to another system, as the socket programming deals only with streams so there is no way to actually control another computer with the software installed. I started using RMI as it allows to call remote methods in another machine and it does this by naming a remote service on the server. The problem is that i can only call methods on the server and not on the clients as RMI requires that the server be registered under a domain and the URL should be known in order to perform a lookup and connect, but since not all the clients ca be registered to a domain how can i be able to call a method remotely from them? I would really appreaciate it if someone could give me a direction somwhere as i am stuck.
thanks in advance
[993 byte] By [
visual007] at [2007-9-30 16:48:23]

It is quite possible to get two- way communications.
You need two RMI interfaces to be implement.ed; the first on the secondary computer and a second back on the primary for secondary sofware initiated communications.
//in the rmi interface for the second computer code
public void addObserver(SecondaryClientInterface secondaryClient) throws RemoteException;
public void removeObserver(SecondaryClientInterface secondaryClient) throws RemoteException;
// and implemented by the secondary
public void addObserver(SecondaryClientInterface secondaryClient) throws RemoteException
{
System.out.println("Inside Observer ");
this.secondaryClient=secondaryClient;
System.out.println("Observer ");
//System.out.println(((SecondaryClient_Stub)secondaryClient));
//secondaryClient
}
public void removeObserver(SecondaryClientInterface secondaryClient) throws RemoteException
{
if(this.secondaryClient==secondaryClient)
{
this.secondaryClient=null;
}
}
//in the primary side
String server = resources.getString("Host");
detectorURL = "rmi://" + server + "/SecondarySupervisor";
// Since you can't create an instance of an interface, what we get
// back from the lookup method is a remote reference to an object
// that implements MyRemoteInterface.
//
// Then we cast the remote reference (serialized stub instance)
// returned from Naming.lookup to a "MyRemoteInterface" so we can
// call the interface method(s).
//
secondarySupervisorInterface = (SecondarySupervisorInterface)Naming.lookup(detectorURL);
System.out.println("Got a remote reference to the object that"+" extends Activatable.");
secondarySupervisorInterface.addObserver(secondaryClient);
the 'secondaryClient' is the object of your implementation of the second rmi interface that will run on the primary and get called from the secondary side. In other words it represents the secondary client on the primary side and is the code that has methods the secondary may run remotely back on the primary.
//in the secondary computer code
try
{
secondaryClient.reportWaterInterlocksOpen(true);
}
catch(java.rmi.RemoteException re)
{
System.out.println(" re: "+re.getMessage());
}
then back on the primary side in the second rmi interface implementation it will run
public boolean reportWaterInterlocksOpen(boolean open) throws RemoteException
{
System.out.println("reportWaterInterlocksOpen "+open);
if(open)
{
for(int i=0; i<actionListeners.size();i++)
{
((ActionListener)actionListeners.elementAt(i)).actionPerformed(new ActionEvent(this,0,"Water Interlocks Open"));
}
}
return true;
}
I then used an event listener to send the action further but it depends what code implements the returning rmi interface(SecondaryClientInterface in my example). The nomenclature is a bit confusing. But it is like having a ServerSocket accept a connection and you happen to name that Socket connection clientSocket to represent the remote client that communicates thru it.
>