Examples

 

Examples

 
1PC
2PC
Currency Broker
Buyer-Seller-Shipper

//$ bin/sessionjc -cp tests/classes/ tests/src/purdue/exceptions/currency_broker/exceptions/Broker.sj -d tests/classes/
//$ bin/sessionj -cp tests/classes/ purdue.currency_broker.Broker & 
package purdue.currency_broker;

import sessionj.runtime.*;
import sessionj.runtime.net.*;
import java.util.Random;
import java.net.*;

 public class Broker{
   participant broker;
       private final noalias protocol invitation {
           participants: buyer, broker, seller
           .buyer: begin
           .buyer -> seller: <String> //Request quote
           .seller -> broker: <Double> //Quote price
           .broker -> buyer: <Double> //Quote + 5%
           .buyer: {
             Accept:
             ptry //during the time the buyer is asked, the currency may have been sold, or the price may change
               seller -> broker: <String> /*Selling*/ | <Exception> //Price may chage
               .broker -> buyer: <String>
             }
             pcatch(Exception) { //Price Changed Exception
               //The buyer may or may not want to continue receiving quotes
               //buyer may retry
               buyer->seller: <Boolean>
               .buyer->broker: <Boolean>
             }
             ,Reject: //Do nothing
           }
      }


  Random r = new Random(System.currentTimeMillis());
  public void run() throws Exception 
  {    
                final noalias SJServerSocket ss;
                try(ss)
                {
                        ss = SJServerSocketImpl.create(invitation, 21301);
      ss.participantName("broker");
      ss.addParticipant("buyer""localhost"0);
      ss.addParticipant("seller""sslab04.cs.purdue.edu"21300);      
                        final noalias SJSocketGroup socket;
                        try(socket)
                        {
          int numTrials = 10000, exceptionProb = 0;
                            socket = ss.accept("buyer");
          System.out.println("Broker connected");
          String req = repeat("a"400);
          String res = "";
          Double price = null;
          Boolean retry = new Boolean(false);
          Boolean bFalse = new Boolean(false);
          for(exceptionProb = -30; exceptionProb <= 90; exceptionProb += 10) {
            for(int i = 0; i < numTrials;) {
              price = (Doublesocket.receive("seller");
        socket.send(price, "buyer");
        socket.inbranch("buyer") {
          case Accept: {
            do {
              ptry {
                res = (String)socket.receive("seller");
          socket.send(req, "buyer");
          retry = bFalse;
              }
              catch(Exception ex) {
                retry = (Booleansocket.receive("buyer");
              }
            while(retry.booleanValue());
            i++;
          }
          case Reject: {
            i++;
          }
        }
            }
          }
                        }
      finally {}
                }
                finally{}

  }
  
  public static void main(String[] argsthrows Exception
  {
    Broker a = new Broker();
    a.run();
  }

        String repeat(String str, int n) {
               String str2 =  new String();
               for(int i = 0; i<n; ++i) {
                       str2+= str;
               }
               return str2;
        }

}
Java2html