GenRunnable.java 2.7 KB

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