myco-agent.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 <sys/types.h>
  16. #include <sys/socket.h>
  17. #include <netinet/in.h>
  18. #include <arpa/inet.h>
  19. #include <unistd.h>
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include "myco-agent.h"
  24. int myco_send (const char *message, const char *myco_daemon_address, uint16_t myco_daemon_port) {
  25. int create_socket;
  26. struct sockaddr_in address;
  27. if ((create_socket = socket (AF_INET, SOCK_STREAM, 0)) < 1)
  28. return -1;
  29. address.sin_family = AF_INET;
  30. address.sin_port = htons (myco_daemon_port);
  31. inet_aton (myco_daemon_address, &address.sin_addr);
  32. if (connect ( create_socket, (struct sockaddr *) &address, sizeof (address)) != 0)
  33. return -1;
  34. send(create_socket, message, strlen (message), 0);
  35. close (create_socket);
  36. return 0;
  37. }
  38. int myco_recv (char *message, const char *myco_daemon_address, uint16_t myco_daemon_port) {
  39. int buf = 1024;
  40. int create_socket, new_socket;
  41. socklen_t addrlen;
  42. char *buffer = malloc (buf);
  43. ssize_t size;
  44. struct sockaddr_in address;
  45. const int y = 1;
  46. printf ("\e[2J");
  47. if ((create_socket=socket (AF_INET, SOCK_STREAM, 0)) > 0)
  48. printf ("Socket wurde angelegt\n");
  49. setsockopt( create_socket, SOL_SOCKET,
  50. SO_REUSEADDR, &y, sizeof(int));
  51. address.sin_family = AF_INET;
  52. address.sin_addr.s_addr = INADDR_ANY;
  53. address.sin_port = htons (myco_daemon_port);
  54. if (bind ( create_socket,
  55. (struct sockaddr *) &address,
  56. sizeof (address)) != 0) {
  57. printf( "Der Port ist nicht frei – belegt!\n");
  58. }
  59. listen (create_socket, 5);
  60. addrlen = sizeof (struct sockaddr_in);
  61. new_socket = accept ( create_socket,
  62. (struct sockaddr *) &address,
  63. &addrlen );
  64. if (new_socket > 0)
  65. printf ("Ein Client (%s) ist verbunden ...\n",
  66. inet_ntoa (address.sin_addr));
  67. size = recv (new_socket, buffer, buf-1, 0);
  68. if( size > 0)
  69. buffer[size] = '\0';
  70. printf ("Nachricht empfangen: %s\n", buffer);
  71. close (new_socket);
  72. return 0;
  73. }
  74. int myco_agent_register (int pid, const char *myco_daemon_address, uint16_t myco_daemon_port) {
  75. int size = 1024;
  76. char *message = malloc (size);
  77. char *buffer = malloc(50);
  78. strcat (message, "REGISTER: ");
  79. strcat (message, myco_daemon_address);
  80. sprintf (buffer, ":%d", myco_daemon_port);
  81. strcat (message, buffer);
  82. sprintf (buffer, " %d", pid);
  83. strcat (message, buffer);
  84. if (myco_send(message, myco_daemon_address, myco_daemon_port) != 0) {
  85. return -1;
  86. }
  87. return 0;
  88. }
  89. int myco_agent_unregister (int pid, const char *myco_daemon_address, uint16_t myco_daemon_port) {
  90. int size = 1024;
  91. char *message = malloc (size);
  92. char *buffer = malloc(50);
  93. strcat (message, "UNREGISTER: ");
  94. strcat (message, myco_daemon_address);
  95. sprintf (buffer, ":%d", myco_daemon_port);
  96. strcat (message, buffer);
  97. sprintf (buffer, " %d", pid);
  98. strcat (message, buffer);
  99. if (myco_send(message, myco_daemon_address, myco_daemon_port) != 0) {
  100. return -1;
  101. }
  102. return 0;
  103. }
  104. int myco_agent_register_resource (int pid, const char *myco_daemon_address, uint16_t myco_daemon_port, const char *resource_name) {
  105. int size = 1024;
  106. char *message = malloc (size);
  107. char *buffer = malloc(50);
  108. strcat (message, "RESOURCE: ");
  109. strcat (message, myco_daemon_address);
  110. sprintf (buffer, ":%d", myco_daemon_port);
  111. strcat (message, buffer);
  112. sprintf (buffer, " %d ", pid);
  113. strcat (message, buffer);
  114. strcat (message, resource_name);
  115. if (myco_send(message, myco_daemon_address, myco_daemon_port) != 0) {
  116. return -1;
  117. }
  118. return myco_recv(message, myco_daemon_address, myco_daemon_port);
  119. }
  120. /**
  121. * myco_agent_request_resource:
  122. * @pid: The PID of the user program.
  123. * @myco_daemon_address: The IP address of the daemon (usually localhost).
  124. * @myco_daemon_port: The port of the daemon.
  125. * @resource_id: The unique resource id.
  126. * @resource_pointer: Pointer to the local memory region where to write the resource to.
  127. * @resource_size: Size in Bytes the resource takes.
  128. * @resource_name: Variable for resource name, filled by daemon.
  129. *
  130. * Requests a resource from the daemon. If size equals actual resource size transfer is
  131. * done immediatly. This only works if resource size is known beforehand. If resource
  132. * size is unknown check with myco_agent_request_resource_info();
  133. *
  134. * Returns: -1 on error, 0 on success
  135. */
  136. int myco_agent_request_resource(int pid, const char *myco_daemon_address, uint16_t myco_daemon_port, int resource_id, void *resource_pointer, int resource_size, char *resource_name);
  137. /**
  138. * myco_agent_request_resource_info:
  139. * @pid: The PID of the user program.
  140. * @myco_daemon_address: The IP address of the daemon (usually localhost).
  141. * @myco_daemon_port: The port of the daemon.
  142. * @resource_id: The unique resource id.
  143. * @resource_size: Size in Bytes the resource takes.
  144. * @resource_name: Variable for resource name, filled by daemon.
  145. *
  146. * Requests resource information from the daemon.
  147. *
  148. * Returns: -1 on error, 0 on success
  149. */
  150. int myco_agent_request_resource_info(int pid, const char *myco_daemon_address, uint16_t myco_daemon_port, int resource_id, int resource_size, char *resource_name);
  151. /**
  152. * myco_agent_request_resource_list:
  153. * @pid: The PID of the user program.
  154. * @myco_daemon_address: The IP address of the daemon (usually localhost).
  155. * @myco_daemon_port: The port of the daemon.
  156. * @resource_list: A comma seperated list of resources (id, name, size).
  157. *
  158. * Requests resource list. Obtains a list of all resources in the current network.
  159. *
  160. * Returns: -1 on error, 0 on success
  161. */
  162. int myco_agent_request_resource_list(int pid, const char *myco_daemon_address, uint16_t myco_daemon_port, char *resource_list);