Protocol.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package protocols;
  2. import communication.Communication;
  3. import crypto.Crypto;
  4. import exceptions.NoSuchPartyException;
  5. import oram.Forest;
  6. import oram.Global;
  7. import oram.Metadata;
  8. import struct.Party;
  9. import util.Bandwidth;
  10. import util.TimeAndBandwidth;
  11. import util.Timer;
  12. public abstract class Protocol {
  13. protected Communication con1;
  14. protected Communication con2;
  15. public Timer timer;
  16. public Bandwidth online_band;
  17. public Bandwidth offline_band;
  18. public static TimeAndBandwidth all;
  19. static {
  20. all = new TimeAndBandwidth();
  21. }
  22. public synchronized void addTime(Timer from, Timer to) {
  23. to.add(from);
  24. }
  25. public synchronized void addBand(Bandwidth from, Bandwidth to) {
  26. to.add(from);
  27. }
  28. /*
  29. * Connections are alphabetized so:
  30. *
  31. * For Eddie con1 = debbie con2 = charlie
  32. *
  33. * For Debbie con1 = eddie con2 = charlie
  34. *
  35. * For Charlie con1 = eddie con2 = debbie
  36. */
  37. public Protocol(Communication con1, Communication con2) {
  38. this.con1 = con1;
  39. this.con2 = con2;
  40. // timer = new Timer();
  41. // online_band = new Bandwidth();
  42. // offline_band = new Bandwidth();
  43. }
  44. private static final boolean ENSURE_SANITY = true;
  45. public boolean ifSanityCheck() {
  46. return ENSURE_SANITY;
  47. }
  48. // Utility function will test for synchrony between the parties.
  49. public void sanityCheck() {
  50. if (ENSURE_SANITY) {
  51. // System.out.println("Sanity check");
  52. con1.write("sanity");
  53. con2.write("sanity");
  54. if (!con1.readString().equals("sanity")) {
  55. System.out.println("Sanity check failed for con1");
  56. }
  57. if (!con2.readString().equals("sanity")) {
  58. System.out.println("Sanity check failed for con2");
  59. }
  60. }
  61. }
  62. public void run(Party party, Metadata md, String forestFile) {
  63. Forest forest = null;
  64. if (party == Party.Eddie) {
  65. if (Global.cheat)
  66. forest = new Forest(md, Crypto.sr);
  67. else if (forestFile == null)
  68. forest = Forest.readFromFile(md.getDefaultSharesName1());
  69. else
  70. forest = Forest.readFromFile(forestFile);
  71. } else if (party == Party.Debbie) {
  72. if (Global.cheat)
  73. forest = new Forest(md, null);
  74. else if (forestFile == null)
  75. forest = Forest.readFromFile(md.getDefaultSharesName2());
  76. else
  77. forest = Forest.readFromFile(forestFile);
  78. } else if (party == Party.Charlie) {
  79. if (Global.cheat)
  80. forest = new Forest(md, null);
  81. } else {
  82. throw new NoSuchPartyException(party.toString());
  83. }
  84. run(party, md, forest);
  85. }
  86. // a simulation using a path instead of building whole tree
  87. public void run(Party party, Metadata md) {
  88. // System.err.println("Check");
  89. assert Global.cheat;
  90. Forest[] forests = new Forest[2];
  91. // Eddie has x1, x2
  92. // Debbie has x1, x3
  93. // Charlie has x2, x3
  94. // only x1 should have content
  95. if (party == Party.Eddie) {
  96. forests[0] = new Forest(md, Crypto.sr);
  97. forests[1] = new Forest(md, null);
  98. } else if (party == Party.Debbie) {
  99. forests[0] = new Forest(md, Crypto.sr);
  100. forests[1] = new Forest(md, null);
  101. } else if (party == Party.Charlie) {
  102. forests[0] = new Forest(md, null);
  103. forests[1] = new Forest(md, null);
  104. } else {
  105. throw new NoSuchPartyException(party.toString());
  106. }
  107. run(party, md, forests);
  108. }
  109. /*
  110. * This is mostly just testing code and may need to change for the purpose of an
  111. * actual execution
  112. */
  113. public abstract void run(Party party, Metadata md, Forest forest);
  114. public abstract void run(Party party, Metadata md, Forest[] forests);
  115. }