small changes to factory and implemented rmi awareness with a simple register/deregister program
This commit is contained in:
18
rmi_Awareness/Client.java
Normal file
18
rmi_Awareness/Client.java
Normal 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);
|
||||
}
|
||||
}
|
||||
44
rmi_Awareness/HelloImplemented.java
Normal file
44
rmi_Awareness/HelloImplemented.java
Normal 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;
|
||||
|
||||
}
|
||||
13
rmi_Awareness/HelloInterface.java
Normal file
13
rmi_Awareness/HelloInterface.java
Normal 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;
|
||||
|
||||
}
|
||||
55
rmi_Awareness/LaunchClient.java
Normal file
55
rmi_Awareness/LaunchClient.java
Normal 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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
28
rmi_Awareness/LaunchServer.java
Normal file
28
rmi_Awareness/LaunchServer.java
Normal 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();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
15
rmi_Awareness/call_me_back.java
Normal file
15
rmi_Awareness/call_me_back.java
Normal 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;
|
||||
|
||||
}
|
||||
13
rmi_Awareness/register_to_server.java
Normal file
13
rmi_Awareness/register_to_server.java
Normal 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;
|
||||
}
|
||||
@@ -9,7 +9,7 @@ import java.rmi.server.UnicastRemoteObject;
|
||||
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 {
|
||||
nome=in;
|
||||
|
||||
@@ -9,7 +9,7 @@ import java.rmi.server.UnicastRemoteObject;
|
||||
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 {
|
||||
nome=in;
|
||||
|
||||
@@ -8,6 +8,8 @@ import java.rmi.server.UnicastRemoteObject;
|
||||
*/
|
||||
public class ServerFactoryImplemented extends UnicastRemoteObject implements ServerFactory {
|
||||
|
||||
private static final long serialVersionUID = 2025591618699941250L;
|
||||
|
||||
|
||||
public ServerFactoryImplemented() throws RemoteException {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user