orchestratorMain.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /**
  2. * orchestratorMain.cpp
  3. * - compiles to bin/orchestrator
  4. * - initiates a set of servers and clients, then commands them, for PRSONA experiments
  5. *
  6. * Stan Gurtler
  7. */
  8. #include <iostream>
  9. #include <fstream>
  10. #include <thread>
  11. #include <csignal>
  12. #include <unistd.h>
  13. #include <sys/types.h>
  14. #include <sys/wait.h>
  15. #include "networkOrchestrator.hpp"
  16. using namespace std;
  17. void cleanup(int signum)
  18. {
  19. while (waitpid(-1, NULL, WNOHANG) > 0) {}
  20. }
  21. /**
  22. * This program (bin/orchestrator) expects to be called as follows:
  23. * `bin/orchestrator <output> <servers_are_malicious>`
  24. *
  25. * <output> - a string that will name the files in which outputs for this run of
  26. * the experiment will be written (that is, timings and traffic data)
  27. * <lambda> - a positive integer that determines the absolute soundness parameter
  28. * for batched proofs
  29. * <servers_are_malicious> - a bool (given as T/t or F/f)
  30. * which is true when servers are in malicious security
  31. * and false when they are in HBC security
  32. */
  33. int main(int argc, char* argv[])
  34. {
  35. /*
  36. * PRELIMINARY SETUP CODE
  37. */
  38. #if USE_SSL
  39. mg_init_library(MG_FEATURES_SSL);
  40. #else
  41. mg_init_library(0);
  42. #endif
  43. string output = "default";
  44. if (argc > 1)
  45. output = argv[1];
  46. // Default to not proof batching if not specified
  47. size_t lambda = 0;
  48. if (argc > 2)
  49. lambda = atoi(argv[2]);
  50. // Default to malicious security if not specified
  51. bool maliciousServers = true;
  52. if (argc > 3)
  53. maliciousServers = argv[3][0] == 't' || argv[3][0] == 'T';
  54. // This seed is used to pick which users vote during which epochs
  55. // and which users make reputation proofs to which other users
  56. string seedStr = output;
  57. seedStr += "-orchestrator";
  58. seed_seq seed(seedStr.begin(), seedStr.end());
  59. default_random_engine rng(seed);
  60. vector<string> serverIPs, clientIPs;
  61. vector<int> serverPorts, clientPorts;
  62. string dealerIP, dealerPortStr;
  63. int dealerPort = 0;
  64. std::map<std::string,std::string> targeter;
  65. targeter["129.97.119.208"] = "tick0";
  66. targeter["129.97.119.209"] = "tick1";
  67. targeter["129.97.119.215"] = "tock";
  68. string configDir = "cfg/" + output;
  69. // Read in from config files the server locations
  70. load_multiple_instances_config(serverIPs, serverPorts, (configDir + "/serverIPs.cfg").c_str());
  71. // And now the client locations
  72. load_multiple_instances_config(clientIPs, clientPorts, (configDir + "/clientIPs.cfg").c_str());
  73. // And finally the dealer location
  74. load_single_instance_config(dealerIP, dealerPortStr, dealerPort, (configDir + "/dealerIP.cfg").c_str());
  75. size_t numServers = serverIPs.size();
  76. size_t numClients = clientIPs.size();
  77. signal(SIGCHLD, cleanup);
  78. /*
  79. * ORCHESTRATOR SETUP CODE
  80. */
  81. cout << "[ORC] This experiment is running with output code: " << output << endl;
  82. cout << "[ORC] This experiment is running with " << (maliciousServers ? "MALICIOUS" : "HBC") << " servers." << endl;
  83. cout << "[ORC] Lambda is " << lambda << "." << endl;
  84. cout << endl;
  85. cout << "[ORC] Starting BGN dealer server." << endl;
  86. vector<thread> serverStartup, clientStartup, clientReady;
  87. serverStartup.push_back(thread(start_remote_actor, targeter[dealerIP], true, "d", output, lambda, maliciousServers));
  88. this_thread::sleep_for(TWO_SECONDS);
  89. cout << "[ORC] Starting other servers." << endl;
  90. for (size_t i = 0; i < numServers; i++)
  91. {
  92. if (serverIPs[i] == dealerIP && serverPorts[i] == dealerPort)
  93. continue;
  94. serverStartup.push_back(thread(start_remote_actor, targeter[serverIPs[i]], true, "s" + to_string(i), output, lambda, maliciousServers));
  95. }
  96. cout << "[ORC] Waiting for confirmation that servers are ready to continue." << endl;
  97. for (size_t i = 0; i < numServers; i++)
  98. serverStartup[i].join();
  99. wait_for_servers_ready(dealerIP, dealerPort);
  100. cout << "[ORC] Starting clients." << endl;
  101. for (size_t i = 0; i < numClients; i++)
  102. {
  103. clientStartup.push_back(thread(start_remote_actor, targeter[clientIPs[i]], false, "c" + to_string(i), output, lambda, maliciousServers));
  104. this_thread::sleep_for(ONE_SECOND);
  105. }
  106. cout << "[ORC] Waiting for confirmation that servers have all clients logged." << endl;
  107. for (size_t i = 0; i < numClients; i++)
  108. clientStartup[i].join();
  109. wait_for_clients_created(dealerIP, dealerPort, numClients);
  110. cout << "[ORC] Waiting for confirmation that clients are ready to continue." << endl;
  111. for (size_t i = 0; i < numClients; i++)
  112. clientReady.push_back(thread(wait_for_client_ready, clientIPs[i], clientPorts[i]));
  113. for (size_t i = 0; i < numClients; i++)
  114. clientReady[i].join();
  115. /*
  116. * MAIN ORCHESTRATOR LOOP CODE
  117. */
  118. cout << "[ORC] Beginning experiment." << endl;
  119. execute_experiment(rng, dealerIP, dealerPort, serverIPs, serverPorts, clientIPs, clientPorts, (configDir + "/commands.cfg").c_str());
  120. /*
  121. * SHUTDOWN CODE
  122. */
  123. cout << "[ORC] Finishing experiment." << endl;
  124. cout << "[ORC] Sending shutdown commands to clients." << endl;
  125. shut_down_remote_actors(clientIPs, clientPorts);
  126. cout << "[ORC] Sending shutdown commands to servers." << endl;
  127. shut_down_remote_actors(serverIPs, serverPorts);
  128. return 0;
  129. }