myco-daemon.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  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 1
  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. // Check if resource exists
  235. if ((current_resource = myco_daemon_find_resource(msg.resource_name)) == NULL) {
  236. sprintf(msg.message, "ERROR: resource %s does not exist\n", msg.resource_name);
  237. myco_send(msg.agent_message_queue_id, msg);
  238. return -1;
  239. }
  240. // Check if agent that requests already owns the resource
  241. if (strcmp(current_resource->agent, msg.agent_name) == 0) {
  242. sprintf(msg.message, "ERROR: resource %s already belongs to agent %s\n", msg.resource_name, msg.agent_name);
  243. myco_send(msg.agent_message_queue_id, msg);
  244. return -1;
  245. }
  246. // Check if resource is transactional
  247. if (current_resource->transactional != RESOURCE_TRANSACTIONAL) {
  248. sprintf(msg.message, "ERROR: resource %s is not transactional\n", msg.resource_name);
  249. myco_send(msg.agent_message_queue_id, msg);
  250. return -1;
  251. }
  252. // Check if resource resides on same node
  253. printf("\n The adress is: %d\n", current_resource->node_ip);
  254. // TODO: Handle case where resource is on another node
  255. // If on same node, send information
  256. msg.sender_pid = current_resource->pid;
  257. msg.resource_size = current_resource->size;
  258. msg.resource_pointer = current_resource->pointer;
  259. sprintf(msg.message, "SUCCESS: resource found on same node, sending information\n");
  260. myco_send(msg.agent_message_queue_id, msg);
  261. msg = myco_receive(msg.agent_message_queue_id);
  262. if (strcmp(msg.message, "RESOURCE GRANTED") == 0) {
  263. // Transfer ownership of resource to agent
  264. sprintf(current_resource->agent, "%s", msg.agent_name);
  265. current_resource->pid = msg.sender_pid;
  266. current_resource->pointer = msg.resource_pointer;
  267. } else {
  268. fprintf(stderr, "ERROR: myco_daemon_request: %s\n", strerror(errno));
  269. return -1;
  270. }
  271. return 0;
  272. }
  273. int myco_daemon_request_list(message msg, pid_t pid) {
  274. myco_resource *current_resource;
  275. current_resource = first_resource;
  276. char *resource_pointer;
  277. char tmp[64];
  278. int resource_size = 0;
  279. if (current_resource == NULL) {
  280. sprintf(msg.message, "ERROR: no resources\n");
  281. myco_send(msg.agent_message_queue_id, msg);
  282. return -1;
  283. }
  284. while (current_resource != NULL) {
  285. resource_size += strlen(current_resource->name) + strlen(current_resource->agent) + strlen("1");
  286. resource_size += strlen("(,,,);");
  287. resource_size += 22;
  288. current_resource = current_resource->next;
  289. }
  290. resource_pointer = malloc(resource_size);
  291. memset(resource_pointer, 0, resource_size);
  292. current_resource = first_resource;
  293. while (current_resource != NULL) {
  294. strcat(resource_pointer, current_resource->name);
  295. strcat(resource_pointer, "(");
  296. strcat(resource_pointer, current_resource->agent);
  297. strcat(resource_pointer, ",");
  298. sprintf(tmp, "%d", current_resource->size);
  299. strcat(resource_pointer, tmp);
  300. strcat(resource_pointer, ",");
  301. sprintf(tmp, "%p", current_resource->pointer);
  302. strcat(resource_pointer, tmp);
  303. strcat(resource_pointer, ",");
  304. sprintf(tmp, "%d", current_resource->read_locked);
  305. strcat(resource_pointer, tmp);
  306. strcat(resource_pointer, ",");
  307. sprintf(tmp, "%d", current_resource->transfer_locked);
  308. strcat(resource_pointer, tmp);
  309. strcat(resource_pointer, ",");
  310. if (current_resource->transactional) {
  311. strcat(resource_pointer, "1");
  312. } else {
  313. strcat(resource_pointer, "0");
  314. }
  315. strcat(resource_pointer, ");");
  316. current_resource = current_resource->next;
  317. }
  318. msg.sender_pid = pid;
  319. msg.resource_size = resource_size;
  320. msg.resource_pointer = (void *)resource_pointer;
  321. sprintf(msg.message, "SUCCESS: sending resource list\n");
  322. myco_send(msg.agent_message_queue_id, msg);
  323. msg = myco_receive(msg.agent_message_queue_id);
  324. if (strcmp(msg.message, "RESOURCE LIST GRANTED") == 0) {
  325. free(resource_pointer);
  326. } else {
  327. fprintf(stderr, "ERROR: resource list was not granted: %s\n", strerror(errno));
  328. free(resource_pointer);
  329. return -1;
  330. }
  331. return 0;
  332. }
  333. int myco_daemon_write_remote_resource(message msg, int force) {
  334. myco_resource *current_resource;
  335. // Check if resource exists
  336. if ((current_resource = myco_daemon_find_resource(msg.resource_name)) == NULL) {
  337. sprintf(msg.message, "ERROR: resource %s does not exist\n", msg.resource_name);
  338. myco_send(msg.agent_message_queue_id, msg);
  339. return -1;
  340. }
  341. if (force == 1) {
  342. sprintf(msg.message, "SUCCESS: forcing to overwrite resource %s, sending information\n", msg.resource_name);
  343. msg.resource_pointer = current_resource->pointer;
  344. msg.resource_size = current_resource->size;
  345. msg.sender_pid = current_resource->pid;
  346. myco_send(msg.agent_message_queue_id, msg);
  347. return 0;
  348. } else {
  349. if (current_resource->version > msg.version) {
  350. sprintf(msg.message, "ERROR: resource %s is newer than your copied version\n", msg.resource_name);
  351. myco_send(msg.agent_message_queue_id, msg);
  352. return -1;
  353. } else {
  354. sprintf(msg.message, "SUCCESS: version updated, sending information for resource %s\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. current_resource->version += 1;
  360. myco_send(msg.agent_message_queue_id, msg);
  361. return 0;
  362. }
  363. }
  364. }
  365. int myco_daemon_read_remote_resource(message msg) {
  366. myco_resource *current_resource;
  367. // Check if resource exists
  368. if ((current_resource = myco_daemon_find_resource(msg.resource_name)) == NULL) {
  369. sprintf(msg.message, "ERROR: resource %s does not exist\n", msg.resource_name);
  370. myco_send(msg.agent_message_queue_id, msg);
  371. return -1;
  372. }
  373. // Check if agent that requests already owns the resource
  374. if (strcmp(current_resource->agent, msg.agent_name) == 0) {
  375. sprintf(msg.message, "ERROR: resource %s already belongs to agent %s\n", msg.resource_name, msg.agent_name);
  376. myco_send(msg.agent_message_queue_id, msg);
  377. return -1;
  378. }
  379. // TODO: Handle case where resource is on another node (another agent)
  380. // If on same node, send information
  381. msg.sender_pid = current_resource->pid;
  382. msg.resource_size = current_resource->size;
  383. msg.resource_pointer = current_resource->pointer;
  384. sprintf(msg.message, "SUCCESS: resource found on same node, sending information\n");
  385. myco_send(msg.agent_message_queue_id, msg);
  386. msg = myco_receive(msg.agent_message_queue_id);
  387. if (strcmp(msg.message, "RESOURCE READ") == 0) {
  388. } else {
  389. fprintf(stderr, "ERROR: myco_daemon_reead_remote: %s\n", strerror(errno));
  390. return -1;
  391. }
  392. return 0;
  393. }
  394. int myco_daemon_lock_resource(message msg) {
  395. myco_resource *current_resource;
  396. current_resource = myco_daemon_find_resource(msg.resource_name);
  397. if (current_resource != NULL && current_resource->transfer_locked == 1 && strcmp(current_resource->agent, msg.agent_name) == 0) {
  398. current_resource->read_locked = 1;
  399. sprintf(msg.message, "SUCCESS: resource %s locked\n", msg.resource_name);
  400. myco_send(msg.agent_message_queue_id, msg);
  401. return 0;
  402. } else {
  403. sprintf(msg.message, "ERROR: resource %s could not be locked\n", msg.resource_name);
  404. myco_send(msg.agent_message_queue_id, msg);
  405. return -1;
  406. }
  407. }
  408. int myco_daemon_release_resource(message msg) {
  409. myco_resource *current_resource;
  410. current_resource = myco_daemon_find_resource(msg.resource_name);
  411. if (current_resource != NULL && strcmp(current_resource->agent, msg.agent_name) == 0) {
  412. current_resource->transfer_locked = 0;
  413. sprintf(msg.message, "SUCCESS: resource %s released\n", msg.resource_name);
  414. myco_send(msg.agent_message_queue_id, msg);
  415. return 0;
  416. } else {
  417. sprintf(msg.message, "ERROR: resource %s could not be released\n", msg.resource_name);
  418. myco_send(msg.agent_message_queue_id, msg);
  419. return -1;
  420. }
  421. }
  422. int myco_daemon_unlock_resource(message msg) {
  423. myco_resource *current_resource;
  424. current_resource = myco_daemon_find_resource(msg.resource_name);
  425. if (current_resource != NULL && current_resource->transfer_locked == 1 && strcmp(current_resource->agent, msg.agent_name) == 0) {
  426. current_resource->read_locked = 0;
  427. sprintf(msg.message, "SUCCESS: resource %s unlocked\n", msg.resource_name);
  428. myco_send(msg.agent_message_queue_id, msg);
  429. return 0;
  430. } else {
  431. sprintf(msg.message, "ERROR: resource %s could not be locked\n", msg.resource_name);
  432. myco_send(msg.agent_message_queue_id, msg);
  433. return -1;
  434. }
  435. }
  436. int myco_daemon_connect(const char* ip, int port) {
  437. struct sockaddr_in address;
  438. int mysocket = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
  439. address.sin_family = AF_INET;
  440. address.sin_port = htons (port);
  441. inet_aton (ip, &address.sin_addr);
  442. connect (mysocket, (struct sockaddr *) &address, sizeof (address));
  443. return mysocket;
  444. }
  445. int myco_daemon_start(pid_t pid, const char *node_ip, const char *indexer_ip) {
  446. in_addr_t myco_daemon_node_ip = inet_network(node_ip);
  447. char *buffer = malloc (1024);
  448. int daemon_message_queue_id;
  449. message msg = {0};
  450. // Connect to indexer
  451. int socket = myco_daemon_connect(indexer_ip, 15000);
  452. // Create message queue
  453. daemon_message_queue_id = myco_create_global_message_queue();
  454. if (daemon_message_queue_id < 0) {
  455. fprintf(stderr, "ERROR: Message queue could not be created. %s\n", strerror(errno));
  456. return -1;
  457. }
  458. // Receive messages
  459. while (1) {
  460. // Receive messages from indexer
  461. int size = recv (socket, buffer, 1023, 0);
  462. if( size > 0)
  463. buffer[size] = '\0';
  464. printf ("%s\n", buffer);
  465. // Receive messages from agents
  466. msg = myco_receive(daemon_message_queue_id);
  467. if (DEBUG) {
  468. printf("%d, %s, %s, %s, %p, %d, %d, %d, %d", msg.agent_message_queue_id, msg.agent_name, msg.message, \
  469. msg.resource_name, msg.resource_pointer, msg.resource_size, msg.resource_transactional, msg.sender_pid, msg.version);
  470. }
  471. if (msg.message == NULL) {
  472. fprintf(stderr, "FATAL ERROR: No message could be received. %s\n", strerror(errno));
  473. return -1;
  474. } else {
  475. // Handle indexer
  476. char tcp_message[MESSAGE_LENGTH + RESOURCE_NAME_LENGTH + AGENT_NAME_LENGTH];
  477. strcpy(tcp_message, msg.message);
  478. strcat(tcp_message, ";");
  479. if (msg.agent_name[0] != '\0') {
  480. strcat(tcp_message, msg.agent_name);
  481. } else {
  482. strcat(tcp_message, "-");
  483. }
  484. strcat(tcp_message, ";");
  485. if (msg.resource_name[0] != '\0') {
  486. strcat(tcp_message, msg.resource_name);
  487. } else {
  488. strcat(tcp_message, "-");
  489. }
  490. send(socket, tcp_message, strlen(tcp_message), 0);
  491. // Handle agents
  492. if (strcmp(msg.message, "REGISTER AGENT") == 0) {
  493. myco_daemon_register_agent(msg);
  494. }
  495. if (strcmp(msg.message, "UNREGISTER AGENT") == 0) {
  496. myco_daemon_unregister_agent(msg);
  497. }
  498. if (strcmp(msg.message, "REGISTER RESOURCE") == 0) {
  499. myco_daemon_register_resource(msg);
  500. }
  501. if (strcmp(msg.message, "UNREGISTER RESOURCE") == 0) {
  502. myco_daemon_unregister_resource(msg);
  503. }
  504. if (strcmp(msg.message, "REQUEST RESOURCE") == 0) {
  505. myco_daemon_request_resource(msg);
  506. }
  507. if (strcmp(msg.message, "REQUEST LIST") == 0) {
  508. myco_daemon_request_list(msg, pid);
  509. }
  510. if (strcmp(msg.message, "WRITE REMOTE RESOURCE") == 0) {
  511. myco_daemon_write_remote_resource(msg, 0);
  512. }
  513. if (strcmp(msg.message, "FORCE WRITE REMOTE RESOURCE") == 0) {
  514. myco_daemon_write_remote_resource(msg, 1);
  515. }
  516. if (strcmp(msg.message, "READ REMOTE RESOURCE") == 0) {
  517. myco_daemon_read_remote_resource(msg);
  518. }
  519. if (strcmp(msg.message, "LOCK RESOURCE") == 0) {
  520. myco_daemon_lock_resource(msg);
  521. }
  522. if (strcmp(msg.message, "UNLOCK RESOURCE") == 0) {
  523. myco_daemon_unlock_resource(msg);
  524. }
  525. if (strcmp(msg.message, "RELEASE RESOURCE") == 0) {
  526. myco_daemon_release_resource(msg);
  527. }
  528. }
  529. }
  530. close (socket);
  531. if (myco_remove_message_queue(daemon_message_queue_id) == -1) {
  532. fprintf(stderr, "FATAL ERROR: could not remove message queue %d\n", daemon_message_queue_id);
  533. return -1;
  534. }
  535. return EXIT_SUCCESS;
  536. }