From 40a630623a204461372c3c71016af730d24d5691 Mon Sep 17 00:00:00 2001 From: Giovanni Di Grezia Date: Tue, 2 Dec 2014 21:50:57 +0100 Subject: [PATCH] synchronized su awarenes e chat via tcp --- rmi_Awareness/HelloImplemented.java | 4 +- socket_chat_tcp/Client.java | 83 +++++++++++++++++++++ socket_chat_tcp/ClientInputManagement.java | 38 ++++++++++ socket_chat_tcp/LaunchClient.java | 24 ++++++ socket_chat_tcp/LaunchServer.java | 20 +++++ socket_chat_tcp/Server.java | 43 +++++++++++ socket_chat_tcp/ServerClientHandler.java | 86 ++++++++++++++++++++++ 7 files changed, 296 insertions(+), 2 deletions(-) create mode 100644 socket_chat_tcp/Client.java create mode 100644 socket_chat_tcp/ClientInputManagement.java create mode 100644 socket_chat_tcp/LaunchClient.java create mode 100644 socket_chat_tcp/LaunchServer.java create mode 100644 socket_chat_tcp/Server.java create mode 100644 socket_chat_tcp/ServerClientHandler.java diff --git a/rmi_Awareness/HelloImplemented.java b/rmi_Awareness/HelloImplemented.java index 95db181..2ac2fc3 100644 --- a/rmi_Awareness/HelloImplemented.java +++ b/rmi_Awareness/HelloImplemented.java @@ -20,7 +20,7 @@ public class HelloImplemented extends UnicastRemoteObject implements HelloInterf } @Override - public void register(call_me_back obj) throws RemoteException { + public synchronized void register(call_me_back obj) throws RemoteException { v.add(obj); Iterator it = v.iterator(); for (;it.hasNext();){ @@ -30,7 +30,7 @@ public class HelloImplemented extends UnicastRemoteObject implements HelloInterf } @Override - public void deregister(call_me_back obj) throws RemoteException { + public synchronized void deregister(call_me_back obj) throws RemoteException { v.remove(obj); Iterator it = v.iterator(); for (;it.hasNext();){ diff --git a/socket_chat_tcp/Client.java b/socket_chat_tcp/Client.java new file mode 100644 index 0000000..1f73b47 --- /dev/null +++ b/socket_chat_tcp/Client.java @@ -0,0 +1,83 @@ +package socket_chat_tcp; + +import java.io.*; +import java.net.Socket; +import java.util.logging.Logger; + +/** + * Created with xgiovio.macbookair. + * User: xgiovio + * Date: 02/12/14 + * Time: 13:54 + */ +public class Client { + + private static final Logger l = Logger.getLogger(ClientInputManagement.class.getName()); + + public Client (String inome ,String iaddress, int iport) { + + address = iaddress; + port= iport; + nome = inome; + + } + + public void launch (){ + + Socket connection = null; + + try { + connection = new Socket(address,port); + DataOutputStream o = new DataOutputStream(new BufferedOutputStream(connection.getOutputStream())); + DataInputStream i = new DataInputStream(new BufferedInputStream(connection.getInputStream())); + BufferedReader userinput = new BufferedReader(new InputStreamReader(System.in)); + + ClientInputManagement cim = new ClientInputManagement(i); + cim.start(); + + o.writeUTF(nome); + o.flush(); + + String command = command(userinput); + for (; !("!quit".equals(command)) ;){ + o.writeUTF(command); + o.flush(); + command = command(userinput); + } + + + } catch (IOException e) { + e.printStackTrace(); + } + + finally { + try { + connection.close(); + } catch (IOException e) { + e.printStackTrace(); + } + + } + + + } + + + private String command (BufferedReader i){ + try { + System.out.print(">> "); + return i.readLine(); + } catch (IOException e) { + e.printStackTrace(); + } + return ""; + } + + + + private final String address; + private final int port; + private final String nome; + + +} diff --git a/socket_chat_tcp/ClientInputManagement.java b/socket_chat_tcp/ClientInputManagement.java new file mode 100644 index 0000000..971bcf6 --- /dev/null +++ b/socket_chat_tcp/ClientInputManagement.java @@ -0,0 +1,38 @@ +package socket_chat_tcp; + +import java.io.DataInputStream; +import java.io.IOException; +import java.util.logging.Logger; + +/** + * Created with xgiovio.macbookair. + * User: xgiovio + * Date: 02/12/14 + * Time: 14:04 + */ +public class ClientInputManagement extends Thread { + + private static final Logger l = Logger.getLogger(ClientInputManagement.class.getName()); + + public ClientInputManagement (DataInputStream input){ + i=input; + } + + + @Override + public void run() { + while (true){ + try { + String ridden = i.readUTF(); + System.out.println(ridden); + } catch (IOException e) { + System.out.println("Bye"); + break; + } + + } + } + + private final DataInputStream i; + +} diff --git a/socket_chat_tcp/LaunchClient.java b/socket_chat_tcp/LaunchClient.java new file mode 100644 index 0000000..581bd80 --- /dev/null +++ b/socket_chat_tcp/LaunchClient.java @@ -0,0 +1,24 @@ +package socket_chat_tcp; + +/** + * Created with xgiovio.macbookair. + * User: xgiovio + * Date: 02/12/14 + * Time: 17:28 + */ +public class LaunchClient { + + public static void main (String[] args) { + + Client c1 = new Client("mario","localhost",20000); + c1.launch(); + + + + + + + } + + +} diff --git a/socket_chat_tcp/LaunchServer.java b/socket_chat_tcp/LaunchServer.java new file mode 100644 index 0000000..4aca159 --- /dev/null +++ b/socket_chat_tcp/LaunchServer.java @@ -0,0 +1,20 @@ +package socket_chat_tcp; + +/** + * Created with xgiovio.macbookair. + * User: xgiovio + * Date: 02/12/14 + * Time: 18:20 + */ +public class LaunchServer { + + public static void main (String[] args) { + + Server s = new Server(20000); + s.launch(); + + + } + + +} diff --git a/socket_chat_tcp/Server.java b/socket_chat_tcp/Server.java new file mode 100644 index 0000000..baea519 --- /dev/null +++ b/socket_chat_tcp/Server.java @@ -0,0 +1,43 @@ +package socket_chat_tcp; + +import java.io.*; +import java.net.ServerSocket; +import java.net.Socket; + +/** + * Created with xgiovio.macbookair. + * User: xgiovio + * Date: 02/12/14 + * Time: 17:30 + */ +public class Server { + + public Server ( int iport){ + port = iport; + } + + + public void launch (){ + + try { + ServerSocket socket = new ServerSocket(port); + + while (true) { + Socket accepted = socket.accept(); + + ServerClientHandler sch = new ServerClientHandler(accepted); + sch.start(); + + } + + } catch (IOException e) { + e.printStackTrace(); + } + + + } + + + private final int port; + +} diff --git a/socket_chat_tcp/ServerClientHandler.java b/socket_chat_tcp/ServerClientHandler.java new file mode 100644 index 0000000..5cdc779 --- /dev/null +++ b/socket_chat_tcp/ServerClientHandler.java @@ -0,0 +1,86 @@ +package socket_chat_tcp; + +import java.io.*; +import java.net.Socket; +import java.util.Iterator; +import java.util.Vector; +import java.util.logging.Logger; + +/** + * Created with xgiovio.macbookair. + * User: xgiovio + * Date: 02/12/14 + * Time: 17:36 + */ +public class ServerClientHandler extends Thread { + + + + public ServerClientHandler (Socket isclient){ + sclient=isclient; + + + + } + + @Override + public void run() { + + synchronized (vs) { + vs.add(sclient); + } + + try { + + DataInputStream i = new DataInputStream(new BufferedInputStream(sclient.getInputStream())); + name = i.readUTF(); + broadcast(name + " si e' connesso"); + while (true){ + String m = i.readUTF(); + broadcast(name + ": " + m); + } + + } catch (IOException e) { + + } + + finally { + try { + sclient.close(); + synchronized (vs){ + vs.remove(sclient); + } + broadcast(name + " si e' disonnesso"); + } catch (IOException e) { + e.printStackTrace(); + } + } + + + } + + + private void broadcast (String s){ + Iterator it = vs.iterator(); + for (;it.hasNext();){ + Socket next = it.next(); + if (next != sclient) { + try { + DataOutputStream o = new DataOutputStream(new BufferedOutputStream(next.getOutputStream())); + o.writeUTF(s); + o.flush(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } + + + private String name; + private final Socket sclient; + private static final Vector vs = new Vector(); + + + +}