orchestratorMain.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 << endl;
  84. cout << "[ORC] Starting BGN dealer server." << endl;
  85. vector<thread> serverStartup, clientStartup, clientReady;
  86. serverStartup.push_back(thread(start_remote_actor, targeter[dealerIP], true, "d", output, lambda, maliciousServers));
  87. this_thread::sleep_for(TWO_SECONDS);
  88. cout << "[ORC] Starting other servers." << endl;
  89. for (size_t i = 0; i < numServers; i++)
  90. {
  91. if (serverIPs[i] == dealerIP && serverPorts[i] == dealerPort)
  92. continue;
  93. serverStartup.push_back(thread(start_remote_actor, targeter[serverIPs[i]], true, "s" + to_string(i), output, lambda, maliciousServers));
  94. }
  95. cout << "[ORC] Waiting for confirmation that servers are ready to continue." << endl;
  96. for (size_t i = 0; i < numServers; i++)
  97. serverStartup[i].join();
  98. wait_for_servers_ready(dealerIP, dealerPort);
  99. cout << "[ORC] Starting clients." << endl;
  100. for (size_t i = 0; i < numClients; i++)
  101. {
  102. clientStartup.push_back(thread(start_remote_actor, targeter[clientIPs[i]], false, "c" + to_string(i), output, lambda, maliciousServers));
  103. this_thread::sleep_for(ONE_SECOND);
  104. }
  105. cout << "[ORC] Waiting for confirmation that servers have all clients logged." << endl;
  106. for (size_t i = 0; i < numClients; i++)
  107. clientStartup[i].join();
  108. wait_for_clients_created(dealerIP, dealerPort, numClients);
  109. cout << "[ORC] Waiting for confirmation that clients are ready to continue." << endl;
  110. for (size_t i = 0; i < numClients; i++)
  111. clientReady.push_back(thread(wait_for_client_ready, clientIPs[i], clientPorts[i]));
  112. for (size_t i = 0; i < numClients; i++)
  113. clientReady[i].join();
  114. /*
  115. * MAIN ORCHESTRATOR LOOP CODE
  116. */
  117. cout << "[ORC] Beginning experiment." << endl;
  118. execute_experiment(rng, dealerIP, dealerPort, serverIPs, serverPorts, clientIPs, clientPorts, (configDir + "/commands.cfg").c_str());
  119. /*
  120. * SHUTDOWN CODE
  121. */
  122. cout << "[ORC] Finishing experiment." << endl;
  123. cout << "[ORC] Sending shutdown commands to clients." << endl;
  124. shut_down_remote_actors(clientIPs, clientPorts);
  125. cout << "[ORC] Sending shutdown commands to servers." << endl;
  126. shut_down_remote_actors(serverIPs, serverPorts);
  127. return 0;
  128. }