Skip to content

Commit

Permalink
Clean up script for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
hoang-ho committed Mar 15, 2021
1 parent fa7b97a commit 2bce1dd
Show file tree
Hide file tree
Showing 13 changed files with 164 additions and 184 deletions.
22 changes: 22 additions & 0 deletions TestCase1.json
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"
]
}
]
}
5 changes: 0 additions & 5 deletions TestCase1.txt

This file was deleted.

5 changes: 0 additions & 5 deletions TestCase1SingleServer.txt

This file was deleted.

31 changes: 31 additions & 0 deletions TestCase2.json
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"
]
}
]
}
6 changes: 0 additions & 6 deletions TestCase2.txt

This file was deleted.

11 changes: 0 additions & 11 deletions TestCase3.txt

This file was deleted.

1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ dependencies {
compile "io.grpc:grpc-stub:${grpcVersion}"
compile group: 'com.google.protobuf', name: 'protobuf-java', version: '3.14.0'
compile group: 'com.google.protobuf', name: 'protobuf-java-util', version: '3.14.0'
compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1.1'

compile 'com.google.protobuf:protobuf-gradle-plugin:0.8.14'

Expand Down
29 changes: 16 additions & 13 deletions doc/test/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,21 @@ echo "Let's start!"
# Compile the project
# Run the project

# Start an EC2 instance
aws ec2 run-instances --image-id ami-0fc61db8544a617ed --instance-type t2.micro --key-name 677kp > instance.json
## Start an EC2 instance
#aws ec2 run-instances --image-id ami-0fc61db8544a617ed --instance-type t2.micro --key-name 677kp > instance.json
#
## Get instance Id
#
#aws ec2 describe-instances --instance-id $InstanceId > runningInstance.json
#
#PublicDnsName=$(grep -m 1 '^ *"PublicDnsName":' runningInstance.json | awk '{ print $2 }' | sed -e 's/,$//' -e 's/^"//' -e 's/"$//')
#
#scp -i "677kp.pem" Config.txt ec2-user@$PublicDnsName
#
#ssh -i "677kp.pem" ec2-user@$PublicDnsName
#
#./gradlew clean build

# Get instance Id
InstanceId=$(grep '^ *"InstanceId":' instance.json | awk '{ print $2 }' | sed -e 's/,$//' -e 's/^"//' -e 's/"$//')

aws ec2 describe-instances --instance-id $InstanceId > runningInstance.json

PublicDnsName=$(grep -m 1 '^ *"PublicDnsName":' runningInstance.json | awk '{ print $2 }' | sed -e 's/,$//' -e 's/^"//' -e 's/"$//')

scp -i "677kp.pem" Config.txt ec2-user@$PublicDnsName

ssh -i "677kp.pem" ec2-user@$PublicDnsName

./gradlew clean build
aws ec2 describe-instances --instance-id $InstanceId
ssh -i "677kp.pem" ec2-user@ec2-54-147-62-56.compute-1.amazonaws.com
28 changes: 28 additions & 0 deletions doc/testVerify.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
## How to make sure the test is correct?


### Milestone 3

Steps to run EC2

For each test case, repeatedly do the following:

1. Create the instance

```
aws ec2 run-instances --image-id ami-07916b33d72291f85 --instance-type t2.micro --key-name 677kp
```

2. From the terminal output, obtain the InstanceId and PrivateIpAddress. Replace the placeholder in TestCase1.txt with corresponding PrivateIpAddress


```
aws ec2 describe-instances --instance-id $InstanceId
```

Get the instance id from the terminal output and run

```
ssh -i "677kp.pem" ec2-user@
```



### Milestone 2

First build the script with:
Expand Down
94 changes: 62 additions & 32 deletions src/main/java/com/p2p/Runner.java
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();
}

}
37 changes: 1 addition & 36 deletions src/main/java/com/p2p/grpc/Buyer.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
import io.grpc.ServerBuilder;
import io.grpc.stub.StreamObserver;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -38,12 +35,6 @@ public Buyer(int id, String IPAddress, int port, int KNeighbors, Product product
ServerBuilder.forPort(port).addService(new MarketPlaceBuyerImpl()).executor(Executors.newFixedThreadPool(KNeighbors + 1)).build();
}

public Buyer(int id, int KNeighbor){
super(id, KNeighbor);
this.buyItems = new ConcurrentHashMap<>();
this.potentialSellers = Collections.synchronizedList(new ArrayList<>());
}

/**
* Implementation lookup interface for the buyer
* This lookup will do a lookupRPC call to all its neighbors
Expand Down Expand Up @@ -145,33 +136,7 @@ public void startServer() {
}
}

public void run(String configFile) throws IOException {
FileInputStream fstream = new FileInputStream(configFile);
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
while ((strLine = br.readLine()) != null) {
// Print the content on the console
String[] vals = strLine.split(" ");
if (vals[0].equals("N") || vals[0].equals("K")) {
continue;
}
if (vals[0].equals("hopCount")) {
this.hopCount = Integer.parseInt(vals[1]);
} else if (Integer.parseInt(vals[0]) == this.getId()) {
this.setIPAddress(vals[1]);
this.setPort(Integer.parseInt(vals[2]));
this.setProduct(vals[4]);
for (int i = 5; i < vals.length; i+=3) {
PeerId neighbor =
PeerId.newBuilder().setIPAddress(vals[i+1]).setId(Integer.parseInt(vals[i])).setPort(Integer.parseInt(vals[i+2])).build();
this.addNeighbor(neighbor);
}
break;
}
}
this.server =
ServerBuilder.forPort(this.getPort()).addService(new MarketPlaceBuyerImpl()).executor(Executors.newFixedThreadPool(this.getNumberNeighbor() + 1)).build();

public void run() {
this.startServer();

// buyer keeps buying products forever
Expand Down
Loading

0 comments on commit 2bce1dd

Please sign in to comment.