123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328 |
- #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-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;
- 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 {
- 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;
- }
|