Browse Source

basic tcp connection established

max 7 years ago
parent
commit
5be6e687c3
6 changed files with 106 additions and 1 deletions
  1. 1 0
      CMakeLists.txt
  2. 26 0
      src/myco-daemon.c
  3. 4 0
      src/myco-daemon.h
  4. 55 0
      src/myco-indexer.c
  5. 1 1
      test/mycodaemon.c
  6. 19 0
      test/mycoindexer.c

+ 1 - 0
CMakeLists.txt

@@ -7,4 +7,5 @@ add_executable(mycoagent test/mycoagent.c)
 add_executable(mycoagent2 test/mycoagent2.c)
 add_executable(mycoagent3 test/mycoagent3.c)
 add_executable(mycodaemon test/mycodaemon.c)
+add_executable(mycoindexer test/mycoindexer.c)
 #target_link_libraries(mycoagent rt)

+ 26 - 0
src/myco-daemon.c

@@ -506,13 +506,27 @@ int myco_daemon_unlock_resource(message msg) {
         myco_send(msg.agent_message_queue_id, msg);
         return -1;
     }
+}
+
+int myco_daemon_connect(char* ip, int port) {
+    struct sockaddr_in address;
+    int mysocket = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
+    address.sin_family = AF_INET;
+    address.sin_port = htons (port);
+    inet_aton (ip, &address.sin_addr);
+    connect (mysocket, (struct sockaddr *) &address, sizeof (address));
 
+    return mysocket;
 }
 
 int myco_daemon_start(pid) {
+    char *buffer = malloc (1024);
     int daemon_message_queue_id;
     message msg = {0};
 
+    // Connect to indexer
+    int socket = myco_daemon_connect("127.0.0.1", 15000);
+
     // Create message queue
     daemon_message_queue_id = myco_create_global_message_queue();
     if (daemon_message_queue_id < 0) {
@@ -522,6 +536,13 @@ int myco_daemon_start(pid) {
 
     // Receive messages
     while (1) {
+        // Receive messages from indexer
+        int size = recv (socket, buffer, 1023, 0);
+        if( size > 0)
+           buffer[size] = '\0';
+        printf ("%s\n", buffer);
+
+        // Receive messages from agents
         msg = myco_receive(daemon_message_queue_id);
         if (DEBUG) {
             printf("%d, %s, %s, %s, %p, %d, %d, %d, %d\n", msg.agent_message_queue_id, msg.agent_name, msg.message, \
@@ -531,6 +552,9 @@ int myco_daemon_start(pid) {
             fprintf(stderr, "FATAL ERROR: No message could be received. %s\n", strerror(errno));
             return -1;
         } else {
+            // Handle indexer
+            send(socket, msg.message, strlen(msg.message), 0);
+            // Handle agents
             if (strcmp(msg.message, "REGISTER AGENT") == 0) {
                 myco_daemon_register_agent(msg);
             }
@@ -570,6 +594,8 @@ int myco_daemon_start(pid) {
         } 
     }
 
+    close (socket);
+
     if (myco_remove_message_queue(daemon_message_queue_id) == -1) {
         fprintf(stderr, "FATAL ERROR: could not remove message queue %d\n", daemon_message_queue_id);
         return -1;

+ 4 - 0
src/myco-daemon.h

@@ -31,6 +31,10 @@
 #include <unistd.h>
 #include <errno.h>
 #include <string.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <fcntl.h>
 #include "myco-ipc.c"
 
 

+ 55 - 0
src/myco-indexer.c

@@ -0,0 +1,55 @@
+#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>
+
+#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;
+}

+ 1 - 1
test/mycodaemon.c

@@ -5,7 +5,7 @@
 #include "../src/myco-daemon.c"
 
 
-// make into daemon later
+// TODO: make into daemon later
 int main() {
 
 

+ 19 - 0
test/mycoindexer.c

@@ -0,0 +1,19 @@
+/*
+ *  mycoindexer.c
+ */
+#include "../src/myco-indexer.c"
+
+
+int main (int argc, char **argv) {
+
+    int port;
+    //port = argv[0];
+    port = 15000;
+
+    if (myco_indexer_start(port) == -1) {
+        return EXIT_FAILURE;
+    }
+
+    return EXIT_SUCCESS;
+
+}