myco-daemon.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. /* Copyright (C) 2016 Max Riechelmann <max.riechelmann@student.kit.edu>
  2. (Karlsruhe Institute of Technology)
  3. This library is free software; you can redistribute it and/or modify it
  4. under the terms of the GNU Lesser General Public License as published by the
  5. Free Software Foundation; either version 2.1 of the License, or (at your
  6. option) any later version.
  7. This library is distributed in the hope that it will be useful, but WITHOUT
  8. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  10. details.
  11. You should have received a copy of the GNU Lesser General Public License along
  12. with this library; if not, write to the Free Software Foundation, Inc., 51
  13. Franklin St, Fifth Floor, Boston, MA 02110, USA
  14. */
  15. #include "myco-daemon.h"
  16. #include "../src/myco-memory.c"
  17. #include "../src/myco-modules.h"
  18. #define DEBUG 0
  19. myco_agent *first_agent = NULL;
  20. myco_resource *first_resource = NULL;
  21. in_addr_t myco_daemon_node_ip;
  22. myco_agent *myco_daemon_find_agent(const char *agent_name) {
  23. myco_agent *current_agent;
  24. current_agent = first_agent;
  25. while (current_agent != NULL) {
  26. if (strcmp(current_agent->name, agent_name) == 0) {
  27. return current_agent;
  28. }
  29. current_agent = current_agent->next;
  30. }
  31. return NULL;
  32. }
  33. int myco_daemon_register_agent(message msg) {
  34. myco_agent *current_agent;
  35. if (myco_daemon_find_agent(msg.agent_name) != NULL) {
  36. sprintf(msg.message, "ERROR: agent %s already exists\n", msg.agent_name);
  37. myco_send(msg.agent_message_queue_id, msg);
  38. return -1;
  39. }
  40. if (first_agent == NULL) {
  41. first_agent = malloc(sizeof(myco_agent));
  42. first_agent->next = NULL;
  43. first_agent->prev = NULL;
  44. sprintf(first_agent->name, "%s", msg.agent_name);
  45. first_agent->message_queue_id = msg.agent_message_queue_id;
  46. } else {
  47. current_agent = first_agent;
  48. while (current_agent->next != NULL) {
  49. current_agent = current_agent->next;
  50. }
  51. current_agent->next = malloc(sizeof(myco_agent));
  52. current_agent->next->prev = current_agent;
  53. current_agent->next->next = NULL;
  54. sprintf(current_agent->next->name, "%s", msg.agent_name);
  55. current_agent->next->message_queue_id = msg.agent_message_queue_id;
  56. }
  57. sprintf(msg.message, "SUCCESS: agent %s registered\n", msg.agent_name);
  58. myco_send(msg.agent_message_queue_id, msg);
  59. return 0;
  60. }
  61. int myco_daemon_unregister_agent(message msg) {
  62. myco_agent *current_agent;
  63. myco_resource *current_resource;
  64. current_agent = myco_daemon_find_agent(msg.agent_name);
  65. if ((current_resource = myco_daemon_find_resource_by_agent(msg.agent_name)) != NULL) {
  66. sprintf(msg.message, "ERROR: agent %s still has resource %s\n", msg.agent_name, current_resource->name);
  67. myco_send(msg.agent_message_queue_id, msg);
  68. return -1;
  69. }
  70. if (current_agent == NULL) {
  71. sprintf(msg.message, "ERROR: agent %s could not be unregistered\n", msg.agent_name);
  72. myco_send(msg.agent_message_queue_id, msg);
  73. return -1;
  74. }
  75. // Element in the middle
  76. if (current_agent->next != NULL && current_agent->prev != NULL) {
  77. current_agent->prev->next = current_agent->next;
  78. current_agent->next->prev = current_agent->prev;
  79. free(current_agent);
  80. sprintf(msg.message, "SUCCESS: agent %s unregistered\n", msg.agent_name);
  81. myco_send(msg.agent_message_queue_id, msg);
  82. return 0;
  83. }
  84. // First element
  85. if (current_agent->next != NULL && current_agent->prev == NULL) {
  86. first_agent = current_agent->next;
  87. current_agent->next->prev = NULL;
  88. free(current_agent);
  89. sprintf(msg.message, "SUCCESS: agent %s unregistered\n", msg.agent_name);
  90. myco_send(msg.agent_message_queue_id, msg);
  91. return 0;
  92. }
  93. // Last element
  94. if (current_agent->next == NULL && current_agent->prev != NULL) {
  95. current_agent->prev->next = NULL;
  96. free(current_agent);
  97. sprintf(msg.message, "SUCCESS: agent %s unregistered\n", msg.agent_name);
  98. myco_send(msg.agent_message_queue_id, msg);
  99. return 0;
  100. }
  101. // Only remaining
  102. if (current_agent->next == NULL && current_agent->prev == NULL) {
  103. first_agent = NULL;
  104. free(current_agent);
  105. sprintf(msg.message, "SUCCESS: agent %s unregistered\n", msg.agent_name);
  106. myco_send(msg.agent_message_queue_id, msg);
  107. return 0;
  108. }
  109. sprintf(msg.message, "ERROR: agent %s could not be unregistered - data structure seems to be damaged!\n", msg.agent_name);
  110. myco_send(msg.agent_message_queue_id, msg);
  111. return -1;
  112. }
  113. myco_resource *myco_daemon_find_resource_by_agent(const char *agent_name) {
  114. myco_resource *current_resource;
  115. current_resource = first_resource;
  116. while (current_resource != NULL) {
  117. if (strcmp(current_resource->agent, agent_name) == 0) {
  118. return current_resource;
  119. }
  120. current_resource = current_resource->next;
  121. }
  122. return NULL;
  123. }
  124. myco_resource *myco_daemon_find_resource(const char *resource_name) {
  125. myco_resource *current_resource;
  126. current_resource = first_resource;
  127. while (current_resource != NULL) {
  128. if (strcmp(current_resource->name, resource_name) == 0) {
  129. return current_resource;
  130. }
  131. current_resource = current_resource->next;
  132. }
  133. return NULL;
  134. }
  135. int myco_daemon_register_resource(message msg) {
  136. myco_resource *current_resource;
  137. if (myco_daemon_find_agent(msg.agent_name) == NULL) {
  138. sprintf(msg.message, "ERROR: agent %s does not exist\n", msg.agent_name);
  139. myco_send(msg.agent_message_queue_id, msg);
  140. return -1;
  141. }
  142. if (myco_daemon_find_resource(msg.resource_name) != NULL) {
  143. sprintf(msg.message, "ERROR: resource %s already exists\n", msg.resource_name);
  144. myco_send(msg.agent_message_queue_id, msg);
  145. return -1;
  146. }
  147. // Insert as first element
  148. if (first_resource == NULL) {
  149. first_resource = malloc(sizeof(myco_resource));
  150. first_resource->next = NULL;
  151. first_resource->prev = NULL;
  152. sprintf(first_resource->name, "%s", msg.resource_name);
  153. sprintf(first_resource->agent, "%s", msg.agent_name);
  154. first_resource->pid = msg.sender_pid;
  155. first_resource->pointer = msg.resource_pointer;
  156. first_resource->size = msg.resource_size;
  157. first_resource->read_locked = 0;
  158. first_resource->transfer_locked = 1;
  159. first_resource->transactional = msg.resource_transactional;
  160. first_resource->version = 0;
  161. first_resource->node_ip = myco_daemon_node_ip;
  162. } else {
  163. // Insert as last element
  164. current_resource = first_resource;
  165. while (current_resource->next != NULL) {
  166. current_resource = current_resource->next;
  167. }
  168. current_resource->next = malloc(sizeof(myco_resource));
  169. current_resource->next->prev = current_resource;
  170. current_resource->next->next = NULL;
  171. sprintf(current_resource->next->name, "%s", msg.resource_name);
  172. sprintf(current_resource->next->agent, "%s", msg.agent_name);
  173. current_resource->next->pid = msg.sender_pid;
  174. current_resource->next->pointer = msg.resource_pointer;
  175. current_resource->next->size = msg.resource_size;
  176. current_resource->next->transfer_locked = 1;
  177. current_resource->next->read_locked = 0;
  178. current_resource->next->transactional = msg.resource_transactional;
  179. current_resource->next->version = 0;
  180. current_resource->next->node_ip = myco_daemon_node_ip;
  181. }
  182. sprintf(msg.message, "SUCCESS: resource %s registered\n", msg.resource_name);
  183. myco_send(msg.agent_message_queue_id, msg);
  184. return 0;
  185. }
  186. int myco_daemon_unregister_resource(message msg) {
  187. myco_resource *current_resource;
  188. current_resource = myco_daemon_find_resource(msg.resource_name);
  189. if (current_resource == NULL) {
  190. sprintf(msg.message, "ERROR: resource %s could not be unregistered\n", msg.resource_name);
  191. myco_send(msg.agent_message_queue_id, msg);
  192. return -1;
  193. }
  194. // Element in the middle
  195. if (current_resource->next != NULL && current_resource->prev != NULL) {
  196. current_resource->prev->next = current_resource->next;
  197. current_resource->next->prev = current_resource->prev;
  198. free(current_resource);
  199. sprintf(msg.message, "SUCCESS: resource %s unregistered\n", msg.resource_name);
  200. myco_send(msg.agent_message_queue_id, msg);
  201. return 0;
  202. }
  203. // First element
  204. if (current_resource->next != NULL && current_resource->prev == NULL) {
  205. first_resource = current_resource->next;
  206. current_resource->next->prev = NULL;
  207. free(current_resource);
  208. sprintf(msg.message, "SUCCESS: resource %s unregistered\n", msg.resource_name);
  209. myco_send(msg.agent_message_queue_id, msg);
  210. return 0;
  211. }
  212. // Last element
  213. if (current_resource->next == NULL && current_resource->prev != NULL) {
  214. current_resource->prev->next = NULL;
  215. free(current_resource);
  216. sprintf(msg.message, "SUCCESS: resource %s unregistered\n", msg.resource_name);
  217. myco_send(msg.agent_message_queue_id, msg);
  218. return 0;
  219. }
  220. // Only remaining
  221. if (current_resource->next == NULL && current_resource->prev == NULL) {
  222. first_resource = NULL;
  223. free(current_resource);
  224. sprintf(msg.message, "SUCCESS: resource %s unregistered\n", msg.resource_name);
  225. myco_send(msg.agent_message_queue_id, msg);
  226. return 0;
  227. }
  228. sprintf(msg.message, "FATAL ERROR: resource %s could not be unregistered - data structure seems to be damaged!\n", msg.resource_name);
  229. myco_send(msg.agent_message_queue_id, msg);
  230. return -1;
  231. }
  232. int myco_daemon_request_resource(message msg) {
  233. myco_resource *current_resource;
  234. // TODO: Uncommented for performance tests...
  235. current_resource = myco_daemon_find_resource(msg.resource_name);
  236. // Check if resource exists on this node
  237. /*if ((current_resource = myco_daemon_find_resource(msg.resource_name)) != NULL) {
  238. // Check if agent that requests already owns the resource
  239. if (strcmp(current_resource->agent, msg.agent_name) == 0) {
  240. sprintf(msg.message, "ERROR: resource %s already belongs to agent %s\n", msg.resource_name, msg.agent_name);
  241. myco_send(msg.agent_message_queue_id, msg);
  242. return -1;
  243. }
  244. // Check if resource is transactional
  245. if (current_resource->transactional != RESOURCE_TRANSACTIONAL) {
  246. sprintf(msg.message, "ERROR: resource %s is not transactional\n", msg.resource_name);
  247. myco_send(msg.agent_message_queue_id, msg);
  248. return -1;
  249. }
  250. */
  251. // Check if resource resides on same node
  252. // printf("\n The adress is: %s\n", inet_ntoa(*(struct in_addr *)&current_resource->node_ip));
  253. // if (current_resource->node_ip == myco_daemon_node_ip) {
  254. // If on same node, send information
  255. msg.sender_pid = current_resource->pid;
  256. msg.resource_size = current_resource->size;
  257. msg.resource_pointer = current_resource->pointer;
  258. sprintf(msg.message, "SUCCESS: resource found on same node, sending information\n");
  259. myco_send(msg.agent_message_queue_id, msg);
  260. msg = myco_receive(msg.agent_message_queue_id);
  261. if (strcmp(msg.message, "RESOURCE GRANTED") == 0) {
  262. // Transfer ownership of resource to agent
  263. sprintf(current_resource->agent, "%s", msg.agent_name);
  264. current_resource->pid = msg.sender_pid;
  265. current_resource->pointer = msg.resource_pointer;
  266. } else {
  267. fprintf(stderr, "ERROR: myco_daemon_request: %s\n", strerror(errno));
  268. return -1;
  269. }
  270. /*} else {
  271. // Handle case where resource is on another node
  272. // TODO: Request Resource information from indexer
  273. // Fetch resource
  274. myco_network_fetch_resource(msg.resource_pointer, msg.sender_pid, msg.resource_size);
  275. // if no resource registered with indexer:
  276. if (0 == 1) {
  277. sprintf(msg.message, "ERROR: resource %s does not exist\n", msg.resource_name);
  278. myco_send(msg.agent_message_queue_id, msg);
  279. return -1;
  280. }
  281. }
  282. */
  283. return 0;
  284. }
  285. int myco_daemon_request_list(message msg, pid_t pid) {
  286. myco_resource *current_resource;
  287. current_resource = first_resource;
  288. char *resource_pointer;
  289. char tmp[64];
  290. int resource_size = 0;
  291. if (current_resource == NULL) {
  292. sprintf(msg.message, "ERROR: no resources\n");
  293. myco_send(msg.agent_message_queue_id, msg);
  294. return -1;
  295. }
  296. while (current_resource != NULL) {
  297. resource_size += strlen(current_resource->name) + strlen(current_resource->agent) + strlen("1");
  298. resource_size += strlen("(,,,);");
  299. resource_size += 22;
  300. current_resource = current_resource->next;
  301. }
  302. resource_pointer = malloc(resource_size);
  303. memset(resource_pointer, 0, resource_size);
  304. current_resource = first_resource;
  305. while (current_resource != NULL) {
  306. strcat(resource_pointer, current_resource->name);
  307. strcat(resource_pointer, "(");
  308. strcat(resource_pointer, current_resource->agent);
  309. strcat(resource_pointer, ",");
  310. sprintf(tmp, "%d", current_resource->size);
  311. strcat(resource_pointer, tmp);
  312. strcat(resource_pointer, ",");
  313. sprintf(tmp, "%p", current_resource->pointer);
  314. strcat(resource_pointer, tmp);
  315. strcat(resource_pointer, ",");
  316. sprintf(tmp, "%d", current_resource->read_locked);
  317. strcat(resource_pointer, tmp);
  318. strcat(resource_pointer, ",");
  319. sprintf(tmp, "%d", current_resource->transfer_locked);
  320. strcat(resource_pointer, tmp);
  321. strcat(resource_pointer, ",");
  322. if (current_resource->transactional) {
  323. strcat(resource_pointer, "1");
  324. } else {
  325. strcat(resource_pointer, "0");
  326. }
  327. strcat(resource_pointer, ");");
  328. current_resource = current_resource->next;
  329. }
  330. msg.sender_pid = pid;
  331. msg.resource_size = resource_size;
  332. msg.resource_pointer = (void *)resource_pointer;
  333. sprintf(msg.message, "SUCCESS: sending resource list\n");
  334. myco_send(msg.agent_message_queue_id, msg);
  335. msg = myco_receive(msg.agent_message_queue_id);
  336. if (strcmp(msg.message, "RESOURCE LIST GRANTED") == 0) {
  337. free(resource_pointer);
  338. } else {
  339. fprintf(stderr, "ERROR: resource list was not granted: %s\n", strerror(errno));
  340. free(resource_pointer);
  341. return -1;
  342. }
  343. return 0;
  344. }
  345. int myco_daemon_write_remote_resource(message msg, int force) {
  346. myco_resource *current_resource;
  347. // Check if resource exists
  348. if ((current_resource = myco_daemon_find_resource(msg.resource_name)) == NULL) {
  349. sprintf(msg.message, "ERROR: resource %s does not exist\n", msg.resource_name);
  350. myco_send(msg.agent_message_queue_id, msg);
  351. return -1;
  352. }
  353. if (force == 1) {
  354. sprintf(msg.message, "SUCCESS: forcing to overwrite resource %s, sending information\n", msg.resource_name);
  355. msg.resource_pointer = current_resource->pointer;
  356. msg.resource_size = current_resource->size;
  357. msg.sender_pid = current_resource->pid;
  358. myco_send(msg.agent_message_queue_id, msg);
  359. return 0;
  360. } else {
  361. if (current_resource->version > msg.version) {
  362. sprintf(msg.message, "ERROR: resource %s is newer than your copied version\n", msg.resource_name);
  363. myco_send(msg.agent_message_queue_id, msg);
  364. return -1;
  365. } else {
  366. sprintf(msg.message, "SUCCESS: version updated, sending information for resource %s\n", msg.resource_name);
  367. msg.resource_pointer = current_resource->pointer;
  368. msg.resource_size = current_resource->size;
  369. msg.sender_pid = current_resource->pid;
  370. myco_send(msg.agent_message_queue_id, msg);
  371. current_resource->version += 1;
  372. myco_send(msg.agent_message_queue_id, msg);
  373. return 0;
  374. }
  375. }
  376. }
  377. int myco_daemon_read_remote_resource(message msg) {
  378. myco_resource *current_resource;
  379. // Check if resource exists
  380. if ((current_resource = myco_daemon_find_resource(msg.resource_name)) == NULL) {
  381. sprintf(msg.message, "ERROR: resource %s does not exist\n", msg.resource_name);
  382. myco_send(msg.agent_message_queue_id, msg);
  383. return -1;
  384. }
  385. // Check if agent that requests already owns the resource
  386. if (strcmp(current_resource->agent, msg.agent_name) == 0) {
  387. sprintf(msg.message, "ERROR: resource %s already belongs to agent %s\n", msg.resource_name, msg.agent_name);
  388. myco_send(msg.agent_message_queue_id, msg);
  389. return -1;
  390. }
  391. // TODO: Handle case where resource is on another node (another agent)
  392. // If on same node, send information
  393. msg.sender_pid = current_resource->pid;
  394. msg.resource_size = current_resource->size;
  395. msg.resource_pointer = current_resource->pointer;
  396. sprintf(msg.message, "SUCCESS: resource found on same node, sending information\n");
  397. myco_send(msg.agent_message_queue_id, msg);
  398. msg = myco_receive(msg.agent_message_queue_id);
  399. if (strcmp(msg.message, "RESOURCE READ") == 0) {
  400. } else {
  401. fprintf(stderr, "ERROR: myco_daemon_reead_remote: %s\n", strerror(errno));
  402. return -1;
  403. }
  404. return 0;
  405. }
  406. int myco_daemon_lock_resource(message msg) {
  407. myco_resource *current_resource;
  408. current_resource = myco_daemon_find_resource(msg.resource_name);
  409. if (current_resource != NULL && current_resource->transfer_locked == 1 && strcmp(current_resource->agent, msg.agent_name) == 0) {
  410. current_resource->read_locked = 1;
  411. sprintf(msg.message, "SUCCESS: resource %s locked\n", msg.resource_name);
  412. myco_send(msg.agent_message_queue_id, msg);
  413. return 0;
  414. } else {
  415. sprintf(msg.message, "ERROR: resource %s could not be locked\n", msg.resource_name);
  416. myco_send(msg.agent_message_queue_id, msg);
  417. return -1;
  418. }
  419. }
  420. int myco_daemon_release_resource(message msg) {
  421. myco_resource *current_resource;
  422. current_resource = myco_daemon_find_resource(msg.resource_name);
  423. if (current_resource != NULL && strcmp(current_resource->agent, msg.agent_name) == 0) {
  424. current_resource->transfer_locked = 0;
  425. sprintf(msg.message, "SUCCESS: resource %s released\n", msg.resource_name);
  426. myco_send(msg.agent_message_queue_id, msg);
  427. return 0;
  428. } else {
  429. sprintf(msg.message, "ERROR: resource %s could not be released\n", msg.resource_name);
  430. myco_send(msg.agent_message_queue_id, msg);
  431. return -1;
  432. }
  433. }
  434. int myco_daemon_unlock_resource(message msg) {
  435. myco_resource *current_resource;
  436. current_resource = myco_daemon_find_resource(msg.resource_name);
  437. if (current_resource != NULL && current_resource->transfer_locked == 1 && strcmp(current_resource->agent, msg.agent_name) == 0) {
  438. current_resource->read_locked = 0;
  439. sprintf(msg.message, "SUCCESS: resource %s unlocked\n", msg.resource_name);
  440. myco_send(msg.agent_message_queue_id, msg);
  441. return 0;
  442. } else {
  443. sprintf(msg.message, "ERROR: resource %s could not be locked\n", msg.resource_name);
  444. myco_send(msg.agent_message_queue_id, msg);
  445. return -1;
  446. }
  447. }
  448. int myco_daemon_connect(const char* ip, int port) {
  449. struct sockaddr_in address;
  450. int mysocket = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
  451. address.sin_family = AF_INET;
  452. address.sin_port = htons (port);
  453. inet_aton (ip, &address.sin_addr);
  454. connect (mysocket, (struct sockaddr *) &address, sizeof (address));
  455. return mysocket;
  456. }
  457. int myco_daemon_start(pid_t pid, const char *node_ip, const char *indexer_ip) {
  458. myco_daemon_node_ip = inet_network(node_ip);
  459. char *buffer = malloc (1024);
  460. int daemon_message_queue_id;
  461. message msg = {0};
  462. // Connect to indexer
  463. int socket = myco_daemon_connect(indexer_ip, 15000);
  464. // Create message queue
  465. daemon_message_queue_id = myco_create_global_message_queue();
  466. if (daemon_message_queue_id < 0) {
  467. fprintf(stderr, "ERROR: Message queue could not be created. %s\n", strerror(errno));
  468. return -1;
  469. }
  470. // Receive messages
  471. while (1) {
  472. // Receive messages from indexer
  473. int size = recv (socket, buffer, 1023, 0);
  474. if( size > 0)
  475. buffer[size] = '\0';
  476. printf ("%s\n", buffer);
  477. // Receive messages from agents
  478. msg = myco_receive(daemon_message_queue_id);
  479. if (DEBUG) {
  480. printf("%d, %s, %s, %s, %p, %d, %d, %d, %d", msg.agent_message_queue_id, msg.agent_name, msg.message, \
  481. msg.resource_name, msg.resource_pointer, msg.resource_size, msg.resource_transactional, msg.sender_pid, msg.version);
  482. }
  483. if (msg.message == NULL) {
  484. fprintf(stderr, "FATAL ERROR: No message could be received. %s\n", strerror(errno));
  485. return -1;
  486. } else {
  487. // Handle indexer
  488. char tcp_message[MESSAGE_LENGTH + RESOURCE_NAME_LENGTH + AGENT_NAME_LENGTH];
  489. strcpy(tcp_message, msg.message);
  490. strcat(tcp_message, ";");
  491. if (msg.agent_name[0] != '\0') {
  492. strcat(tcp_message, msg.agent_name);
  493. } else {
  494. strcat(tcp_message, "-");
  495. }
  496. strcat(tcp_message, ";");
  497. if (msg.resource_name[0] != '\0') {
  498. strcat(tcp_message, msg.resource_name);
  499. } else {
  500. strcat(tcp_message, "-");
  501. }
  502. send(socket, tcp_message, strlen(tcp_message), 0);
  503. // Handle agents
  504. if (strcmp(msg.message, "REGISTER AGENT") == 0) {
  505. myco_daemon_register_agent(msg);
  506. }
  507. if (strcmp(msg.message, "UNREGISTER AGENT") == 0) {
  508. myco_daemon_unregister_agent(msg);
  509. }
  510. if (strcmp(msg.message, "REGISTER RESOURCE") == 0) {
  511. myco_daemon_register_resource(msg);
  512. }
  513. if (strcmp(msg.message, "UNREGISTER RESOURCE") == 0) {
  514. myco_daemon_unregister_resource(msg);
  515. }
  516. if (strcmp(msg.message, "REQUEST RESOURCE") == 0) {
  517. myco_daemon_request_resource(msg);
  518. }
  519. if (strcmp(msg.message, "REQUEST LIST") == 0) {
  520. myco_daemon_request_list(msg, pid);
  521. }
  522. if (strcmp(msg.message, "WRITE REMOTE RESOURCE") == 0) {
  523. myco_daemon_write_remote_resource(msg, 0);
  524. }
  525. if (strcmp(msg.message, "FORCE WRITE REMOTE RESOURCE") == 0) {
  526. myco_daemon_write_remote_resource(msg, 1);
  527. }
  528. if (strcmp(msg.message, "READ REMOTE RESOURCE") == 0) {
  529. myco_daemon_read_remote_resource(msg);
  530. }
  531. if (strcmp(msg.message, "LOCK RESOURCE") == 0) {
  532. myco_daemon_lock_resource(msg);
  533. }
  534. if (strcmp(msg.message, "UNLOCK RESOURCE") == 0) {
  535. myco_daemon_unlock_resource(msg);
  536. }
  537. if (strcmp(msg.message, "RELEASE RESOURCE") == 0) {
  538. myco_daemon_release_resource(msg);
  539. }
  540. }
  541. }
  542. close (socket);
  543. if (myco_remove_message_queue(daemon_message_queue_id) == -1) {
  544. fprintf(stderr, "FATAL ERROR: could not remove message queue %d\n", daemon_message_queue_id);
  545. return -1;
  546. }
  547. return EXIT_SUCCESS;
  548. }