gnunet-service-testbed_peers.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2008--2013 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file testbed/gnunet-service-testbed_peers.c
  19. * @brief implementation of TESTBED service that deals with peer management
  20. * @author Sree Harsha Totakura <sreeharsha@totakura.in>
  21. */
  22. #include "gnunet-service-testbed.h"
  23. #include "gnunet_arm_service.h"
  24. #include <zlib.h>
  25. /**
  26. * A list of peers we know about
  27. */
  28. struct Peer **GST_peer_list;
  29. /**
  30. * The current number of peers running locally under this controller
  31. */
  32. unsigned int GST_num_local_peers;
  33. /**
  34. * Context information to manage peers' services
  35. */
  36. struct ManageServiceContext
  37. {
  38. /**
  39. * DLL next ptr
  40. */
  41. struct ManageServiceContext *next;
  42. /**
  43. * DLL prev ptr
  44. */
  45. struct ManageServiceContext *prev;
  46. /**
  47. * The ARM handle of the peer
  48. */
  49. struct GNUNET_ARM_Handle *ah;
  50. /**
  51. * peer whose service has to be managed
  52. */
  53. struct Peer *peer;
  54. /**
  55. * The client which requested to manage the peer's service
  56. */
  57. struct GNUNET_SERVER_Client *client;
  58. /**
  59. * The operation id of the associated request
  60. */
  61. uint64_t op_id;
  62. /**
  63. * 1 if the service at the peer has to be started; 0 if it has to be stopped
  64. */
  65. uint8_t start;
  66. /**
  67. * Is this context expired? Do not work on this context if it is set to
  68. * GNUNET_YES
  69. */
  70. uint8_t expired;
  71. };
  72. /**
  73. * Context information for peer re-configure operations
  74. */
  75. struct PeerReconfigureContext
  76. {
  77. /**
  78. * DLL next for inclusoin in peer reconfigure operations list
  79. */
  80. struct PeerReconfigureContext *next;
  81. /**
  82. * DLL prev
  83. */
  84. struct PeerReconfigureContext *prev;
  85. /**
  86. * The client which gave this operation to us
  87. */
  88. struct GNUNET_SERVER_Client *client;
  89. /**
  90. * The configuration handle to use as the new template
  91. */
  92. struct GNUNET_CONFIGURATION_Handle *cfg;
  93. /**
  94. * The id of the operation
  95. */
  96. uint64_t op_id;
  97. /**
  98. * The id of the peer which has to be reconfigured
  99. */
  100. uint32_t peer_id;
  101. /**
  102. * The the peer stopped? Used while cleaning up this context to decide
  103. * whether the asynchronous stop request through Testing/ARM API has to be
  104. * cancelled
  105. */
  106. uint8_t stopped;
  107. };
  108. /**
  109. * The DLL head for the peer reconfigure list
  110. */
  111. static struct PeerReconfigureContext *prc_head;
  112. /**
  113. * The DLL tail for the peer reconfigure list
  114. */
  115. static struct PeerReconfigureContext *prc_tail;
  116. /**
  117. * DLL head for queue of manage service requests
  118. */
  119. static struct ManageServiceContext *mctx_head;
  120. /**
  121. * DLL tail for queue of manage service requests
  122. */
  123. static struct ManageServiceContext *mctx_tail;
  124. /**
  125. * Adds a peer to the peer array
  126. *
  127. * @param peer the peer to add
  128. */
  129. static void
  130. peer_list_add (struct Peer *peer)
  131. {
  132. if (peer->id >= GST_peer_list_size)
  133. GST_array_grow_large_enough (GST_peer_list, GST_peer_list_size, peer->id);
  134. GNUNET_assert (NULL == GST_peer_list[peer->id]);
  135. GST_peer_list[peer->id] = peer;
  136. if (GNUNET_NO == peer->is_remote)
  137. GST_num_local_peers++;
  138. }
  139. /**
  140. * Removes a the give peer from the peer array
  141. *
  142. * @param peer the peer to be removed
  143. */
  144. static void
  145. peer_list_remove (struct Peer *peer)
  146. {
  147. unsigned int orig_size;
  148. uint32_t id;
  149. if (GNUNET_NO == peer->is_remote)
  150. GST_num_local_peers--;
  151. GST_peer_list[peer->id] = NULL;
  152. orig_size = GST_peer_list_size;
  153. while (GST_peer_list_size >= LIST_GROW_STEP)
  154. {
  155. for (id = GST_peer_list_size - 1;
  156. (id >= GST_peer_list_size - LIST_GROW_STEP) && (id != UINT32_MAX);
  157. id--)
  158. if (NULL != GST_peer_list[id])
  159. break;
  160. if (id != ((GST_peer_list_size - LIST_GROW_STEP) - 1))
  161. break;
  162. GST_peer_list_size -= LIST_GROW_STEP;
  163. }
  164. if (orig_size == GST_peer_list_size)
  165. return;
  166. GST_peer_list =
  167. GNUNET_realloc (GST_peer_list,
  168. sizeof (struct Peer *) * GST_peer_list_size);
  169. }
  170. /**
  171. * The task to be executed if the forwarded peer create operation has been
  172. * timed out
  173. *
  174. * @param cls the FowardedOperationContext
  175. * @param tc the TaskContext from the scheduler
  176. */
  177. static void
  178. peer_create_forward_timeout (void *cls,
  179. const struct GNUNET_SCHEDULER_TaskContext *tc)
  180. {
  181. struct ForwardedOperationContext *fopc = cls;
  182. GNUNET_free (fopc->cls);
  183. GST_forwarded_operation_timeout (fopc, tc);
  184. }
  185. /**
  186. * Callback to be called when forwarded peer create operation is successfull. We
  187. * have to relay the reply msg back to the client
  188. *
  189. * @param cls ForwardedOperationContext
  190. * @param msg the peer create success message
  191. */
  192. static void
  193. peer_create_success_cb (void *cls, const struct GNUNET_MessageHeader *msg)
  194. {
  195. struct ForwardedOperationContext *fopc = cls;
  196. struct Peer *remote_peer;
  197. if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_TESTBED_CREATE_PEER_SUCCESS)
  198. {
  199. GNUNET_assert (NULL != fopc->cls);
  200. remote_peer = fopc->cls;
  201. peer_list_add (remote_peer);
  202. }
  203. GST_forwarded_operation_reply_relay (fopc, msg);
  204. }
  205. /**
  206. * Function to destroy a peer
  207. *
  208. * @param peer the peer structure to destroy
  209. */
  210. void
  211. GST_destroy_peer (struct Peer *peer)
  212. {
  213. GNUNET_break (0 == peer->reference_cnt);
  214. if (GNUNET_YES == peer->is_remote)
  215. {
  216. peer_list_remove (peer);
  217. GNUNET_free (peer);
  218. return;
  219. }
  220. if (GNUNET_YES == peer->details.local.is_running)
  221. {
  222. GNUNET_TESTING_peer_stop (peer->details.local.peer);
  223. peer->details.local.is_running = GNUNET_NO;
  224. }
  225. GNUNET_TESTING_peer_destroy (peer->details.local.peer);
  226. GNUNET_CONFIGURATION_destroy (peer->details.local.cfg);
  227. peer_list_remove (peer);
  228. GNUNET_free (peer);
  229. }
  230. /**
  231. * Callback to be called when forwarded peer destroy operation is successfull. We
  232. * have to relay the reply msg back to the client
  233. *
  234. * @param cls ForwardedOperationContext
  235. * @param msg the peer create success message
  236. */
  237. static void
  238. peer_destroy_success_cb (void *cls, const struct GNUNET_MessageHeader *msg)
  239. {
  240. struct ForwardedOperationContext *fopc = cls;
  241. struct Peer *remote_peer;
  242. if (GNUNET_MESSAGE_TYPE_TESTBED_GENERIC_OPERATION_SUCCESS ==
  243. ntohs (msg->type))
  244. {
  245. remote_peer = fopc->cls;
  246. GNUNET_assert (NULL != remote_peer);
  247. remote_peer->destroy_flag = GNUNET_YES;
  248. if (0 == remote_peer->reference_cnt)
  249. GST_destroy_peer (remote_peer);
  250. }
  251. GST_forwarded_operation_reply_relay (fopc, msg);
  252. }
  253. /**
  254. * Handler for GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER messages
  255. *
  256. * @param cls NULL
  257. * @param client identification of the client
  258. * @param message the actual message
  259. */
  260. void
  261. GST_handle_peer_create (void *cls, struct GNUNET_SERVER_Client *client,
  262. const struct GNUNET_MessageHeader *message)
  263. {
  264. const struct GNUNET_TESTBED_PeerCreateMessage *msg;
  265. struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *reply;
  266. struct GNUNET_CONFIGURATION_Handle *cfg;
  267. struct ForwardedOperationContext *fo_ctxt;
  268. struct Route *route;
  269. struct Peer *peer;
  270. char *emsg;
  271. uint32_t host_id;
  272. uint32_t peer_id;
  273. uint16_t msize;
  274. msize = ntohs (message->size);
  275. if (msize <= sizeof (struct GNUNET_TESTBED_PeerCreateMessage))
  276. {
  277. GNUNET_break (0); /* We need configuration */
  278. GNUNET_SERVER_receive_done (client, GNUNET_OK);
  279. return;
  280. }
  281. msg = (const struct GNUNET_TESTBED_PeerCreateMessage *) message;
  282. host_id = ntohl (msg->host_id);
  283. peer_id = ntohl (msg->peer_id);
  284. if (VALID_PEER_ID (peer_id))
  285. {
  286. (void) GNUNET_asprintf (&emsg, "Peer with ID %u already exists", peer_id);
  287. GST_send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
  288. emsg);
  289. GNUNET_free (emsg);
  290. GNUNET_SERVER_receive_done (client, GNUNET_OK);
  291. return;
  292. }
  293. if (UINT32_MAX == peer_id)
  294. {
  295. GST_send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
  296. "Cannot create peer with given ID");
  297. GNUNET_SERVER_receive_done (client, GNUNET_OK);
  298. return;
  299. }
  300. if (host_id == GST_context->host_id)
  301. {
  302. /* We are responsible for this peer */
  303. cfg = GNUNET_TESTBED_extract_config_ (message);
  304. if (NULL == cfg)
  305. {
  306. GNUNET_break (0);
  307. GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
  308. return;
  309. }
  310. GNUNET_CONFIGURATION_set_value_number (cfg, "TESTBED", "PEERID",
  311. (unsigned long long) peer_id);
  312. GNUNET_CONFIGURATION_set_value_number (cfg, "PATHS", "PEERID",
  313. (unsigned long long) peer_id);
  314. peer = GNUNET_new (struct Peer);
  315. peer->is_remote = GNUNET_NO;
  316. peer->details.local.cfg = cfg;
  317. peer->id = peer_id;
  318. LOG_DEBUG ("Creating peer with id: %u\n", (unsigned int) peer->id);
  319. peer->details.local.peer =
  320. GNUNET_TESTING_peer_configure (GST_context->system,
  321. peer->details.local.cfg, peer->id,
  322. NULL /* Peer id */ ,
  323. &emsg);
  324. if (NULL == peer->details.local.peer)
  325. {
  326. LOG (GNUNET_ERROR_TYPE_WARNING, "Configuring peer failed: %s\n", emsg);
  327. GNUNET_free (emsg);
  328. GNUNET_free (peer);
  329. GNUNET_break (0);
  330. GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
  331. return;
  332. }
  333. peer->details.local.is_running = GNUNET_NO;
  334. peer_list_add (peer);
  335. reply = GNUNET_new (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage);
  336. reply->header.size =
  337. htons (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
  338. reply->header.type =
  339. htons (GNUNET_MESSAGE_TYPE_TESTBED_CREATE_PEER_SUCCESS);
  340. reply->peer_id = msg->peer_id;
  341. reply->operation_id = msg->operation_id;
  342. GST_queue_message (client, &reply->header);
  343. GNUNET_SERVER_receive_done (client, GNUNET_OK);
  344. return;
  345. }
  346. /* Forward peer create request */
  347. route = GST_find_dest_route (host_id);
  348. if (NULL == route)
  349. {
  350. GNUNET_break (0);
  351. GNUNET_SERVER_receive_done (client, GNUNET_OK);
  352. return;
  353. }
  354. peer = GNUNET_new (struct Peer);
  355. peer->is_remote = GNUNET_YES;
  356. peer->id = peer_id;
  357. peer->details.remote.slave = GST_slave_list[route->dest];
  358. peer->details.remote.remote_host_id = host_id;
  359. fo_ctxt = GNUNET_new (struct ForwardedOperationContext);
  360. GNUNET_SERVER_client_keep (client);
  361. fo_ctxt->client = client;
  362. fo_ctxt->operation_id = GNUNET_ntohll (msg->operation_id);
  363. fo_ctxt->cls = peer;
  364. fo_ctxt->type = OP_PEER_CREATE;
  365. fo_ctxt->opc =
  366. GNUNET_TESTBED_forward_operation_msg_ (GST_slave_list
  367. [route->dest]->controller,
  368. fo_ctxt->operation_id,
  369. &msg->header,
  370. peer_create_success_cb, fo_ctxt);
  371. fo_ctxt->timeout_task =
  372. GNUNET_SCHEDULER_add_delayed (GST_timeout, &peer_create_forward_timeout,
  373. fo_ctxt);
  374. GNUNET_CONTAINER_DLL_insert_tail (fopcq_head, fopcq_tail, fo_ctxt);
  375. GNUNET_SERVER_receive_done (client, GNUNET_OK);
  376. }
  377. /**
  378. * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
  379. *
  380. * @param cls NULL
  381. * @param client identification of the client
  382. * @param message the actual message
  383. */
  384. void
  385. GST_handle_peer_destroy (void *cls, struct GNUNET_SERVER_Client *client,
  386. const struct GNUNET_MessageHeader *message)
  387. {
  388. const struct GNUNET_TESTBED_PeerDestroyMessage *msg;
  389. struct ForwardedOperationContext *fopc;
  390. struct Peer *peer;
  391. uint32_t peer_id;
  392. msg = (const struct GNUNET_TESTBED_PeerDestroyMessage *) message;
  393. peer_id = ntohl (msg->peer_id);
  394. LOG_DEBUG ("Received peer destory on peer: %u and operation id: %ul\n",
  395. peer_id, GNUNET_ntohll (msg->operation_id));
  396. if (!VALID_PEER_ID (peer_id))
  397. {
  398. LOG (GNUNET_ERROR_TYPE_ERROR,
  399. "Asked to destroy a non existent peer with id: %u\n", peer_id);
  400. GST_send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
  401. "Peer doesn't exist");
  402. GNUNET_SERVER_receive_done (client, GNUNET_OK);
  403. return;
  404. }
  405. peer = GST_peer_list[peer_id];
  406. if (GNUNET_YES == peer->is_remote)
  407. {
  408. /* Forward the destory message to sub controller */
  409. fopc = GNUNET_new (struct ForwardedOperationContext);
  410. GNUNET_SERVER_client_keep (client);
  411. fopc->client = client;
  412. fopc->cls = peer;
  413. fopc->type = OP_PEER_DESTROY;
  414. fopc->operation_id = GNUNET_ntohll (msg->operation_id);
  415. fopc->opc =
  416. GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.
  417. slave->controller,
  418. fopc->operation_id, &msg->header,
  419. &peer_destroy_success_cb, fopc);
  420. fopc->timeout_task =
  421. GNUNET_SCHEDULER_add_delayed (GST_timeout, &GST_forwarded_operation_timeout,
  422. fopc);
  423. GNUNET_CONTAINER_DLL_insert_tail (fopcq_head, fopcq_tail, fopc);
  424. GNUNET_SERVER_receive_done (client, GNUNET_OK);
  425. return;
  426. }
  427. peer->destroy_flag = GNUNET_YES;
  428. if (0 == peer->reference_cnt)
  429. GST_destroy_peer (peer);
  430. else
  431. LOG (GNUNET_ERROR_TYPE_DEBUG,
  432. "Delaying peer destroy as peer is currently in use\n");
  433. GST_send_operation_success_msg (client, GNUNET_ntohll (msg->operation_id));
  434. GNUNET_SERVER_receive_done (client, GNUNET_OK);
  435. }
  436. /**
  437. * Stats a peer
  438. *
  439. * @param peer the peer to start
  440. * @return GNUNET_OK upon success; GNUNET_SYSERR upon failure
  441. */
  442. static int
  443. start_peer (struct Peer *peer)
  444. {
  445. GNUNET_assert (GNUNET_NO == peer->is_remote);
  446. if (GNUNET_OK != GNUNET_TESTING_peer_start (peer->details.local.peer))
  447. return GNUNET_SYSERR;
  448. peer->details.local.is_running = GNUNET_YES;
  449. return GNUNET_OK;
  450. }
  451. /**
  452. * Stops a peer
  453. *
  454. * @param peer the peer to stop
  455. * @return GNUNET_OK upon success; GNUNET_SYSERR upon failure
  456. */
  457. static int
  458. stop_peer (struct Peer *peer)
  459. {
  460. GNUNET_assert (GNUNET_NO == peer->is_remote);
  461. if (GNUNET_OK != GNUNET_TESTING_peer_kill (peer->details.local.peer))
  462. return GNUNET_SYSERR;
  463. peer->details.local.is_running = GNUNET_NO;
  464. return GNUNET_OK;
  465. }
  466. /**
  467. * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
  468. *
  469. * @param cls NULL
  470. * @param client identification of the client
  471. * @param message the actual message
  472. */
  473. void
  474. GST_handle_peer_start (void *cls, struct GNUNET_SERVER_Client *client,
  475. const struct GNUNET_MessageHeader *message)
  476. {
  477. const struct GNUNET_TESTBED_PeerStartMessage *msg;
  478. struct GNUNET_TESTBED_PeerEventMessage *reply;
  479. struct ForwardedOperationContext *fopc;
  480. struct Peer *peer;
  481. uint32_t peer_id;
  482. msg = (const struct GNUNET_TESTBED_PeerStartMessage *) message;
  483. peer_id = ntohl (msg->peer_id);
  484. if (!VALID_PEER_ID (peer_id))
  485. {
  486. GNUNET_break (0);
  487. LOG (GNUNET_ERROR_TYPE_ERROR,
  488. "Asked to start a non existent peer with id: %u\n", peer_id);
  489. GNUNET_SERVER_receive_done (client, GNUNET_OK);
  490. return;
  491. }
  492. peer = GST_peer_list[peer_id];
  493. if (GNUNET_YES == peer->is_remote)
  494. {
  495. fopc = GNUNET_new (struct ForwardedOperationContext);
  496. GNUNET_SERVER_client_keep (client);
  497. fopc->client = client;
  498. fopc->operation_id = GNUNET_ntohll (msg->operation_id);
  499. fopc->type = OP_PEER_START;
  500. fopc->opc =
  501. GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.
  502. slave->controller,
  503. fopc->operation_id, &msg->header,
  504. &GST_forwarded_operation_reply_relay,
  505. fopc);
  506. fopc->timeout_task =
  507. GNUNET_SCHEDULER_add_delayed (GST_timeout, &GST_forwarded_operation_timeout,
  508. fopc);
  509. GNUNET_CONTAINER_DLL_insert_tail (fopcq_head, fopcq_tail, fopc);
  510. GNUNET_SERVER_receive_done (client, GNUNET_OK);
  511. return;
  512. }
  513. if (GNUNET_OK != start_peer (peer))
  514. {
  515. GST_send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
  516. "Failed to start");
  517. GNUNET_SERVER_receive_done (client, GNUNET_OK);
  518. return;
  519. }
  520. reply = GNUNET_new (struct GNUNET_TESTBED_PeerEventMessage);
  521. reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEER_EVENT);
  522. reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
  523. reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_START);
  524. reply->host_id = htonl (GST_context->host_id);
  525. reply->peer_id = msg->peer_id;
  526. reply->operation_id = msg->operation_id;
  527. GST_queue_message (client, &reply->header);
  528. GNUNET_SERVER_receive_done (client, GNUNET_OK);
  529. }
  530. /**
  531. * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
  532. *
  533. * @param cls NULL
  534. * @param client identification of the client
  535. * @param message the actual message
  536. */
  537. void
  538. GST_handle_peer_stop (void *cls, struct GNUNET_SERVER_Client *client,
  539. const struct GNUNET_MessageHeader *message)
  540. {
  541. const struct GNUNET_TESTBED_PeerStopMessage *msg;
  542. struct GNUNET_TESTBED_PeerEventMessage *reply;
  543. struct ForwardedOperationContext *fopc;
  544. struct Peer *peer;
  545. uint32_t peer_id;
  546. msg = (const struct GNUNET_TESTBED_PeerStopMessage *) message;
  547. peer_id = ntohl (msg->peer_id);
  548. LOG (GNUNET_ERROR_TYPE_DEBUG, "Received PEER_STOP for peer %u\n", peer_id);
  549. if (!VALID_PEER_ID (peer_id))
  550. {
  551. GST_send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
  552. "Peer not found");
  553. GNUNET_SERVER_receive_done (client, GNUNET_OK);
  554. return;
  555. }
  556. peer = GST_peer_list[peer_id];
  557. if (GNUNET_YES == peer->is_remote)
  558. {
  559. LOG (GNUNET_ERROR_TYPE_DEBUG, "Forwarding PEER_STOP for peer %u\n",
  560. peer_id);
  561. fopc = GNUNET_new (struct ForwardedOperationContext);
  562. GNUNET_SERVER_client_keep (client);
  563. fopc->client = client;
  564. fopc->operation_id = GNUNET_ntohll (msg->operation_id);
  565. fopc->type = OP_PEER_STOP;
  566. fopc->opc =
  567. GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.
  568. slave->controller,
  569. fopc->operation_id, &msg->header,
  570. &GST_forwarded_operation_reply_relay,
  571. fopc);
  572. fopc->timeout_task =
  573. GNUNET_SCHEDULER_add_delayed (GST_timeout, &GST_forwarded_operation_timeout,
  574. fopc);
  575. GNUNET_CONTAINER_DLL_insert_tail (fopcq_head, fopcq_tail, fopc);
  576. GNUNET_SERVER_receive_done (client, GNUNET_OK);
  577. return;
  578. }
  579. if (GNUNET_OK != stop_peer (peer))
  580. {
  581. LOG (GNUNET_ERROR_TYPE_WARNING, "Stopping peer %u failed\n", peer_id);
  582. GST_send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
  583. "Peer not running");
  584. GNUNET_SERVER_receive_done (client, GNUNET_OK);
  585. return;
  586. }
  587. LOG (GNUNET_ERROR_TYPE_DEBUG, "Peer %u successfully stopped\n", peer_id);
  588. reply = GNUNET_new (struct GNUNET_TESTBED_PeerEventMessage);
  589. reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEER_EVENT);
  590. reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
  591. reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_STOP);
  592. reply->host_id = htonl (GST_context->host_id);
  593. reply->peer_id = msg->peer_id;
  594. reply->operation_id = msg->operation_id;
  595. GST_queue_message (client, &reply->header);
  596. GNUNET_SERVER_receive_done (client, GNUNET_OK);
  597. GNUNET_TESTING_peer_wait (peer->details.local.peer);
  598. }
  599. /**
  600. * Handler for GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG messages
  601. *
  602. * @param cls NULL
  603. * @param client identification of the client
  604. * @param message the actual message
  605. */
  606. void
  607. GST_handle_peer_get_config (void *cls, struct GNUNET_SERVER_Client *client,
  608. const struct GNUNET_MessageHeader *message)
  609. {
  610. const struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
  611. struct GNUNET_TESTBED_PeerConfigurationInformationMessage *reply;
  612. struct ForwardedOperationContext *fopc;
  613. struct Peer *peer;
  614. char *config;
  615. char *xconfig;
  616. size_t c_size;
  617. size_t xc_size;
  618. uint32_t peer_id;
  619. uint16_t msize;
  620. msg = (const struct GNUNET_TESTBED_PeerGetConfigurationMessage *) message;
  621. peer_id = ntohl (msg->peer_id);
  622. LOG_DEBUG ("Received GET_CONFIG for peer %u\n", peer_id);
  623. if (!VALID_PEER_ID (peer_id))
  624. {
  625. GST_send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
  626. "Peer not found");
  627. GNUNET_SERVER_receive_done (client, GNUNET_OK);
  628. return;
  629. }
  630. peer = GST_peer_list[peer_id];
  631. if (GNUNET_YES == peer->is_remote)
  632. {
  633. LOG_DEBUG ("Forwarding PEER_GET_CONFIG for peer: %u\n", peer_id);
  634. fopc = GNUNET_new (struct ForwardedOperationContext);
  635. GNUNET_SERVER_client_keep (client);
  636. fopc->client = client;
  637. fopc->operation_id = GNUNET_ntohll (msg->operation_id);
  638. fopc->type = OP_PEER_INFO;
  639. fopc->opc =
  640. GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.
  641. slave->controller,
  642. fopc->operation_id, &msg->header,
  643. &GST_forwarded_operation_reply_relay,
  644. fopc);
  645. fopc->timeout_task =
  646. GNUNET_SCHEDULER_add_delayed (GST_timeout, &GST_forwarded_operation_timeout,
  647. fopc);
  648. GNUNET_CONTAINER_DLL_insert_tail (fopcq_head, fopcq_tail, fopc);
  649. GNUNET_SERVER_receive_done (client, GNUNET_OK);
  650. return;
  651. }
  652. LOG_DEBUG ("Received PEER_GET_CONFIG for peer: %u\n", peer_id);
  653. config =
  654. GNUNET_CONFIGURATION_serialize (GST_peer_list[peer_id]->details.local.cfg,
  655. &c_size);
  656. xc_size = GNUNET_TESTBED_compress_config_ (config, c_size, &xconfig);
  657. GNUNET_free (config);
  658. msize =
  659. xc_size +
  660. sizeof (struct GNUNET_TESTBED_PeerConfigurationInformationMessage);
  661. reply = GNUNET_realloc (xconfig, msize);
  662. (void) memmove (&reply[1], reply, xc_size);
  663. reply->header.size = htons (msize);
  664. reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEER_INFORMATION);
  665. reply->peer_id = msg->peer_id;
  666. reply->operation_id = msg->operation_id;
  667. GNUNET_TESTING_peer_get_identity (GST_peer_list[peer_id]->details.local.peer,
  668. &reply->peer_identity);
  669. reply->config_size = htons ((uint16_t) c_size);
  670. GST_queue_message (client, &reply->header);
  671. GNUNET_SERVER_receive_done (client, GNUNET_OK);
  672. }
  673. /**
  674. * Cleans up the given PeerReconfigureContext
  675. *
  676. * @param prc the PeerReconfigureContext
  677. */
  678. static void
  679. cleanup_prc (struct PeerReconfigureContext *prc)
  680. {
  681. struct Peer *peer;
  682. if (VALID_PEER_ID (prc->peer_id))
  683. {
  684. peer = GST_peer_list [prc->peer_id];
  685. if (1 != prc->stopped)
  686. {
  687. GNUNET_TESTING_peer_stop_async_cancel (peer->details.local.peer);
  688. stop_peer (peer); /* Stop the peer synchronously */
  689. }
  690. }
  691. if (NULL != prc->cfg)
  692. GNUNET_CONFIGURATION_destroy (prc->cfg);
  693. GNUNET_SERVER_client_drop (prc->client);
  694. GNUNET_CONTAINER_DLL_remove (prc_head, prc_tail, prc);
  695. GNUNET_free (prc);
  696. }
  697. /**
  698. * Cleans up the Peer reconfigure context list
  699. */
  700. void
  701. GST_free_prcq ()
  702. {
  703. while (NULL != prc_head)
  704. cleanup_prc (prc_head);
  705. }
  706. /**
  707. * Update peer configuration
  708. *
  709. * @param peer the peer to update
  710. * @param cfg the new configuration
  711. * @return error message (freshly allocated); NULL upon success
  712. */
  713. static char *
  714. update_peer_config (struct Peer *peer,
  715. struct GNUNET_CONFIGURATION_Handle *cfg)
  716. {
  717. char *emsg;
  718. GNUNET_TESTING_peer_destroy (peer->details.local.peer);
  719. GNUNET_CONFIGURATION_destroy (peer->details.local.cfg);
  720. peer->details.local.cfg = cfg;
  721. emsg = NULL;
  722. peer->details.local.peer
  723. = GNUNET_TESTING_peer_configure (GST_context->system,
  724. peer->details.local.cfg, peer->id,
  725. NULL /* Peer id */ ,
  726. &emsg);
  727. return emsg;
  728. }
  729. /**
  730. * Callback to inform whether the peer is running or stopped.
  731. *
  732. * @param cls the closure given to GNUNET_TESTING_peer_stop_async()
  733. * @param p the respective peer whose status is being reported
  734. * @param success GNUNET_YES if the peer is stopped; GNUNET_SYSERR upon any
  735. * error
  736. */
  737. static void
  738. prc_stop_cb (void *cls, struct GNUNET_TESTING_Peer *p, int success)
  739. {
  740. struct PeerReconfigureContext *prc = cls;
  741. struct Peer *peer;
  742. char *emsg;
  743. GNUNET_assert (VALID_PEER_ID (prc->peer_id));
  744. peer = GST_peer_list [prc->peer_id];
  745. GNUNET_assert (GNUNET_NO == peer->is_remote);
  746. emsg = update_peer_config (peer, prc->cfg);
  747. prc->cfg = NULL;
  748. prc->stopped = 1;
  749. if (NULL != emsg)
  750. {
  751. GST_send_operation_fail_msg (prc->client, prc->op_id, emsg);
  752. goto cleanup;
  753. }
  754. if (GNUNET_OK != start_peer (peer))
  755. {
  756. GST_send_operation_fail_msg (prc->client, prc->op_id,
  757. "Failed to start reconfigured peer");
  758. goto cleanup;
  759. }
  760. GST_send_operation_success_msg (prc->client, prc->op_id);
  761. cleanup:
  762. cleanup_prc (prc);
  763. return;
  764. }
  765. /**
  766. * Handler for GNUNET_MESSAGE_TYPDE_TESTBED_RECONFIGURE_PEER type messages.
  767. * Should stop the peer asyncronously, destroy it and create it again with the
  768. * new configuration.
  769. *
  770. * @param cls NULL
  771. * @param client identification of the client
  772. * @param message the actual message
  773. */
  774. void
  775. GST_handle_peer_reconfigure (void *cls, struct GNUNET_SERVER_Client *client,
  776. const struct GNUNET_MessageHeader *message)
  777. {
  778. const struct GNUNET_TESTBED_PeerReconfigureMessage *msg;
  779. struct Peer *peer;
  780. struct GNUNET_CONFIGURATION_Handle *cfg;
  781. struct ForwardedOperationContext *fopc;
  782. struct PeerReconfigureContext *prc;
  783. char *emsg;
  784. uint64_t op_id;
  785. uint32_t peer_id;
  786. uint16_t msize;
  787. msize = ntohs (message->size);
  788. if (msize <= sizeof (struct GNUNET_TESTBED_PeerReconfigureMessage))
  789. {
  790. GNUNET_break_op (0);
  791. GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
  792. return;
  793. }
  794. msg = (const struct GNUNET_TESTBED_PeerReconfigureMessage *) message;
  795. peer_id = ntohl (msg->peer_id);
  796. op_id = GNUNET_ntohll (msg->operation_id);
  797. if (!VALID_PEER_ID (peer_id))
  798. {
  799. GNUNET_break (0);
  800. GST_send_operation_fail_msg (client, op_id, "Peer not found");
  801. GNUNET_SERVER_receive_done (client, GNUNET_OK);
  802. return;
  803. }
  804. peer = GST_peer_list[peer_id];
  805. if (GNUNET_YES == peer->is_remote)
  806. {
  807. LOG_DEBUG ("Forwarding PEER_RECONFIGURE for peer: %u\n", peer_id);
  808. fopc = GNUNET_new (struct ForwardedOperationContext);
  809. GNUNET_SERVER_client_keep (client);
  810. fopc->client = client;
  811. fopc->operation_id = op_id;
  812. fopc->type = OP_PEER_RECONFIGURE;
  813. fopc->opc =
  814. GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.
  815. slave->controller,
  816. fopc->operation_id, &msg->header,
  817. &GST_forwarded_operation_reply_relay,
  818. fopc);
  819. fopc->timeout_task =
  820. GNUNET_SCHEDULER_add_delayed (GST_timeout, &GST_forwarded_operation_timeout,
  821. fopc);
  822. GNUNET_CONTAINER_DLL_insert_tail (fopcq_head, fopcq_tail, fopc);
  823. GNUNET_SERVER_receive_done (client, GNUNET_OK);
  824. return;
  825. }
  826. LOG_DEBUG ("Received PEER_RECONFIGURE for peer %u\n", peer_id);
  827. if (0 < peer->reference_cnt)
  828. {
  829. GNUNET_break (0);
  830. GST_send_operation_fail_msg (client, op_id, "Peer in use");
  831. GNUNET_SERVER_receive_done (client, GNUNET_OK);
  832. return;
  833. }
  834. if (GNUNET_YES == peer->destroy_flag)
  835. {
  836. GNUNET_break (0);
  837. GST_send_operation_fail_msg (client, op_id, "Peer is being destroyed");
  838. GNUNET_SERVER_receive_done (client, GNUNET_OK);
  839. return;
  840. }
  841. cfg = GNUNET_TESTBED_extract_config_ (message);
  842. if (NULL == cfg)
  843. {
  844. GNUNET_break (0);
  845. GST_send_operation_fail_msg (client, op_id, "Compression error");
  846. GNUNET_SERVER_receive_done (client, GNUNET_OK);
  847. return;
  848. }
  849. if (GNUNET_NO == peer->details.local.is_running)
  850. {
  851. emsg = update_peer_config (peer, cfg);
  852. if (NULL != emsg)
  853. GST_send_operation_fail_msg (client, op_id, emsg);
  854. GST_send_operation_success_msg (client, op_id);
  855. GNUNET_SERVER_receive_done (client, GNUNET_OK);
  856. GNUNET_free_non_null (emsg);
  857. return;
  858. }
  859. prc = GNUNET_new (struct PeerReconfigureContext);
  860. if (GNUNET_OK !=
  861. GNUNET_TESTING_peer_stop_async (peer->details.local.peer, &prc_stop_cb,
  862. prc))
  863. {
  864. GNUNET_assert (0 < GNUNET_asprintf (&emsg,
  865. "Error trying to stop peer %u asynchronously\n",
  866. peer_id));
  867. LOG (GNUNET_ERROR_TYPE_ERROR, "%s\n", emsg);
  868. GST_send_operation_fail_msg (client, op_id, emsg);
  869. GNUNET_SERVER_receive_done (client, GNUNET_OK);
  870. GNUNET_free (prc);
  871. GNUNET_free (emsg);
  872. return;
  873. }
  874. prc->cfg = cfg;
  875. prc->peer_id = peer_id;
  876. prc->op_id = op_id;
  877. prc->client = client;
  878. GNUNET_SERVER_client_keep (client);
  879. GNUNET_CONTAINER_DLL_insert_tail (prc_head, prc_tail, prc);
  880. GNUNET_SERVER_receive_done (client, GNUNET_OK);
  881. }
  882. /**
  883. * Cleanup the context information created for managing a peer's service
  884. *
  885. * @param mctx the ManageServiceContext
  886. */
  887. static void
  888. cleanup_mctx (struct ManageServiceContext *mctx)
  889. {
  890. mctx->expired = GNUNET_YES;
  891. GNUNET_CONTAINER_DLL_remove (mctx_head, mctx_tail, mctx);
  892. GNUNET_SERVER_client_drop (mctx->client);
  893. GNUNET_ARM_disconnect_and_free (mctx->ah);
  894. GNUNET_assert (0 < mctx->peer->reference_cnt);
  895. mctx->peer->reference_cnt--;
  896. if ( (GNUNET_YES == mctx->peer->destroy_flag)
  897. && (0 == mctx->peer->reference_cnt) )
  898. GST_destroy_peer (mctx->peer);
  899. GNUNET_free (mctx);
  900. }
  901. /**
  902. * Frees the ManageServiceContext queue
  903. */
  904. void
  905. GST_free_mctxq ()
  906. {
  907. while (NULL != mctx_head)
  908. cleanup_mctx (mctx_head);
  909. }
  910. /**
  911. * Returns a string interpretation of 'rs'
  912. *
  913. * @param rs the request status from ARM
  914. * @return a string interpretation of the request status
  915. */
  916. static const char *
  917. arm_req_string (enum GNUNET_ARM_RequestStatus rs)
  918. {
  919. switch (rs)
  920. {
  921. case GNUNET_ARM_REQUEST_SENT_OK:
  922. return _("Message was sent successfully");
  923. case GNUNET_ARM_REQUEST_CONFIGURATION_ERROR:
  924. return _("Misconfiguration (can't connect to the ARM service)");
  925. case GNUNET_ARM_REQUEST_DISCONNECTED:
  926. return _("We disconnected from ARM before we could send a request");
  927. case GNUNET_ARM_REQUEST_BUSY:
  928. return _("ARM API is busy");
  929. case GNUNET_ARM_REQUEST_TOO_LONG:
  930. return _("Request doesn't fit into a message");
  931. case GNUNET_ARM_REQUEST_TIMEOUT:
  932. return _("Request timed out");
  933. }
  934. return _("Unknown request status");
  935. }
  936. /**
  937. * Returns a string interpretation of the 'result'
  938. *
  939. * @param result the arm result
  940. * @return a string interpretation
  941. */
  942. static const char *
  943. arm_ret_string (enum GNUNET_ARM_Result result)
  944. {
  945. switch (result)
  946. {
  947. case GNUNET_ARM_RESULT_STOPPED:
  948. return _("%s is stopped");
  949. case GNUNET_ARM_RESULT_STARTING:
  950. return _("%s is starting");
  951. case GNUNET_ARM_RESULT_STOPPING:
  952. return _("%s is stopping");
  953. case GNUNET_ARM_RESULT_IS_STARTING_ALREADY:
  954. return _("%s is starting already");
  955. case GNUNET_ARM_RESULT_IS_STOPPING_ALREADY:
  956. return _("%s is stopping already");
  957. case GNUNET_ARM_RESULT_IS_STARTED_ALREADY:
  958. return _("%s is started already");
  959. case GNUNET_ARM_RESULT_IS_STOPPED_ALREADY:
  960. return _("%s is stopped already");
  961. case GNUNET_ARM_RESULT_IS_NOT_KNOWN:
  962. return _("%s service is not known to ARM");
  963. case GNUNET_ARM_RESULT_START_FAILED:
  964. return _("%s service failed to start");
  965. case GNUNET_ARM_RESULT_IN_SHUTDOWN:
  966. return _("%s service can't be started because ARM is shutting down");
  967. }
  968. return _("%.s Unknown result code.");
  969. }
  970. /**
  971. * Function called in response to a start/stop request.
  972. * Will be called when request was not sent successfully,
  973. * or when a reply comes. If the request was not sent successfully,
  974. * 'rs' will indicate that, and 'service' and 'result' will be undefined.
  975. *
  976. * @param cls ManageServiceContext
  977. * @param rs status of the request
  978. * @param service service name
  979. * @param result result of the operation
  980. */
  981. static void
  982. service_manage_result_cb (void *cls,
  983. enum GNUNET_ARM_RequestStatus rs,
  984. const char *service, enum GNUNET_ARM_Result result)
  985. {
  986. struct ManageServiceContext *mctx = cls;
  987. char *emsg;
  988. emsg = NULL;
  989. if (GNUNET_YES == mctx->expired)
  990. return;
  991. if (GNUNET_ARM_REQUEST_SENT_OK != rs)
  992. {
  993. GNUNET_asprintf (&emsg, "Error communicating with Peer %u's ARM: %s",
  994. mctx->peer->id, arm_req_string (rs));
  995. goto ret;
  996. }
  997. if (1 == mctx->start)
  998. goto service_start_check;
  999. if (! ((GNUNET_ARM_RESULT_STOPPED == result)
  1000. || (GNUNET_ARM_RESULT_STOPPING == result)
  1001. || (GNUNET_ARM_RESULT_IS_STOPPING_ALREADY == result)
  1002. || (GNUNET_ARM_RESULT_IS_STOPPED_ALREADY == result)) )
  1003. {
  1004. /* stopping a service failed */
  1005. GNUNET_asprintf (&emsg, arm_ret_string (result), service);
  1006. goto ret;
  1007. }
  1008. /* service stopped successfully */
  1009. goto ret;
  1010. service_start_check:
  1011. if (! ((GNUNET_ARM_RESULT_STARTING == result)
  1012. || (GNUNET_ARM_RESULT_IS_STARTING_ALREADY == result)
  1013. || (GNUNET_ARM_RESULT_IS_STARTED_ALREADY == result)) )
  1014. {
  1015. /* starting a service failed */
  1016. GNUNET_asprintf (&emsg, arm_ret_string (result), service);
  1017. goto ret;
  1018. }
  1019. /* service started successfully */
  1020. ret:
  1021. if (NULL != emsg)
  1022. {
  1023. LOG_DEBUG ("%s\n", emsg);
  1024. GST_send_operation_fail_msg (mctx->client, mctx->op_id, emsg);
  1025. }
  1026. else
  1027. GST_send_operation_success_msg (mctx->client, mctx->op_id);
  1028. GNUNET_free_non_null (emsg);
  1029. cleanup_mctx (mctx);
  1030. }
  1031. /**
  1032. * Handler for GNUNET_TESTBED_ManagePeerServiceMessage message
  1033. *
  1034. * @param cls NULL
  1035. * @param client identification of client
  1036. * @param message the actual message
  1037. */
  1038. void
  1039. GST_handle_manage_peer_service (void *cls, struct GNUNET_SERVER_Client *client,
  1040. const struct GNUNET_MessageHeader *message)
  1041. {
  1042. const struct GNUNET_TESTBED_ManagePeerServiceMessage *msg;
  1043. const char* service;
  1044. struct Peer *peer;
  1045. char *emsg;
  1046. struct GNUNET_ARM_Handle *ah;
  1047. struct ManageServiceContext *mctx;
  1048. struct ForwardedOperationContext *fopc;
  1049. uint64_t op_id;
  1050. uint32_t peer_id;
  1051. uint16_t msize;
  1052. msize = ntohs (message->size);
  1053. if (msize <= sizeof (struct GNUNET_TESTBED_ManagePeerServiceMessage))
  1054. {
  1055. GNUNET_break_op (0);
  1056. GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
  1057. return;
  1058. }
  1059. msg = (const struct GNUNET_TESTBED_ManagePeerServiceMessage *) message;
  1060. service = (const char *) &msg[1];
  1061. if ('\0' != service[msize - sizeof
  1062. (struct GNUNET_TESTBED_ManagePeerServiceMessage) - 1])
  1063. {
  1064. GNUNET_break_op (0);
  1065. GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
  1066. return;
  1067. }
  1068. if (1 < msg->start)
  1069. {
  1070. GNUNET_break_op (0);
  1071. GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
  1072. return;
  1073. }
  1074. peer_id = ntohl (msg->peer_id);
  1075. op_id = GNUNET_ntohll (msg->operation_id);
  1076. LOG_DEBUG ("Received request to manage service %s on peer %u\n",
  1077. service, (unsigned int) peer_id);
  1078. if ((GST_peer_list_size <= peer_id)
  1079. || (NULL == (peer = GST_peer_list[peer_id])))
  1080. {
  1081. GNUNET_asprintf (&emsg, "Asked to manage service of a non existent peer "
  1082. "with id: %u", peer_id);
  1083. goto err_ret;
  1084. }
  1085. if (0 == strcasecmp ("arm", service))
  1086. {
  1087. emsg = GNUNET_strdup ("Cannot start/stop peer's ARM service. "
  1088. "Use peer start/stop for that");
  1089. goto err_ret;
  1090. }
  1091. if (GNUNET_YES == peer->is_remote)
  1092. {
  1093. /* Forward the destory message to sub controller */
  1094. fopc = GNUNET_new (struct ForwardedOperationContext);
  1095. GNUNET_SERVER_client_keep (client);
  1096. fopc->client = client;
  1097. fopc->cls = peer;
  1098. fopc->type = OP_MANAGE_SERVICE;
  1099. fopc->operation_id = op_id;
  1100. fopc->opc =
  1101. GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.
  1102. slave->controller,
  1103. fopc->operation_id, &msg->header,
  1104. &GST_forwarded_operation_reply_relay,
  1105. fopc);
  1106. fopc->timeout_task =
  1107. GNUNET_SCHEDULER_add_delayed (GST_timeout, &GST_forwarded_operation_timeout,
  1108. fopc);
  1109. GNUNET_CONTAINER_DLL_insert_tail (fopcq_head, fopcq_tail, fopc);
  1110. GNUNET_SERVER_receive_done (client, GNUNET_OK);
  1111. return;
  1112. }
  1113. if (GNUNET_NO == peer->details.local.is_running)
  1114. {
  1115. emsg = GNUNET_strdup ("Peer not running\n");
  1116. goto err_ret;
  1117. }
  1118. if ((0 != peer->reference_cnt)
  1119. && ( (0 == strcasecmp ("core", service))
  1120. || (0 == strcasecmp ("transport", service)) ) )
  1121. {
  1122. GNUNET_asprintf (&emsg, "Cannot stop %s service of peer with id: %u "
  1123. "since it is required by existing operations",
  1124. service, peer_id);
  1125. goto err_ret;
  1126. }
  1127. ah = GNUNET_ARM_connect (peer->details.local.cfg, NULL, NULL);
  1128. if (NULL == ah)
  1129. {
  1130. GNUNET_asprintf (&emsg,
  1131. "Cannot connect to ARM service of peer with id: %u",
  1132. peer_id);
  1133. goto err_ret;
  1134. }
  1135. mctx = GNUNET_new (struct ManageServiceContext);
  1136. mctx->peer = peer;
  1137. peer->reference_cnt++;
  1138. mctx->op_id = op_id;
  1139. mctx->ah = ah;
  1140. GNUNET_SERVER_client_keep (client);
  1141. mctx->client = client;
  1142. mctx->start = msg->start;
  1143. GNUNET_CONTAINER_DLL_insert_tail (mctx_head, mctx_tail, mctx);
  1144. if (1 == mctx->start)
  1145. GNUNET_ARM_request_service_start (mctx->ah, service,
  1146. GNUNET_OS_INHERIT_STD_ERR,
  1147. GST_timeout,
  1148. service_manage_result_cb,
  1149. mctx);
  1150. else
  1151. GNUNET_ARM_request_service_stop (mctx->ah, service,
  1152. GST_timeout,
  1153. service_manage_result_cb,
  1154. mctx);
  1155. GNUNET_SERVER_receive_done (client, GNUNET_OK);
  1156. return;
  1157. err_ret:
  1158. LOG (GNUNET_ERROR_TYPE_ERROR, "%s\n", emsg);
  1159. GST_send_operation_fail_msg (client, op_id, emsg);
  1160. GNUNET_free (emsg);
  1161. GNUNET_SERVER_receive_done (client, GNUNET_OK);
  1162. }
  1163. /**
  1164. * Stops and destroys all peers
  1165. */
  1166. void
  1167. GST_destroy_peers ()
  1168. {
  1169. struct Peer *peer;
  1170. unsigned int id;
  1171. if (NULL == GST_peer_list)
  1172. return;
  1173. for (id = 0; id < GST_peer_list_size; id++)
  1174. {
  1175. peer = GST_peer_list[id];
  1176. if (NULL == peer)
  1177. continue;
  1178. /* If destroy flag is set it means that this peer should have been
  1179. * destroyed by a context which we destroy before */
  1180. GNUNET_break (GNUNET_NO == peer->destroy_flag);
  1181. /* counter should be zero as we free all contexts before */
  1182. GNUNET_break (0 == peer->reference_cnt);
  1183. if ((GNUNET_NO == peer->is_remote) &&
  1184. (GNUNET_YES == peer->details.local.is_running))
  1185. GNUNET_TESTING_peer_kill (peer->details.local.peer);
  1186. }
  1187. for (id = 0; id < GST_peer_list_size; id++)
  1188. {
  1189. peer = GST_peer_list[id];
  1190. if (NULL == peer)
  1191. continue;
  1192. if (GNUNET_NO == peer->is_remote)
  1193. {
  1194. if (GNUNET_YES == peer->details.local.is_running)
  1195. GNUNET_TESTING_peer_wait (peer->details.local.peer);
  1196. GNUNET_TESTING_peer_destroy (peer->details.local.peer);
  1197. GNUNET_CONFIGURATION_destroy (peer->details.local.cfg);
  1198. }
  1199. GNUNET_free (peer);
  1200. }
  1201. GNUNET_free_non_null (GST_peer_list);
  1202. GST_peer_list = NULL;
  1203. GST_peer_list_size = 0;
  1204. }
  1205. /**
  1206. * The reply msg handler forwarded SHUTDOWN_PEERS operation. Checks if a
  1207. * success reply is received from all clients and then sends the success message
  1208. * to the client
  1209. *
  1210. * @param cls ForwardedOperationContext
  1211. * @param msg the message to relay
  1212. */
  1213. static void
  1214. shutdown_peers_reply_cb (void *cls,
  1215. const struct GNUNET_MessageHeader *msg)
  1216. {
  1217. struct ForwardedOperationContext *fo_ctxt = cls;
  1218. struct HandlerContext_ShutdownPeers *hc;
  1219. hc = fo_ctxt->cls;
  1220. GNUNET_assert (0 < hc->nslaves);
  1221. hc->nslaves--;
  1222. if (GNUNET_MESSAGE_TYPE_TESTBED_GENERIC_OPERATION_SUCCESS !=
  1223. ntohs (msg->type))
  1224. hc->timeout = GNUNET_YES;
  1225. if (0 == hc->nslaves)
  1226. {
  1227. if (GNUNET_YES == hc->timeout)
  1228. GST_send_operation_fail_msg (fo_ctxt->client, fo_ctxt->operation_id,
  1229. "Timeout at a slave controller");
  1230. else
  1231. GST_send_operation_success_msg (fo_ctxt->client, fo_ctxt->operation_id);
  1232. GNUNET_free (hc);
  1233. hc = NULL;
  1234. }
  1235. GNUNET_SERVER_client_drop (fo_ctxt->client);
  1236. GNUNET_CONTAINER_DLL_remove (fopcq_head, fopcq_tail, fo_ctxt);
  1237. GNUNET_free (fo_ctxt);
  1238. }
  1239. /**
  1240. * Handler for GNUNET_MESSAGE_TYPE_TESTBED_SHUTDOWN_PEERS messages
  1241. *
  1242. * @param cls NULL
  1243. * @param client identification of the client
  1244. * @param message the actual message
  1245. */
  1246. void
  1247. GST_handle_shutdown_peers (void *cls, struct GNUNET_SERVER_Client *client,
  1248. const struct GNUNET_MessageHeader *message)
  1249. {
  1250. const struct GNUNET_TESTBED_ShutdownPeersMessage *msg;
  1251. struct HandlerContext_ShutdownPeers *hc;
  1252. struct Slave *slave;
  1253. struct ForwardedOperationContext *fo_ctxt;
  1254. uint64_t op_id;
  1255. unsigned int cnt;
  1256. msg = (const struct GNUNET_TESTBED_ShutdownPeersMessage *) message;
  1257. LOG_DEBUG ("Received SHUTDOWN_PEERS\n");
  1258. /* Stop and destroy all peers */
  1259. GST_free_mctxq ();
  1260. GST_free_occq ();
  1261. GST_free_roccq ();
  1262. GST_clear_fopcq ();
  1263. /* Forward to all slaves which we have started */
  1264. op_id = GNUNET_ntohll (msg->operation_id);
  1265. hc = GNUNET_new (struct HandlerContext_ShutdownPeers);
  1266. /* FIXME: have a better implementation where we track which slaves are
  1267. started by this controller */
  1268. for (cnt = 0; cnt < GST_slave_list_size; cnt++)
  1269. {
  1270. slave = GST_slave_list[cnt];
  1271. if (NULL == slave)
  1272. continue;
  1273. if (NULL == slave->controller_proc) /* We didn't start the slave */
  1274. continue;
  1275. LOG_DEBUG ("Forwarding SHUTDOWN_PEERS\n");
  1276. hc->nslaves++;
  1277. fo_ctxt = GNUNET_new (struct ForwardedOperationContext);
  1278. GNUNET_SERVER_client_keep (client);
  1279. fo_ctxt->client = client;
  1280. fo_ctxt->operation_id = op_id;
  1281. fo_ctxt->cls = hc;
  1282. fo_ctxt->type = OP_SHUTDOWN_PEERS;
  1283. fo_ctxt->opc =
  1284. GNUNET_TESTBED_forward_operation_msg_ (slave->controller,
  1285. fo_ctxt->operation_id,
  1286. &msg->header,
  1287. shutdown_peers_reply_cb,
  1288. fo_ctxt);
  1289. GNUNET_CONTAINER_DLL_insert_tail (fopcq_head, fopcq_tail, fo_ctxt);
  1290. }
  1291. LOG_DEBUG ("Shutting down peers\n");
  1292. GST_destroy_peers ();
  1293. if (0 == hc->nslaves)
  1294. {
  1295. GST_send_operation_success_msg (client, op_id);
  1296. GNUNET_free (hc);
  1297. }
  1298. GNUNET_SERVER_receive_done (client, GNUNET_OK);
  1299. }