Skip to content

Commit

Permalink
Client implementation & tests completed
Browse files Browse the repository at this point in the history
  • Loading branch information
Bencor29 committed Oct 8, 2018
1 parent 9026622 commit 9b143f4
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 18 deletions.
4 changes: 4 additions & 0 deletions fr/bencor29/datatransfer/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}


}
11 changes: 11 additions & 0 deletions fr/bencor29/datatransfer/events/ClientEvent.java
Original file line number Diff line number Diff line change
@@ -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();

}
22 changes: 17 additions & 5 deletions fr/bencor29/datatransfer/events/ConnectionEvent.java
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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();
}

}
2 changes: 1 addition & 1 deletion fr/bencor29/datatransfer/events/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
24 changes: 24 additions & 0 deletions fr/bencor29/datatransfer/events/ServerTransferEvent.java
Original file line number Diff line number Diff line change
@@ -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();
}

}
19 changes: 14 additions & 5 deletions fr/bencor29/datatransfer/server/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
Expand All @@ -31,27 +31,36 @@ 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;
}

public void close() {
try {
socket.close();
th.interrupt();
th.interrupt();
} catch (IOException e) {}
}

Expand Down
5 changes: 3 additions & 2 deletions fr/bencor29/datatransfer/server/DTServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
16 changes: 15 additions & 1 deletion test/TTClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
18 changes: 14 additions & 4 deletions test/TTServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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());
}
});

Expand All @@ -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();
}
}
});

Expand All @@ -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");
}

}

0 comments on commit 9b143f4

Please sign in to comment.