myco-agent.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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-agent.h"
  16. #define DEBUG 1
  17. int myco_agent_register(const char *agent_name) {
  18. int agent_message_queue_id;
  19. int global_message_queue_id;
  20. message msg;
  21. sprintf(msg.agent_name, "%s", agent_name);
  22. agent_message_queue_id = myco_create_private_message_queue();
  23. msg.agent_message_queue_id = agent_message_queue_id;
  24. sprintf(msg.message, "REGISTER AGENT\n");
  25. msg = myco_send_and_receive(msg, myco_get_global_message_queue(), agent_message_queue_id);
  26. if (DEBUG) {
  27. printf("%s", msg.message);
  28. }
  29. return agent_message_queue_id;
  30. }
  31. int myco_agent_unregister (const char *agent_name, int agent_message_queue_id) {
  32. message msg;
  33. msg.agent_message_queue_id = agent_message_queue_id;
  34. sprintf(msg.agent_name, "%s", agent_name);
  35. sprintf(msg.message, "UNREGISTER AGENT\n");
  36. msg = myco_send_and_receive(msg, myco_get_global_message_queue(), agent_message_queue_id);
  37. if (DEBUG) {
  38. printf("%s\n", msg.message);
  39. }
  40. myco_remove_message_queue(agent_message_queue_id);
  41. return 0;
  42. }
  43. int myco_agent_register_resource (int agent_message_queue_id, const char *agent_name, const char *resource_name, int resource_transactional) {
  44. message msg;
  45. msg.agent_message_queue_id = agent_message_queue_id;
  46. sprintf(msg.agent_name, "%s", agent_name);
  47. sprintf(msg.resource_name, "%s", resource_name);
  48. msg.resource_transactional = resource_transactional;
  49. sprintf(msg.message, "REGISTER RESOURCE\n");
  50. msg = myco_send_and_receive(msg, myco_get_global_message_queue(), agent_message_queue_id);
  51. if(DEBUG) {
  52. printf("%s", msg.message);
  53. }
  54. return 0;
  55. }
  56. int myco_agent_unregister_resource (int agent_message_queue_id, const char *resource_name) {
  57. message msg;
  58. msg.agent_message_queue_id = agent_message_queue_id;
  59. sprintf(msg.resource_name, "%s", resource_name);
  60. sprintf(msg.message, "UNREGISTER RESOURCE\n");
  61. msg = myco_send_and_receive(msg, myco_get_global_message_queue(), agent_message_queue_id);
  62. if(DEBUG) {
  63. printf("%s", msg.message);
  64. }
  65. return 0;
  66. }
  67. int myco_agent_request_resource(int agent_message_queue_id, char *resource_name) {
  68. char *resource_pointer;
  69. message msg;
  70. msg.agent_message_queue_id = agent_message_queue_id;
  71. sprintf(msg.resource_name, "%s", resource_name);
  72. sprintf(msg.message, "REQUEST RESOURCE\n");
  73. msg = myco_send_and_receive(msg, myco_get_global_message_queue(), agent_message_queue_id);
  74. if(DEBUG) {
  75. printf("%s", msg.message);
  76. }
  77. resource_pointer = malloc(msg.resource_size);
  78. if (myco_read_transactional(msg.sender_pid, msg.resource_pointer, msg.resource_size, (void *)resource_pointer, msg.resource_size) == -1) {
  79. fprintf(stderr, "%s\n", strerror(errno));
  80. return -1;
  81. }
  82. return 0;
  83. }
  84. int myco_agent_request_resource_list(int agent_message_queue_id) {
  85. char *resource_pointer;
  86. char *split;
  87. message msg;
  88. msg.agent_message_queue_id = agent_message_queue_id;
  89. sprintf(msg.message, "REQUEST LIST\n");
  90. msg.resource_size = 0;
  91. msg = myco_send_and_receive(msg, myco_get_global_message_queue(), agent_message_queue_id);
  92. if(DEBUG) {
  93. printf("%s", msg.message);
  94. }
  95. resource_pointer = malloc(msg.resource_size);
  96. if (myco_read_transactional(msg.sender_pid, msg.resource_pointer, msg.resource_size, (void *)resource_pointer, msg.resource_size) == -1) {
  97. printf("ERROR: %s\n", strerror(errno));
  98. return -1;
  99. }
  100. sprintf(msg.message, "RESOURCE GRANTED\n");
  101. myco_send(agent_message_queue_id, msg);
  102. printf("%s\n", resource_pointer);
  103. /*
  104. split = strtok(resource_pointer, ";");
  105. while (split != NULL) {
  106. printf("%s\n", split);
  107. split = strtok(NULL, ";");
  108. }
  109. */
  110. free(resource_pointer);
  111. return 0;
  112. }
  113. int myco_agent_request_remote_resource(int agent_message_queue_id, char *resource_name) {
  114. message msg;
  115. msg.agent_message_queue_id = agent_message_queue_id;
  116. sprintf(msg.resource_name, "%s", resource_name);
  117. sprintf(msg.message, "REQUEST REMOTE RESOURCE\n");
  118. msg = myco_send_and_receive(msg, myco_get_global_message_queue(), agent_message_queue_id);
  119. return 0;
  120. }
  121. int myco_agent_write_remote_resource(int agent_message_queue_id, char *resource_name) {
  122. message msg;
  123. msg.agent_message_queue_id = agent_message_queue_id;
  124. sprintf(msg.resource_name, "%s", resource_name);
  125. sprintf(msg.message, "WRITE REMOTE RESOURCE\n");
  126. msg = myco_send_and_receive(msg, myco_get_global_message_queue(), agent_message_queue_id);
  127. return 0;
  128. }
  129. int myco_agent_read_remote_resource(int agent_message_queue_id, char *resource_name) {
  130. message msg;
  131. msg.agent_message_queue_id = agent_message_queue_id;
  132. sprintf(msg.resource_name, "%s", resource_name);
  133. sprintf(msg.message, "READ REMOTE RESOURCE\n");
  134. msg = myco_send_and_receive(msg, myco_get_global_message_queue(), agent_message_queue_id);
  135. return 0;
  136. }