Commit 45571847 authored by Rakhmatullah Yoga Sutrisna's avatar Rakhmatullah Yoga Sutrisna
Browse files

lanjut server

parent c4e21383
......@@ -31,8 +31,8 @@ public class DummyClient {
List<String> value = new ArrayList<>();
key.add("method"); value.add("login");
key.add("username"); value.add("user");
key.add("password"); value.add("bbjbjb");
key.add("username"); value.add("rafi");
key.add("password"); value.add("rafi");
request = manager.createJSON(key, value);
try {
System.out.println("Connecting to "+hostName+" on port "+portNumber);
......
......@@ -40,16 +40,13 @@ public class ClientHandler extends Thread {
MethodHandler handler = new MethodHandler();
while(true) {
try {
DataInputStream in = new DataInputStream(socketListener.getInputStream());
response = in.readUTF();
System.out.println(response);
DataOutputStream out = new DataOutputStream(socketListener.getOutputStream());
switch (parse.parseJSON(response, "method")) {
case "login":
out.writeUTF(handler.Login(response));
break;
case "signup":
out.writeUTF(handler.Signup(response));
......@@ -105,13 +102,7 @@ public class ClientHandler extends Thread {
} catch (IOException ex1) {
//Logger.getLogger(ClientHandler.class.getName()).log(Level.SEVERE, null, ex1);
}
} catch (ParseException ex) {
Logger.getLogger(ClientHandler.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(ClientHandler.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoSuchAlgorithmException ex) {
Logger.getLogger(ClientHandler.class.getName()).log(Level.SEVERE, null, ex);
} catch (JSONException ex) {
} catch (ParseException | SQLException | NoSuchAlgorithmException | JSONException ex) {
Logger.getLogger(ClientHandler.class.getName()).log(Level.SEVERE, null, ex);
}
}
......
......@@ -8,9 +8,10 @@ package grandquestserver;
import java.io.IOException;
import java.net.ServerSocket;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONException;
import org.json.simple.parser.ParseException;
/**
*
......@@ -21,22 +22,23 @@ public class GrandQuestServer {
/**
* @param args the command line arguments
* @throws java.io.IOException
* @throws java.sql.SQLException
* @throws java.lang.ClassNotFoundException
* @throws java.lang.InstantiationException
* @throws java.lang.IllegalAccessException
* @throws org.json.simple.parser.ParseException
* @throws org.json.JSONException
*/
public static void main(String[] args) throws IOException, SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
public static void main(String[] args) throws IOException, ParseException, JSONException {
int port = 8000;
List<Thread> clientList = new ArrayList<>();
ServerSocket serverSock = new ServerSocket(port);
while(true) {
Thread t = new ClientHandler(serverSock);
t.start();
clientList.add(t);
TrackerListener trackerListen = new TrackerListener();
trackerListen.newServerRequest();
trackerListen.start();
if(trackerListen.ready) {
List<Thread> clientList = new ArrayList<>();
ServerSocket serverSock = new ServerSocket(port);
while(true) {
Thread t = new ClientHandler(serverSock);
t.start();
clientList.add(t);
}
}
//DBHandler database = new DBHandler();
//database.addInventory("ardi", 2);
}
}
......@@ -155,7 +155,8 @@ public class MethodHandler {
int x = db.getPosX(username);
int y = db.getPosY(username);
log.setUsername(username); log.setToken(token);
log.setUsername(username);
log.setToken(token);
session.add(log);
boolean valid = db.authenticate(username, password);
......
......@@ -6,13 +6,18 @@
package grandquestserver;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.OutputStream;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.simple.JSONValue;
import org.json.simple.parser.ParseException;
/**
......@@ -20,10 +25,11 @@ import org.json.simple.parser.ParseException;
* @author Rakhmatullah Yoga S
*/
public class TrackerListener extends Thread {
private Socket trackListener;
private final Socket trackListener;
private final String trackerAddress = "167.205.32.46";
private final int trackerPort = 8000;
private boolean ready;
private JSONArray serverList;
public boolean ready;
public TrackerListener() throws IOException {
System.out.println("Connecting to tracker . . .");
......@@ -31,20 +37,28 @@ public class TrackerListener extends Thread {
System.out.println("Just connected to tracker!");
ready = false;
}
public void newServerRequest() throws ParseException {
public void newServerRequest() throws ParseException, JSONException {
try {
OutputStream outToServer = trackListener.getOutputStream();
DataOutputStream out = new DataOutputStream(outToServer);
JSONParse parser = new JSONParse();
out.writeUTF(trackerAddress);
DataInputStream in = new DataInputStream(trackListener.getInputStream());
String response = in.readUTF();
switch (parser.parseJSON(response, "status")) {
JSONParse parse = new JSONParse();
String output = JSONValue.toJSONString(parse.newServerJSON(trackListener));
trackListener.getOutputStream().write(output.getBytes());
BufferedReader in = new BufferedReader(new InputStreamReader(trackListener.getInputStream()));
char[] buffer = new char[4096];
int length_buff = 0;
String response = "{}";
if((length_buff = in.read(buffer)) >= 0) {
response = new String(buffer).substring(0, length_buff);
}
switch (parse.parseJSON(response, "status")) {
case "ok":
// nyimpen data semua server
JSONObject obj = new JSONObject(response);
serverList = obj.getJSONArray("value");
ready = true;
System.out.println("Server established!");
System.out.println(serverList);
break;
case "error":
System.out.println("Server failed to establish!");
break;
default:
break;
......@@ -55,6 +69,32 @@ public class TrackerListener extends Thread {
}
@Override
public void run() {
try {
while (true) {
JSONParse parse = new JSONParse();
BufferedReader in = new BufferedReader(new InputStreamReader(trackListener.getInputStream()));
char[] buffer = new char[4096];
int length_buff = 0;
String response = "{}";
if((length_buff = in.read(buffer)) >= 0) {
response = new String(buffer).substring(0, length_buff);
System.out.println("Tracker: "+response);
if(response.length()!=0) {
if (parse.parseJSON(response, "method").equals("serverStatus")) {
JSONObject obj = new JSONObject(response);
serverList = obj.getJSONArray("server");
Map responseUpdate = new LinkedHashMap();
responseUpdate.put("status", "ok");
}
else {
Map responseUpdate = new LinkedHashMap();
responseUpdate.put("status", "error");
}
}
}
}
} catch (IOException | ParseException | JSONException ex) {
Logger.getLogger(TrackerListener.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