oblivds.cpp 4.2 KB

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