//$ bin/sessionjc -cp tests/classes/ tests/src/purdue/exceptions/buyer_seller/exceptions/Buyer.sj -d tests/classes/
//$ bin/sessionj -cp tests/classes/ purdue.buyer_seller.Buyer
package purdue.buyer_seller;
import sessionj.runtime.*;
import sessionj.runtime.net.*;
import java.net.*;
import java.util.Random;
public class Buyer{
participant buyer;
private final noalias protocol invitation {
participants: buyer, shipper, seller
.buyer: begin
.ptry {
buyer -> seller: <Long> //Product ID
.seller-> buyer: <Double> | <Exception> //Price or out-of-stock exception
.buyer: {
Accept:
seller -> buyer: <Long> //confirmation number
.seller -> shipper: <String> //Request delivery details
.shipper -> buyer: <String> //Delivery details
.shipper -> seller: <String>, //Delivery details
Reject: //Do nothing
}
}
pcatch(Exception) { //Out of stock
seller -> buyer: <String>
}
}
Random r = new Random(System.currentTimeMillis());
public void run(int singleSession) throws Exception {
final noalias SJService c = SJService.create(invitation, "localhost", 1000);
c.participantName("buyer");
c.addParticipant("seller", "sslab04.cs.purdue.edu", 21300);
c.addParticipant("shipper", "mc04.cs.purdue.edu", 21301);
final noalias SJSocketGroup ps;
long duration = 0;
try (ps)
{
ps = c.request();
System.out.println("Buyer connected");
String req = repeat("a", 400);
String res = "";
Long productID = new Long(r.nextLong());
for(int exceptionProb = -30; exceptionProb <= 90; exceptionProb += 10) {
long startTime = System.nanoTime();
int numTrials = 10000, exceptionCount = 0, completedTrials = 0;;
for(int i = 0; i < numTrials;) {
completedTrials++;
ptry {
ps.send(productID, "seller");
Double price = (Double)ps.receive("seller");
if(price.doubleValue() < 100) {
ps.outbranch(Accept) {
Long confirmationNumber = (Long) ps.receive("seller");
res = (String) ps.receive("shipper");
}
}
else {
ps.outbranch(Reject) {
}
}
i++;
}
catch(Exception e) {
res = (String) ps.receive("seller");
}
}
duration = System.nanoTime() - startTime;
System.out.println("\n\nResults for exception Probability: " + exceptionProb + "%");
System.out.println("Completed Trials: " + completedTrials);
System.out.println("Average Request Time (ms): " + ((double) duration) / numTrials / 1000000.0);
System.out.println("Exception 1 rate: " + exceptionCount*100.0/completedTrials + "%");
}
}
finally{}
}
public static void main(String[] args) throws Exception{
Buyer a = new Buyer();
a.run(1);
}
String repeat(String str, int n) {
String str2 = new String();
for(int i = 0; i<n; ++i) {
str2+= str;
}
return str2;
}
}
|