TestCLI.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 protocols.Party;
  13. import protocols.Protocol;
  14. import protocols.SSCOT;
  15. public class TestCLI {
  16. public static final int DEFAULT_PORT = 8000;
  17. public static final String DEFAULT_IP = "localhost";
  18. public static void main(String[] args) {
  19. // Setup command line argument parser
  20. Options options = new Options();
  21. options.addOption("config", true, "Config file");
  22. options.addOption("forest", true, "Forest file");
  23. options.addOption("eddie_ip", true, "IP to look for eddie");
  24. options.addOption("debbie_ip", true, "IP to look for debbie");
  25. options.addOption("protocol", true, "Algorithim to test");
  26. // Parse the command line arguments
  27. CommandLineParser cmdParser = new GnuParser();
  28. CommandLine cmd = null;
  29. try {
  30. cmd = cmdParser.parse(options, args);
  31. } catch (ParseException e1) {
  32. e1.printStackTrace();
  33. }
  34. String configFile = cmd.getOptionValue("config", "config.yaml");
  35. String forestFile = cmd.getOptionValue("forest", null);
  36. String party = null;
  37. String[] positionalArgs = cmd.getArgs();
  38. if (positionalArgs.length > 0) {
  39. party = positionalArgs[0];
  40. } else {
  41. try {
  42. throw new ParseException("No party specified");
  43. } catch (ParseException e) {
  44. e.printStackTrace();
  45. System.exit(-1);
  46. }
  47. }
  48. int extra_port = 1;
  49. int eddiePort1 = DEFAULT_PORT;
  50. int eddiePort2 = eddiePort1 + extra_port;
  51. int debbiePort = eddiePort2 + extra_port;
  52. String eddieIp = cmd.getOptionValue("eddie_ip", DEFAULT_IP);
  53. String debbieIp = cmd.getOptionValue("debbie_ip", DEFAULT_IP);
  54. Class<? extends Protocol> operation = null;
  55. String protocol = cmd.getOptionValue("protocol", "retrieve").toLowerCase();
  56. if (protocol.equals("sscot")) {
  57. operation = SSCOT.class;
  58. } else {
  59. System.out.println("Protocol " + protocol + " not supported");
  60. System.exit(-1);
  61. }
  62. Constructor<? extends Protocol> operationCtor = null;
  63. try {
  64. operationCtor = operation.getDeclaredConstructor(Communication.class, Communication.class);
  65. } catch (NoSuchMethodException | SecurityException e1) {
  66. e1.printStackTrace();
  67. }
  68. // For now all logic happens here. Eventually this will get wrapped
  69. // up in party specific classes.
  70. System.out.println("Starting " + party + "...");
  71. if (party.equals("eddie")) {
  72. Communication debbieCon = new Communication();
  73. debbieCon.start(eddiePort1);
  74. Communication charlieCon = new Communication();
  75. charlieCon.start(eddiePort2);
  76. System.out.println("Waiting to establish connections...");
  77. while (debbieCon.getState() != Communication.STATE_CONNECTED)
  78. ;
  79. while (charlieCon.getState() != Communication.STATE_CONNECTED)
  80. ;
  81. System.out.println("Connection established.");
  82. debbieCon.write("start");
  83. charlieCon.write("start");
  84. debbieCon.readString();
  85. charlieCon.readString();
  86. try {
  87. operationCtor.newInstance(debbieCon, charlieCon).run(Party.Eddie, configFile, forestFile);
  88. } catch (InstantiationException | IllegalAccessException | IllegalArgumentException
  89. | InvocationTargetException e) {
  90. e.printStackTrace();
  91. }
  92. debbieCon.write("end");
  93. charlieCon.write("end");
  94. debbieCon.readString();
  95. charlieCon.readString();
  96. try {
  97. Thread.sleep(1000);
  98. } catch (InterruptedException e) {
  99. e.printStackTrace();
  100. }
  101. debbieCon.stop();
  102. charlieCon.stop();
  103. } else if (party.equals("debbie")) {
  104. Communication eddieCon = new Communication();
  105. InetSocketAddress eddieAddr = new InetSocketAddress(eddieIp, eddiePort1);
  106. eddieCon.connect(eddieAddr);
  107. Communication charlieCon = new Communication();
  108. charlieCon.start(debbiePort);
  109. System.out.println("Waiting to establish connections...");
  110. while (eddieCon.getState() != Communication.STATE_CONNECTED)
  111. ;
  112. while (charlieCon.getState() != Communication.STATE_CONNECTED)
  113. ;
  114. System.out.println("Connection established");
  115. eddieCon.write("start");
  116. charlieCon.write("start");
  117. eddieCon.readString();
  118. charlieCon.readString();
  119. try {
  120. operationCtor.newInstance(eddieCon, charlieCon).run(Party.Debbie, configFile, forestFile);
  121. } catch (InstantiationException | IllegalAccessException | IllegalArgumentException
  122. | InvocationTargetException e) {
  123. e.printStackTrace();
  124. }
  125. eddieCon.write("end");
  126. charlieCon.write("end");
  127. eddieCon.readString();
  128. charlieCon.readString();
  129. try {
  130. Thread.sleep(1000);
  131. } catch (InterruptedException e) {
  132. e.printStackTrace();
  133. }
  134. eddieCon.stop();
  135. charlieCon.stop();
  136. } else if (party.equals("charlie")) {
  137. Communication debbieCon = new Communication();
  138. Communication eddieCon = new Communication();
  139. InetSocketAddress eddieAddr = new InetSocketAddress(eddieIp, eddiePort2);
  140. eddieCon.connect(eddieAddr);
  141. InetSocketAddress debbieAddr = new InetSocketAddress(debbieIp, debbiePort);
  142. debbieCon.connect(debbieAddr);
  143. System.out.println("Waiting to establish connections...");
  144. while (eddieCon.getState() != Communication.STATE_CONNECTED)
  145. ;
  146. while (debbieCon.getState() != Communication.STATE_CONNECTED)
  147. ;
  148. System.out.println("Connection established");
  149. eddieCon.write("start");
  150. debbieCon.write("start");
  151. eddieCon.readString();
  152. debbieCon.readString();
  153. try {
  154. operationCtor.newInstance(eddieCon, debbieCon).run(Party.Charlie, configFile, forestFile);
  155. } catch (InstantiationException | IllegalAccessException | IllegalArgumentException
  156. | InvocationTargetException e) {
  157. e.printStackTrace();
  158. }
  159. eddieCon.write("end");
  160. debbieCon.write("end");
  161. eddieCon.readString();
  162. debbieCon.readString();
  163. try {
  164. Thread.sleep(1000);
  165. } catch (InterruptedException e) {
  166. e.printStackTrace();
  167. }
  168. eddieCon.stop();
  169. debbieCon.stop();
  170. } else {
  171. throw new NoSuchPartyException(party);
  172. }
  173. System.out.println(party + " exiting...");
  174. }
  175. }