Examples

 

Examples

 
1PC
2PC
Currency Broker
Buyer-Seller-Shipper

//$ bin/sessionjc -cp tests/classes/ tests/src/purdue/exceptions/2pc/exceptions/Client1.sj -d tests/classes/
//$ bin/sessionj -cp tests/classes/ purdue.twopc.Client1 & 
package purdue.twopc;

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

 public class Client1{
   participant client1;
       private final noalias protocol invitation {
         participants: coord, client1, client2
         .coord: begin
         .coord->client1: <Long>
         .coord->client2: <Long>
         .ptry {
           client1->coord: <Boolean> | <Exception>
           .client2->coord: <Boolean> | <Exception>
           .coord->client1: <Boolean>
           .coord->client2: <Boolean>
         }
         pcatch(Exception) {
           coord->client1: <Boolean>
           .coord->client2: <Boolean>
         }
       }

  Random r = new Random(System.currentTimeMillis());
  public void run() throws Exception 
  {    
                final noalias SJServerSocket ss;
                try(ss)
                {
                        ss = SJServerSocketImpl.create(invitation, 21102);
      ss.participantName("client1");
      ss.addParticipant("coord""localhost"21101);
                        final noalias SJSocketGroup socket;
      
                        try(socket)
                        {
          int exNumber = 0, numTrials = 10000, exceptionProb = 0;
                            socket = ss.accept("coord");
          System.out.println("Client1 connected");
          Boolean bTrue = new Boolean(true);
          SJSyncObject syncObj = new SJSyncObject();
          Exception myException = new Exception();
          Boolean b = null;
          boolean ex = false;
          Long XID = null;
          for(exceptionProb = -30; exceptionProb <= 90; exceptionProb += 10) {
            for(int i = 0; i < numTrials;) {  
        XID = (Longsocket.receive("coord");
        ex = randomBoolean(exceptionProb);
        ptry {          
          socket.send(bTrue, "coord", ex, "Exception");
          b = (Booleansocket.receive("coord");
          if(b.booleanValue() == true)
            i++;
        }
        catch(Exception e) {
          b = (Booleansocket.receive("coord");
        }
            }
          }
                        }
      finally {}
                }
                finally{}

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