Commit 9f001f81 authored by adwisatya's avatar adwisatya
Browse files

Cleaning up #2

parent 50757501
#Tue, 28 Apr 2015 19:46:55 +0700
#Tue, 28 Apr 2015 20:32:32 +0700
C\:\\Users\\adwisatya\\Documents\\Semester\ 6\\Tugas\\IF3230\ Sister\\IF3230-Tugas-Besar-Sister-2015\\src\\server=
......@@ -42,6 +42,7 @@ import org.json.JSONException;
import org.json.*;
import org.json.JSONObject;
import org.json.simple.parser.JSONParser;
import static thealchemist.TCPServer.portNumber;
/**
*
* @author adwisatya
......@@ -118,6 +119,12 @@ public class Client extends Thread{
out.print(tmpStr);
System.out.println("Response: "+tmpStr);
}
public void sendToOthers(String host, int port,String strToSent) throws IOException{
Socket senderSocket = new Socket(host,port);
PrintStream outToOthers = new PrintStream(senderSocket.getOutputStream());
outToOthers.print(strToSent);
}
public void checkCommand(String cmd) throws IOException, NoSuchAlgorithmException, JSONException {
//cmdJoin();
System.out.println("Request: " + cmd);
......@@ -202,15 +209,14 @@ public class Client extends Thread{
for(int i = 0; i<objArr.length();i++){
objArr.getJSONObject(i).get("ip");
objArr.getJSONObject(i).get("port");
if(servers.contains(objArr.getJSONObject(i))){
System.out.println("Masukkan server baru");
if(!servers.contains(objArr.getJSONObject(i))){
System.out.println("Masukkan server baru:"+objArr.getJSONObject(i).toString());
servers.add(objArr.getJSONObject(i).toString());
}
}
} catch (JSONException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println(request);
}
public void cmdJoin() throws IOException, JSONException{
String response = "";
......@@ -416,13 +422,26 @@ public class Client extends Thread{
}
/* Sukses */
public void cmdFindOffer(int itemId) throws IOException, NoSuchAlgorithmException, JSONException{
JSONObject obj = new JSONObject();
try {
obj = database.getOfferList(itemId);
} catch (ClassNotFoundException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
JSONObject obj=new JSONObject();
obj.put("method", "findoffer");
obj.put("item", itemId);
String hostname;
int port;
for(int i=0;i<servers.size();i++){
//System.out.println(servers.get(i).toString());
hostname = utility.JSONtoString("ip",servers.get(i).toString());
port = new Integer(utility.JSONtoString("port",servers.get(i)));
sendToOthers(hostname,port,obj.toString());
System.out.println(hostname + " " + port + " " + obj.toString());
}
sendToClient(obj.toString());
// try {
// obj = database.getOfferList(itemId);
// } catch (ClassNotFoundException ex) {
// Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
// }
}
/* Sukses */
public String cmdSendAccept(String token, String offer_token) throws IOException, NoSuchAlgorithmException, JSONException{
......
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