GenRunnable.java 2.7 KB

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