From 9b143f4f9c7899fcc5f2cf543f0b0f8593ee0fa6 Mon Sep 17 00:00:00 2001 From: Bencor29 Date: Mon, 8 Oct 2018 18:52:00 +0200 Subject: [PATCH] Client implementation & tests completed --- fr/bencor29/datatransfer/Utils.java | 4 ++++ .../datatransfer/events/ClientEvent.java | 11 +++++++++ .../datatransfer/events/ConnectionEvent.java | 22 +++++++++++++---- fr/bencor29/datatransfer/events/Event.java | 2 +- .../events/ServerTransferEvent.java | 24 +++++++++++++++++++ fr/bencor29/datatransfer/server/Client.java | 19 +++++++++++---- fr/bencor29/datatransfer/server/DTServer.java | 5 ++-- test/TTClient.java | 16 ++++++++++++- test/TTServer.java | 18 ++++++++++---- 9 files changed, 103 insertions(+), 18 deletions(-) create mode 100644 fr/bencor29/datatransfer/events/ClientEvent.java create mode 100644 fr/bencor29/datatransfer/events/ServerTransferEvent.java diff --git a/fr/bencor29/datatransfer/Utils.java b/fr/bencor29/datatransfer/Utils.java index 7b5bab3..570a6a9 100644 --- a/fr/bencor29/datatransfer/Utils.java +++ b/fr/bencor29/datatransfer/Utils.java @@ -11,6 +11,10 @@ public static void sendString(Socket s, String str) throws IOException { pw.println(str); pw.flush(); } + + public static String getAddress(Socket socket) { + return socket.getInetAddress().toString().split("/")[1]; + } } diff --git a/fr/bencor29/datatransfer/events/ClientEvent.java b/fr/bencor29/datatransfer/events/ClientEvent.java new file mode 100644 index 0000000..69291af --- /dev/null +++ b/fr/bencor29/datatransfer/events/ClientEvent.java @@ -0,0 +1,11 @@ +package fr.bencor29.datatransfer.events; + +import fr.bencor29.datatransfer.server.Client; + +public abstract interface ClientEvent { + + public Client getClient(); + + public int getClientID(); + +} diff --git a/fr/bencor29/datatransfer/events/ConnectionEvent.java b/fr/bencor29/datatransfer/events/ConnectionEvent.java index 8ed9e3c..771608c 100644 --- a/fr/bencor29/datatransfer/events/ConnectionEvent.java +++ b/fr/bencor29/datatransfer/events/ConnectionEvent.java @@ -1,14 +1,16 @@ package fr.bencor29.datatransfer.events; -import java.net.Socket; +import fr.bencor29.datatransfer.server.Client; -public class ConnectionEvent extends Event { +public class ConnectionEvent extends Event implements ClientEvent { private boolean c; + private Client cl; - public ConnectionEvent(Socket f) { - super(f); - c = false; + public ConnectionEvent(Client c) { + super(c.getSocket()); + this.c = false; + this.cl = c; } public boolean isCancelled() { @@ -19,4 +21,14 @@ public void setCancelled(boolean cancelled) { this.c = cancelled; } + @Override + public Client getClient() { + return cl; + } + + @Override + public int getClientID() { + return cl.getID(); + } + } diff --git a/fr/bencor29/datatransfer/events/Event.java b/fr/bencor29/datatransfer/events/Event.java index 0aa05c9..a3e9783 100644 --- a/fr/bencor29/datatransfer/events/Event.java +++ b/fr/bencor29/datatransfer/events/Event.java @@ -18,7 +18,7 @@ public Socket getSocket() { } public String getAddress() { - return s.getInetAddress().toString().split("/")[1]; + return Utils.getAddress(s); } public void send(String str) throws IOException { diff --git a/fr/bencor29/datatransfer/events/ServerTransferEvent.java b/fr/bencor29/datatransfer/events/ServerTransferEvent.java new file mode 100644 index 0000000..d43e372 --- /dev/null +++ b/fr/bencor29/datatransfer/events/ServerTransferEvent.java @@ -0,0 +1,24 @@ +package fr.bencor29.datatransfer.events; + +import fr.bencor29.datatransfer.server.Client; + +public class ServerTransferEvent extends TransferEvent implements ClientEvent { + + private Client c; + + public ServerTransferEvent(Client c, String d) { + super(c.getSocket(), d); + this.c = c; + } + + @Override + public Client getClient() { + return c; + } + + @Override + public int getClientID() { + return c.getID(); + } + +} diff --git a/fr/bencor29/datatransfer/server/Client.java b/fr/bencor29/datatransfer/server/Client.java index 8a3c588..0ae6e5b 100644 --- a/fr/bencor29/datatransfer/server/Client.java +++ b/fr/bencor29/datatransfer/server/Client.java @@ -6,7 +6,7 @@ import java.net.Socket; import fr.bencor29.datatransfer.Utils; -import fr.bencor29.datatransfer.events.TransferEvent; +import fr.bencor29.datatransfer.events.ServerTransferEvent; import fr.bencor29.datatransfer.events.listeners.TransferListener; public class Client { @@ -17,7 +17,7 @@ public class Client { private Socket socket; private Thread th; - public Client(Socket so, DTServer server) { + protected Client(Socket so, DTServer server) { lastID++; this.id = lastID; @@ -31,19 +31,28 @@ public void run() { BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); String str = reader.readLine(); if(str != null) { - TransferEvent tev = new TransferEvent(socket, str); + ServerTransferEvent ste = new ServerTransferEvent(Client.this, str); for(TransferListener tl : server.getListeners()) - tl.onReceive(tev); + tl.onReceive(ste); } } catch (IOException e) {} } }); } + protected void accept() { + if(!th.isAlive()) + th.start(); + } + public int getID() { return id; } + public String getAddress() { + return Utils.getAddress(socket); + } + public Socket getSocket() { return socket; } @@ -51,7 +60,7 @@ public Socket getSocket() { public void close() { try { socket.close(); - th.interrupt(); + th.interrupt(); } catch (IOException e) {} } diff --git a/fr/bencor29/datatransfer/server/DTServer.java b/fr/bencor29/datatransfer/server/DTServer.java index 43d2335..f241ebf 100644 --- a/fr/bencor29/datatransfer/server/DTServer.java +++ b/fr/bencor29/datatransfer/server/DTServer.java @@ -33,12 +33,13 @@ public void run() { while(!close) try { Socket s = ss.accept(); - ConnectionEvent ev = new ConnectionEvent(s); + Client c = new Client(s, DTServer.this); + ConnectionEvent ev = new ConnectionEvent(c); for(ConnectionListener clis : cl) clis.onConnection(ev); if(!ev.isCancelled()) { - Client c = new Client(s, DTServer.this); sts.add(c); + c.accept(); } } catch (IOException e) {} try { diff --git a/test/TTClient.java b/test/TTClient.java index 2c57df1..704d5dd 100644 --- a/test/TTClient.java +++ b/test/TTClient.java @@ -5,16 +5,30 @@ import java.net.UnknownHostException; import fr.bencor29.datatransfer.DTClient; +import fr.bencor29.datatransfer.events.TransferEvent; +import fr.bencor29.datatransfer.events.listeners.TransferListener; public class TTClient { - public static void main(String[] args) throws UnknownHostException, IOException { + public static void main(String[] args) throws UnknownHostException, IOException, InterruptedException { // We connect to the server port DTClient c = new DTClient(InetAddress.getLocalHost(), 8888); + // TransferListener are used when a string is received + c.addListener(new TransferListener() { + + @Override + public void onReceive(TransferEvent event) { + System.out.println("[Client] Received \"" + event.getData() + "\" from server."); + } + }); + // We send a simple string c.send("Hello, World!"); + // We wait 1s for the server reply + Thread.sleep(1000); + // We close the socket // Highly recommended because it also stop running threads c.close(); diff --git a/test/TTServer.java b/test/TTServer.java index 0553820..0b5364a 100644 --- a/test/TTServer.java +++ b/test/TTServer.java @@ -3,6 +3,7 @@ import java.io.IOException; import fr.bencor29.datatransfer.events.ConnectionEvent; +import fr.bencor29.datatransfer.events.ServerTransferEvent; import fr.bencor29.datatransfer.events.TransferEvent; import fr.bencor29.datatransfer.events.listeners.ConnectionListener; import fr.bencor29.datatransfer.events.listeners.TransferListener; @@ -13,7 +14,7 @@ public class TTServer { public static void main(String[] args) throws IOException, InterruptedException { // Server created with port 8888 DTServer srv = new DTServer(8888); - System.out.println("Server openned"); + System.out.println("[Server] openned"); // Let's add listeners // We can add as many Listener as we want @@ -25,7 +26,7 @@ public static void main(String[] args) throws IOException, InterruptedException @Override public void onConnection(ConnectionEvent event) { - System.out.println("Client connected with ip: " + event.getAddress()); + System.out.println("[Server] Client connected with ip: " + event.getAddress()); } }); @@ -34,7 +35,16 @@ public void onConnection(ConnectionEvent event) { @Override public void onReceive(TransferEvent event) { - System.out.println("Received \"" + event.getData() + "\" from " + event.getAddress()); + // The server receive a ServerTransferEvent. ServerTransferEvent is a TransferEvent with a Client object + ServerTransferEvent ev = (ServerTransferEvent) event; + + System.out.println("[Server] Received \"" + ev.getData() + "\" from " + ev.getClientID() + " (@" + ev.getClient().getAddress() + ")"); + try { + // We reply to the client + ev.send("Data received !"); + } catch (IOException e) { + e.printStackTrace(); + } } }); @@ -45,7 +55,7 @@ public void onReceive(TransferEvent event) { // This method close the ServerSocket and all // the Client's sockets. This also stop running threads. srv.stop(); - System.out.println("Server closed"); + System.out.println("[Server] closed"); } }