Socket added for tracking. Bugs fixed with IP address not properly set in sockets
authorFrancoisGrondin <francois.grondin2@usherbrooke.ca>
Wed, 16 Aug 2017 16:34:42 +0000 (12:34 -0400)
committerFrancoisGrondin <francois.grondin2@usherbrooke.ca>
Wed, 16 Aug 2017 16:34:42 +0000 (12:34 -0400)
src/sink/snk_pots.c
src/sink/snk_tracks.c
src/sink/snk_tracks.h

index 4de81896137c1f45c9621d105532c19224b13daf..c6ccd491d9823e3d7c28ea090196bcfb769275bf 100644 (file)
 
             case interface_socket:
         
+                memset(&(obj->sclient), 0x00, sizeof(struct sockaddr_in));
+
                 obj->sclient.sin_family = AF_INET;
+                obj->sclient.sin_addr.s_addr = inet_addr(obj->interface->ip);
                 obj->sclient.sin_port = htons(obj->interface->port);
                 obj->sid = socket(AF_INET, SOCK_STREAM, 0);
 
index bc5ced99d0323818c8559c78b39bb1620985d62e..9b850adde3fd4d6d3fe27615e6a19b4d1ad3aa42 100644 (file)
@@ -17,7 +17,7 @@
 
         switch (obj->format->type) {
             
-            case format_float: break;
+            case format_json: break;
             default:
 
                 printf("Invalid format.\n");
 
         }
 
+        obj->fp = (FILE *) NULL;
+
+        obj->smessage = (char *) malloc(sizeof(char) * (1024 + 1024 * msg_tracks_config->nTracks));
+
         obj->in = (msg_tracks_obj *) NULL;
 
         return obj;
@@ -35,6 +39,8 @@
 
     void snk_tracks_destroy(snk_tracks_obj * obj) {
 
+        free((void *) obj->smessage);
+
         format_destroy(obj->format);
         interface_destroy(obj->interface);
 
 
             case interface_socket:
 
-                printf("Not implemented yet.\n");
-                exit(EXIT_FAILURE);
+                memset(&(obj->sclient), 0x00, sizeof(struct sockaddr_in));
+
+                obj->sclient.sin_family = AF_INET;
+                obj->sclient.sin_addr.s_addr = inet_addr(obj->interface->ip);
+                obj->sclient.sin_port = htons(obj->interface->port);
+                obj->sid = socket(AF_INET, SOCK_STREAM, 0);
+
+                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_tracks_process_socket(snk_tracks_obj * obj) {
 
-        printf("Not implemented\n");
-        exit(EXIT_FAILURE);
+        int rtnValue;
+        unsigned int iTrack;
+
+        if (obj->in->timeStamp != 0) {
+
+            switch(obj->format->type) {
+
+                case format_json:
+
+                    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 (iTrack = 0; iTrack < obj->nTracks; iTrack++) {
+
+                        sprintf(obj->smessage,"%s        { \"id\": %llu, \"x\": %1.3f, \"y\": %1.3f, \"z\": %1.3f }", obj->smessage, 
+                                obj->in->tracks->ids[iTrack],
+                                obj->in->tracks->array[iTrack*3+0], obj->in->tracks->array[iTrack*3+1], obj->in->tracks->array[iTrack*3+2]);
+
+                        if (iTrack != (obj->nTracks - 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 6d9a907a40d156c78b3ce36b89edd764558997cb..c0ee30acaf7c3ca67b5912f173e75407cbc0b75a 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_tracks_obj * in;
 
     } snk_tracks_obj;