small changes to factory and implemented rmi awareness with a simple register/deregister program

This commit is contained in:
2014-12-01 02:03:48 +01:00
parent 238c61f4d3
commit 1ef2207245
10 changed files with 190 additions and 2 deletions

18
rmi_Awareness/Client.java Normal file
View File

@@ -0,0 +1,18 @@
package rmi_Awareness;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
/**
* Created by Giovanni on 01/12/2014.
*/
public class Client extends UnicastRemoteObject implements call_me_back {
public Client() throws RemoteException {
}
@Override
public void notify(String in) throws RemoteException {
System.out.println(in);
}
}

View File

@@ -0,0 +1,44 @@
package rmi_Awareness;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.Iterator;
import java.util.Vector;
/**
* Created by Giovanni on 01/12/2014.
*/
public class HelloImplemented extends UnicastRemoteObject implements HelloInterface, register_to_server {
public HelloImplemented () throws RemoteException{
v = new Vector<call_me_back>();
}
@Override
public String sayhello() throws RemoteException {
return "HI!";
}
@Override
public void register(call_me_back obj) throws RemoteException {
v.add(obj);
Iterator<call_me_back> it = v.iterator();
for (;it.hasNext();){
it.next().notify("Un nuovo utente si e' registrato");
}
}
@Override
public void deregister(call_me_back obj) throws RemoteException {
v.remove(obj);
Iterator<call_me_back> it = v.iterator();
for (;it.hasNext();){
it.next().notify("Un utente si e' cancellato ");
}
}
private Vector<call_me_back> v;
}

View File

@@ -0,0 +1,13 @@
package rmi_Awareness;
import java.rmi.Remote;
import java.rmi.RemoteException;
/**
* Created by Giovanni on 01/12/2014.
*/
public interface HelloInterface extends Remote {
String sayhello() throws RemoteException;
}

View File

@@ -0,0 +1,55 @@
package rmi_Awareness;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
/**
* Created by Giovanni on 01/12/2014.
*/
public class LaunchClient {
public static void main (String[] args) {
BufferedReader i = new BufferedReader(new InputStreamReader(System.in));
register_to_server rs = null;
call_me_back client = null;
try {
client = new Client();
Object server_object = (Object) Naming.lookup("rmi://localhost/HelloServer");
rs = (register_to_server) server_object;
rs.register(client);
((HelloInterface) server_object).sayhello();
System.out.println("Press enter to exit and deregister");
i.readLine();
} catch (RemoteException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (NotBoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
rs.deregister(client);
} catch (RemoteException e) {
e.printStackTrace();
}
System.exit(0);
}
}
}

View File

@@ -0,0 +1,28 @@
package rmi_Awareness;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
/**
* Created by Giovanni on 01/12/2014.
*/
public class LaunchServer {
public static void main (String[] args){
try {
HelloInterface h = new HelloImplemented();
Registry r = LocateRegistry.getRegistry();
r.rebind("HelloServer",h);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}

View File

@@ -0,0 +1,15 @@
package rmi_Awareness;
import java.rmi.Remote;
import java.rmi.RemoteException;
/**
* Created by Giovanni on 01/12/2014.
*/
public interface call_me_back extends Remote{
void notify (String in) throws RemoteException;
}

View File

@@ -0,0 +1,13 @@
package rmi_Awareness;
import java.rmi.Remote;
import java.rmi.RemoteException;
/**
* Created by Giovanni on 01/12/2014.
*/
public interface register_to_server extends Remote {
void register (call_me_back obj) throws RemoteException;
void deregister (call_me_back obj) throws RemoteException;
}

View File

@@ -9,7 +9,7 @@ import java.rmi.server.UnicastRemoteObject;
public class HelloImplementedFr extends UnicastRemoteObject implements Hello { public class HelloImplementedFr extends UnicastRemoteObject implements Hello {
private static final long serialVersionUID = 2025591618699941247L; private static final long serialVersionUID = 2025591618699941248L;
public HelloImplementedFr(String in) throws RemoteException { public HelloImplementedFr(String in) throws RemoteException {
nome=in; nome=in;

View File

@@ -9,7 +9,7 @@ import java.rmi.server.UnicastRemoteObject;
public class HelloImplementedIt extends UnicastRemoteObject implements Hello { public class HelloImplementedIt extends UnicastRemoteObject implements Hello {
private static final long serialVersionUID = 2025591618699941247L; private static final long serialVersionUID = 2025591618699941249L;
public HelloImplementedIt(String in) throws RemoteException { public HelloImplementedIt(String in) throws RemoteException {
nome=in; nome=in;

View File

@@ -8,6 +8,8 @@ import java.rmi.server.UnicastRemoteObject;
*/ */
public class ServerFactoryImplemented extends UnicastRemoteObject implements ServerFactory { public class ServerFactoryImplemented extends UnicastRemoteObject implements ServerFactory {
private static final long serialVersionUID = 2025591618699941250L;
public ServerFactoryImplemented() throws RemoteException { public ServerFactoryImplemented() throws RemoteException {