Commit ecf70eb1 authored by Rafi Ramadhan's avatar Rafi Ramadhan
Browse files

clientHandler

parent 84503872
......@@ -11,6 +11,7 @@ import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.simple.parser.ParseException;
......@@ -36,53 +37,55 @@ public class ClientHandler extends Thread {
try {
DataInputStream in = new DataInputStream(socketListener.getInputStream());
response = in.readUTF();
System.out.println(response);
DataOutputStream out = new DataOutputStream(socketListener.getOutputStream());
if(parse.parseJSON(response, "method").equals("login")) {
out.writeUTF(handler.Login(response));
}
else if(parse.parseJSON(response, "method").equals("signup")) {
out.writeUTF(handler.Signup(response));
}
else if(parse.parseJSON(response, "method").equals("inventory")) {
out.writeUTF(handler.Inventory(response));
}
else if(parse.parseJSON(response, "method").equals("mixitem")) {
out.writeUTF(handler.MixItem(response));
}
else if(parse.parseJSON(response, "method").equals("map")) {
out.writeUTF(handler.Map(response));
}
else if(parse.parseJSON(response, "method").equals("move")) {
out.writeUTF(handler.Move(response));
}
else if(parse.parseJSON(response, "method").equals("field")) {
out.writeUTF(handler.Field(response));
}
else if(parse.parseJSON(response, "method").equals("offer")) {
out.writeUTF(handler.Offer(response));
}
else if(parse.parseJSON(response, "method").equals("tradebox")) {
out.writeUTF(handler.TradeBox(response));
}
else if(parse.parseJSON(response, "method").equals("sendfind")) {
out.writeUTF(handler.sendFind(response));
}
else if(parse.parseJSON(response, "method").equals("findoffer")) {
out.writeUTF(handler.sendFind(response));
}
else if(parse.parseJSON(response, "method").equals("sendaccept")) {
out.writeUTF(handler.sendFind(response));
}
else if(parse.parseJSON(response, "method").equals("accept")) {
out.writeUTF(handler.sendFind(response));
}
else {
out.writeUTF("Thank you for connecting to "
+ socketListener.getLocalSocketAddress() + "\nGoodbye!");
}
switch (parse.parseJSON(response, "method")) {
case "login":
String login = handler.Login(response);
out.writeUTF(login);
break;
case "signup":
out.writeUTF(handler.Signup(response));
break;
case "inventory":
out.writeUTF(handler.Inventory(response));
break;
case "mixitem":
out.writeUTF(handler.MixItem(response));
break;
case "map":
out.writeUTF(handler.Map(response));
break;
case "move":
out.writeUTF(handler.Move(response));
break;
case "field":
out.writeUTF(handler.Field(response));
break;
case "offer":
out.writeUTF(handler.Offer(response));
break;
case "tradebox":
out.writeUTF(handler.TradeBox(response));
break;
case "sendfind":
out.writeUTF(handler.sendFind(response));
break;
case "findoffer":
out.writeUTF(handler.sendFind(response));
break;
case "sendaccept":
out.writeUTF(handler.sendFind(response));
break;
case "accept":
out.writeUTF(handler.sendFind(response));
break;
default:
out.writeUTF("Thank you for connecting to "
+ socketListener.getLocalSocketAddress() + "\nGoodbye!");
break;
}
} catch (IOException ex) {
Logger.getLogger(ClientHandler.class.getName()).log(Level.SEVERE, null, ex);
try {
......@@ -92,6 +95,8 @@ public class ClientHandler extends Thread {
}
} 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);
}
}
}
......
......@@ -12,236 +12,245 @@ import java.util.List;
import java.util.Map;
import org.json.simple.JSONValue;
import org.json.simple.parser.ParseException;
import java.sql.*;
/**
*
* @author Rakhmatullah Yoga S
*/
public class MethodHandler {
public String Signup(String input) {
JSONParse parse = new JSONParse();
Map response = new LinkedHashMap();
response = parse.signupJSON(true);
return JSONValue.toJSONString(response);
}
public String Login(String input) throws ParseException {
JSONParse parse = new JSONParse();
Map response = new LinkedHashMap();
if(parse.parseJSON(input, "username").equals("user")) {
public String Signup(String input) {
JSONParse parse = new JSONParse();
Map response = new LinkedHashMap();
response = parse.signupJSON(true);
return JSONValue.toJSONString(response);
}
public String Login(String input) throws ParseException, SQLException {
DBHandler db = new DBHandler();
JSONParse parse = new JSONParse();
Map response = new LinkedHashMap();
response = parse.loginJSON(false);
String username = parse.parseJSON(input, "username");
String password = parse.parseJSON(input, "password");
boolean valid = db.authenticate(username, password);
if(valid) {
response = parse.loginJSON(true);
}
else {
response = parse.loginJSON(false);
}
return JSONValue.toJSONString(response);
}
return JSONValue.toJSONString(response);
}
public String Inventory(String input) {
List<Integer> inv = new ArrayList<>();
JSONParse parse = new JSONParse();
Map response = new LinkedHashMap();
inv.add(1); inv.add(2); inv.add(3); inv.add(4); inv.add(5);
inv.add(1); inv.add(2); inv.add(3); inv.add(4); inv.add(5);
response = parse.invJSON(inv);
return JSONValue.toJSONString(response);
}
public String MixItem(String input) throws ParseException {
JSONParse parse = new JSONParse();
Map response = new LinkedHashMap();
Long item1 = parse.parseIntJSON(input, "item1");
Long item2 = parse.parseIntJSON(input, "item2");
int item_id = Combine(item1,item2);
response = parse.mixItemJSON(item_id, true);
System.out.println(JSONValue.toJSONString(response));
return JSONValue.toJSONString(response);
}
public int Combine(Long item1, Long item2) {
int id = 0;
return id;
}
public String Map(String input) throws ParseException {
JSONParse parse = new JSONParse();
Map response = new LinkedHashMap();
String location = parse.parseJSON(input, "name");
Long width = parse.parseIntJSON(input, "width");
Long height = parse.parseIntJSON(input, "height");
response = parse.mapJSON(location, width, height);
System.out.println(JSONValue.toJSONString(response));
return JSONValue.toJSONString(response);
}
public String Move(String input) throws ParseException {
JSONParse parse = new JSONParse();
Map response = new LinkedHashMap();
String token = parse.parseJSON(input, "token");
Long x = parse.parseIntJSON(input, "x");
Long y = parse.parseIntJSON(input, "y");
Long time = 6876781679l;
response = parse.moveJSON(token, time, true);
System.out.println(JSONValue.toJSONString(response));
return JSONValue.toJSONString(response);
}
public String Field(String input) throws ParseException {
JSONParse parse = new JSONParse();
Map response = new LinkedHashMap();
String token = parse.parseJSON(input, "token");
int item = 4;
response = parse.fieldJSON(token, item, true);
System.out.println(JSONValue.toJSONString(response));
return JSONValue.toJSONString(response);
}
public String Offer(String input) throws ParseException {
JSONParse parse = new JSONParse();
Map response = new LinkedHashMap();
response = parse.offerJSON(true);
System.out.println(JSONValue.toJSONString(response));
return JSONValue.toJSONString(response);
}
public String TradeBox(String input) throws ParseException {
List<Object> offer = new ArrayList<>();
List<Object> temp = new ArrayList<>();
List<Object> offers = new ArrayList<>();
JSONParse parse = new JSONParse();
Map response = new LinkedHashMap();
offer.add(5); offer.add(3); offer.add(6);
offer.add(1); offer.add(false); offer.add("we23klf9vv4");
temp = copyList(offer);
offer.clear();
offers.add(temp);
//offers.add(temp);
response = parse.tradeboxJSON(offers);
System.out.println(JSONValue.toJSONString(response));
return JSONValue.toJSONString(response);
}
public List<Object> copyList(List<Object> target) {
List<Object> result = new ArrayList<>();
for (int i=0; i<target.size(); i++) {
result.add(target.get(i));
public String Inventory(String input) {
List<Integer> inv = new ArrayList<>();
JSONParse parse = new JSONParse();
Map response = new LinkedHashMap();
inv.add(1); inv.add(2); inv.add(3); inv.add(4); inv.add(5);
inv.add(1); inv.add(2); inv.add(3); inv.add(4); inv.add(5);
response = parse.invJSON(inv);
return JSONValue.toJSONString(response);
}
return result;
}
public String sendFind(String input) throws ParseException {
List<Object> offer = new ArrayList<>();
List<Object> temp = new ArrayList<>();
List<Object> offers = new ArrayList<>();
JSONParse parse = new JSONParse();
Map response = new LinkedHashMap();
Long item_id = parse.parseIntJSON(input, "item");
System.out.println(item_id);
if (item_id==2) {
offer.add(5); offer.add(3); offer.add(6);
offer.add(1); offer.add(false); offer.add("we23klf9vv4");
temp = copyList(offer);
offer.clear();
offers.add(temp);
public String MixItem(String input) throws ParseException {
JSONParse parse = new JSONParse();
Map response = new LinkedHashMap();
Long item1 = parse.parseIntJSON(input, "item1");
Long item2 = parse.parseIntJSON(input, "item2");
int item_id = Combine(item1,item2);
response = parse.mixItemJSON(item_id, true);
System.out.println(JSONValue.toJSONString(response));
return JSONValue.toJSONString(response);
}
else {
public int Combine(Long item1, Long item2) {
int id = 0;
return id;
}
response = parse.sendfindJSON(offers);
System.out.println(JSONValue.toJSONString(response));
return JSONValue.toJSONString(response);
}
public String findOffer(String input) throws ParseException {
List<Object> offer = new ArrayList<>();
List<Object> temp = new ArrayList<>();
List<Object> offers = new ArrayList<>();
JSONParse parse = new JSONParse();
Map response = new LinkedHashMap();
Long item_id = parse.parseIntJSON(input, "item");
if (item_id==2) {
public String Map(String input) throws ParseException {
JSONParse parse = new JSONParse();
Map response = new LinkedHashMap();
String location = parse.parseJSON(input, "name");
Long width = parse.parseIntJSON(input, "width");
Long height = parse.parseIntJSON(input, "height");
response = parse.mapJSON(location, width, height);
System.out.println(JSONValue.toJSONString(response));
return JSONValue.toJSONString(response);
}
public String Move(String input) throws ParseException {
JSONParse parse = new JSONParse();
Map response = new LinkedHashMap();
String token = parse.parseJSON(input, "token");
Long x = parse.parseIntJSON(input, "x");
Long y = parse.parseIntJSON(input, "y");
Long time = 6876781679l;
response = parse.moveJSON(token, time, true);
System.out.println(JSONValue.toJSONString(response));
return JSONValue.toJSONString(response);
}
public String Field(String input) throws ParseException {
JSONParse parse = new JSONParse();
Map response = new LinkedHashMap();
String token = parse.parseJSON(input, "token");
int item = 4;
response = parse.fieldJSON(token, item, true);
System.out.println(JSONValue.toJSONString(response));
return JSONValue.toJSONString(response);
}
public String Offer(String input) throws ParseException {
JSONParse parse = new JSONParse();
Map response = new LinkedHashMap();
response = parse.offerJSON(true);
System.out.println(JSONValue.toJSONString(response));
return JSONValue.toJSONString(response);
}
public String TradeBox(String input) throws ParseException {
List<Object> offer = new ArrayList<>();
List<Object> temp = new ArrayList<>();
List<Object> offers = new ArrayList<>();
JSONParse parse = new JSONParse();
Map response = new LinkedHashMap();
offer.add(5); offer.add(3); offer.add(6);
offer.add(1); offer.add(false); offer.add("we23klf9vv4");
temp = copyList(offer);
offer.clear();
offers.add(temp);
//offers.add(temp);
response = parse.tradeboxJSON(offers);
System.out.println(JSONValue.toJSONString(response));
return JSONValue.toJSONString(response);
}
else {
public List<Object> copyList(List<Object> target) {
List<Object> result = new ArrayList<>();
for (int i=0; i<target.size(); i++) {
result.add(target.get(i));
}
return result;
}
public String sendFind(String input) throws ParseException {
List<Object> offer = new ArrayList<>();
List<Object> temp = new ArrayList<>();
List<Object> offers = new ArrayList<>();
JSONParse parse = new JSONParse();
Map response = new LinkedHashMap();
Long item_id = parse.parseIntJSON(input, "item");
System.out.println(item_id);
if (item_id==2) {
offer.add(5); offer.add(3); offer.add(6);
offer.add(1); offer.add(false); offer.add("we23klf9vv4");
temp = copyList(offer);
offer.clear();
offers.add(temp);
}
else {
}
response = parse.sendfindJSON(offers);
System.out.println(JSONValue.toJSONString(response));
return JSONValue.toJSONString(response);
}
public String findOffer(String input) throws ParseException {
List<Object> offer = new ArrayList<>();
List<Object> temp = new ArrayList<>();
List<Object> offers = new ArrayList<>();
JSONParse parse = new JSONParse();
Map response = new LinkedHashMap();
Long item_id = parse.parseIntJSON(input, "item");
if (item_id==2) {
offer.add(5); offer.add(3); offer.add(6);
offer.add(1); offer.add(false); offer.add("we23klf9vv4");
temp = copyList(offer);
offer.clear();
offers.add(temp);
}
else {
}
response = parse.sendfindJSON(offers);
System.out.println(JSONValue.toJSONString(response));
return JSONValue.toJSONString(response);
}
public String sendAccept(String input) {
JSONParse parse = new JSONParse();
Map response = new LinkedHashMap();
response = parse.sendacceptJSON(true);
return JSONValue.toJSONString(response);
}
public String Accept(String input) {
JSONParse parse = new JSONParse();
Map response = new LinkedHashMap();
response = parse.acceptJSON(true);
return JSONValue.toJSONString(response);
}
public String fetchItem(String input) {
JSONParse parse = new JSONParse();
Map response = new LinkedHashMap();
response = parse.fetchitemJSON(true);
return JSONValue.toJSONString(response);
}
public String cancelOffer(String input) {
JSONParse parse = new JSONParse();
Map response = new LinkedHashMap();
response = parse.cancelofferJSON(true);
return JSONValue.toJSONString(response);
}
response = parse.sendfindJSON(offers);
System.out.println(JSONValue.toJSONString(response));
return JSONValue.toJSONString(response);
}
public String sendAccept(String input) {
JSONParse parse = new JSONParse();
Map response = new LinkedHashMap();
response = parse.sendacceptJSON(true);
return JSONValue.toJSONString(response);
}
public String Accept(String input) {
JSONParse parse = new JSONParse();
Map response = new LinkedHashMap();
response = parse.acceptJSON(true);
return JSONValue.toJSONString(response);
}
public String fetchItem(String input) {
JSONParse parse = new JSONParse();
Map response = new LinkedHashMap();
response = parse.fetchitemJSON(true);
return JSONValue.toJSONString(response);
}
public String cancelOffer(String input) {
JSONParse parse = new JSONParse();
Map response = new LinkedHashMap();
response = parse.cancelofferJSON(true);