Commit a48460aa authored by Jonathan Sudibya's avatar Jonathan Sudibya
Browse files

testing tacker

parent cf89d8f6
...@@ -2,11 +2,18 @@ package Controller; ...@@ -2,11 +2,18 @@ package Controller;
import Model.Client; import Model.Client;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.Charset;
import java.util.Vector; import java.util.Vector;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.json.simple.JSONObject;
/* /*
* To change this license header, choose License Headers in Project Properties. * To change this license header, choose License Headers in Project Properties.
...@@ -43,6 +50,9 @@ public class ConnectionHandler implements Runnable { ...@@ -43,6 +50,9 @@ public class ConnectionHandler implements Runnable {
@Override @Override
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...");
connectTracker();
System.out.println("Connection to tracker server successfull... Ready to recieve connection!");
while(active){ while(active){
try { try {
clients.add(new Client(globalID,welcomeSocket.accept())); clients.add(new Client(globalID,welcomeSocket.accept()));
...@@ -66,4 +76,27 @@ public class ConnectionHandler implements Runnable { ...@@ -66,4 +76,27 @@ public class ConnectionHandler implements Runnable {
{ {
active = _active; active = _active;
} }
private void connectTracker()
{
try {
String sentence;
String modifiedSentence;
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
Socket newServer = new Socket("167.205.32.46", 8000);
DataOutputStream outToTracker = new DataOutputStream(newServer.getOutputStream());
BufferedReader inFromTracker = new BufferedReader(new InputStreamReader(newServer.getInputStream()));
JSONObject join = new JSONObject();
join.put("method", "join");
join.put("ip",InetAddress.getLocalHost().getHostAddress());
join.put("port",6789);
sentence = join.toJSONString();
outToTracker.write(sentence.getBytes(Charset.forName("UTF-8")));
modifiedSentence = inFromTracker.readLine();
System.out.println("FROM TRACKER: " + modifiedSentence);
newServer.close();
} catch (IOException ex) {
Logger.getLogger(ConnectionHandler.class.getName()).log(Level.SEVERE, null, ex);
}
}
} }
...@@ -59,8 +59,12 @@ public class Client implements Runnable { ...@@ -59,8 +59,12 @@ public class Client implements Runnable {
if (obj != null){ if (obj != null){
JSONObject data = (JSONObject) obj; JSONObject data = (JSONObject) obj;
} }
capitalizedSentence = clientSentence.toUpperCase() + '\n'; JSONObject correct = new JSONObject();
correct.put("status", "ok");
capitalizedSentence = correct.toJSONString();
outToClient.writeBytes(capitalizedSentence); outToClient.writeBytes(capitalizedSentence);
connectionSocket.close();
active = false;
} }
else{ else{
active = false; active = false;
......
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