Commit 0d30fd8e authored by Junita Sinambela's avatar Junita Sinambela
Browse files

taktau

parent e0c9d8b5
......@@ -8,11 +8,8 @@ package server;
import java.io.*;
import java.net.Socket;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import server.helper.Message;
import server.helper.ServerLogger;
/**
*
......@@ -25,7 +22,7 @@ public class ClientHandler extends Thread{
}
@Override
public void run(){
log("Received request from IP : " + clientSocket.getInetAddress());
ServerLogger.log("CLIENT_THREAD", "Received request from IP : " + clientSocket.getInetAddress());
try {
InputStream in = clientSocket.getInputStream();
DataOutputStream out = new DataOutputStream(clientSocket.getOutputStream());
......@@ -35,25 +32,18 @@ public class ClientHandler extends Thread{
String request = new String(byt);
if(!request.equals("")){
// request = request.substring(0,count);
log("Request : " + request);
ServerLogger.log("CLIENT_THREAD", "Request : " + request);
String response = Message.receive(request);
log("Response : " + response);
ServerLogger.log("CLIENT_THREAD", "Response : " + response);
out.write(response.getBytes());
}
else{
log("Null Message");
ServerLogger.log("CLIENT_THREAD", "Null Message");
}
} catch (IOException ex) {
log("IOExcepttion detected : " + ex.getMessage());
ServerLogger.log("CLIENT_THREAD", "IOExcepttion detected : " + ex.getMessage());
ex.printStackTrace();
}
log("Terminated");
}
public void log(String message){
Date date = new Date();
String threadName = "CLIENT_THREAD";
DateFormat time = new SimpleDateFormat("dd/MM/yyy HH:mm:ss");
time.setTimeZone(TimeZone.getTimeZone("Asia/Jakarta"));
System.out.println("["+ time.format(date) + " | " + threadName + "] " + message);
ServerLogger.log("CLIENT_THREAD", "Terminated");
}
}
......@@ -8,11 +8,7 @@ package server;
import java.io.*;
import java.net.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
import java.util.TimeZone;
import org.json.JSONException;
import org.json.JSONObject;
import server.helper.*;
......@@ -55,17 +51,6 @@ public class Server {
}
return isSuccess;
}
/**
* Print log message to command line
* @param message
*/
public static void log(String message){
Date date = new Date();
String threadName ="MAIN_THREAD";
DateFormat time = new SimpleDateFormat("dd/MM/yyy HH:mm:ss");
time.setTimeZone(TimeZone.getTimeZone("Asia/Jakarta"));
System.out.println("["+ time.format(date) + " | " + threadName + "] " + message);
}
/**
* @param args the command line arguments
*/
......@@ -76,21 +61,21 @@ public class Server {
Socket trackerSocket = null;
try {
trackerSocket = new Socket("167.205.32.46", 8000);
log("Sending request join");
ServerLogger.log("SERVER_THREAD", "Sending request join");
while(!joiningTracker(trackerSocket)){
log("Sending request join (again)");
ServerLogger.log("SERVER_THREAD", "Sending request join (again)");
}
ServerSocket welcomeSocket = null;
Socket clientSocket = null;
welcomeSocket = new ServerSocket(6789);
log("Start listening to new connection");
ServerLogger.log("SERVER_THREAD", "Start listening to new connection");
while(true){
clientSocket = welcomeSocket.accept();
log("New connection accepted");
ServerLogger.log("SERVER_THREAD", "New connection accepted");
new Thread(new ClientHandler(clientSocket)).start();
}
} catch (IOException | JSONException ex) {
log("Failed");
ServerLogger.log("SERVER_THREAD", "Failed");
System.out.println(ex.getMessage());
ex.printStackTrace();
}
......
......@@ -8,12 +8,8 @@ package server;
import java.io.*;
import java.net.Socket;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import server.helper.*;
/**
......@@ -31,7 +27,7 @@ public class ServerHandler{
}
@Override
public void run(){
log("Started");
ServerLogger.log("SERVER_THREAD", "Started");
Socket s = null;
try {
s = new Socket(ip.getIP(),ip.getPort());
......@@ -46,22 +42,11 @@ public class ServerHandler{
// request = request.substring(0,count);
listResponses.add(response);
}
} catch (IOException ex) {
}
try {
s.close();
} catch (IOException ex) {
}
log("Terminated");
}
public void log(String message){
Date date = new Date();
String threadName ="SERVER_THREAD";
DateFormat time = new SimpleDateFormat("dd/MM/yyy HH:mm:ss");
time.setTimeZone(TimeZone.getTimeZone("Asia/Jakarta"));
System.out.println("["+ time.format(date) + " : " + threadName + "] " + message);
ServerLogger.log("SERVER_THREAD","Terminated");
}
}
/**
......@@ -70,9 +55,18 @@ public class ServerHandler{
* @return
*/
public static String broadcast(String request){
List<Thread> listThread = new ArrayList<>();
listResponses = new ArrayList<>();
for(int i = 0; i < Store.getServers().size(); i++){
new Thread(new ThreadServer(Store.getServer(i),request)).start();
listThread.add(new Thread(new ThreadServer(Store.getServer(i),request)));
listThread.get(i).start();
}
for(int i = 0; i < Store.getServers().size(); i++){
try {
listThread.get(i).join();
} catch (InterruptedException ex) {
ServerLogger.log("SERVER_THREAD", "Interrupted");
}
}
//handle all the response
String responses = null;
......
......@@ -18,6 +18,7 @@ import objek.Person;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import server.ServerHandler;
/**
*
......@@ -603,6 +604,7 @@ public class Message {
{
String response;
JSONObject jsonAllOffers;
JSONObject jsonObj;
try{
ArrayList<ArrayList<Object>> allOffers = Database.getTradebox(token,itemId);
jsonAllOffers = new JSONObject();
......@@ -614,7 +616,11 @@ public class Message {
}
else
{
response = error();
jsonObj = new JSONObject();
jsonObj.put("method", "findoffer");
jsonObj.put("item", itemId);
response = ServerHandler.broadcast(jsonObj.toString());
//tangani kalau mau dimasukin ke database
}
}
......@@ -640,17 +646,8 @@ public class Message {
public static String sendFindOffer(int item) //12
{
JSONObject jsonObj = new JSONObject();
String response;
try
{
jsonObj.put("method", "sendfind");
jsonObj.put("item", item);
response = jsonObj.toString();
}
catch(JSONException ex)
{
response = error();
}
String response = null;
if(log)
{
System.out.println(response);
......@@ -658,6 +655,13 @@ public class Message {
return response;
}
public static String sendAccept() //String token, int itemId) //11
{
String response = null;
if(log)
System.out.println(response);
return response;
}
/**
* Fungsi untuk catch method find offer dari server lain
* @param itemIdx
......
/*
* 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 server.helper;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
/**
*
* @author user
*/
public class ServerLogger {
public static void log(String threadName, String message){
Date date = new Date();
DateFormat time = new SimpleDateFormat("dd/MM/yyy HH:mm:ss");
time.setTimeZone(TimeZone.getTimeZone("Asia/Jakarta"));
System.out.println("["+ time.format(date) + " : " + threadName + "] " + message);
}
}
......@@ -6,7 +6,11 @@
package server.helper;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
/**
*
......
......@@ -6,12 +6,8 @@
package server.helper;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import org.json.*;
/**
......@@ -31,11 +27,11 @@ public class TrackerMessageHandler {
case "ok":
JSONArray arrayServerJSON = json.getJSONArray("value");
handleJSONArrayofServer(arrayServerJSON);
log("Response from tracker successfully parsed");
ServerLogger.log("MAIN_THREAD", "Response from tracker successfully parsed");
break;
case "error":
log("Response from tracker : " + json.getString("status"));
log("Description : " + json.getString("description"));
ServerLogger.log("MAIN_THREAD","Response from tracker : " + json.getString("status"));
ServerLogger.log("MAIN_THREAD", "Description : " + json.getString("description"));
throw new JSONException("JSON with status error");
default :
throw new JSONException("Wrong response");
......@@ -49,18 +45,11 @@ public class TrackerMessageHandler {
public static void handleJSONArrayofServer(JSONArray array) throws JSONException{
List<IPAddress> listServer = new ArrayList<>();
for(int i = 0; i < array.length(); i++){
log(array.get(i).toString());
ServerLogger.log("MAIN_THREAD", array.get(i).toString());
JSONObject j = new JSONObject(array.get(i).toString());
IPAddress ip = new IPAddress(j.getString("ip"), j.getInt("port"));
listServer.add(ip);
}
Store.updateServers(listServer);
}
public static void log(String message){
Date date = new Date();
String threadName ="MAIN_THREAD";
DateFormat time = new SimpleDateFormat("dd/MM/yyy HH:mm:ss");
time.setTimeZone(TimeZone.getTimeZone("Asia/Jakarta"));
System.out.println("["+ time.format(date) + " | " + threadName + "] " + message);
}
}
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