CLI.java 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. package ui;
  2. import java.lang.reflect.Constructor;
  3. import java.lang.reflect.InvocationTargetException;
  4. import java.net.InetSocketAddress;
  5. import org.apache.commons.cli.CommandLine;
  6. import org.apache.commons.cli.CommandLineParser;
  7. import org.apache.commons.cli.GnuParser;
  8. import org.apache.commons.cli.Options;
  9. import org.apache.commons.cli.ParseException;
  10. import communication.Communication;
  11. import exceptions.NoSuchPartyException;
  12. import oram.Global;
  13. import oram.Metadata;
  14. import protocols.*;
  15. import pir.*;
  16. import protocols.struct.Party;
  17. public class CLI {
  18. public static final int DEFAULT_PORT = 8000;
  19. public static final String DEFAULT_IP = "localhost";
  20. public static void main(String[] args) {
  21. // Setup command line argument parser
  22. Options options = new Options();
  23. options.addOption("config", true, "Config file");
  24. options.addOption("forest", true, "Forest file");
  25. options.addOption("eddie_ip", true, "IP to look for eddie");
  26. options.addOption("debbie_ip", true, "IP to look for debbie");
  27. options.addOption("protocol", true, "Algorithim to test");
  28. options.addOption("pipeline", false, "Whether to do pipelined eviction");
  29. // Parse the command line arguments
  30. CommandLineParser cmdParser = new GnuParser();
  31. CommandLine cmd = null;
  32. try {
  33. cmd = cmdParser.parse(options, args);
  34. } catch (ParseException e1) {
  35. e1.printStackTrace();
  36. }
  37. Global.pipeline = cmd.hasOption("pipeline");
  38. String configFile = cmd.getOptionValue("config", "config.yaml");
  39. String forestFile = cmd.getOptionValue("forest", null);
  40. String party = null;
  41. String[] positionalArgs = cmd.getArgs();
  42. if (positionalArgs.length > 0) {
  43. party = positionalArgs[0];
  44. } else {
  45. try {
  46. throw new ParseException("No party specified");
  47. } catch (ParseException e) {
  48. e.printStackTrace();
  49. System.exit(-1);
  50. }
  51. }
  52. int extra_port = 1;
  53. int eddiePort1 = DEFAULT_PORT;
  54. int eddiePort2 = eddiePort1 + extra_port;
  55. int debbiePort = eddiePort2 + extra_port;
  56. String eddieIp = cmd.getOptionValue("eddie_ip", DEFAULT_IP);
  57. String debbieIp = cmd.getOptionValue("debbie_ip", DEFAULT_IP);
  58. Class<? extends Protocol> operation = null;
  59. String protocol = cmd.getOptionValue("protocol", "retrieve").toLowerCase();
  60. if (protocol.equals("acc")) {
  61. operation = Access.class;
  62. } else if (protocol.equals("cot")) {
  63. operation = SSCOT.class;
  64. } else if (protocol.equals("iot")) {
  65. operation = SSIOT.class;
  66. } else if (protocol.equals("rsf")) {
  67. operation = Reshuffle.class;
  68. } else if (protocol.equals("ppt")) {
  69. operation = PostProcessT.class;
  70. } else if (protocol.equals("ur")) {
  71. operation = UpdateRoot.class;
  72. } else if (protocol.equals("evi")) {
  73. operation = Eviction.class;
  74. } else if (protocol.equals("pt")) {
  75. operation = PermuteTarget.class;
  76. } else if (protocol.equals("pi")) {
  77. operation = PermuteIndex.class;
  78. } else if (protocol.equals("xot")) {
  79. operation = SSXOT.class;
  80. } else if (protocol.equals("rtv")) {
  81. operation = Retrieve.class;
  82. } else if (protocol.equals("pircot")) {
  83. operation = PIRCOT.class;
  84. } else if (protocol.equals("piriot")) {
  85. operation = PIRIOT.class;
  86. } else if (protocol.equals("piracc")) {
  87. operation = PIRAccess.class;
  88. } else if (protocol.equals("pirrtv")) {
  89. operation = PIRRetrieve.class;
  90. } else if (protocol.equals("pirrsf")) {
  91. operation = PIRReshuffle.class;
  92. } else if (protocol.equals("sspir")) {
  93. operation = SSPIR.class;
  94. } else {
  95. System.out.println("Protocol " + protocol + " not supported");
  96. System.exit(-1);
  97. }
  98. Constructor<? extends Protocol> operationCtor = null;
  99. try {
  100. operationCtor = operation.getDeclaredConstructor(Communication.class, Communication.class);
  101. } catch (NoSuchMethodException | SecurityException e1) {
  102. e1.printStackTrace();
  103. }
  104. // For now all logic happens here. Eventually this will get wrapped
  105. // up in party specific classes.
  106. System.out.println("Starting " + party + "...");
  107. Metadata md = new Metadata(configFile);
  108. int numComs = Global.pipeline ? md.getNumTrees() + 1 : 1;
  109. Communication[] con1 = new Communication[numComs];
  110. Communication[] con2 = new Communication[numComs];
  111. if (party.equals("eddie")) {
  112. System.out.print("Waiting to establish debbie connections...");
  113. for (int i = 0; i < numComs; i++) {
  114. con1[i] = new Communication();
  115. con1[i].start(eddiePort1);
  116. eddiePort1 += 3;
  117. while (con1[i].getState() != Communication.STATE_CONNECTED)
  118. ;
  119. }
  120. System.out.println(" done!");
  121. System.out.print("Waiting to establish charlie connections...");
  122. for (int i = 0; i < numComs; i++) {
  123. con2[i] = new Communication();
  124. con2[i].start(eddiePort2);
  125. eddiePort2 += 3;
  126. while (con2[i].getState() != Communication.STATE_CONNECTED)
  127. ;
  128. }
  129. System.out.println(" done!");
  130. for (int i = 0; i < numComs; i++) {
  131. con1[i].setTcpNoDelay(true);
  132. con2[i].setTcpNoDelay(true);
  133. }
  134. try {
  135. Protocol p = operationCtor.newInstance(con1[0], con2[0]);
  136. if (protocol.equals("rtv")) {
  137. ((Retrieve) p).setCons(con1, con2);
  138. }
  139. if (protocol.equals("pirrtv")) {
  140. ((PIRRetrieve) p).setCons(con1, con2);
  141. }
  142. if (!Global.usePIR) {
  143. p.run(Party.Eddie, md, forestFile);
  144. } else {
  145. p.run(Party.Eddie, md);
  146. }
  147. } catch (InstantiationException | IllegalAccessException | IllegalArgumentException
  148. | InvocationTargetException e) {
  149. e.printStackTrace();
  150. }
  151. } else if (party.equals("debbie")) {
  152. System.out.print("Waiting to establish eddie connections...");
  153. for (int i = 0; i < numComs; i++) {
  154. con1[i] = new Communication();
  155. InetSocketAddress addr = new InetSocketAddress(eddieIp, eddiePort1);
  156. con1[i].connect(addr);
  157. eddiePort1 += 3;
  158. while (con1[i].getState() != Communication.STATE_CONNECTED)
  159. ;
  160. }
  161. System.out.println(" done!");
  162. System.out.print("Waiting to establish charlie connections...");
  163. for (int i = 0; i < numComs; i++) {
  164. con2[i] = new Communication();
  165. con2[i].start(debbiePort);
  166. debbiePort += 3;
  167. while (con2[i].getState() != Communication.STATE_CONNECTED)
  168. ;
  169. }
  170. System.out.println(" done!");
  171. for (int i = 0; i < numComs; i++) {
  172. con1[i].setTcpNoDelay(true);
  173. con2[i].setTcpNoDelay(true);
  174. }
  175. try {
  176. Protocol p = operationCtor.newInstance(con1[0], con2[0]);
  177. if (protocol.equals("rtv")) {
  178. ((Retrieve) p).setCons(con1, con2);
  179. }
  180. if (protocol.equals("pirrtv")) {
  181. ((PIRRetrieve) p).setCons(con1, con2);
  182. }
  183. if (!Global.usePIR) {
  184. p.run(Party.Debbie, md, forestFile);
  185. } else {
  186. p.run(Party.Debbie, md);
  187. }
  188. } catch (InstantiationException | IllegalAccessException | IllegalArgumentException
  189. | InvocationTargetException e) {
  190. e.printStackTrace();
  191. }
  192. } else if (party.equals("charlie")) {
  193. System.out.print("Waiting to establish eddie connections...");
  194. for (int i = 0; i < numComs; i++) {
  195. con1[i] = new Communication();
  196. InetSocketAddress addr = new InetSocketAddress(eddieIp, eddiePort2);
  197. con1[i].connect(addr);
  198. eddiePort2 += 3;
  199. while (con1[i].getState() != Communication.STATE_CONNECTED)
  200. ;
  201. }
  202. System.out.println(" done!");
  203. System.out.print("Waiting to establish debbie connections...");
  204. for (int i = 0; i < numComs; i++) {
  205. con2[i] = new Communication();
  206. InetSocketAddress addr = new InetSocketAddress(debbieIp, debbiePort);
  207. con2[i].connect(addr);
  208. debbiePort += 3;
  209. while (con2[i].getState() != Communication.STATE_CONNECTED)
  210. ;
  211. }
  212. System.out.println(" done!");
  213. for (int i = 0; i < numComs; i++) {
  214. con1[i].setTcpNoDelay(true);
  215. con2[i].setTcpNoDelay(true);
  216. }
  217. try {
  218. Protocol p = operationCtor.newInstance(con1[0], con2[0]);
  219. if (protocol.equals("rtv")) {
  220. ((Retrieve) p).setCons(con1, con2);
  221. }
  222. if (protocol.equals("pirrtv")) {
  223. ((PIRRetrieve) p).setCons(con1, con2);
  224. }
  225. if (!Global.usePIR) {
  226. p.run(Party.Charlie, md, forestFile);
  227. } else {
  228. p.run(Party.Charlie, md);
  229. }
  230. } catch (InstantiationException | IllegalAccessException | IllegalArgumentException
  231. | InvocationTargetException e) {
  232. e.printStackTrace();
  233. }
  234. } else {
  235. throw new NoSuchPartyException(party);
  236. }
  237. try {
  238. Thread.sleep(1000);
  239. } catch (InterruptedException e) {
  240. e.printStackTrace();
  241. }
  242. for (int i = 0; i < numComs; i++) {
  243. con1[i].stop();
  244. con2[i].stop();
  245. }
  246. System.out.println(party + " exiting...");
  247. }
  248. }