chat rmi server/client and peer2peer

This commit is contained in:
2014-12-10 01:49:28 +01:00
parent 4d3128b724
commit b01bc963c8
11 changed files with 394 additions and 0 deletions

44
rmi_chat_p2p/Client.java Normal file
View File

@@ -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<ClientInterface>();
}
@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<ClientInterface> getList() throws RemoteException{
return list;
}
private ArrayList<ClientInterface> list;
}

View File

@@ -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;
}

View File

@@ -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<ClientInterface> list = ((Client)ci).getList();
Iterator<ClientInterface> it = list.iterator();
for ( ;it.hasNext();){
it.next().speak(new Message(name,cmd));
}
} else {
ArrayList<ClientInterface> list = ((Client)ci).getList();
Iterator<ClientInterface> 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";
}
}

26
rmi_chat_p2p/Message.java Normal file
View File

@@ -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;
}