|
@@ -6,50 +6,328 @@
|
|
|
#include <stdlib.h>
|
|
|
#include <stdio.h>
|
|
|
#include <string.h>
|
|
|
+#include "myco-indexer.h"
|
|
|
+
|
|
|
+myco_agent *first_agent = NULL;
|
|
|
+myco_resource *first_resource = NULL;
|
|
|
+
|
|
|
+myco_agent *myco_indexer_find_agent(const char *agent_name) {
|
|
|
+ myco_agent *current_agent;
|
|
|
+ current_agent = first_agent;
|
|
|
+ while (current_agent != NULL) {
|
|
|
+ if (strcmp(current_agent->name, agent_name) == 0) {
|
|
|
+ return current_agent;
|
|
|
+ }
|
|
|
+ current_agent = current_agent->next;
|
|
|
+ }
|
|
|
+ return NULL;
|
|
|
+}
|
|
|
+
|
|
|
+myco_resource *myco_indexer_find_resource_by_agent(const char *agent_name) {
|
|
|
+ myco_resource *current_resource;
|
|
|
+ current_resource = first_resource;
|
|
|
+ while (current_resource != NULL) {
|
|
|
+ if (strcmp(current_resource->agent, agent_name) == 0) {
|
|
|
+ return current_resource;
|
|
|
+ }
|
|
|
+ current_resource = current_resource->next;
|
|
|
+ }
|
|
|
+ return NULL;
|
|
|
+}
|
|
|
+
|
|
|
+myco_resource *myco_indexer_find_resource(const char *resource_name) {
|
|
|
+ myco_resource *current_resource;
|
|
|
+ current_resource = first_resource;
|
|
|
+ while (current_resource != NULL) {
|
|
|
+ if (strcmp(current_resource->name, resource_name) == 0) {
|
|
|
+ return current_resource;
|
|
|
+ }
|
|
|
+ current_resource = current_resource->next;
|
|
|
+ }
|
|
|
+ return NULL;
|
|
|
+}
|
|
|
+
|
|
|
+int myco_indexer_register_agent(command, agent, resource){
|
|
|
+ myco_agent *current_agent;
|
|
|
+
|
|
|
+ if (myco_indexer_find_agent(agent) != NULL) {
|
|
|
+ printf("ERROR: agent %s already exists\n", agent);
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (first_agent == NULL) {
|
|
|
+ first_agent = malloc(sizeof(myco_agent));
|
|
|
+ first_agent->next = NULL;
|
|
|
+ first_agent->prev = NULL;
|
|
|
+ sprintf(first_agent->name, "%s", agent);
|
|
|
+ } else {
|
|
|
+ current_agent = first_agent;
|
|
|
+ while (current_agent->next != NULL) {
|
|
|
+ current_agent = current_agent->next;
|
|
|
+ }
|
|
|
+ current_agent->next = malloc(sizeof(myco_agent));
|
|
|
+ current_agent->next->prev = current_agent;
|
|
|
+ current_agent->next->next = NULL;
|
|
|
+ sprintf(current_agent->next->name, "%s", agent);
|
|
|
+ }
|
|
|
+
|
|
|
+ printf("SUCCESS: agent %s registered\n", agent);
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+int myco_indexer_unregister_agent(command, agent, resource){
|
|
|
+ myco_agent *current_agent;
|
|
|
+ myco_resource *current_resource;
|
|
|
+
|
|
|
+ current_agent = myco_indexer_find_agent(agent);
|
|
|
+
|
|
|
+ if ((current_resource = myco_indexer_find_resource_by_agent(agent)) != NULL) {
|
|
|
+ printf("ERROR: agent %s still has resource %s\n", agent, current_resource->name);
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (current_agent == NULL) {
|
|
|
+ printf("ERROR: agent %s could not be unregistered\n", agent);
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ // Element in the middle
|
|
|
+ if (current_agent->next != NULL && current_agent->prev != NULL) {
|
|
|
+ current_agent->prev->next = current_agent->next;
|
|
|
+ current_agent->next->prev = current_agent->prev;
|
|
|
+ free(current_agent);
|
|
|
+ printf("SUCCESS: agent %s unregistered\n", agent);
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ // First element
|
|
|
+ if (current_agent->next != NULL && current_agent->prev == NULL) {
|
|
|
+ first_agent = current_agent->next;
|
|
|
+ current_agent->next->prev = NULL;
|
|
|
+ free(current_agent);
|
|
|
+ printf("SUCCESS: agent %s unregistered\n", agent);
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ // Last element
|
|
|
+ if (current_agent->next == NULL && current_agent->prev != NULL) {
|
|
|
+ current_agent->prev->next = NULL;
|
|
|
+ free(current_agent);
|
|
|
+ printf("SUCCESS: agent %s unregistered\n", agent);
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ // Only remaining
|
|
|
+ if (current_agent->next == NULL && current_agent->prev == NULL) {
|
|
|
+ first_agent = NULL;
|
|
|
+ free(current_agent);
|
|
|
+ printf("SUCCESS: agent %s unregistered\n", agent);
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ printf("ERROR: agent %s could not be unregistered - data structure seems to be damaged!\n", agent);
|
|
|
+
|
|
|
+ return -1;
|
|
|
+}
|
|
|
+
|
|
|
+int myco_indexer_register_resource(command, agent, resource){
|
|
|
+ myco_resource *current_resource;
|
|
|
+
|
|
|
+ if (myco_indexer_find_agent(agent) == NULL) {
|
|
|
+ printf("ERROR: agent %s does not exist\n", agent);
|
|
|
+
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (myco_indexer_find_resource(resource) != NULL) {
|
|
|
+ printf("ERROR: resource %s already exists\n", resource);
|
|
|
+
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ // Insert as first element
|
|
|
+ if (first_resource == NULL) {
|
|
|
+ first_resource = malloc(sizeof(myco_resource));
|
|
|
+ first_resource->next = NULL;
|
|
|
+ first_resource->prev = NULL;
|
|
|
+ sprintf(first_resource->name, "%s", resource);
|
|
|
+ sprintf(first_resource->agent, "%s", agent);
|
|
|
+ } else {
|
|
|
+ // Insert as last element
|
|
|
+ current_resource = first_resource;
|
|
|
+ while (current_resource->next != NULL) {
|
|
|
+ current_resource = current_resource->next;
|
|
|
+ }
|
|
|
+ current_resource->next = malloc(sizeof(myco_resource));
|
|
|
+ current_resource->next->prev = current_resource;
|
|
|
+ current_resource->next->next = NULL;
|
|
|
+ sprintf(current_resource->next->name, "%s", resource);
|
|
|
+ sprintf(current_resource->next->agent, "%s", agent);
|
|
|
+ }
|
|
|
+
|
|
|
+ printf("SUCCESS: resource %s registered\n", resource);
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+int myco_indexer_unregister_resource(command, agent, resource){
|
|
|
+ myco_resource *current_resource;
|
|
|
+
|
|
|
+ current_resource = myco_indexer_find_resource(resource);
|
|
|
+
|
|
|
+ if (current_resource == NULL) {
|
|
|
+ printf("ERROR: resource %s could not be unregistered\n", resource);
|
|
|
+
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ // Element in the middle
|
|
|
+ if (current_resource->next != NULL && current_resource->prev != NULL) {
|
|
|
+ current_resource->prev->next = current_resource->next;
|
|
|
+ current_resource->next->prev = current_resource->prev;
|
|
|
+ free(current_resource);
|
|
|
+ printf("SUCCESS: resource %s unregistered\n", resource);
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ // First element
|
|
|
+ if (current_resource->next != NULL && current_resource->prev == NULL) {
|
|
|
+ first_resource = current_resource->next;
|
|
|
+ current_resource->next->prev = NULL;
|
|
|
+ free(current_resource);
|
|
|
+ printf("SUCCESS: resource %s unregistered\n", resource);
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ // Last element
|
|
|
+ if (current_resource->next == NULL && current_resource->prev != NULL) {
|
|
|
+ current_resource->prev->next = NULL;
|
|
|
+ free(current_resource);
|
|
|
+ printf("SUCCESS: resource %s unregistered\n", resource);
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ // Only remaining
|
|
|
+ if (current_resource->next == NULL && current_resource->prev == NULL) {
|
|
|
+ first_resource = NULL;
|
|
|
+ free(current_resource);
|
|
|
+ printf("SUCCESS: resource %s unregistered\n", resource);
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ printf("FATAL ERROR: resource %s could not be unregistered - data structure seems to be damaged!\n", resource);
|
|
|
+
|
|
|
+ return -1;
|
|
|
+}
|
|
|
+
|
|
|
+int myco_indexer_request_resource(command, agent, resource){
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+int myco_indexer_write_remote_resource(command, agent, resource){
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+int myco_indexer_read_remote_resource(command, agent, resource){
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+int myco_indexer_lock_resource(command, agent, resource){
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+int myco_indexer_unlock_resource(command, agent, resource){
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+int myco_indexer_release_resource(command, agent, resource){
|
|
|
+
|
|
|
+}
|
|
|
|
|
|
#define BUF 1024
|
|
|
int myco_indexer_start(int port) {
|
|
|
- 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 (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);
|
|
|
- while (1) {
|
|
|
- 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));
|
|
|
- do {
|
|
|
- /*
|
|
|
- printf ("Nachricht zum Versenden: ");
|
|
|
- fgets (buffer, BUF, stdin);
|
|
|
- send (new_socket, buffer, strlen (buffer), 0);
|
|
|
- */
|
|
|
- size = recv (new_socket, buffer, BUF-1, 0);
|
|
|
- if( size > 0)
|
|
|
- buffer[size] = '\0';
|
|
|
- printf ("Nachricht empfangen: %s\n", buffer);
|
|
|
- } while (strcmp (buffer, "quit\n") != 0);
|
|
|
- close (new_socket);
|
|
|
- }
|
|
|
- close (create_socket);
|
|
|
- return EXIT_SUCCESS;
|
|
|
+ int create_socket, new_socket;
|
|
|
+ socklen_t addrlen;
|
|
|
+ char *buffer = malloc (BUF);
|
|
|
+ ssize_t size;
|
|
|
+ struct sockaddr_in address;
|
|
|
+ const int y = 1;
|
|
|
+ char *command;
|
|
|
+ char *agent;
|
|
|
+ char *resource;
|
|
|
+
|
|
|
+ printf ("\e[2J");
|
|
|
+ create_socket=socket (AF_INET, SOCK_STREAM, 0);
|
|
|
+ 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 (port);
|
|
|
+ if (bind ( create_socket,
|
|
|
+ (struct sockaddr *) &address,
|
|
|
+ sizeof (address)) != 0) {
|
|
|
+ printf( "Port already taken!\n");
|
|
|
+ }
|
|
|
+ listen (create_socket, 5);
|
|
|
+ addrlen = sizeof (struct sockaddr_in);
|
|
|
+ while (1) {
|
|
|
+ new_socket = accept ( create_socket,
|
|
|
+ (struct sockaddr *) &address,
|
|
|
+ &addrlen );
|
|
|
+ if (new_socket > 0)
|
|
|
+ inet_ntoa (address.sin_addr);
|
|
|
+ do {
|
|
|
+ /*
|
|
|
+ printf ("Nachricht zum Versenden: ");
|
|
|
+ fgets (buffer, BUF, stdin);
|
|
|
+ send (new_socket, buffer, strlen (buffer), 0);
|
|
|
+ */
|
|
|
+ size = recv (new_socket, buffer, BUF-1, 0);
|
|
|
+ if( size > 0)
|
|
|
+ buffer[size] = '\0';
|
|
|
+
|
|
|
+ // Handle agents
|
|
|
+ command = strtok(buffer, ";");
|
|
|
+ agent = strtok(NULL, ";");
|
|
|
+ resource = strtok(NULL, ";");
|
|
|
+
|
|
|
+ if (strcmp(command, "REGISTER AGENT") == 0) {
|
|
|
+ myco_indexer_register_agent(command, agent, resource);
|
|
|
+ }
|
|
|
+ if (strcmp(command, "UNREGISTER AGENT") == 0) {
|
|
|
+ myco_indexer_unregister_agent(command, agent, resource);
|
|
|
+ }
|
|
|
+ if (strcmp(command, "REGISTER RESOURCE") == 0) {
|
|
|
+ myco_indexer_register_resource(command, agent, resource);
|
|
|
+ }
|
|
|
+ if (strcmp(command, "UNREGISTER RESOURCE") == 0) {
|
|
|
+ myco_indexer_unregister_resource(command, agent, resource);
|
|
|
+ }
|
|
|
+ if (strcmp(command, "REQUEST RESOURCE") == 0) {
|
|
|
+ myco_indexer_request_resource(command, agent, resource);
|
|
|
+ }
|
|
|
+ if (strcmp(command, "WRITE REMOTE RESOURCE") == 0) {
|
|
|
+ myco_indexer_write_remote_resource(command, agent, resource, 0);
|
|
|
+ }
|
|
|
+ if (strcmp(command, "FORCE WRITE REMOTE RESOURCE") == 0) {
|
|
|
+ myco_indexer_write_remote_resource(command, agent, resource, 1);
|
|
|
+ }
|
|
|
+ if (strcmp(command, "READ REMOTE RESOURCE") == 0) {
|
|
|
+ myco_indexer_read_remote_resource(command, agent, resource);
|
|
|
+ }
|
|
|
+ if (strcmp(command, "LOCK RESOURCE") == 0) {
|
|
|
+ myco_indexer_lock_resource(command, agent, resource);
|
|
|
+ }
|
|
|
+ if (strcmp(command, "UNLOCK RESOURCE") == 0) {
|
|
|
+ myco_indexer_unlock_resource(command, agent, resource);
|
|
|
+ }
|
|
|
+ if (strcmp(command, "RELEASE RESOURCE") == 0) {
|
|
|
+ myco_indexer_release_resource(command, agent, resource);
|
|
|
+ }
|
|
|
+ } while (strcmp (command, "quit\n") != 0);
|
|
|
+ close (new_socket);
|
|
|
+ }
|
|
|
+ close (create_socket);
|
|
|
+ return EXIT_SUCCESS;
|
|
|
}
|