prac.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. mpcio.dump_stats(std::cout);
  40. }
  41. static void server_player_main(boost::asio::io_context &io_context,
  42. bool preprocessing, int num_threads, const char *p0addr,
  43. const char *p1addr, char **args)
  44. {
  45. std::deque<tcp::socket> p0socks, p1socks;
  46. mpcio_setup_server(io_context, p0addr, p1addr, num_threads,
  47. p0socks, p1socks);
  48. MPCServerIO mpcserverio(preprocessing, p0socks, p1socks);
  49. // Queue up the work to be done
  50. boost::asio::post(io_context, [&]{
  51. if (preprocessing) {
  52. preprocessing_server(mpcserverio, num_threads, args);
  53. } else {
  54. online_main(mpcserverio, num_threads, args);
  55. }
  56. });
  57. // Start another thread; one will perform the work and the other
  58. // will execute the async_write handlers
  59. boost::thread t([&]{io_context.run();});
  60. io_context.run();
  61. t.join();
  62. mpcserverio.dump_stats(std::cout);
  63. }
  64. int main(int argc, char **argv)
  65. {
  66. char **args = argv+1; // Skip argv[0] (the program name)
  67. bool preprocessing = false;
  68. unsigned player = 0;
  69. int num_threads = 1;
  70. const char *p0addr = NULL;
  71. const char *p1addr = NULL;
  72. // Get the options
  73. while (*args && *args[0] == '-') {
  74. if (!strcmp("-p", *args)) {
  75. preprocessing = true;
  76. ++args;
  77. } else if (!strcmp("-t", *args)) {
  78. if (args[1]) {
  79. num_threads = atoi(args[1]);
  80. if (num_threads < 1) {
  81. usage(argv[0]);
  82. }
  83. args += 2;
  84. } else {
  85. usage(argv[0]);
  86. }
  87. }
  88. }
  89. if (*args == NULL) {
  90. // No arguments?
  91. usage(argv[0]);
  92. } else {
  93. player = atoi(*args);
  94. ++args;
  95. }
  96. if (player > 2) {
  97. usage(argv[0]);
  98. }
  99. if (player > 0) {
  100. if (*args == NULL) {
  101. usage(argv[0]);
  102. } else {
  103. p0addr = *args;
  104. ++args;
  105. }
  106. }
  107. if (player > 1) {
  108. if (*args == NULL) {
  109. usage(argv[0]);
  110. } else {
  111. p1addr = *args;
  112. ++args;
  113. }
  114. }
  115. /*
  116. std::cout << "Preprocessing = " <<
  117. (preprocessing ? "true" : "false") << "\n";
  118. std::cout << "Thread count = " << num_threads << "\n";
  119. std::cout << "Player = " << player << "\n";
  120. if (p0addr) {
  121. std::cout << "Player 0 addr = " << p0addr << "\n";
  122. }
  123. if (p1addr) {
  124. std::cout << "Player 1 addr = " << p1addr << "\n";
  125. }
  126. std::cout << "Args =";
  127. for (char **a = args; *a; ++a) {
  128. std::cout << " " << *a;
  129. }
  130. std::cout << "\n";
  131. */
  132. // Make the network connections
  133. boost::asio::io_context io_context;
  134. if (player < 2) {
  135. comp_player_main(io_context, player, preprocessing, num_threads, p0addr, args);
  136. } else {
  137. server_player_main(io_context, preprocessing, num_threads, p0addr, p1addr, args);
  138. }
  139. return 0;
  140. }