Commit 0046a56f authored by Rafi Ramadhan's avatar Rafi Ramadhan
Browse files

Koreksi clientHandler

parent 13cdf1b9
......@@ -12,6 +12,7 @@ import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
......@@ -23,10 +24,12 @@ import org.json.simple.parser.ParseException;
*/
public class ClientHandler extends Thread {
private Socket socketListener;
private ServerSocket server;
public ClientHandler(ServerSocket sock) throws IOException {
System.out.println("Waiting for client on " + InetAddress.getLocalHost().getHostAddress()+ ":" + sock.getLocalPort() + "...");
socketListener = sock.accept();
server = sock;
socketListener = server.accept();
System.out.println("Just connected to " + socketListener.getRemoteSocketAddress());
}
@Override
......@@ -36,11 +39,11 @@ public class ClientHandler extends Thread {
MethodHandler handler = new MethodHandler();
while(true) {
try {
DataInputStream in = new DataInputStream(socketListener.getInputStream());
response = in.readUTF();
DataOutputStream out = new DataOutputStream(socketListener.getOutputStream());
switch (parse.parseJSON(response, "method")) {
case "login":
......@@ -88,7 +91,6 @@ public class ClientHandler extends Thread {
+ socketListener.getLocalSocketAddress() + "\nGoodbye!");
break;
}
//out.writeUTF("Hello client");
} catch (IOException ex) {
Logger.getLogger(ClientHandler.class.getName()).log(Level.SEVERE, null, ex);
try {
......@@ -100,7 +102,9 @@ public class ClientHandler extends Thread {
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);
}
}
}
}
......@@ -26,12 +26,12 @@ public class JSONParse {
}
//Respon JSON untuk login
public Map loginJSON(boolean valid) {
public Map loginJSON(boolean valid, String token) {
Map response = new LinkedHashMap();
if (valid) {
response.put("status", "OK");
response.put("token", "bss7d91234fef");
response.put("token", token);
response.put("x", 0);
response.put("y", 0);
response.put("time", 1213131311);
......
......@@ -6,6 +6,8 @@
package grandquestserver;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
......@@ -13,12 +15,29 @@ import java.util.Map;
import org.json.simple.JSONValue;
import org.json.simple.parser.ParseException;
import java.sql.*;
import java.util.Calendar;
/**
*
* @author Rakhmatullah Yoga S
*/
public class MethodHandler {
public String genToken(String value) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(value.getBytes());
byte byteData[] = md.digest();
StringBuffer sb = new StringBuffer();
for(int i=0; i<byteData.length; i++) {
sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
}
return sb.toString();
}
public String Signup(String input) {
JSONParse parse = new JSONParse();
......@@ -29,7 +48,7 @@ public class MethodHandler {
return JSONValue.toJSONString(response);
}
public String Login(String input) throws ParseException, SQLException {
public String Login(String input) throws ParseException, SQLException, NoSuchAlgorithmException {
DBHandler db = new DBHandler();
JSONParse parse = new JSONParse();
......@@ -37,14 +56,15 @@ public class MethodHandler {
String username = parse.parseJSON(input, "username");
String password = parse.parseJSON(input, "password");
String token = genToken(username);
boolean valid = db.authenticate(username, password);
if(valid) {
response = parse.loginJSON(true);
response = parse.loginJSON(true, token);
}
else {
response = parse.loginJSON(false);
response = parse.loginJSON(false, token);
}
return JSONValue.toJSONString(response);
}
......
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