TestCLI.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. public class TestCLI {
  15. public static final int DEFAULT_PORT = 8000;
  16. public static final String DEFAULT_IP = "localhost";
  17. public static void main(String[] args) {
  18. // Setup command line argument parser
  19. Options options = new Options();
  20. options.addOption("config", true, "Config file");
  21. options.addOption("forest", true, "Forest file");
  22. options.addOption("eddie_ip", true, "IP to look for eddie");
  23. options.addOption("debbie_ip", true, "IP to look for debbie");
  24. options.addOption("protocol", true, "Algorithim to test");
  25. // Parse the command line arguments
  26. CommandLineParser cmdParser = new GnuParser();
  27. CommandLine cmd = null;
  28. try {
  29. cmd = cmdParser.parse(options, args);
  30. } catch (ParseException e1) {
  31. e1.printStackTrace();
  32. }
  33. String configFile = cmd.getOptionValue("config", "config.yaml");
  34. String party = null;
  35. String[] positionalArgs = cmd.getArgs();
  36. if (positionalArgs.length > 0) {
  37. party = positionalArgs[0];
  38. } else {
  39. try {
  40. throw new ParseException("No party specified");
  41. } catch (ParseException e) {
  42. e.printStackTrace();
  43. }
  44. }
  45. int extra_port = 1;
  46. int eddiePort1 = DEFAULT_PORT;
  47. int eddiePort2 = eddiePort1 + extra_port;
  48. int debbiePort = eddiePort2 + extra_port;
  49. String eddieIp = cmd.getOptionValue("eddie_ip", DEFAULT_IP);
  50. String debbieIp = cmd.getOptionValue("debbie_ip", DEFAULT_IP);
  51. //
  52. // Class<? extends Protocol> operation = null;
  53. // String protocol = cmd.getOptionValue("protocol",
  54. // "retrieve").toLowerCase();
  55. //
  56. // if (protocol.equals("access")) {
  57. // operation = Access.class;
  58. // } else {
  59. // System.out.println("Method " + protocol + " not supported");
  60. // System.exit(-1);
  61. // }
  62. //
  63. // Constructor<? extends Protocol> operationCtor = null;
  64. // try {
  65. // operationCtor = operation.getDeclaredConstructor(Communication.class,
  66. // Communication.class);
  67. // } catch (NoSuchMethodException | SecurityException e1) {
  68. // e1.printStackTrace();
  69. // }
  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.write("start");
  86. charlieCon.write("start");
  87. debbieCon.readString();
  88. charlieCon.readString();
  89. // try {
  90. // operationCtor.newInstance(debbieCon, charlieCon).run(Party.Eddie,
  91. // configFile, null);
  92. // } catch (InstantiationException | IllegalAccessException |
  93. // IllegalArgumentException
  94. // | InvocationTargetException e1) {
  95. // e1.printStackTrace();
  96. // }
  97. debbieCon.write("end");
  98. charlieCon.write("end");
  99. debbieCon.readString();
  100. charlieCon.readString();
  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,
  121. // configFile, null);
  122. // } catch (InstantiationException | IllegalAccessException |
  123. // IllegalArgumentException
  124. // | InvocationTargetException e1) {
  125. // e1.printStackTrace();
  126. // }
  127. eddieCon.write("end");
  128. charlieCon.write("end");
  129. eddieCon.readString();
  130. charlieCon.readString();
  131. eddieCon.stop();
  132. charlieCon.stop();
  133. } else if (party.equals("charlie")) {
  134. Communication debbieCon = new Communication();
  135. Communication eddieCon = new Communication();
  136. InetSocketAddress eddieAddr = new InetSocketAddress(eddieIp, eddiePort2);
  137. eddieCon.connect(eddieAddr);
  138. InetSocketAddress debbieAddr = new InetSocketAddress(debbieIp, debbiePort);
  139. debbieCon.connect(debbieAddr);
  140. System.out.println("Waiting to establish connections...");
  141. while (eddieCon.getState() != Communication.STATE_CONNECTED)
  142. ;
  143. while (debbieCon.getState() != Communication.STATE_CONNECTED)
  144. ;
  145. System.out.println("Connection established");
  146. eddieCon.write("start");
  147. debbieCon.write("start");
  148. eddieCon.readString();
  149. debbieCon.readString();
  150. // try {
  151. // operationCtor.newInstance(eddieCon, debbieCon).run(Party.Charlie,
  152. // configFile, null);
  153. // } catch (InstantiationException | IllegalAccessException |
  154. // IllegalArgumentException
  155. // | InvocationTargetException e1) {
  156. // e1.printStackTrace();
  157. // }
  158. eddieCon.write("end");
  159. debbieCon.write("end");
  160. eddieCon.readString();
  161. debbieCon.readString();
  162. eddieCon.stop();
  163. debbieCon.stop();
  164. } else {
  165. throw new NoSuchPartyException(party);
  166. }
  167. System.out.println(party + " exiting...");
  168. }
  169. }