| Fall Semester 2007 |
Imagine two programs being run side by side.
Can methods from one call methods from the other?
The answer is: yes. Today we will learn how that's done and investigate some of the implications.
1. Side by Side: What is That ?
Imagine a program (a main method) being invoked at the prompt. It starts and creates an object, call it a.
Imagine a second program (a second, separate
Can either one of the two objects invoke any of the methods of the other? That's our problem for today, and the answer is: yes.
Details on how that can be achieved are presented below.
2. Of Clients and Servers.
We set up this situation as follows:
What we truly want, in most operational terms, would be that an object of this kind:
Would this work? Is that what we have in mind:
Huh? Is that it, then?
The requirement is that
Moreover, each of the two
3. What is a Server?
A server is very much like a business: it's something you can find listed in the Yellow Pages.
This is how we advertise:
4. The Mysterious, Magical
A class is given that allows the server to be available remotely. Here's how we use it:
Recall, the client wants to access the server's
Something like:
With this, we're done. The client is a remote peer too, but we don't know that (nor do we need it) at this point.
In other words
We'll take advantage of that soon. Meanwhile let's just explain how we test our application.
5. Necessities of Deployment
There needs to be a file that gives the server permission to answer the phone (take/process incoming calls):
Next I compile everything:
The policy file is also there, it just isn't a part of the compilation stage.
Next we invoke the RMI compiler on the classes that implement an interface that extends
Now we start the server:
6. In Class
After we verify this is working well on each machine:
We can then do the networked chat with shared whiteboard (just compile and run it).
7. Why Transparent Networking?
We can now take look at the
Here's our original sketch of a code:
So, all the networking is outside the dialog between the client and the server.
The method doesn't know (or care to know) if it's called over the network at all.
8. How Does
"Any sufficiently advanced technology is indistinguishable from magic." (Arthur C. Clarke)
main) being invoked separately. It starts and creates another object, call it
Let's just call this type of object(s), public class Server {
public String reportDate() {
try {
return " the time is " + new java.util.Date() + "\n " + " on " + java.net.InetAddress.getLocalHost() + "\n";
} catch (Exception e) {
return "Error... " + e;
}
}
public static void main(String[] args) {
Server a = new Server();
System.out.println(a.reportDate());
}
}Server: it provides some kind of service.
... would be able to locate (from its public class Client {
public static void main(String[] args) {
Client b = new Client();
// now what?...
}
}main()) an existing Server and call its method reportDate().
This couldn't be it for two reasons: public class Client {
public static void main(String[] args) {
Client b = new Client();
// now what?... how about:
Server a = new Server();
System.out.println(a.reportDate());
// ... is that it?
}
}
main method.
This is much, much closer; but it still has one drawback; there is only one public class Client {
Server server;
Client(Server server) {
this.server = server;
}
void illustrate() {
System.out.println(server.reportDate());
}
public static void main(String[] args) {
Server a = new Server();
Client b = new Client(a);
b.illustrate();
}
}main here... a and b be created in separate main(...) methods. main methods is started by an own invocation to javac at the command prompt.
It's a small price to pay. What else do we need to do? import java.rmi.*;
public interface ServerYellowPagesAd extends Remote {
public String reportDate() throws RemoteException;
}NetworkPeer Class
Before we even start thinking of this class (import java.rmi.*;
public class Server extends NetworkPeer implements ServerYellowPagesAd {
public String reportDate() throws RemoteException {
try {
return " the time is " + new java.util.Date() + "\n " + " on " + java.net.InetAddress.getLocalHost() + "\n";
} catch (Exception e) {
return "Error... " + e;
}
}
public static void main(String[] args) {
Server a = new Server();
String name = args[0], port = args[1];
a.startAsNetworkServer(name, Integer.parseInt(port)); // client better know the host name (which is implicit here)
}
}NetworkPeer), let's see how the clients needs to use it. reportDate method. Here's the client code:
Here we need to provide a clear definition of the import java.rmi.*;
class Client extends NetworkPeer {
public static void main(String[] args) throws Exception {
String serverHostName = args[0], // three command line arguments
serverPortNumber = args[1],
serverName = args[2];
Client client = new Client();
client.startAsNetworkClientOf(serverHostName,
Integer.parseInt(serverPortNumber),
serverName);
client.illustrate();
}
public void startAsClientOf(java.rmi.Remote peer) throws java.rmi.RemoteException {
ServerYellowPagesAd server = (ServerYellowPagesAd)peer;
this.server = server;
} // this is one method that needs to be provided. It's not very complex, either.
ServerYellowPagesAd server; // the client refers to the server by its business card
void illustrate() throws RemoteException { // because of reportDate()
System.out.println(server.reportDate());
}
} NetworkPeer's class API.
startAsClientOf method
paint in applets, or talk in Unicorns
NetworkPeer truly gives us peer to peer, bidirectional networking capability.
You can refine this, too. I call this file grant {
permission java.net.SocketPermission "*", "connect,accept";
};java.policy and make sure is in the same folder with everything else. javac *.java
Of course, it's good to always know what that means, we have these files:
ServerYellowPagesAd.java
Server.java
NetworkPeer.java
Client.java
java.rmi.Remote:
NetworkPeer, and
Server
The server is ready, and it reports that, so we can start the client. java -Djava.security.policy=java.policy Server Dave 12009
And that should do it. java Client 127.0.0.1 12009 Dave
NetworkPeer class, but maybe we shouldn't (just yet)?
Compile and run this program and it works like a charm. public class One {
public static void main(String[] args) throws Exception {
Server a = new Server();
Client b = new Client();
b.startAsClientOf(a);
b.illustrate();
}
}NetworkPeer Work?
Last updated: Nov 12, 2007. Copyright © Adrian German. All rights reserved.