orchestratorMain.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. targeter["127.0.0.1"] = "self";
  69. string configDir = "cfg/" + output;
  70. // Read in from config files the server locations
  71. load_multiple_instances_config(serverIPs, serverPorts, (configDir + "/serverIPs.cfg").c_str());
  72. // And now the client locations
  73. load_multiple_instances_config(clientIPs, clientPorts, (configDir + "/clientIPs.cfg").c_str());
  74. // And finally the dealer location
  75. load_single_instance_config(dealerIP, dealerPortStr, dealerPort, (configDir + "/dealerIP.cfg").c_str());
  76. size_t numServers = serverIPs.size();
  77. size_t numClients = clientIPs.size();
  78. signal(SIGCHLD, cleanup);
  79. /*
  80. * ORCHESTRATOR SETUP CODE
  81. */
  82. cout << "[ORC] This experiment is running with output code: " << output << endl;
  83. cout << "[ORC] This experiment is running with " << (maliciousServers ? "MALICIOUS" : "HBC") << " servers." << endl;
  84. cout << "[ORC] Lambda is " << lambda << "." << endl;
  85. cout << endl;
  86. cout << "[ORC] Starting BGN dealer server." << endl;
  87. vector<thread> serverStartup, clientStartup, clientReady;
  88. serverStartup.push_back(thread(start_remote_actor, targeter[dealerIP], true, "d", output, lambda, maliciousServers));
  89. this_thread::sleep_for(TWO_SECONDS);
  90. cout << "[ORC] Starting other servers." << endl;
  91. for (size_t i = 0; i < numServers; i++)
  92. {
  93. if (serverIPs[i] == dealerIP && serverPorts[i] == dealerPort)
  94. continue;
  95. serverStartup.push_back(thread(start_remote_actor, targeter[serverIPs[i]], true, "s" + to_string(i), output, lambda, maliciousServers));
  96. }
  97. cout << "[ORC] Waiting for confirmation that servers are ready to continue." << endl;
  98. for (size_t i = 0; i < numServers; i++)
  99. serverStartup[i].join();
  100. wait_for_servers_ready(dealerIP, dealerPort);
  101. cout << "[ORC] Starting clients." << endl;
  102. for (size_t i = 0; i < numClients; i++)
  103. {
  104. clientStartup.push_back(thread(start_remote_actor, targeter[clientIPs[i]], false, "c" + to_string(i), output, lambda, maliciousServers));
  105. this_thread::sleep_for(ONE_SECOND);
  106. }
  107. cout << "[ORC] Waiting for confirmation that servers have all clients logged." << endl;
  108. for (size_t i = 0; i < numClients; i++)
  109. clientStartup[i].join();
  110. wait_for_clients_created(dealerIP, dealerPort, numClients);
  111. cout << "[ORC] Waiting for confirmation that clients are ready to continue." << endl;
  112. for (size_t i = 0; i < numClients; i++)
  113. clientReady.push_back(thread(wait_for_client_ready, clientIPs[i], clientPorts[i]));
  114. for (size_t i = 0; i < numClients; i++)
  115. clientReady[i].join();
  116. /*
  117. * MAIN ORCHESTRATOR LOOP CODE
  118. */
  119. cout << "[ORC] Beginning experiment." << endl;
  120. execute_experiment(rng, dealerIP, dealerPort, serverIPs, serverPorts, clientIPs, clientPorts, (configDir + "/commands.cfg").c_str());
  121. /*
  122. * SHUTDOWN CODE
  123. */
  124. cout << "[ORC] Finishing experiment." << endl;
  125. cout << "[ORC] Sending shutdown commands to clients." << endl;
  126. shut_down_remote_actors(clientIPs, clientPorts);
  127. cout << "[ORC] Sending shutdown commands to servers." << endl;
  128. shut_down_remote_actors(serverIPs, serverPorts);
  129. return 0;
  130. }