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

edit clientHandler

parent 79a8e39a
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package grandquestserver;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.*;
import org.json.simple.*;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
/**
*
* @author Rakhmatullah Yoga S
*/
public class Server extends Thread {
private ServerSocket serverSocket;
public Server(int port) throws IOException {
serverSocket = new ServerSocket(port);
//serverSocket.setSoTimeout(10000);
}
@Override
public void run()
{
String response;
JSONParse parse = new JSONParse();
MethodHandler handler = new MethodHandler();
while(true)
{
try
{
System.out.println("Waiting for client on port " +
serverSocket.getLocalPort() + "...");
try (Socket server = serverSocket.accept()) {
System.out.println("Just connected to "
+ server.getRemoteSocketAddress());
DataInputStream in =
new DataInputStream(server.getInputStream());
response = in.readUTF();
System.out.println(response);
DataOutputStream out =
new DataOutputStream(server.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 "
+ server.getLocalSocketAddress() + "\nGoodbye!");
}
server.close();
} catch (ParseException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
}catch(SocketTimeoutException s)
{
System.out.println("Socket timed out!");
break;
}catch(IOException e)
{
break;
}
}
}
}
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