rmi observing the distributed pattern. adrian german lecturer, computer science indiana university...

89
RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

Upload: justina-ward

Post on 17-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

RMI

Observing the Distributed Pattern.

Adrian German

Lecturer, Computer Science

Indiana University Bloomington

Page 2: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

RMI

Observing the Distributed Pattern.

Adrian German

Lecturer, Computer Science

Indiana University Bloomington

Java

Page 3: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington
Page 4: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington
Page 5: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington
Page 6: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington
Page 7: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington
Page 8: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington
Page 9: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

join

Page 10: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

registered

Page 11: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

join

Page 12: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

registered

Page 13: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

join

Page 14: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

registered

Page 15: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington
Page 16: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

broadcast

Page 17: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

broadcast

“Hello!”

Page 18: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

broadcast( )

“Hello!”

Page 19: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

broadcast( )“Hello!”

Page 20: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

“Hello!”

“Hello!”“Hello!”

Page 21: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington
Page 22: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

client

Page 23: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

client

client

Page 24: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

client

client

client

Page 25: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

client

client

clientserver

Page 26: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

client

client

clientserver (host)

Page 27: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

client

client

clientserver (host)

The difference is that the server is listed in the phone book.

Page 28: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

client

client

clientserver (host)

The difference is that the server is listed in the phone book (while a client isn’t).

Page 29: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

client

client

clientserver (host)

How do we implement this?

Page 30: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

client

client

clientserver (host)

Page 31: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

client

server (host)

Page 32: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

client

server (host)

class Server {

Client[] clients;

void broadcast(String message) { ... }

int register(Client client) { ... }

}

Page 33: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

client

server (host)

class Server {

Client[] clients;

void broadcast(String message) { ... }

int register(Client client) { ... }

}

class Client {

void register(int index,

Client client) { ... }

void update(Message msg) { ... }

}

Page 34: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

client

server (host)

class Server {

Client[] clients;

void broadcast(String message) { ... }

int register(Client client) { ... }

}

class Client {

void register(int index,

Client client) { ... }

void update(Message msg) { ... }

}

Clients call the server's broadcast method in turn, which calls the clients' update.

Page 35: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

client

server (host)

Page 36: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

clientserver

Page 37: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

serverclient

Page 38: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

serverclient

f

Page 39: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

serverclient

ff

client.f(...) calls server.f(...)

Page 40: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

serverclient

ff

g

client.f(...) calls server.f(...)

which in turn calls client.g(...)

Page 41: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

serverclient

ff

g

client.f(...) calls server.f(...)

which in turn calls client.g(...)

The question is: how do we turn this code into a distributed program?

Page 42: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

serverclient

ff

g

client.f(...) calls server.f(...)

which in turn calls client.g(...)

The question is: how do we turn this code into a distributed program?

Page 43: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

serverclient

ff

g

client.f(...) calls server.f(...)

which in turn calls client.g(...)

The question is: how do we turn this code into a distributed program?

(Hawaii)

(Sweden)Internet

Internet

this local program

Page 44: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

even simpler

Page 45: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

function call

server

add()

client

fun()

value returned

Page 46: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

function call

server

add()

client

fun()

value returned

class Server { int add(int a, int b) { return a + b; }}

class Client { void fun(Server server) { System.out.println(server.add(1, 2)); }}

class LocalProgram { public static void main(String[] args) { Server a = new Server(); Client b = new Client(); b.fun(a); }}

Page 47: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

function call

server

add()

client

fun()

value returned

class LocalProgram { public static void main(String[] args) { Server a = new Server(); Client b = new Client(); b.fun(a); }}

Important questions:a) who creates the server?b) who creates the client?c) does the client really initiate anything?

Page 48: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

function call

server

add()

client

fun()

value returned

The difference between local and distributed computing is real.

Differences are impossible to ignore at least in the following areas:

a) latency, b) memory access, c) partial failure, and d) concurrency.

... that is, at least at the present time as well as in the near future.

Page 49: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

function call

server

add()

client

fun()

value returned

