EvaRunnable.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package com.oblivm.backend.util;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.util.Arrays;
  5. import java.util.Scanner;
  6. import org.apache.commons.cli.ParseException;
  7. import com.oblivm.backend.flexsc.CompEnv;
  8. import com.oblivm.backend.flexsc.Flag;
  9. import com.oblivm.backend.flexsc.Mode;
  10. import com.oblivm.backend.flexsc.Party;
  11. public abstract class EvaRunnable<T> extends com.oblivm.backend.network.Client implements Runnable {
  12. public abstract void prepareInput(CompEnv<T> gen) throws Exception;
  13. public abstract void secureCompute(CompEnv<T> gen) throws Exception;
  14. public abstract void prepareOutput(CompEnv<T> gen) throws Exception;
  15. Mode m;
  16. int port;
  17. String host;
  18. protected String[] args;
  19. public boolean verbose = true;
  20. public void setParameter(Mode m, String host, int port, String[] args) {
  21. this.m = m;
  22. this.port = port;
  23. this.host = host;
  24. this.args = args;
  25. }
  26. public void setParameter(Mode m, String host, int port) {
  27. this.m = m;
  28. this.port = port;
  29. this.host = host;
  30. }
  31. public void runCore() throws Exception {
  32. if (verbose)
  33. System.out.println("connecting " + host + " " + port);
  34. connect(host, port);
  35. if (verbose)
  36. System.out.println("connected");
  37. @SuppressWarnings("unchecked")
  38. CompEnv<T> env = CompEnv.getEnv(m, Party.Bob, this);
  39. double s = System.nanoTime();
  40. Flag.sw.startTotal();
  41. prepareInput(env);
  42. os.flush();
  43. secureCompute(env);
  44. os.flush();
  45. prepareOutput(env);
  46. os.flush();
  47. Flag.sw.stopTotal();
  48. double e = System.nanoTime();
  49. disconnect();
  50. if (verbose) {
  51. System.out.println("Eva running time:" + (e - s) / 1e9);
  52. System.out.println("Number Of AND Gates:" + env.numOfAnds);
  53. }
  54. }
  55. public void run() {
  56. try {
  57. runCore();
  58. } catch (Exception e) {
  59. e.printStackTrace();
  60. System.exit(1);
  61. }
  62. }
  63. public void loadConfig() {
  64. loadConfig("config/Config.conf");
  65. }
  66. public void loadConfig(String fileName) {
  67. File file = new File(fileName);
  68. Scanner scanner;
  69. String host = null;
  70. int port = 0;
  71. Mode mode = null;
  72. try {
  73. scanner = new Scanner(file);
  74. while (scanner.hasNextLine()) {
  75. String a = scanner.nextLine();
  76. String[] content = a.split(":");
  77. if (content.length == 2) {
  78. if (content[0].equals("Host"))
  79. host = content[1].replace(" ", "");
  80. else if (content[0].equals("Port"))
  81. port = new Integer(content[1].replace(" ", ""));
  82. else if (content[0].equals("Mode"))
  83. mode = Mode.getMode(content[1].replace(" ", ""));
  84. else {
  85. }
  86. }
  87. }
  88. scanner.close();
  89. } catch (FileNotFoundException e) {
  90. // TODO Auto-generated catch block
  91. e.printStackTrace();
  92. }
  93. this.setParameter(mode, host, port);
  94. }
  95. @SuppressWarnings("rawtypes")
  96. public static void main(String[] args)
  97. throws InstantiationException, IllegalAccessException, ParseException, ClassNotFoundException {
  98. Class<?> clazz = Class.forName(args[0] + "$Evaluator");
  99. EvaRunnable run = (EvaRunnable) clazz.newInstance();
  100. run.loadConfig();
  101. run.run();
  102. if (Flag.CountTime)
  103. Flag.sw.print();
  104. if (Flag.countIO)
  105. run.printStatistic();
  106. }
  107. }