CLI.java 6.4 KB

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