Skip to content

Commit

Permalink
Adding client id system.
Browse files Browse the repository at this point in the history
  • Loading branch information
CORNOU Benjamin committed Oct 8, 2018
1 parent 223982f commit 9026622
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 36 deletions.
62 changes: 62 additions & 0 deletions fr/bencor29/datatransfer/server/Client.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package fr.bencor29.datatransfer.server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

import fr.bencor29.datatransfer.Utils;
import fr.bencor29.datatransfer.events.TransferEvent;
import fr.bencor29.datatransfer.events.listeners.TransferListener;

public class Client {

private static int lastID = 0;

private int id;
private Socket socket;
private Thread th;

public Client(Socket so, DTServer server) {
lastID++;

this.id = lastID;
this.socket = so;
th = new Thread(new Runnable() {

@Override
public void run() {
while(!socket.isClosed())
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String str = reader.readLine();
if(str != null) {
TransferEvent tev = new TransferEvent(socket, str);
for(TransferListener tl : server.getListeners())
tl.onReceive(tev);
}
} catch (IOException e) {}
}
});
}

public int getID() {
return id;
}

public Socket getSocket() {
return socket;
}

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

public void sendString(String str) throws IOException {
Utils.sendString(socket, str);
}

}
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
package fr.bencor29.datatransfer;
package fr.bencor29.datatransfer.server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;

import fr.bencor29.datatransfer.DTMaster;
import fr.bencor29.datatransfer.events.ConnectionEvent;
import fr.bencor29.datatransfer.events.TransferEvent;
import fr.bencor29.datatransfer.events.listeners.ConnectionListener;
import fr.bencor29.datatransfer.events.listeners.TransferListener;

public class DTServer extends DTMaster {

private ServerSocket ss;
private Thread t;
private ArrayList<ConnectionListener> cl;
private HashMap<Socket, Thread> sts;
private ArrayList<Client> sts;

private boolean close;

Expand All @@ -29,7 +24,7 @@ public DTServer(int port) throws IOException {
ss.setSoTimeout(100);

cl = new ArrayList<>();
sts = new HashMap<>();
sts = new ArrayList<>();

close = false;
t = new Thread(new Runnable() {
Expand All @@ -42,24 +37,8 @@ public void run() {
for(ConnectionListener clis : cl)
clis.onConnection(ev);
if(!ev.isCancelled()) {
Thread t = new Thread(new Runnable() {

@Override
public void run() {
while(!s.isClosed())
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(s.getInputStream()));
String str = reader.readLine();
if(str != null) {
TransferEvent tev = new TransferEvent(s, str);
for(TransferListener tl : DTServer.super.getListeners())
tl.onReceive(tev);
}
} catch (IOException e) {}
}
});
sts.put(s, t);
t.start();
Client c = new Client(s, DTServer.this);
sts.add(c);
}
} catch (IOException e) {}
try {
Expand All @@ -70,13 +49,23 @@ public void run() {
t.start();
}

public void checkClient(Client c) {
if(c.getSocket().isClosed() && sts.contains(c))
sts.remove(c);
else if(!c.getSocket().isClosed() && !sts.contains(c))
sts.add(c);
}

public void broadcast(String str) throws IOException {
for(Socket s : sts.keySet())
Utils.sendString(s, str);
for(Client c : sts)
c.sendString(str);
}

public Socket[] getSockets() {
return (Socket[]) sts.keySet().toArray();
ArrayList<Socket> sockets = new ArrayList<>();
for(Client c : sts)
sockets.add(c.getSocket());
return (Socket[]) sockets.toArray();
}

public void addListener(ConnectionListener cl) {
Expand All @@ -93,11 +82,8 @@ public ArrayList<ConnectionListener> getConnectionListeners() {

public void stop() {
this.close = true;
for(Entry<Socket, Thread> st : sts.entrySet())
try {
st.getKey().close();
st.getValue().interrupt();
} catch (IOException e) {}
for(Client c : sts)
c.close();
}

public boolean isAlive() {
Expand Down
2 changes: 1 addition & 1 deletion test/TTServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import java.io.IOException;

import fr.bencor29.datatransfer.DTServer;
import fr.bencor29.datatransfer.events.ConnectionEvent;
import fr.bencor29.datatransfer.events.TransferEvent;
import fr.bencor29.datatransfer.events.listeners.ConnectionListener;
import fr.bencor29.datatransfer.events.listeners.TransferListener;
import fr.bencor29.datatransfer.server.DTServer;

public class TTServer {

Expand Down

0 comments on commit 9026622

Please sign in to comment.