networkOrchestrator.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. #include <algorithm>
  5. #include <cstdlib>
  6. #include <thread>
  7. #include <unistd.h>
  8. #include <sys/types.h>
  9. #include <sys/wait.h>
  10. #include "networkOrchestrator.hpp"
  11. /***************************************************
  12. ********* *********
  13. ********* orchestrator public functions *********
  14. ********* *********
  15. ***************************************************/
  16. /*
  17. * START UP AND SHUT DOWN INSTANCES
  18. */
  19. void start_remote_actor(
  20. const std::string& target,
  21. bool server,
  22. const std::string& id,
  23. const std::string& output,
  24. size_t lambda,
  25. bool maliciousServers)
  26. {
  27. const char* sshFile = "/usr/bin/ssh";
  28. const char* serverFile = "bin/server";
  29. const char* clientFile = "bin/client";
  30. const char* calledFile = (target != "self" && !target.empty() ? sshFile : (server ? serverFile : clientFile));
  31. char *argv[6];
  32. char fileBuffer[13];
  33. strncpy(fileBuffer, calledFile, 13);
  34. argv[0] = fileBuffer;
  35. char flagBuffer[3];
  36. strncpy(flagBuffer, "-n", 3);
  37. char targetBuffer[64];
  38. strncpy(targetBuffer, target.c_str(), 64);
  39. std::string fullArgString("~/prsona/prsona/scripts/startup.sh ");
  40. char fullArgBuffer[256];
  41. fullArgString = fullArgString + (server ? "server" : "client") + " ";
  42. char idBuffer[64];
  43. strncpy(idBuffer, id.c_str(), 64);
  44. fullArgString = fullArgString + id + " ";
  45. char outputBuffer[128];
  46. strncpy(outputBuffer, output.c_str(), 128);
  47. fullArgString = fullArgString + output + " ";
  48. std::stringstream lambdaStream;
  49. lambdaStream << lambda;
  50. char lambdaBuffer[3];
  51. strncpy(lambdaBuffer, lambdaStream.str().c_str(), 3);
  52. fullArgString = fullArgString + lambdaStream.str() + " ";
  53. char maliciousBuffer[3];
  54. if (maliciousServers)
  55. strncpy(maliciousBuffer, "T", 2);
  56. else
  57. strncpy(maliciousBuffer, "F", 2);
  58. fullArgString = fullArgString + (maliciousServers ? "T" : "F");
  59. strncpy(fullArgBuffer, fullArgString.c_str(), 256);
  60. if (target != "self" && !target.empty())
  61. {
  62. argv[1] = flagBuffer;
  63. argv[2] = targetBuffer;
  64. argv[3] = fullArgBuffer;
  65. argv[4] = NULL;
  66. }
  67. else
  68. {
  69. argv[1] = idBuffer;
  70. argv[2] = outputBuffer;
  71. argv[3] = lambdaBuffer;
  72. argv[4] = maliciousBuffer;
  73. argv[5] = NULL;
  74. }
  75. int pid = fork();
  76. if (pid < 0)
  77. exit(1);
  78. if (pid == 0)
  79. execv(calledFile, argv);
  80. }
  81. void shut_down_remote_actors(
  82. const std::vector<std::string>& relevantIPs,
  83. const std::vector<int>& relevantPorts)
  84. {
  85. for (size_t i = 0; i < relevantIPs.size(); i++)
  86. {
  87. // Shut downs are triggered by a GET request to the correct location
  88. std::stringstream sysString;
  89. std::string data;
  90. sysString << "GET " << EXIT_URI << " HTTP/1.1\r\n";
  91. sysString << "Host: " << relevantIPs[i] << ":" << relevantPorts[i] << "\r\n\r\n";
  92. data = sysString.str();
  93. struct mg_connection *conn = NULL;
  94. // Connect to the instance
  95. while (!conn)
  96. {
  97. conn = mg_connect_client(relevantIPs[i].c_str(), relevantPorts[i], USE_SSL, NULL, 0);
  98. if (!conn)
  99. std::cerr << "Couldn't connect to instance at " << relevantIPs[i] << ":" << relevantPorts[i] << " for shut down." << std::endl;
  100. }
  101. // Make correct GET request
  102. mg_write(conn, data.c_str(), data.length());
  103. // Close connection
  104. mg_close_connection(conn);
  105. }
  106. }
  107. /*
  108. * SYNCHRONIZATION
  109. */
  110. void wait_for_servers_ready(
  111. std::string dealer,
  112. int dealerPort)
  113. {
  114. // Requesting information about servers being ready is done via a GET request
  115. std::stringstream sysString;
  116. std::string data;
  117. sysString << "GET " << EPOCH_READY_URI << " HTTP/1.1\r\n";
  118. sysString << "Host: " << dealer << ":" << dealerPort << "\r\n\r\n";
  119. data = sysString.str();
  120. bool ready = false;
  121. while (!ready)
  122. {
  123. struct mg_connection *conn = NULL;
  124. // Connect to the dealer
  125. while (!conn)
  126. {
  127. conn = mg_connect_client(dealer.c_str(), dealerPort, USE_SSL, NULL, 0);
  128. if (!conn)
  129. {
  130. std::cerr << "Couldn't make connection while waiting for servers to be ready." << std::endl;
  131. std::this_thread::sleep_for(HALF_SECOND);
  132. }
  133. }
  134. // Make the correct GET request
  135. mg_write(conn, data.c_str(), data.length());
  136. // Wait for a response
  137. mg_get_response(conn, NULL, 0, 250);
  138. const struct mg_response_info *info = mg_get_response_info(conn);
  139. // Close connection
  140. mg_close_connection(conn);
  141. // If the dealer says it's ready, then we can move on
  142. if (info->status_code == 200)
  143. ready = true;
  144. }
  145. }
  146. void wait_for_clients_created(
  147. std::string dealer,
  148. int dealerPort,
  149. size_t numClients)
  150. {
  151. bool ready = false;
  152. while (!ready)
  153. {
  154. struct synchronization_tool sync;
  155. struct mg_connection *conn = NULL;
  156. // Connect to the dealer
  157. std::unique_lock<std::mutex> lck(sync.mtx);
  158. sync.val = 0;
  159. sync.val2 = 0;
  160. while (!conn)
  161. {
  162. conn = mg_connect_websocket_client(dealer.c_str(), dealerPort, USE_SSL, NULL, 0, REQUEST_NUM_CLIENTS_URI, "null", clients_websocket_data_handler, synchro_websocket_close_handler, &sync);
  163. if (!conn)
  164. {
  165. std::cerr << "Couldn't make connection while waiting for clients to be ready." << std::endl;
  166. std::this_thread::sleep_for(HALF_SECOND);
  167. }
  168. }
  169. // Tell the dealer we're ready for its response
  170. mg_websocket_client_write(conn, MG_WEBSOCKET_OPCODE_DATACOMPLETE, "", 0);
  171. // Wait for that response
  172. while (!sync.val2)
  173. sync.cv.wait(lck);
  174. // Close connection
  175. mg_close_connection(conn);
  176. // If the dealer says it's ready, then we can move on
  177. if (sync.val == numClients)
  178. ready = true;
  179. }
  180. }
  181. void wait_for_client_ready(
  182. std::string client,
  183. int clientPort)
  184. {
  185. // Requesting information about clients being ready is done via a GET request
  186. std::stringstream sysString;
  187. std::string data;
  188. sysString << "GET " << CLIENT_READY_URI << " HTTP/1.1\r\n";
  189. sysString << "Host: " << client << ":" << clientPort << "\r\n\r\n";
  190. data = sysString.str();
  191. bool ready = false;
  192. while (!ready)
  193. {
  194. struct mg_connection *conn = NULL;
  195. // Connect to the client
  196. while (!conn)
  197. {
  198. conn = mg_connect_client(client.c_str(), clientPort, USE_SSL, NULL, 0);
  199. if (!conn)
  200. {
  201. std::cerr << "Couldn't make connection while waiting for client (" << client << ":" << clientPort << ") to be ready." << std::endl;
  202. std::this_thread::sleep_for(HALF_SECOND);
  203. }
  204. }
  205. // Make the correct GET request
  206. mg_write(conn, data.c_str(), data.length());
  207. // Wait for a response
  208. mg_get_response(conn, NULL, 0, 250);
  209. const struct mg_response_info *info = mg_get_response_info(conn);
  210. // Close connection
  211. mg_close_connection(conn);
  212. // If the client says it's ready, then we can move on
  213. if (info->status_code == 200)
  214. ready = true;
  215. }
  216. }
  217. /*
  218. * RUN EXPERIMENT
  219. */
  220. void execute_experiment(
  221. std::default_random_engine& rng,
  222. std::string dealerIP,
  223. int dealerPort,
  224. std::vector<std::string> serverIPs,
  225. std::vector<int> serverPorts,
  226. std::vector<std::string> clientIPs,
  227. std::vector<int> clientPorts,
  228. const char *filename)
  229. {
  230. size_t line = 1;
  231. // Iterate across each line in the command file, which contains one command per line
  232. char buffer[128];
  233. std::ifstream commands(filename);
  234. while (!commands.eof())
  235. {
  236. commands.getline(buffer, 128);
  237. if (strlen(buffer) == 0)
  238. {
  239. line++;
  240. continue;
  241. }
  242. std::cout << "Command " << line << ": " << std::string(buffer) << std::endl;
  243. std::vector<size_t> whichActors;
  244. std::vector<std::vector<size_t>> proofActors;
  245. std::vector<std::thread> clientWaiters;
  246. int numVoters, numProofs;
  247. // The first character of each command tells us which it is
  248. switch(buffer[0])
  249. {
  250. // Vote triggers come in form `V <numVoters>`
  251. case 'V':
  252. numVoters = atoi(strtok(buffer + 1, " "));
  253. whichActors = generate_random_set(rng, numVoters, clientIPs.size());
  254. for (size_t i = 0; i < whichActors.size(); i++)
  255. trigger_vote(clientIPs[whichActors[i]], clientPorts[whichActors[i]]);
  256. std::this_thread::sleep_for(HALF_SECOND);
  257. for (size_t i = 0; i < whichActors.size(); i++)
  258. clientWaiters.push_back(std::thread(wait_for_client_ready, clientIPs[whichActors[i]], clientPorts[whichActors[i]]));
  259. for (size_t i = 0; i < clientWaiters.size(); i++)
  260. clientWaiters[i].join();
  261. clientWaiters.clear();
  262. break;
  263. // Reputation proof triggers come in form `R <numProofs>`
  264. case 'R':
  265. numProofs = atoi(strtok(buffer + 1, " "));
  266. for (int i = 0; i < numProofs; i++)
  267. {
  268. whichActors = generate_random_set(rng, 2, clientIPs.size());
  269. trigger_reputation_proof(
  270. clientIPs[whichActors[0]],
  271. clientPorts[whichActors[0]],
  272. clientIPs[whichActors[1]],
  273. clientPorts[whichActors[1]]);
  274. proofActors.push_back(whichActors);
  275. }
  276. std::this_thread::sleep_for(HALF_SECOND);
  277. for (size_t i = 0; i < proofActors.size(); i++)
  278. clientWaiters.push_back(std::thread(wait_for_client_ready, clientIPs[proofActors[i][0]], clientPorts[proofActors[i][0]]));
  279. for (size_t i = 0; i < clientWaiters.size(); i++)
  280. clientWaiters[i].join();
  281. proofActors.clear();
  282. clientWaiters.clear();
  283. break;
  284. // Epoch change triggers come in form `E`
  285. case 'E':
  286. trigger_epoch_change(dealerIP, dealerPort);
  287. std::this_thread::sleep_for(HALF_SECOND);
  288. wait_for_servers_ready(dealerIP, dealerPort);
  289. break;
  290. default:
  291. break;
  292. }
  293. line++;
  294. }
  295. // Don't let ourselves shut down servers and clients until we're sure they're not in the middle of anything else
  296. wait_for_servers_ready(dealerIP, dealerPort);
  297. for (size_t i = 0; i < clientIPs.size(); i++)
  298. wait_for_client_ready(clientIPs[i], clientPorts[i]);
  299. }
  300. /****************************************************
  301. ********* *********
  302. ********* orchestrator private functions *********
  303. ********* *********
  304. ****************************************************/
  305. /*
  306. * TRIGGER EXPERIMENT EVENTS
  307. */
  308. void trigger_epoch_change(
  309. std::string dealer,
  310. int dealerPort)
  311. {
  312. // Epoch changes are triggered via GET request to the correct location
  313. std::stringstream sysString;
  314. std::string data;
  315. sysString << "GET " << TRIGGER_EPOCH_URI << " HTTP/1.1\r\n";
  316. sysString << "Host: " << dealer << ":" << dealerPort << "\r\n\r\n";
  317. data = sysString.str();
  318. struct mg_connection *conn = NULL;
  319. // Connect to the dealer
  320. while (!conn)
  321. {
  322. conn = mg_connect_client(dealer.c_str(), dealerPort, USE_SSL, NULL, 0);
  323. if (!conn)
  324. std::cerr << "Couldn't connect to dealer to trigger epoch change." << std::endl;
  325. }
  326. // Make the relevant GET request
  327. mg_write(conn, data.c_str(), data.length());
  328. // Close connection
  329. mg_close_connection(conn);
  330. }
  331. void trigger_vote(
  332. std::string target,
  333. int targetPort)
  334. {
  335. // New votes are triggered via GET request to the correct location
  336. std::stringstream sysString;
  337. std::string data;
  338. sysString << "GET " << TRIGGER_VOTE_URI << " HTTP/1.1\r\n";
  339. sysString << "Host: " << target << ":" << targetPort << "\r\n\r\n";
  340. data = sysString.str();
  341. struct mg_connection *conn = NULL;
  342. // Connect to the client
  343. while (!conn)
  344. {
  345. conn = mg_connect_client(target.c_str(), targetPort, USE_SSL, NULL, 0);
  346. if (!conn)
  347. std::cerr << "Couldn't connect to client at " << target << ":" << targetPort << " to trigger new vote." << std::endl;
  348. }
  349. // Make the relevant GET request
  350. mg_write(conn, data.c_str(), data.length());
  351. // Close connection
  352. mg_close_connection(conn);
  353. }
  354. void trigger_reputation_proof(
  355. std::string target,
  356. int targetPort,
  357. std::string verifier,
  358. int verifierPort)
  359. {
  360. // Reputation proofs are triggered via GET request to the correct location (with a parameter for the intended verifier)
  361. std::stringstream sysString;
  362. std::string data;
  363. sysString << "GET " << TRIGGER_REP_URI << "?" << verifier << ":" << verifierPort << " HTTP/1.1\r\n";
  364. sysString << "Host: " << target << "\r\n\r\n";
  365. data = sysString.str();
  366. struct mg_connection *conn = NULL;
  367. // Connect to the client
  368. while (!conn)
  369. {
  370. conn = mg_connect_client(target.c_str(), targetPort, USE_SSL, NULL, 0);
  371. if (!conn)
  372. std::cerr << "Couldn't connect to client at " << target << ":" << targetPort << " to trigger reputation proof." << std::endl;
  373. }
  374. // Make the relevant GET request
  375. mg_write(conn, data.c_str(), data.length());
  376. // Close connection
  377. mg_close_connection(conn);
  378. }
  379. /*
  380. * EXECUTOR HELPER
  381. */
  382. std::vector<size_t> generate_random_set(
  383. std::default_random_engine& rng,
  384. size_t size,
  385. size_t maxVal)
  386. {
  387. std::vector<size_t> holder;
  388. for (size_t i = 0; i < maxVal; i++)
  389. holder.push_back(i);
  390. shuffle(holder.begin(), holder.end(), rng);
  391. if (size > holder.size())
  392. size = holder.size();
  393. return std::vector<size_t>(holder.begin(), holder.begin() + size);
  394. }