oblivds.cpp 3.5 KB

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