EvaRunnable.java 3.0 KB

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