networkOrchestrator.cpp 14 KB

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