Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. package it.unica.co2.progettoco2;
  2.  
  3. import static it.unica.co2.api.contract.utils.ContractFactory.*;
  4.  
  5. import java.util.Base64;
  6. import java.util.Base64.Encoder;
  7.  
  8. import co2api.ContractException;
  9. import co2api.ContractExpiredException;
  10. import co2api.Message;
  11. import co2api.Session;
  12. import co2api.TST;
  13. import co2api.TimeExpiredException;
  14. import it.unica.co2.api.contract.Contract;
  15. import it.unica.co2.api.process.Participant;
  16. import it.unica.co2.api.process.SkipMethod;
  17. import it.unica.co2.honesty.HonestyChecker;
  18.  
  19. public class Store extends Participant {
  20.  
  21. private static final long serialVersionUID = 1L;
  22.  
  23. private static final String username = "65006@co2.unica.it";
  24. private static final String password = "65006";
  25.  
  26. protected Store() {
  27. super(username, password);
  28. }
  29.  
  30.  
  31. int itemPrice = 50;
  32. Contract cb = externalSum().add("order", internalSum().add("abort").add("amount", externalSum().add("pay")));
  33. Contract ic = internalSum().add("req", externalSum().add("ok").add("no"));
  34. @Override
  35. public void run() {
  36.  
  37. /*System.out.println(ic.toTST());
  38. System.out.println(cb.toTST());*/
  39.  
  40. Session<TST> s = tellAndWait(cb);
  41. s.waitForReceive("order");
  42.  
  43. if(itemPrice < 100){
  44. s.sendIfAllowed("amount", itemPrice);
  45. s.waitForReceive("pay");
  46. }
  47. else{
  48. try{
  49. Session<TST> s2 = tellAndWait(ic);
  50. s2.sendIfAllowed("req", itemPrice);
  51.  
  52. Message msg = s2.waitForReceive("ok", "no");
  53. if(msg.getLabel().equals("ok")){
  54. s.sendIfAllowed("amount", itemPrice);
  55. s.waitForReceive("pay");
  56. }
  57. else{
  58. s.sendIfAllowed("abort");
  59. }
  60. }catch(ContractExpiredException e){
  61. s.sendIfAllowed("abort");
  62. }
  63. }
  64.  
  65.  
  66. }
  67.  
  68. public static void main(String[] args) {
  69. HonestyChecker.isHonest(Store.class);
  70. //new Store().run();
  71.  
  72. }
  73.  
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement