myco-indexer.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. #include <sys/types.h>
  2. #include <sys/socket.h>
  3. #include <netinet/in.h>
  4. #include <arpa/inet.h>
  5. #include <unistd.h>
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include "myco-indexer.h"
  10. myco_agent *first_agent = NULL;
  11. myco_resource *first_resource = NULL;
  12. myco_agent *myco_indexer_find_agent(const char *agent_name) {
  13. myco_agent *current_agent;
  14. current_agent = first_agent;
  15. while (current_agent != NULL) {
  16. if (strcmp(current_agent->name, agent_name) == 0) {
  17. return current_agent;
  18. }
  19. current_agent = current_agent->next;
  20. }
  21. return NULL;
  22. }
  23. myco_resource *myco_indexer_find_resource_by_agent(const char *agent_name) {
  24. myco_resource *current_resource;
  25. current_resource = first_resource;
  26. while (current_resource != NULL) {
  27. if (strcmp(current_resource->agent, agent_name) == 0) {
  28. return current_resource;
  29. }
  30. current_resource = current_resource->next;
  31. }
  32. return NULL;
  33. }
  34. myco_resource *myco_indexer_find_resource(const char *resource_name) {
  35. myco_resource *current_resource;
  36. current_resource = first_resource;
  37. while (current_resource != NULL) {
  38. if (strcmp(current_resource->name, resource_name) == 0) {
  39. return current_resource;
  40. }
  41. current_resource = current_resource->next;
  42. }
  43. return NULL;
  44. }
  45. int myco_indexer_register_agent(command, agent, resource){
  46. myco_agent *current_agent;
  47. if (myco_indexer_find_agent(agent) != NULL) {
  48. printf("ERROR: agent %s already exists\n", agent);
  49. return -1;
  50. }
  51. if (first_agent == NULL) {
  52. first_agent = malloc(sizeof(myco_agent));
  53. first_agent->next = NULL;
  54. first_agent->prev = NULL;
  55. sprintf(first_agent->name, "%s", agent);
  56. } else {
  57. current_agent = first_agent;
  58. while (current_agent->next != NULL) {
  59. current_agent = current_agent->next;
  60. }
  61. current_agent->next = malloc(sizeof(myco_agent));
  62. current_agent->next->prev = current_agent;
  63. current_agent->next->next = NULL;
  64. sprintf(current_agent->next->name, "%s", agent);
  65. }
  66. printf("SUCCESS: agent %s registered\n", agent);
  67. return 0;
  68. }
  69. int myco_indexer_unregister_agent(command, agent, resource){
  70. myco_agent *current_agent;
  71. myco_resource *current_resource;
  72. current_agent = myco_indexer_find_agent(agent);
  73. if ((current_resource = myco_indexer_find_resource_by_agent(agent)) != NULL) {
  74. printf("ERROR: agent %s still has resource %s\n", agent, current_resource->name);
  75. return -1;
  76. }
  77. if (current_agent == NULL) {
  78. printf("ERROR: agent %s could not be unregistered\n", agent);
  79. return -1;
  80. }
  81. // Element in the middle
  82. if (current_agent->next != NULL && current_agent->prev != NULL) {
  83. current_agent->prev->next = current_agent->next;
  84. current_agent->next->prev = current_agent->prev;
  85. free(current_agent);
  86. printf("SUCCESS: agent %s unregistered\n", agent);
  87. return 0;
  88. }
  89. // First element
  90. if (current_agent->next != NULL && current_agent->prev == NULL) {
  91. first_agent = current_agent->next;
  92. current_agent->next->prev = NULL;
  93. free(current_agent);
  94. printf("SUCCESS: agent %s unregistered\n", agent);
  95. return 0;
  96. }
  97. // Last element
  98. if (current_agent->next == NULL && current_agent->prev != NULL) {
  99. current_agent->prev->next = NULL;
  100. free(current_agent);
  101. printf("SUCCESS: agent %s unregistered\n", agent);
  102. return 0;
  103. }
  104. // Only remaining
  105. if (current_agent->next == NULL && current_agent->prev == NULL) {
  106. first_agent = NULL;
  107. free(current_agent);
  108. printf("SUCCESS: agent %s unregistered\n", agent);
  109. return 0;
  110. }
  111. printf("ERROR: agent %s could not be unregistered - data structure seems to be damaged!\n", agent);
  112. return -1;
  113. }
  114. int myco_indexer_register_resource(command, agent, resource){
  115. myco_resource *current_resource;
  116. if (myco_indexer_find_agent(agent) == NULL) {
  117. printf("ERROR: agent %s does not exist\n", agent);
  118. return -1;
  119. }
  120. if (myco_indexer_find_resource(resource) != NULL) {
  121. printf("ERROR: resource %s already exists\n", resource);
  122. return -1;
  123. }
  124. // Insert as first element
  125. if (first_resource == NULL) {
  126. first_resource = malloc(sizeof(myco_resource));
  127. first_resource->next = NULL;
  128. first_resource->prev = NULL;
  129. sprintf(first_resource->name, "%s", resource);
  130. sprintf(first_resource->agent, "%s", agent);
  131. } else {
  132. // Insert as last element
  133. current_resource = first_resource;
  134. while (current_resource->next != NULL) {
  135. current_resource = current_resource->next;
  136. }
  137. current_resource->next = malloc(sizeof(myco_resource));
  138. current_resource->next->prev = current_resource;
  139. current_resource->next->next = NULL;
  140. sprintf(current_resource->next->name, "%s", resource);
  141. sprintf(current_resource->next->agent, "%s", agent);
  142. }
  143. printf("SUCCESS: resource %s registered\n", resource);
  144. return 0;
  145. }
  146. int myco_indexer_unregister_resource(command, agent, resource){
  147. myco_resource *current_resource;
  148. current_resource = myco_indexer_find_resource(resource);
  149. if (current_resource == NULL) {
  150. printf("ERROR: resource %s could not be unregistered\n", resource);
  151. return -1;
  152. }
  153. // Element in the middle
  154. if (current_resource->next != NULL && current_resource->prev != NULL) {
  155. current_resource->prev->next = current_resource->next;
  156. current_resource->next->prev = current_resource->prev;
  157. free(current_resource);
  158. printf("SUCCESS: resource %s unregistered\n", resource);
  159. return 0;
  160. }
  161. // First element
  162. if (current_resource->next != NULL && current_resource->prev == NULL) {
  163. first_resource = current_resource->next;
  164. current_resource->next->prev = NULL;
  165. free(current_resource);
  166. printf("SUCCESS: resource %s unregistered\n", resource);
  167. return 0;
  168. }
  169. // Last element
  170. if (current_resource->next == NULL && current_resource->prev != NULL) {
  171. current_resource->prev->next = NULL;
  172. free(current_resource);
  173. printf("SUCCESS: resource %s unregistered\n", resource);
  174. return 0;
  175. }
  176. // Only remaining
  177. if (current_resource->next == NULL && current_resource->prev == NULL) {
  178. first_resource = NULL;
  179. free(current_resource);
  180. printf("SUCCESS: resource %s unregistered\n", resource);
  181. return 0;
  182. }
  183. printf("FATAL ERROR: resource %s could not be unregistered - data structure seems to be damaged!\n", resource);
  184. return -1;
  185. }
  186. int myco_indexer_request_resource(command, agent, resource){
  187. }
  188. int myco_indexer_write_remote_resource(command, agent, resource){
  189. }
  190. int myco_indexer_read_remote_resource(command, agent, resource){
  191. }
  192. int myco_indexer_lock_resource(command, agent, resource){
  193. }
  194. int myco_indexer_unlock_resource(command, agent, resource){
  195. }
  196. int myco_indexer_release_resource(command, agent, resource){
  197. }
  198. #define BUF 1024
  199. int myco_indexer_start(int port) {
  200. int create_socket, new_socket;
  201. socklen_t addrlen;
  202. char *buffer = malloc (BUF);
  203. ssize_t size;
  204. struct sockaddr_in address;
  205. const int y = 1;
  206. char *command;
  207. char *agent;
  208. char *resource;
  209. printf ("\e[2J");
  210. create_socket=socket (AF_INET, SOCK_STREAM, 0);
  211. setsockopt( create_socket, SOL_SOCKET,
  212. SO_REUSEADDR, &y, sizeof(int));
  213. address.sin_family = AF_INET;
  214. address.sin_addr.s_addr = INADDR_ANY;
  215. address.sin_port = htons (port);
  216. if (bind ( create_socket,
  217. (struct sockaddr *) &address,
  218. sizeof (address)) != 0) {
  219. printf( "Port already taken!\n");
  220. }
  221. listen (create_socket, 5);
  222. addrlen = sizeof (struct sockaddr_in);
  223. while (1) {
  224. new_socket = accept ( create_socket,
  225. (struct sockaddr *) &address,
  226. &addrlen );
  227. if (new_socket > 0)
  228. inet_ntoa (address.sin_addr);
  229. do {
  230. size = recv (new_socket, buffer, BUF-1, 0);
  231. if( size > 0)
  232. buffer[size] = '\0';
  233. // Handle agents
  234. command = strtok(buffer, ";");
  235. agent = strtok(NULL, ";");
  236. resource = strtok(NULL, ";");
  237. if (strcmp(command, "REGISTER AGENT") == 0) {
  238. myco_indexer_register_agent(command, agent, resource);
  239. }
  240. if (strcmp(command, "UNREGISTER AGENT") == 0) {
  241. myco_indexer_unregister_agent(command, agent, resource);
  242. }
  243. if (strcmp(command, "REGISTER RESOURCE") == 0) {
  244. myco_indexer_register_resource(command, agent, resource);
  245. }
  246. if (strcmp(command, "UNREGISTER RESOURCE") == 0) {
  247. myco_indexer_unregister_resource(command, agent, resource);
  248. }
  249. if (strcmp(command, "REQUEST RESOURCE") == 0) {
  250. myco_indexer_request_resource(command, agent, resource);
  251. }
  252. if (strcmp(command, "WRITE REMOTE RESOURCE") == 0) {
  253. myco_indexer_write_remote_resource(command, agent, resource, 0);
  254. }
  255. if (strcmp(command, "FORCE WRITE REMOTE RESOURCE") == 0) {
  256. myco_indexer_write_remote_resource(command, agent, resource, 1);
  257. }
  258. if (strcmp(command, "READ REMOTE RESOURCE") == 0) {
  259. myco_indexer_read_remote_resource(command, agent, resource);
  260. }
  261. if (strcmp(command, "LOCK RESOURCE") == 0) {
  262. myco_indexer_lock_resource(command, agent, resource);
  263. }
  264. if (strcmp(command, "UNLOCK RESOURCE") == 0) {
  265. myco_indexer_unlock_resource(command, agent, resource);
  266. }
  267. if (strcmp(command, "RELEASE RESOURCE") == 0) {
  268. myco_indexer_release_resource(command, agent, resource);
  269. }
  270. } while (strcmp (command, "quit\n") != 0);
  271. close (new_socket);
  272. }
  273. close (create_socket);
  274. return EXIT_SUCCESS;
  275. }