Commit 97f096dc authored by Jonathan Sudibya's avatar Jonathan Sudibya
Browse files

tracker final

parent a48460aa
...@@ -33,13 +33,15 @@ public class ConnectionHandler implements Runnable { ...@@ -33,13 +33,15 @@ public class ConnectionHandler implements Runnable {
private Vector<Client> clients; private Vector<Client> clients;
private boolean active; private boolean active;
private int globalID; private int globalID;
private int serverSocket;
public ConnectionHandler(){ public ConnectionHandler(){
threadName = "Connection-Handler"; threadName = "Connection-Handler";
active = true; active = true;
globalID = 0; globalID = 0;
serverSocket = 6789;
try { try {
welcomeSocket = new ServerSocket(6789); welcomeSocket = new ServerSocket(serverSocket);
clients = new Vector<>(); clients = new Vector<>();
} catch (IOException ex) { } catch (IOException ex) {
Logger.getLogger(ConnectionHandler.class.getName()).log(Level.SEVERE, null, ex); Logger.getLogger(ConnectionHandler.class.getName()).log(Level.SEVERE, null, ex);
...@@ -51,17 +53,22 @@ public class ConnectionHandler implements Runnable { ...@@ -51,17 +53,22 @@ public class ConnectionHandler implements Runnable {
public void run() { public void run() {
System.out.println("Running thread " + threadName); System.out.println("Running thread " + threadName);
System.out.println("Connecting to tracker server..."); System.out.println("Connecting to tracker server...");
connectTracker(); if (connectTracker())
System.out.println("Connection to tracker server successfull... Ready to recieve connection!"); {
while(active){ System.out.println("Connection to tracker server successfull... Ready to recieve connection!");
try { while(active){
clients.add(new Client(globalID,welcomeSocket.accept())); try {
clients.lastElement().start(); clients.add(new Client(globalID,welcomeSocket.accept()));
globalID++; clients.lastElement().start();
} catch (IOException ex) { globalID++;
Logger.getLogger(ConnectionHandler.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) {
Logger.getLogger(ConnectionHandler.class.getName()).log(Level.SEVERE, null, ex);
}
} }
} }
else {
System.out.println("Error, Failed to connect server.. ");
}
System.out.println("Thread " + threadName + " exiting..."); System.out.println("Thread " + threadName + " exiting...");
} }
...@@ -77,8 +84,9 @@ public class ConnectionHandler implements Runnable { ...@@ -77,8 +84,9 @@ public class ConnectionHandler implements Runnable {
active = _active; active = _active;
} }
private void connectTracker() private boolean connectTracker()
{ {
boolean ret;
try { try {
String sentence; String sentence;
String modifiedSentence; String modifiedSentence;
...@@ -95,8 +103,11 @@ public class ConnectionHandler implements Runnable { ...@@ -95,8 +103,11 @@ public class ConnectionHandler implements Runnable {
modifiedSentence = inFromTracker.readLine(); modifiedSentence = inFromTracker.readLine();
System.out.println("FROM TRACKER: " + modifiedSentence); System.out.println("FROM TRACKER: " + modifiedSentence);
newServer.close(); newServer.close();
ret = true;
} catch (IOException ex) { } catch (IOException ex) {
ret = false;
Logger.getLogger(ConnectionHandler.class.getName()).log(Level.SEVERE, null, ex); Logger.getLogger(ConnectionHandler.class.getName()).log(Level.SEVERE, null, ex);
} }
return ret;
} }
} }
...@@ -2,6 +2,7 @@ package Model; ...@@ -2,6 +2,7 @@ package Model;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
...@@ -29,7 +30,6 @@ public class Client implements Runnable { ...@@ -29,7 +30,6 @@ public class Client implements Runnable {
private Socket connectionSocket; private Socket connectionSocket;
private Thread t; private Thread t;
private boolean active; private boolean active;
private String clientSentence;
private String capitalizedSentence; private String capitalizedSentence;
public Client(int _id,Socket _client) public Client(int _id,Socket _client)
...@@ -48,16 +48,29 @@ public class Client implements Runnable { ...@@ -48,16 +48,29 @@ public class Client implements Runnable {
while(active) while(active)
{ {
DataOutputStream outToClient = null; DataOutputStream outToClient = null;
BufferedReader inFromClient = null; //BufferedReader inFromClient = null;
InputStreamReader inFromClient = null;
String clientSentence = "";
int buf;
try { try {
inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); System.out.println("Client ip "+threadName+ " :" +connectionSocket.getRemoteSocketAddress());
clientSentence = inFromClient.readLine(); //connectionSocket.setSoTimeout(3000);
if (clientSentence != null){ //inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
//clientSentence = inFromClient.readLine();
inFromClient = new InputStreamReader(connectionSocket.getInputStream());
while ((buf=inFromClient.read()) != -1){
char c = (char)buf;
clientSentence += Character.toString(c);
//System.out.print(c);
}
//System.out.println();
System.out.println("Received From "+connectionSocket.getInetAddress().getHostAddress()+ ": " + clientSentence);
if (!clientSentence.equals("")){
outToClient = new DataOutputStream(connectionSocket.getOutputStream()); outToClient = new DataOutputStream(connectionSocket.getOutputStream());
System.out.println("Received: " + clientSentence);
Object obj = parser.parse(clientSentence); Object obj = parser.parse(clientSentence);
if (obj != null){ if (obj != null){
JSONObject data = (JSONObject) obj; JSONObject data = (JSONObject) obj;
System.out.println("Message :" + data.toJSONString());
} }
JSONObject correct = new JSONObject(); JSONObject correct = new JSONObject();
correct.put("status", "ok"); correct.put("status", "ok");
...@@ -67,6 +80,7 @@ public class Client implements Runnable { ...@@ -67,6 +80,7 @@ public class Client implements Runnable {
active = false; active = false;
} }
else{ else{
System.out.println("Null message detected");
active = false; active = false;
} }
} catch (IOException ex) { } catch (IOException ex) {
...@@ -76,7 +90,7 @@ public class Client implements Runnable { ...@@ -76,7 +90,7 @@ public class Client implements Runnable {
} finally { } finally {
try { try {
inFromClient.close(); inFromClient.close();
outToClient.close(); //outToClient.close();
} catch (IOException ex) { } catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex); Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment