diff --git a/rmi_chat/ChatInterface.java b/rmi_chat/ChatInterface.java new file mode 100644 index 0000000..8799fb9 --- /dev/null +++ b/rmi_chat/ChatInterface.java @@ -0,0 +1,16 @@ +package rmi_chat; + +import java.rmi.Remote; +import java.rmi.RemoteException; + +/** + * Created by Giovanni on 09/12/2014. + */ +public interface ChatInterface extends Remote{ + + void speak (Message m) throws RemoteException; + void register (ClientInterface c, String n) throws RemoteException; + void unregister (ClientInterface c, String n) throws RemoteException; + + +} diff --git a/rmi_chat/ChatServer.java b/rmi_chat/ChatServer.java new file mode 100644 index 0000000..a8dd940 --- /dev/null +++ b/rmi_chat/ChatServer.java @@ -0,0 +1,46 @@ +package rmi_chat; + +import java.rmi.RemoteException; +import java.rmi.server.UnicastRemoteObject; +import java.util.ArrayList; +import java.util.Iterator; + +/** + * Created by Giovanni on 09/12/2014. + */ +public class ChatServer extends UnicastRemoteObject implements ChatInterface { + + public ChatServer () throws RemoteException{ + list = new ArrayList(); + } + + + @Override + public void speak( Message m) throws RemoteException { + Iterator it = list.iterator(); + for (;it.hasNext();){ + it.next().spoken(m); + } + } + + @Override + public void register(ClientInterface c, String n) throws RemoteException { + Iterator it = list.iterator(); + for (;it.hasNext();){ + it.next().welcome(n); + } + list.add(c); + } + + @Override + public void unregister(ClientInterface c, String n) throws RemoteException { + Iterator it = list.iterator(); + for (;it.hasNext();){ + it.next().bye(n); + } + list.remove(c); + } + + + private ArrayList list; +} diff --git a/rmi_chat/Client.java b/rmi_chat/Client.java new file mode 100644 index 0000000..a1070e2 --- /dev/null +++ b/rmi_chat/Client.java @@ -0,0 +1,31 @@ +package rmi_chat; + +import java.rmi.RemoteException; +import java.rmi.server.UnicastRemoteObject; + +/** + * Created by Giovanni on 10/12/2014. + */ +public class Client extends UnicastRemoteObject implements ClientInterface { + + public Client () throws RemoteException { + + } + + @Override + public void spoken(Message m) throws RemoteException { + System.out.println(m.getFrom() +" : " + m.getM()); + } + + @Override + public void bye(String to) throws RemoteException { + System.out.println(to + " has quit"); + + } + + @Override + public void welcome(String to) throws RemoteException { + System.out.println(to + " has joined"); + + } +} diff --git a/rmi_chat/ClientInterface.java b/rmi_chat/ClientInterface.java new file mode 100644 index 0000000..fe93f52 --- /dev/null +++ b/rmi_chat/ClientInterface.java @@ -0,0 +1,15 @@ +package rmi_chat; + +import java.rmi.Remote; +import java.rmi.RemoteException; + +/** + * Created by Giovanni on 09/12/2014. + */ +public interface ClientInterface extends Remote { + + void spoken (Message m) throws RemoteException; + void bye (String to) throws RemoteException; + void welcome (String to) throws RemoteException; + +} diff --git a/rmi_chat/LaunchClient.java b/rmi_chat/LaunchClient.java new file mode 100644 index 0000000..fb662da --- /dev/null +++ b/rmi_chat/LaunchClient.java @@ -0,0 +1,60 @@ +package rmi_chat; + +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 10/12/2014. + */ +public class LaunchClient { + + public static void main (String[] args ) { + + String name = "default_name"; // or args[0] + + try { + ClientInterface ci = new Client(); + ChatInterface server = (ChatInterface) Naming.lookup("rmi://localhost/serverchat"); + server.register(ci,name); + BufferedReader i = new BufferedReader(new InputStreamReader(System.in)); + while (true) { + String cmd = command(i); + if (!cmd.equals("quit")) { + server.speak(new Message(name, cmd)); + } else { + server.unregister(ci, name); + break; + } + } + } catch (RemoteException e) { + e.printStackTrace(); + } catch (MalformedURLException e) { + e.printStackTrace(); + } catch (NotBoundException e) { + e.printStackTrace(); + } + finally { + System.exit(0); + } + + + } + + private static String command (BufferedReader i){ + try { + return i.readLine(); + } catch (IOException e) { + e.printStackTrace(); + } + return "quit"; + } + + + + +} diff --git a/rmi_chat/LaunchServer.java b/rmi_chat/LaunchServer.java new file mode 100644 index 0000000..ade8466 --- /dev/null +++ b/rmi_chat/LaunchServer.java @@ -0,0 +1,29 @@ +package rmi_chat; + +import java.rmi.RemoteException; +import java.rmi.registry.LocateRegistry; +import java.rmi.registry.Registry; + +/** + * Created by Giovanni on 10/12/2014. + */ +public class LaunchServer { + + public static void main (String[] args ){ + + try { + ChatInterface cs = new ChatServer(); + Registry r = LocateRegistry.getRegistry(); + r.rebind("serverchat",cs); + + + + } catch (RemoteException e) { + e.printStackTrace(); + } + + + } + + +} diff --git a/rmi_chat/Message.java b/rmi_chat/Message.java new file mode 100644 index 0000000..afc80ef --- /dev/null +++ b/rmi_chat/Message.java @@ -0,0 +1,28 @@ +package rmi_chat; + +import rmi_hello_world_factory.ServerFactory; + +import java.io.Serializable; + +/** + * Created by Giovanni on 10/12/2014. + */ +public class Message implements Serializable{ + + public Message (String ifrom, String im){ + from = ifrom; + m = im; + } + + public String getFrom() { + return from; + } + + public String getM() { + return m; + } + + private final String from; + private final String m; + +} diff --git a/rmi_chat_p2p/Client.java b/rmi_chat_p2p/Client.java new file mode 100644 index 0000000..9248204 --- /dev/null +++ b/rmi_chat_p2p/Client.java @@ -0,0 +1,44 @@ +package rmi_chat_p2p; + + + +import java.rmi.RemoteException; +import java.rmi.server.UnicastRemoteObject; +import java.util.ArrayList; + +/** + * Created by Giovanni on 10/12/2014. + */ +public class Client extends UnicastRemoteObject implements ClientInterface { + + public Client() throws RemoteException { + list = new ArrayList(); + } + + + @Override + public void speak(Message m) throws RemoteException { + System.out.println(m.getFrom() +" : " + m.getM()); + } + + @Override + public void register(ClientInterface c, String n) throws RemoteException { + list.add(c); + System.out.println(n + " has joined"); + + } + + @Override + public void unregister(ClientInterface c, String n) throws RemoteException { + list.remove(c); + System.out.println(n + " has quit"); + + } + + public ArrayList getList() throws RemoteException{ + return list; + } + + private ArrayList list; + +} diff --git a/rmi_chat_p2p/ClientInterface.java b/rmi_chat_p2p/ClientInterface.java new file mode 100644 index 0000000..6fb7365 --- /dev/null +++ b/rmi_chat_p2p/ClientInterface.java @@ -0,0 +1,17 @@ +package rmi_chat_p2p; + +import java.rmi.Remote; +import java.rmi.RemoteException; +import java.util.ArrayList; + +/** + * Created by Giovanni on 09/12/2014. + */ +public interface ClientInterface extends Remote { + + + void speak (Message m) throws RemoteException; + void register (ClientInterface c, String n) throws RemoteException; + void unregister (ClientInterface c, String n) throws RemoteException; + +} diff --git a/rmi_chat_p2p/LaunchClient.java b/rmi_chat_p2p/LaunchClient.java new file mode 100644 index 0000000..d590dd3 --- /dev/null +++ b/rmi_chat_p2p/LaunchClient.java @@ -0,0 +1,82 @@ +package rmi_chat_p2p; + +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; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.logging.Logger; + +/** + * Created by Giovanni on 10/12/2014. + */ +public class LaunchClient { + + public static Logger l = Logger.getLogger("p2p"); + public static void main (String[] args ) { + + String name = "default_name"; // or args[0] + String registry_id_name = name + String.valueOf(Math.random()); + System.out.println("Name : " + name + " id :" + registry_id_name); + + try { + ClientInterface ci = new Client(); + String[] nodes = Naming.list("rmi://localhost"); + l.info("List of connected nodes from registry : " + nodes.length ); + for (int i = 0 ; i < nodes.length;i++){ + l.info("Naming lookup of " + nodes[i] ); + ClientInterface node = (ClientInterface) Naming.lookup( nodes[i]); + node.register(ci,name); + ((Client)ci).getList().add(node); + } + Naming.rebind(registry_id_name,ci); + + + BufferedReader i = new BufferedReader(new InputStreamReader(System.in)); + while (true) { + String cmd = command(i); + if (!cmd.equals("quit")) { + ArrayList list = ((Client)ci).getList(); + Iterator it = list.iterator(); + for ( ;it.hasNext();){ + it.next().speak(new Message(name,cmd)); + } + } else { + ArrayList list = ((Client)ci).getList(); + Iterator it = list.iterator(); + for ( ;it.hasNext();){ + it.next().unregister(ci,name); + } + Naming.unbind(registry_id_name); + break; + } + } + } catch (RemoteException e) { + e.printStackTrace(); + } catch (MalformedURLException e) { + e.printStackTrace(); + } catch (NotBoundException e) { + e.printStackTrace(); + } + finally { + System.exit(0); + } + + + } + + private static String command (BufferedReader i){ + try { + return i.readLine(); + } catch (IOException e) { + e.printStackTrace(); + } + return "quit"; + } + + +} diff --git a/rmi_chat_p2p/Message.java b/rmi_chat_p2p/Message.java new file mode 100644 index 0000000..237f4de --- /dev/null +++ b/rmi_chat_p2p/Message.java @@ -0,0 +1,26 @@ +package rmi_chat_p2p; + +import java.io.Serializable; + +/** + * Created by Giovanni on 10/12/2014. + */ +public class Message implements Serializable{ + + public Message(String ifrom, String im){ + from = ifrom; + m = im; + } + + public String getFrom() { + return from; + } + + public String getM() { + return m; + } + + private final String from; + private final String m; + +}