-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
164 additions
and
184 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"N": "2", | ||
"K": "1", | ||
"network": [ | ||
{ | ||
"id": "0", | ||
"IPAddress": "localhost", | ||
"port": "8080", | ||
"neighbors": [ | ||
"1" | ||
] | ||
}, | ||
{ | ||
"id": "1", | ||
"IPAddress": "localhost", | ||
"port": "8081", | ||
"neighbors": [ | ||
"0" | ||
] | ||
} | ||
] | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"N": "3", | ||
"K": "1", | ||
"network": [ | ||
{ | ||
"id": "0", | ||
"IPAddress": "localhost", | ||
"port": "8080", | ||
"neighbors": [ | ||
"2" | ||
] | ||
}, | ||
{ | ||
"id": "1", | ||
"IPAddress": "localhost", | ||
"port": "8081", | ||
"neighbors": [ | ||
"2" | ||
] | ||
}, | ||
{ | ||
"id": "2", | ||
"IPAddress": "localhost", | ||
"port": "8082", | ||
"neighbors": [ | ||
"0", | ||
"1" | ||
] | ||
} | ||
] | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,76 @@ | ||
package com.p2p; | ||
|
||
import com.google.gson.JsonParser; | ||
import org.json.simple.JSONArray; | ||
import org.json.simple.JSONObject; | ||
import com.p2p.grpc.Buyer; | ||
import com.p2p.grpc.PeerId; | ||
import com.p2p.grpc.PeerImpl; | ||
import com.p2p.grpc.Product; | ||
import com.p2p.grpc.Seller; | ||
import org.json.simple.parser.JSONParser; | ||
import org.json.simple.parser.ParseException; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.FileInputStream; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
|
||
public class Runner { | ||
public static void main(String[] args) throws IOException { | ||
static final String CONFIG = "-config"; | ||
static final String ROLE = "-role"; | ||
static final String ID = "-id"; | ||
static final String PRODUCT = "-product"; | ||
static final String STOCK = "-stock"; | ||
|
||
public static void main(String[] args) throws IOException, ParseException { | ||
// Read the config file and start running on EC2 | ||
int id = Integer.parseInt(args[1]); | ||
int N = 0, K = 0; | ||
FileInputStream fstream = new FileInputStream(args[0]); | ||
BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); | ||
String strLine; | ||
while ((strLine = br.readLine()) != null) { | ||
String[] vals = strLine.split(" "); | ||
if (vals[0].equals("N")) { | ||
N = Integer.parseInt(vals[1]); | ||
} else if (vals[0].equals("K")) { | ||
K = Integer.parseInt(vals[1]); | ||
} else if (vals[0].equals("hopCount")) { | ||
continue; | ||
} | ||
else if (Integer.parseInt(vals[0]) == id) { | ||
// create the peer accordingly to the config file | ||
if (vals[3].equals("buyer")) { | ||
// create a buyer | ||
Buyer buyer = new Buyer(id, K); | ||
buyer.run(args[0]); | ||
} else if (vals[3].equals("seller")) { | ||
// create a seller | ||
Seller seller = new Seller(id, K); | ||
seller.run(args[0]); | ||
} else { | ||
PeerImpl peer = new PeerImpl(id, K); | ||
peer.run(args[0]); | ||
} | ||
String configFile = "", role = "", id = "", product = "FISH"; | ||
int stock = 1; | ||
for (int i = 0; i < args.length; i++) { | ||
if (args[i].equals(CONFIG)) { | ||
i++; | ||
configFile = args[i]; | ||
} else if (args[i].equals(ROLE)) { | ||
i++; | ||
role = args[i]; | ||
} else if (args[i].equals(ID)) { | ||
i++; | ||
id = args[i]; | ||
} else if (args[i].equals(PRODUCT)) { | ||
i++; | ||
product = args[i].toUpperCase(); | ||
} else if (args[i].equals(STOCK)) { | ||
i++; | ||
stock = Integer.parseInt(args[i]); | ||
} | ||
} | ||
|
||
// create the peer and run it | ||
JSONObject jsonParser = (JSONObject) new JSONParser().parse(new FileReader(configFile)); | ||
JSONArray network = (JSONArray) jsonParser.get("network"); | ||
JSONObject nodeConfig = (JSONObject) network.get(Integer.parseInt(id)); | ||
JSONArray neighbors = (JSONArray) nodeConfig.get("neighbors"); | ||
PeerImpl peer; | ||
if (role.toLowerCase().equals("buyer")) { | ||
// Create a buyer | ||
peer = new Buyer(Integer.parseInt(id), (String) nodeConfig.get("IPAddress"), | ||
Integer.parseInt((String) nodeConfig.get("port")), neighbors.size(), Product.valueOf(product)); | ||
|
||
} else if (role.toLowerCase().equals("seller")) { | ||
peer = new Seller(Integer.parseInt(id), (String) nodeConfig.get("IPAddress"), | ||
Integer.parseInt((String) nodeConfig.get("port")), neighbors.size(), Product.valueOf(product), stock); | ||
} else { | ||
peer = new PeerImpl(Integer.parseInt(id), (String) nodeConfig.get("IPAddress"), | ||
Integer.parseInt((String) nodeConfig.get("port")), neighbors.size()); | ||
} | ||
|
||
for (int i = 0; i < neighbors.size(); i++) { | ||
int neighborId = Integer.parseInt((String) neighbors.get(i)); | ||
String neighIP = (String) ((JSONObject) network.get(neighborId)).get("IPAddress"); | ||
int port = Integer.parseInt((String) ((JSONObject) network.get(neighborId)).get("port")); | ||
PeerId neigh = PeerId.newBuilder().setId(neighborId).setIPAddress(neighIP).setPort(port).build(); | ||
peer.addNeighbor(neigh); | ||
} | ||
peer.run(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.