oblivds.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #include <iostream>
  2. #include <deque>
  3. #include "mpcio.hpp"
  4. #include "preproc.hpp"
  5. #include "online.hpp"
  6. static void usage(const char *progname)
  7. {
  8. std::cerr << "Usage: " << progname << " [-p] [-t num] player_num player_addrs args ...\n";
  9. std::cerr << "-p: preprocessing mode\n";
  10. std::cerr << "-t num: use num threads\n";
  11. std::cerr << "player_num = 0 or 1 for the computational players\n";
  12. std::cerr << "player_num = 2 for the server player\n";
  13. std::cerr << "player_addrs is omitted for player 0\n";
  14. std::cerr << "player_addrs is p0's hostname for player 1\n";
  15. std::cerr << "player_addrs is p0's hostname followed by p1's hostname for player 2\n";
  16. exit(1);
  17. }
  18. static void comp_player_main(boost::asio::io_context &io_context,
  19. unsigned player, bool preprocessing, int num_threads, const char *p0addr,
  20. char **args)
  21. {
  22. std::deque<tcp::socket> peersocks, serversocks;
  23. mpcio_setup_computational(player, io_context, p0addr, num_threads,
  24. peersocks, serversocks);
  25. MPCPeerIO mpcio(player, preprocessing, peersocks, serversocks);
  26. // Queue up the work to be done
  27. boost::asio::post(io_context, [&]{
  28. if (preprocessing) {
  29. preprocessing_comp(mpcio, num_threads, args);
  30. } else {
  31. online_main(mpcio, num_threads, args);
  32. }
  33. });
  34. // Start another thread; one will perform the work and the other
  35. // will execute the async_write handlers
  36. boost::thread t([&]{io_context.run();});
  37. io_context.run();
  38. t.join();
  39. }
  40. static void server_player_main(boost::asio::io_context &io_context,
  41. bool preprocessing, int num_threads, const char *p0addr,
  42. const char *p1addr, char **args)
  43. {
  44. std::deque<tcp::socket> p0socks, p1socks;
  45. mpcio_setup_server(io_context, p0addr, p1addr, num_threads,
  46. p0socks, p1socks);
  47. MPCServerIO mpcserverio(preprocessing, p0socks, p1socks);
  48. // Queue up the work to be done
  49. boost::asio::post(io_context, [&]{
  50. if (preprocessing) {
  51. preprocessing_server(mpcserverio, num_threads, args);
  52. } else {
  53. online_main(mpcserverio, num_threads, args);
  54. }
  55. });
  56. // Start another thread; one will perform the work and the other
  57. // will execute the async_write handlers
  58. boost::thread t([&]{io_context.run();});
  59. io_context.run();
  60. t.join();
  61. }
  62. int main(int argc, char **argv)
  63. {
  64. char **args = argv+1; // Skip argv[0] (the program name)
  65. bool preprocessing = false;
  66. unsigned player = 0;
  67. int num_threads = 1;
  68. const char *p0addr = NULL;
  69. const char *p1addr = NULL;
  70. // Get the options
  71. while (*args && *args[0] == '-') {
  72. if (!strcmp("-p", *args)) {
  73. preprocessing = true;
  74. ++args;
  75. } else if (!strcmp("-t", *args)) {
  76. if (args[1]) {
  77. num_threads = atoi(args[1]);
  78. if (num_threads < 1) {
  79. usage(argv[0]);
  80. }
  81. args += 2;
  82. } else {
  83. usage(argv[0]);
  84. }
  85. }
  86. }
  87. if (*args == NULL) {
  88. // No arguments?
  89. usage(argv[0]);
  90. } else {
  91. player = atoi(*args);
  92. ++args;
  93. }
  94. if (player > 2) {
  95. usage(argv[0]);
  96. }
  97. if (player > 0) {
  98. if (*args == NULL) {
  99. usage(argv[0]);
  100. } else {
  101. p0addr = *args;
  102. ++args;
  103. }
  104. }
  105. if (player > 1) {
  106. if (*args == NULL) {
  107. usage(argv[0]);
  108. } else {
  109. p1addr = *args;
  110. ++args;
  111. }
  112. }
  113. /*
  114. std::cout << "Preprocessing = " <<
  115. (preprocessing ? "true" : "false") << "\n";
  116. std::cout << "Thread count = " << num_threads << "\n";
  117. std::cout << "Player = " << player << "\n";
  118. if (p0addr) {
  119. std::cout << "Player 0 addr = " << p0addr << "\n";
  120. }
  121. if (p1addr) {
  122. std::cout << "Player 1 addr = " << p1addr << "\n";
  123. }
  124. std::cout << "Args =";
  125. for (char **a = args; *a; ++a) {
  126. std::cout << " " << *a;
  127. }
  128. std::cout << "\n";
  129. */
  130. // Make the network connections
  131. boost::asio::io_context io_context;
  132. if (player < 2) {
  133. comp_player_main(io_context, player, preprocessing, num_threads, p0addr, args);
  134. } else {
  135. server_player_main(io_context, preprocessing, num_threads, p0addr, p1addr, args);
  136. }
  137. return 0;
  138. }