123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- /* Copyright (C) 2016 Max Riechelmann <max.riechelmann@student.kit.edu>
- (Karlsruhe Institute of Technology)
- This library is free software; you can redistribute it and/or modify it
- under the terms of the GNU Lesser General Public License as published by the
- Free Software Foundation; either version 2.1 of the License, or (at your
- option) any later version.
- This library is distributed in the hope that it will be useful, but WITHOUT
- ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
- FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
- details.
- You should have received a copy of the GNU Lesser General Public License along
- with this library; if not, write to the Free Software Foundation, Inc., 51
- Franklin St, Fifth Floor, Boston, MA 02110, USA
- */
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include "myco-agent.h"
- int myco_send (const char *message, const char *myco_daemon_address, uint16_t myco_daemon_port) {
- int create_socket;
- struct sockaddr_in address;
- if ((create_socket = socket (AF_INET, SOCK_STREAM, 0)) < 1)
- return -1;
- address.sin_family = AF_INET;
- address.sin_port = htons (myco_daemon_port);
- inet_aton (myco_daemon_address, &address.sin_addr);
- if (connect ( create_socket, (struct sockaddr *) &address, sizeof (address)) != 0)
- return -1;
- send(create_socket, message, strlen (message), 0);
- close (create_socket);
- return 0;
- }
- int myco_recv (char *message, const char *myco_daemon_address, uint16_t myco_daemon_port) {
- int buf = 1024;
- int create_socket, new_socket;
- socklen_t addrlen;
- char *buffer = malloc (buf);
- ssize_t size;
- struct sockaddr_in address;
- const int y = 1;
- printf ("\e[2J");
- if ((create_socket=socket (AF_INET, SOCK_STREAM, 0)) > 0)
- printf ("Socket wurde angelegt\n");
- setsockopt( create_socket, SOL_SOCKET,
- SO_REUSEADDR, &y, sizeof(int));
- address.sin_family = AF_INET;
- address.sin_addr.s_addr = INADDR_ANY;
- address.sin_port = htons (myco_daemon_port);
- if (bind ( create_socket,
- (struct sockaddr *) &address,
- sizeof (address)) != 0) {
- printf( "Der Port ist nicht frei – belegt!\n");
- }
- listen (create_socket, 5);
- addrlen = sizeof (struct sockaddr_in);
- new_socket = accept ( create_socket,
- (struct sockaddr *) &address,
- &addrlen );
- if (new_socket > 0)
- printf ("Ein Client (%s) ist verbunden ...\n",
- inet_ntoa (address.sin_addr));
- size = recv (new_socket, buffer, buf-1, 0);
- if( size > 0)
- buffer[size] = '\0';
- printf ("Nachricht empfangen: %s\n", buffer);
- close (new_socket);
- return 0;
- }
- int myco_agent_register (int pid, const char *myco_daemon_address, uint16_t myco_daemon_port) {
- int size = 1024;
- char *message = malloc (size);
- char *buffer = malloc(50);
- strcat (message, "REGISTER: ");
- strcat (message, myco_daemon_address);
- sprintf (buffer, ":%d", myco_daemon_port);
- strcat (message, buffer);
- sprintf (buffer, " %d", pid);
- strcat (message, buffer);
- if (myco_send(message, myco_daemon_address, myco_daemon_port) != 0) {
- return -1;
- }
- return 0;
- }
- int myco_agent_unregister (int pid, const char *myco_daemon_address, uint16_t myco_daemon_port) {
- int size = 1024;
- char *message = malloc (size);
- char *buffer = malloc(50);
- strcat (message, "UNREGISTER: ");
- strcat (message, myco_daemon_address);
- sprintf (buffer, ":%d", myco_daemon_port);
- strcat (message, buffer);
- sprintf (buffer, " %d", pid);
- strcat (message, buffer);
- if (myco_send(message, myco_daemon_address, myco_daemon_port) != 0) {
- return -1;
- }
- return 0;
- }
- int myco_agent_register_resource (int pid, const char *myco_daemon_address, uint16_t myco_daemon_port, const char *resource_name) {
- int size = 1024;
- char *message = malloc (size);
- char *buffer = malloc(50);
- strcat (message, "RESOURCE: ");
- strcat (message, myco_daemon_address);
- sprintf (buffer, ":%d", myco_daemon_port);
- strcat (message, buffer);
- sprintf (buffer, " %d ", pid);
- strcat (message, buffer);
- strcat (message, resource_name);
- if (myco_send(message, myco_daemon_address, myco_daemon_port) != 0) {
- return -1;
- }
- return myco_recv(message, myco_daemon_address, myco_daemon_port);
- }
- /**
- * myco_agent_request_resource:
- * @pid: The PID of the user program.
- * @myco_daemon_address: The IP address of the daemon (usually localhost).
- * @myco_daemon_port: The port of the daemon.
- * @resource_id: The unique resource id.
- * @resource_pointer: Pointer to the local memory region where to write the resource to.
- * @resource_size: Size in Bytes the resource takes.
- * @resource_name: Variable for resource name, filled by daemon.
- *
- * Requests a resource from the daemon. If size equals actual resource size transfer is
- * done immediatly. This only works if resource size is known beforehand. If resource
- * size is unknown check with myco_agent_request_resource_info();
- *
- * Returns: -1 on error, 0 on success
- */
- 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);
- /**
- * myco_agent_request_resource_info:
- * @pid: The PID of the user program.
- * @myco_daemon_address: The IP address of the daemon (usually localhost).
- * @myco_daemon_port: The port of the daemon.
- * @resource_id: The unique resource id.
- * @resource_size: Size in Bytes the resource takes.
- * @resource_name: Variable for resource name, filled by daemon.
- *
- * Requests resource information from the daemon.
- *
- * Returns: -1 on error, 0 on success
- */
- 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);
- /**
- * myco_agent_request_resource_list:
- * @pid: The PID of the user program.
- * @myco_daemon_address: The IP address of the daemon (usually localhost).
- * @myco_daemon_port: The port of the daemon.
- * @resource_list: A comma seperated list of resources (id, name, size).
- *
- * Requests resource list. Obtains a list of all resources in the current network.
- *
- * Returns: -1 on error, 0 on success
- */
- int myco_agent_request_resource_list(int pid, const char *myco_daemon_address, uint16_t myco_daemon_port, char *resource_list);
|