abstract class NetworkPeer implements java.rmi.Remote { public void exportMethods() throws java.rmi.RemoteException { java.rmi.server.UnicastRemoteObject.exportObject(this);

} public java.rmi.Remote locatePeer(String peerHost, int peerPort, String peerName) throws Exception { return java.rmi.Naming.lookup("rmi://" + peerHost + ":" + peerPort + "/" + peerName);

} public void startAsNetworkClient(String peerHost, int peerPort, String peerName) throws Exception { this.exportMethods(); java.rmi.Remote peer = this.locatePeer(peerHost, peerPort, peerName); this.startAsClientOf(peer); // see below ... }

public abstract void startAsClientOf(java.rmi.Remote peer) throws java.rmi.RemoteException; public abstract void startAsServer(); // ... start as local server

}

Page 50: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

function call

server

add()

client

fun()

value returned

abstract class NetworkPeer implements java.rmi.Remote { public void exportMethods() throws java.rmi.RemoteException { java.rmi.server.UnicastRemoteObject.exportObject(this);

} public java.rmi.Remote locatePeer(String peerHost, int peerPort, String peerName) throws Exception { return java.rmi.Naming.lookup("rmi://" + peerHost + ":" + peerPort + "/" + peerName);

} public void startAsNetworkClient(String peerHost, int peerPort, String peerName) throws Exception { this.exportMethods(); java.rmi.Remote peer = this.locatePeer(peerHost, peerPort, peerName); this.startAsClientOf(peer); // see below ... }

public abstract void startAsClientOf(java.rmi.Remote peer) throws java.rmi.RemoteException; public abstract void startAsServer(); // ... start as local server

}Distributed processing is a world of free agents.

The class above is all that an object needs to become a free agent. Any object.

Page 51: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

function call

server

add()

client

fun()

value returned

abstract class NetworkPeer implements java.rmi.Remote { public void exportMethods() throws java.rmi.RemoteException { java.rmi.server.UnicastRemoteObject.exportObject(this);

} public java.rmi.Remote locatePeer(String peerHost, int peerPort, String peerName) throws Exception { return java.rmi.Naming.lookup("rmi://" + peerHost + ":" + peerPort + "/" + peerName);

} public void startAsNetworkClient(String peerHost, int peerPort, String peerName) throws Exception { this.exportMethods(); java.rmi.Remote peer = this.locatePeer(peerHost, peerPort, peerName); this.startAsClientOf(peer); // see below ... }

public abstract void startAsClientOf(java.rmi.Remote peer) throws java.rmi.RemoteException; public abstract void startAsServer(); // ... start as local server

}Distributed processing is a world of free agents.

The class above is all that an object needs to become a free agent. Any object.

Page 52: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

function call

server

add()

client

fun()

value returned

abstract class NetworkPeer implements java.rmi.Remote { public void exportMethods() throws java.rmi.RemoteException { java.rmi.server.UnicastRemoteObject.exportObject(this);

} public java.rmi.Remote locatePeer(String peerHost, int peerPort, String peerName) throws Exception { return java.rmi.Naming.lookup("rmi://" + peerHost + ":" + peerPort + "/" + peerName);

} public void startAsNetworkClient(String peerHost, int peerPort, String peerName) throws Exception { this.exportMethods(); java.rmi.Remote peer = this.locatePeer(peerHost, peerPort, peerName); this.startAsClientOf(peer); // see below ... }

public abstract void startAsClientOf(java.rmi.Remote peer) throws java.rmi.RemoteException; public abstract void startAsServer(); // ... start as local server

}Distributed processing is a world of free agents.

The class above is all that an object needs to become a free agent. Any object.

Page 53: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

function call

server

add()

client

fun()

value returned

class Server { int add(int a, int b) { return a + b; }}

class Client { void fun(Server server) { System.out.println(server.add(1, 2)); }}

class LocalProgram { public static void main(String[] args) { Server a = new Server(); Client b = new Client(); b.fun(a); }}

Page 54: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

class Server { int add(int a, int b) { return a + b; }}

Page 55: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

public class ServerImplementation extends NetworkPeer implements Server { public static void main(String[] args) { String portNumber = args[0], ownName = args[1]; ServerImplementation here = new ServerImplementation(); here.startAsNetworkServer(ownName, Integer.parseInt(portNumber)); } public void startAsClientOf(java.rmi.Remote peer) { } public void startAsServer() { } public int add(int a, int b) throws java.rmi.RemoteException { System.out.println("I am adding for the audience..."); return a + b; } }public interface Server extends java.rmi.Remote { public int add(int a, int b) throws java.rmi.RemoteException; }

function call

server

add()

client

fun()

value returned

class ServerImplementation extends NetworkPeer implements Server { public int add(int a, int b) throws java.rmi.RemoteException { return a + b; } public void startAsServer() { } public void startAsClientOf(java.rmi.Remote peer) { } public static void main(String[] args) { String portNumber = args[0], ownName = args[1]; ServerImplementation here = new ServerImplementation(); here.startAsNetworkServer(ownName, Integer.parseInt(portNumber)); } }public interface Server extends java.rmi.Remote { public int add(int a, int b) throws java.rmi.RemoteException; }

Page 56: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

class Client { void fun(Server server) { System.out.println(server.add(1, 2)); }}

Page 57: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

public class ClientImplementation extends NetworkPeer {

public void startAsClientOf(java.rmi.Remote peer) throws java.rmi.RemoteException { this.fun((Server) peer); }

void fun(Server server) throws java.rmi.RemoteException { System.out.println(server.add(2, 3)); }

public static void main(String[] args) throws Exception { String ownName = args[0], serverHostName = args[1], serverPortNumber = args[2], serverName = args[3]; ClientImplementation client = new ClientImplementation(); client.startAsNetworkClientOf(serverHostName, Integer.parseInt(serverPortNumber), serverName); }

public void startAsServer() { }

}

Page 58: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

class LocalProgram { public static void main(String[] args) { Server a = new Server(); Client b = new Client(); b.fun(a); }}

Page 59: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

class LocalSetup { public static void main(String[] args) throws java.rmi.RemoteException { ServerImplementation server = new ServerImplementation(); server.startAsServer(); ClientImplementation client = new ClientImplementation(); client.startAsClientOf(server); } }

class LocalProgram { public static void main(String[] args) { Server a = new Server(); Client b = new Client(); b.fun(a); }}

Page 60: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

class LocalSetup { public static void main(String[] args) throws java.rmi.RemoteException { ServerImplementation server = new ServerImplementation(); server.startAsServer(); ClientImplementation client = new ClientImplementation(); client.startAsClientOf(server); } }

class LocalProgram { public static void main(String[] args) { Server a = new Server(); Client b = new Client(); b.fun(a); }}

Page 61: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

class LocalSetup { public static void main(String[] args) throws java.rmi.RemoteException { ServerImplementation server = new ServerImplementation(); server.startAsServer(); ClientImplementation client = new ClientImplementation(); client.startAsClientOf(server); } }

Page 62: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

class LocalSetup { public static void main(String[] args) throws java.rmi.RemoteException {

ServerImplementation server = new ServerImplementation(); server.startAsServer();

ClientImplementation client = new ClientImplementation(); client.startAsClientOf(server); } }

Page 63: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

class LocalSetup { public static void main(String[] args) throws java.rmi.RemoteException {

ServerImplementation server = new ServerImplementation(); server.startAsServer();

ClientImplementation client = new ClientImplementation(); client.startAsClientOf(server); } }

LocalSetup

Page 64: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

class LocalSetup { public static void main(String[] args) throws java.rmi.RemoteException {

ServerImplementation server = new ServerImplementation(); server.startAsServer();

ClientImplementation client = new ClientImplementation(); client.startAsClientOf(server); } }

LocalSetup

Server

Page 65: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

class LocalSetup { public static void main(String[] args) throws java.rmi.RemoteException {

ServerImplementation server = new ServerImplementation(); server.startAsServer();

ClientImplementation client = new ClientImplementation(); client.startAsClientOf(server); } }

LocalSetup

Client

Server

Page 66: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

class LocalSetup { public static void main(String[] args) throws java.rmi.RemoteException {

ServerImplementation server = new ServerImplementation(); server.startAsServer();

ClientImplementation client = new ClientImplementation(); client.startAsClientOf(server); } }

LocalSetup

Client

Server

Page 67: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

class LocalSetup { public static void main(String[] args) throws java.rmi.RemoteException {

ServerImplementation server = new ServerImplementation(); server.startAsServer();

ClientImplementation client = new ClientImplementation(); client.startAsClientOf(server); } }

LocalSetup

Client

Server

Page 68: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

class LocalSetup { public static void main(String[] args) throws java.rmi.RemoteException {

ServerImplementation server = new ServerImplementation(); server.startAsServer();

ClientImplementation client = new ClientImplementation(); client.startAsClientOf(server); } }

LocalSetup

Client

Server

Page 69: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

class LocalSetup { public static void main(String[] args) throws java.rmi.RemoteException {

ServerImplementation server = new ServerImplementation(); server.startAsServer();

ClientImplementation client = new ClientImplementation(); client.startAsClientOf(server); } }

LocalSetup

Client

Server

Page 70: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

class LocalSetup { public static void main(String[] args) throws java.rmi.RemoteException {

ServerImplementation server = new ServerImplementation(); server.startAsServer();

ClientImplementation client = new ClientImplementation(); client.startAsClientOf(server); } }

LocalSetup

Client

Server

Page 71: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

class LocalSetup { public static void main(String[] args) throws java.rmi.RemoteException {

ServerImplementation server = new ServerImplementation(); server.startAsServer();

ClientImplementation client = new ClientImplementation(); client.startAsClientOf(server); } }

LocalSetup

Client

Server

So although there is no network there is already a change of attitude.

Page 72: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

public abstract class NetworkPeer implements java.rmi.Remote {

public void exportMethods() throws java.rmi.RemoteException { java.rmi.server.UnicastRemoteObject.exportObject(this); }

public java.rmi.Remote locatePeer(String peerHost, int peerPort, String peerName) throws Exception { return java.rmi.Naming.lookup("rmi://" + peerHost + ":" + peerPort + "/" + peerName); }

public void startAsNetworkClientOf(String peerHost, int peerPort, String peerName) throws Exception { this.exportMethods(); java.rmi.Remote peer = this.locatePeer(peerHost, peerPort, peerName); this.startAsClientOf(peer); // see below ... }

public abstract void startAsClientOf(java.rmi.Remote peer) throws java.rmi.RemoteException;

public void startAsNetworkServer(String name, int port) { System.setSecurityManager(new java.rmi.RMISecurityManager()); try { this.exportMethods(); java.rmi.registry.Registry registry = java.rmi.registry.LocateRegistry.createRegistry(port); registry.bind(name, this); this.startAsServer(); // see below ... System.out.println("Server is ready ... "); } catch (Exception e) { System.out.println("Server error: " + e + " ... "); } }

public abstract void startAsServer();

}

Page 73: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

public abstract class NetworkPeer implements java.rmi.Remote {

public void exportMethods() throws java.rmi.RemoteException { java.rmi.server.UnicastRemoteObject.exportObject(this); }

public java.rmi.Remote locatePeer(String peerHost, int peerPort, String peerName) throws Exception { return java.rmi.Naming.lookup("rmi://" + peerHost + ":" + peerPort + "/" + peerName); }

public void startAsNetworkClientOf(String peerHost, int peerPort, String peerName) throws Exception { this.exportMethods(); java.rmi.Remote peer = this.locatePeer(peerHost, peerPort, peerName); this.startAsClientOf(peer); // see below ... }

public abstract void startAsClientOf(java.rmi.Remote peer) throws java.rmi.RemoteException;

public void startAsNetworkServer(String name, int port) { System.setSecurityManager(new java.rmi.RMISecurityManager()); try { this.exportMethods(); java.rmi.registry.Registry registry = java.rmi.registry.LocateRegistry.createRegistry(port); registry.bind(name, this); this.startAsServer(); // see below ... System.out.println("Server is ready ... "); } catch (Exception e) { System.out.println("Server error: " + e + " ... "); } }

public abstract void startAsServer();

}

Page 74: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

public abstract class NetworkPeer implements java.rmi.Remote {

public void exportMethods() throws java.rmi.RemoteException { ... }

public java.rmi.Remote locatePeer(String peerHost, int peerPort, String peerName) throws Exception { return java.rmi.Naming.lookup("rmi://" + peerHost + ":" + peerPort + "/" + peerName); }

public void startAsNetworkClientOf(String peerHost, int peerPort, String peerName) throws Exception { this.exportMethods(); java.rmi.Remote peer = this.locatePeer(peerHost, peerPort, peerName); this.startAsClientOf(peer); // see below ... }

public abstract void startAsClientOf(java.rmi.Remote peer) throws java.rmi.RemoteException;

public void startAsNetworkServer(String name, int port) { System.setSecurityManager(new java.rmi.RMISecurityManager()); try { this.exportMethods(); java.rmi.registry.Registry registry = java.rmi.registry.LocateRegistry.createRegistry(port); registry.bind(name, this); this.startAsServer(); // see below ... System.out.println("Server is ready ... "); } catch (Exception e) { System.out.println("Server error: " + e + " ... "); } }

public abstract void startAsServer();

}

Page 75: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

public abstract class NetworkPeer implements java.rmi.Remote {

public void exportMethods() throws java.rmi.RemoteException { ... }

public java.rmi.Remote locatePeer(... peerHost, ... peerPort, ... peerName) ... { ... }

public void startAsNetworkClientOf(String peerHost, int peerPort, String peerName) throws Exception { this.exportMethods(); java.rmi.Remote peer = this.locatePeer(peerHost, peerPort, peerName); this.startAsClientOf(peer); // see below ... }

public abstract void startAsClientOf(java.rmi.Remote peer) throws java.rmi.RemoteException;

public void startAsNetworkServer(String name, int port) { System.setSecurityManager(new java.rmi.RMISecurityManager()); try { this.exportMethods(); java.rmi.registry.Registry registry = java.rmi.registry.LocateRegistry.createRegistry(port); registry.bind(name, this); this.startAsServer(); // see below ... System.out.println("Server is ready ... "); } catch (Exception e) { System.out.println("Server error: " + e + " ... "); } }

public abstract void startAsServer();

}

Page 76: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

public abstract class NetworkPeer implements java.rmi.Remote {

public void exportMethods() throws java.rmi.RemoteException { ... }

public java.rmi.Remote locatePeer(... peerHost, ... peerPort, ... peerName) ... { ... }

public void startAsNetworkClientOf(String peerHost, int peerPort, String peerName) throws Exception { this.exportMethods(); java.rmi.Remote peer = this.locatePeer(peerHost, peerPort, peerName); this.startAsClientOf(peer); // see below ... }

public abstract void startAsClientOf(java.rmi.Remote peer) throws java.rmi.RemoteException;

public void startAsNetworkServer(String name, int port) { System.setSecurityManager(new java.rmi.RMISecurityManager()); try { this.exportMethods(); java.rmi.registry.Registry registry = java.rmi.registry.LocateRegistry.createRegistry(port); registry.bind(name, this); this.startAsServer(); // see below ... System.out.println("Server is ready ... "); } catch (Exception e) { System.out.println("Server error: " + e + " ... "); } }

public abstract void startAsServer();

}

Page 77: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

public abstract class NetworkPeer implements java.rmi.Remote {

public void exportMethods() throws java.rmi.RemoteException { ... }

public java.rmi.Remote locatePeer(... peerHost, ... peerPort, ... peerName) ... { ... }

public void startAsNetworkClientOf(... peerHost, ... peerPort, ... peerName) ... { ... }

public abstract void startAsClientOf(java.rmi.Remote peer) throws java.rmi.RemoteException;

public void startAsNetworkServer(String name, int port) { System.setSecurityManager(new java.rmi.RMISecurityManager()); try { this.exportMethods(); java.rmi.registry.Registry registry = java.rmi.registry.LocateRegistry.createRegistry(port); registry.bind(name, this); this.startAsServer(); // see below ... System.out.println("Server is ready ... "); } catch (Exception e) { System.out.println("Server error: " + e + " ... "); } }

public abstract void startAsServer();

}

Page 78: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

public abstract class NetworkPeer implements java.rmi.Remote {

public void exportMethods() throws java.rmi.RemoteException { ... }

public java.rmi.Remote locatePeer(... peerHost, ... peerPort, ... peerName) ... { ... }

public void startAsNetworkClientOf(... peerHost, ... peerPort, ... peerName) ... { ... }

public abstract void startAsClientOf(java.rmi.Remote peer) throws java.rmi.RemoteException;

public void startAsNetworkServer(String name, int port) { System.setSecurityManager(new java.rmi.RMISecurityManager()); try { this.exportMethods(); java.rmi.registry.Registry registry = java.rmi.registry.LocateRegistry.createRegistry(port); registry.bind(name, this); this.startAsServer(); // see below ... System.out.println("Server is ready ... "); } catch (Exception e) { System.out.println("Server error: " + e + " ... "); } }

public abstract void startAsServer();

}

Page 79: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

public abstract class NetworkPeer implements java.rmi.Remote {

public void exportMethods() throws java.rmi.RemoteException { ... }

public java.rmi.Remote locatePeer(... peerHost, ... peerPort, ... peerName) ... { ... }

public void startAsNetworkClientOf(... peerHost, ... peerPort, ... peerName) ... { ... }

public abstract void startAsClientOf(java.rmi.Remote peer) throws java.rmi.RemoteException;

public void startAsNetworkServer(String name, int port) { System.setSecurityManager(new java.rmi.RMISecurityManager()); try { this.exportMethods(); java.rmi.registry.Registry registry = java.rmi.registry.LocateRegistry.createRegistry(port); registry.bind(name, this); this.startAsServer(); // see below ... System.out.println("Server is ready ... "); } catch (Exception e) { System.out.println("Server error: " + e + " ... "); } }

public abstract void startAsServer();

}

Page 80: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

tucotuco.cs.indiana.edu% pwd/nfs/paca/san/r1a0l1/dgerman/october-21tucotuco.cs.indiana.edu% ls -ld *.java-rw-r--r-- 1 dgerman faculty 761 Oct 21 00:59 ClientImplementation.java-rw-r--r-- 1 dgerman faculty 293 Oct 21 00:57 LocalSetup.java-rw-r--r-- 1 dgerman faculty 1524 Oct 21 00:54 NetworkPeer.java-rw-r--r-- 1 dgerman faculty 117 Oct 21 00:59 Server.java-rw-r--r-- 1 dgerman faculty 551 Oct 21 01:06 ServerImplementation.javatucotuco.cs.indiana.edu% javac *.javatucotuco.cs.indiana.edu% rmic NetworkPeertucotuco.cs.indiana.edu% rmic ServerImplementationtucotuco.cs.indiana.edu% java ServerImplementation 16450 Dirac

burrowww.cs.indiana.edu% pwd/nfs/paca/san/r1a0l1/dgerman/october-21burrowww.cs.indiana.edu% ls -ld *-rw-r--r-- 1 dgerman faculty 975 Oct 21 01:34 ClientImplementation.class-rw-r--r-- 1 dgerman faculty 761 Oct 21 00:59 ClientImplementation.java-rw-r--r-- 1 dgerman faculty 498 Oct 21 01:34 LocalSetup.class-rw-r--r-- 1 dgerman faculty 293 Oct 21 00:57 LocalSetup.java-rw-r--r-- 1 dgerman faculty 1938 Oct 21 01:34 NetworkPeer.class-rw-r--r-- 1 dgerman faculty 1524 Oct 21 00:54 NetworkPeer.java-rw-r--r-- 1 dgerman faculty 908 Oct 21 01:34 NetworkPeer_Skel.class-rw-r--r-- 1 dgerman faculty 482 Oct 21 01:34 NetworkPeer_Stub.class-rw-r--r-- 1 dgerman faculty 191 Oct 21 01:34 Server.class-rw-r--r-- 1 dgerman faculty 117 Oct 21 00:59 Server.java-rw-r--r-- 1 dgerman faculty 886 Oct 21 01:34 ServerImplementation.class-rw-r--r-- 1 dgerman faculty 551 Oct 21 01:06 ServerImplementation.java-rw-r--r-- 1 dgerman faculty 1611 Oct 21 01:34 ServerImplementation_Skel.class-rw-r--r-- 1 dgerman faculty 3134 Oct 21 01:34 ServerImplementation_Stub.classburrowww.cs.indiana.edu% java ClientImplementation Larry tucotuco.cs.indiana.edu 16450 Dirac

burrowww.cs.indiana.edu% java LocalSetupI am adding for the audience...5

burrowww.cs.indiana.edu% java ServerImplementation 16450 DiracServer is ready ... I am adding for the audience...

tucotuco.cs.indiana.edu% java ClientImplementation Larry burrowww.cs.indiana.edu 16450 Dirac5

12

3

4

5

Page 81: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

More sophisticated example:

Page 82: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

More sophisticated example:

Page 83: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

More sophisticated example:

Page 84: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

More sophisticated example:

Page 85: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

More sophisticated example:

Page 86: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

More sophisticated example:

Page 87: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

More sophisticated example:

Page 88: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

In this last example topology was more general.

Clients were also acting as servers.

The only difference is that they still need a moderator to start participating.

Page 89: RMI Observing the Distributed Pattern. Adrian German Lecturer, Computer Science Indiana University Bloomington

Conclusions

1. The NetworkPeer abstraction successfully summarizes the "RMI recipe".

2. The LocalSetup program allows for the local development and testing.

3. The use of this pattern is similar to the use of the MVC pattern.

4. Clients need to be implemented as Threads. Their synchronization rules must be as simple as possible. As "decentralized" as possible, so as to not assume the existence of the OS.

5. Using the programmer's API the application can run both ways.

6. This approach is good when few developers and use of Java is preferred.

7. Further work will address the question:

does an extremely unreliable network breaks any of the above?

Unreliable means: lost references, exceptions are actually the rule, etc.