Socket added to the sink of potential sources
authorFrancoisGrondin <francois.grondin2@usherbrooke.ca>
Tue, 1 Aug 2017 15:28:24 +0000 (11:28 -0400)
committerFrancoisGrondin <francois.grondin2@usherbrooke.ca>
Tue, 1 Aug 2017 15:28:24 +0000 (11:28 -0400)
CMakeLists.txt
demo/server.c [new file with mode: 0644]
src/general/interface.c
src/sink/snk_pots.c
src/sink/snk_pots.h

index 708b5a24f587c4ba413e907febb427f66928a8b7..2cc02fc4d6d650a3f2fd562bdce0d4eafaf81a12 100644 (file)
@@ -169,3 +169,7 @@ add_executable(core
 target_link_libraries(core
     odas
 )
+
+add_executable(server
+    demo/server.c
+)
diff --git a/demo/server.c b/demo/server.c
new file mode 100644 (file)
index 0000000..683d473
--- /dev/null
@@ -0,0 +1,53 @@
+    #include <stdlib.h>
+    #include <stdio.h>
+    #include <string.h>
+    #include <sys/socket.h>
+    #include <netinet/in.h>
+    #include <arpa/inet.h>
+
+    int main(int argc, char* argv[])
+    {
+
+        int server_id;
+        struct sockaddr_in server_address;
+        int connection_id;
+        char * message;
+        int messageSize;
+
+        if (argc != 2) {
+
+            printf("Expecting one argument\n");
+            exit(EXIT_FAILURE);
+        }
+
+        server_id = socket(AF_INET, SOCK_STREAM, 0);
+
+        server_address.sin_family = AF_INET;
+        server_address.sin_addr.s_addr = htonl(INADDR_ANY);
+        server_address.sin_port = htons(atoi(argv[1]));
+
+        printf("Binding socket... ");  fflush(stdout);
+        bind(server_id, (struct sockaddr *) &server_address, sizeof(server_address));
+        printf("[OK]\n");
+
+        printf("Listening socket... ");  fflush(stdout);
+        listen(server_id, 1);
+        printf("[OK]\n");
+
+        printf("Waiting for connection..."); fflush(stdout);
+        connection_id = accept(server_id, (struct sockaddr*) NULL, NULL);
+        printf("[OK]\n");
+
+        message = (char *) malloc(sizeof(char) * 1024 * 10);
+
+        while( (messageSize = recv(connection_id, message, 1024 * 10, 0)) > 0) {
+
+            message[messageSize] = 0x00;
+
+            printf("Received (%u): %s\n",messageSize,message);
+
+        }
+
+        printf("messageSize = %d\n",messageSize);
+
+    }
\ No newline at end of file
index 697f52ddabb3285181c87c175a547098f9105e7c..1f010cf292f9664894b615aba70edcf3cb2afc46 100644 (file)
 
             if (obj->type == interface_socket) {
                 
-                clone->ip = (char *) malloc(sizeof(char) * (strlen(obj->fileName) + 1));
+                clone->ip = (char *) malloc(sizeof(char) * (strlen(obj->ip) + 1));
                 strcpy(clone->ip, obj->ip);
                 clone->port = obj->port;
 
index 1772ee6e42efecf4040499bbd6ab10968eda5656..150d0a3410dacc4f4319bad06398916f029ba5c9 100644 (file)
 
         }
 
+        obj->fp = (FILE *) NULL;
+
+        obj->smessage = (char *) malloc(sizeof(char) * (1024 + 1024 * msg_pots_config->nPots));
+
         obj->in = (msg_pots_obj *) NULL;
 
         return obj;
@@ -35,6 +39,8 @@
 
     void snk_pots_destroy(snk_pots_obj * obj) {
 
+        free((void *) obj->smessage);
+
         format_destroy(obj->format);
         interface_destroy(obj->interface);
 
             break;
 
             case interface_socket:
+        
+                obj->sclient.sin_family = AF_INET;
+                obj->sclient.sin_port = htons(obj->interface->port);
+                obj->sid = socket(AF_INET, SOCK_STREAM, 0);
 
-                printf("Not implemented yet.\n");
-                exit(EXIT_FAILURE);
+                if ( (connect(obj->sid, (struct sockaddr *) &(obj->sclient), sizeof(obj->sclient))) < 0 ) {
+
+                    printf("Cannot connect to server\n");
+                    exit(EXIT_FAILURE);
+
+                }                
 
             break;
 
 
             case interface_socket:
 
-                printf("Not implemented yet.\n");
-                exit(EXIT_FAILURE);
+                close(obj->sid);
 
             break;
 
 
     int snk_pots_process_socket(snk_pots_obj * obj) {
 
-        printf("Not implemented\n");
-        exit(EXIT_FAILURE);
+        int rtnValue;
+        unsigned int iPot;
+
+        if (obj->in->timeStamp != 0) {
+
+            switch(obj->format->type) {
+
+                case format_float:
+
+                    obj->smessage[0] = 0x00;
+
+                    sprintf(obj->smessage,"%s{\n",obj->smessage);
+                    sprintf(obj->smessage,"%s    \"timeStamp\": %llu,\n",obj->smessage,obj->in->timeStamp);
+                    sprintf(obj->smessage,"%s    \"src\": [\n",obj->smessage);
+
+                    for (iPot = 0; iPot < obj->nPots; iPot++) {
+
+                        sprintf(obj->smessage,"%s        { x = %+1.3f, y = %+1.3f, z = %+1.3f, E = %+1.3f }", obj->smessage, 
+                                obj->in->pots->array[iPot*4+0], obj->in->pots->array[iPot*4+1], obj->in->pots->array[iPot*4+2], obj->in->pots->array[iPot*4+3]);
+
+                        if (iPot != (obj->nPots - 1)) {
+
+                            sprintf(obj->smessage,"%s,",obj->smessage);
+
+                        }
+
+                        sprintf(obj->smessage,"%s\n",obj->smessage);
+
+                    }
+                    
+                    sprintf(obj->smessage,"%s    ]\n",obj->smessage);
+                    sprintf(obj->smessage,"%s}\n",obj->smessage);
+
+                    if (send(obj->sid, obj->smessage, strlen(obj->smessage), 0) < 0) {
+                        printf("Could not send message.\n");
+                        exit(EXIT_FAILURE);
+                    }
+
+                    rtnValue = 0;
+
+                break;               
+
+            }
+
+        }
+        else {
+
+            rtnValue = -1;
+
+        }
+
+        return rtnValue;
 
     }
 
index 0582ffd6a6b645e294cf0d79d05b0e9a5c28d6c1..706e95dea85e35e5176b089b5485ed1ad4a861d5 100644 (file)
@@ -4,6 +4,10 @@
     #include <stdlib.h>
     #include <stdio.h>
     #include <string.h>
+    #include <sys/socket.h>
+    #include <netinet/in.h>
+    #include <arpa/inet.h>
+    #include <unistd.h>
 
     #include "../general/format.h"
     #include "../general/interface.h"
 
         FILE * fp;
 
+        char * smessage;
+        struct sockaddr_in sclient;
+        int sid;
+
         msg_pots_obj * in;
 
     } snk_pots_obj;