Skip to content
Snippets Groups Projects
Commit 1e586a87 authored by Thea Olivia's avatar Thea Olivia
Browse files

fixing things, add channel to processID

parent 7247a3f8
Branches
No related merge requests found
...@@ -41,6 +41,12 @@ public class SisTerClient { ...@@ -41,6 +41,12 @@ public class SisTerClient {
c.sendMessageToClient("localhost", 8888,"{\"method\":\"prepare_proposal\",\"proposal_id\": ["+c.getProposalNum()+","+c.getPlayerID()+"]}"); c.sendMessageToClient("localhost", 8888,"{\"method\":\"prepare_proposal\",\"proposal_id\": ["+c.getProposalNum()+","+c.getPlayerID()+"]}");
} public void vote(){
}
public void send_request(R){
}
} }
...@@ -7,11 +7,19 @@ import org.json.simple.JSONArray; ...@@ -7,11 +7,19 @@ import org.json.simple.JSONArray;
import org.json.simple.JSONObject; import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser; import org.json.simple.parser.JSONParser;
import java.util.HashMap;
import java.lang.Thread;
public class Paxos public class Paxos
{ {
Server server;
DatagramSocket clientSocket; DatagramSocket clientSocket;
private int n; // number of request private int n; // number of request
public Paxos(){
this.
}
public void sendRequest(String sentence, String host, int port) public void sendRequest(String sentence, String host, int port)
{ {
clientSocket = new DatagramSocket(); clientSocket = new DatagramSocket();
...@@ -23,6 +31,30 @@ public class Paxos ...@@ -23,6 +31,30 @@ public class Paxos
clientSocket.send(sendPacket); clientSocket.send(sendPacket);
} }
public void runPaxos() {
int n_proses = server.getnumProposers() + server.getnumAcceptors() + server.getnumLearners();
for (int i=0; i<n_proses; i++){
Thread t = new Thread(this, ""+i);
t.start();
}
}
public int nextProposalNumber(int PID, int cur_Pnum){
int i = 0;
while (true) {
if (i<=cur_Pnum){
i++;
} else {
if (i%server.getnumProposers()==PID)
return i;
else
i++;
}
if (i==MAX_PROPNUM)
return -1;
}
}
public boolean IsUnique(){ public boolean IsUnique(){
// check the uniqueness of ID // check the uniqueness of ID
} }
......
...@@ -15,129 +15,46 @@ import org.json.JSONObject; ...@@ -15,129 +15,46 @@ import org.json.JSONObject;
public class Server implements Runnable public class Server implements Runnable
{ {
private Paxos paxos; private int totalProcesses;
private String host; private int numProposers;
private int port; private int numAcceptors;
private Socket socket; private int numLearners;
private final String DEFAULT_HOST = "localhost"; private int decision=-1;
private ServerSocket serverSocket;
private int numPlayer; public Network(int numProposers, int numAcceptors, int numLearners){
private boolean playing; totalProcesses = numProposers + numAcceptors + numLearners;
//PrintStream streamToClient; queues = new LinkedList[totalProcesses];
BufferedReader streamFromClient; for (int i=0; i<totalProcesses; i++) {
Socket fromClient; queues[i] = new LinkedList<String>();
static int count = 0;
Thread thread;
public Server()
{
this.host = "localhost";
this.port = 9999;
this.socket = null;
try{
serverSocket = new ServerSocket(1001);
} catch (Exception e) {
e.printStackTrace();
} }
this.numProposers = numProposers;
this.numAcceptors = numAcceptors;
this.numLearners = numLearners;
} }
public Server(String _host, int _port){
try{
host = _host;
port = _port;
serverSocket = new ServerSocket(_port);
socket = serverSocket.accept();
playing = false;
}catch(IOException e){
}
}
public boolean isWerewolf()
{
boolean yes = false;
// for werewolf
return yes;
}
public void connect(String host, int port) throws IOException
{
this.host = host;
this.port = port;
socket = new Socket(host, port);
System.out.println("Game sudah dimulai... V^__^V");
public int getnumAcceptors(){
return numAcceptors;
} }
public JSONObject receiveJSON() throws IOException public int getnumProposers(){
{ return numProposers;
InputStream in = socket.getInputStream();
ObjectInputStream i = new ObjectInputStream(in);
JSONObject line = null;
try {
line = (JSONObject) i.readObject();
} catch (ClassNotFoundException e){
e.printStackTrace();
}
return line;
} }
public void sendJSON(JSONObject jsonObj) throws IOException public int getnumLearners(){
{ return numLearners
JSONObject jsonObject2 = new JSONObject();
OutputStream out = socket.getOutputStream();
ObjectOutputStream o = new ObjectOutputStream(out);
o.writeObject(jsonObject2);
out.flush();
} }
public void runServer()
{
try{
while(true){
fromClient = serverSocket.accept();
count++;
//streamFromClient = new BufferedReader(fromClient.getInputStream());
InputStreamReader in = new InputStreamReader((fromClient.getInputStream()));
//PrintStream streamToClient = new PrintStream(fromClient.getInputStream());
String str = streamFromClient.readLine();
System.out.println(str);
//streamToClient.println("Halo, "+str);
}
} catch (Exception e){
e.printStackTrace();
} finally {
try {
fromClient.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public String receiveMessage() throws IOException{
String temp = null;
BufferedReader inFromClient =new BufferedReader(new InputStreamReader(socket.getInputStream()));
temp = inFromClient.readLine();
return temp;
}
public void sendMessage(String out) throws IOException{
DataOutputStream outToClient = new DataOutputStream(socket.getOutputStream());
String response = out + '\n';
outToClient.writeBytes(response);
}
@Override
public void run() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
// untuk channel communication dari proses processID
public Channel getChannel(int processID){
if (processID <0 || processID >= totalProcesses) {
throw new Error ("Invalid process ID");
}
Channel c = new Channel();
c.index = processID;
c.network = this;
return c;
}
} }
package sisterserver;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import org.json.JSONObject;
public class Server implements Runnable
{
<<<<<<< HEAD
private Paxos paxos;
private String host;
private String host;
=======
private Paxos paxos;
private String host;
>>>>>>> 69b6100463f19dcd49485fc68a92559c4d2bd04a
private int port;
private Socket socket;
private final String DEFAULT_HOST = "localhost";
private ServerSocket serverSocket;
private int numPlayer;
private boolean playing;
//PrintStream streamToClient;
BufferedReader streamFromClient;
Socket fromClient;
static int count = 0;
Thread thread;
public Server()
{
this.host = "localhost";
this.port = 9999;
this.socket = null;
try{
serverSocket = new ServerSocket(1001);
} catch (Exception e) {
e.printStackTrace();
}
}
public Server(String _host, int _port){
try{
host = _host;
port = _port;
serverSocket = new ServerSocket(_port);
socket = serverSocket.accept();
playing = false;
}catch(IOException e){
}
}
public boolean isWerewolf()
{
boolean yes = false;
// for werewolf
return yes;
}
public void connect(String host, int port) throws IOException
{
this.host = host;
this.port = port;
socket = new Socket(host, port);
System.out.println("Game sudah dimulai... V^__^V");
}
public JSONObject receiveJSON() throws IOException
{
InputStream in = socket.getInputStream();
ObjectInputStream i = new ObjectInputStream(in);
JSONObject line = null;
try {
line = (JSONObject) i.readObject();
} catch (ClassNotFoundException e){
e.printStackTrace();
}
return line;
}
public void sendJSON(JSONObject jsonObj) throws IOException
{
JSONObject jsonObject2 = new JSONObject();
OutputStream out = socket.getOutputStream();
ObjectOutputStream o = new ObjectOutputStream(out);
o.writeObject(jsonObject2);
out.flush();
}
public void runServer()
{
try{
while(true){
fromClient = serverSocket.accept();
count++;
//streamFromClient = new BufferedReader(fromClient.getInputStream());
InputStreamReader in = new InputStreamReader((fromClient.getInputStream()));
//PrintStream streamToClient = new PrintStream(fromClient.getInputStream());
String str = streamFromClient.readLine();
System.out.println(str);
//streamToClient.println("Halo, "+str);
}
} catch (Exception e){
e.printStackTrace();
} finally {
try {
fromClient.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
<<<<<<< HEAD
=======
>>>>>>> 69b6100463f19dcd49485fc68a92559c4d2bd04a
public String receiveMessage() throws IOException{
String temp = null;
BufferedReader inFromClient =new BufferedReader(new InputStreamReader(socket.getInputStream()));
temp = inFromClient.readLine();
return temp;
}
public void sendMessage(String out) throws IOException{
DataOutputStream outToClient = new DataOutputStream(socket.getOutputStream());
String response = out + '\n';
outToClient.writeBytes(response);
}
@Override
public void run() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
<<<<<<< HEAD
=======
>>>>>>> 69b6100463f19dcd49485fc68a92559c4d2bd04a
}
...@@ -10,6 +10,11 @@ import java.io.DataOutputStream; ...@@ -10,6 +10,11 @@ import java.io.DataOutputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.net.*; import java.net.*;
import org.json.JSONObject; import org.json.JSONObject;
import org.json.JSONException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/** /**
* *
...@@ -28,7 +33,7 @@ public class SisterServer { ...@@ -28,7 +33,7 @@ public class SisterServer {
while(true) while(true)
{ {
Socket connectionSocket = welcomeSocket.accept(); Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient =new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); BufferedReasder inFromClient =new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream()); DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
clientSentence = inFromClient.readLine(); clientSentence = inFromClient.readLine();
System.out.println("Received: " + clientSentence); System.out.println("Received: " + clientSentence);
...@@ -43,11 +48,14 @@ public class SisterServer { ...@@ -43,11 +48,14 @@ public class SisterServer {
System.out.println(j.get("method"));*/ System.out.println(j.get("method"));*/
private int sequenceID; private int sequenceID;
private int processID; private int processID;
int
Client c = new Client(1,9999); Client c = new Client(1,9999);
c.receiveMessageFromClient("localhost", 8888); c.receiveMessageFromClient("localhost", 8888);
// creating consensus // creating consensus
public void consensus(){
}
} }
......
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