fix if statements and sds publishing
[mTask.git] / int / main.c
index 78f44bb..886170e 100644 (file)
@@ -1,3 +1,4 @@
+#define _BSD_SOURCE
 #include <netdb.h>
 #include <netinet/in.h>
 #include <signal.h>
@@ -6,6 +7,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <strings.h>
 #include <sys/socket.h>
 #include <sys/types.h>
 #include <sys/time.h>
@@ -31,7 +33,7 @@ int port = 8123;
 
 void killHandler(int i)
 {
-       printf("%s caught, Bye...\n", strsignal(i));
+       printf("%i caught, Bye...\n", i);
        exit(1);
 }
 
@@ -77,11 +79,18 @@ void read_message(int fd_in, int fd_out)
        case MSG_GET_TASK:
                debug("Receiving a task\n");
                c = task_register(fd_in);
-               write(fd_out, &c, 1);
+//             write(fd_out, &c, 1);
+//             write(fd_out, 
                break;
+       case '\n':
+               break;
+//     case '\0':
+//             debug("iTasks server shut down\n");
+//             exit(EXIT_SUCCESS);
        default:
-               debug("Unknown message: %X?\n", c);
+               debug("Unknown message: %X\n", c);
        }
+       (void) fd_out;
 }
 
 void open_filedescriptors()
@@ -96,9 +105,9 @@ void open_filedescriptors()
        if((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
                pdie("socket");
        if(bind(sock_fd, (struct sockaddr*)&sa, sizeof(sa)) == -1)
-               pdie("bind\n");
+               pdie("bind");
        if(listen(sock_fd, 10) == -1)
-               pdie("bind\n");
+               pdie("listen");
 
        printf("Listening on %d\n", port);
        fflush(stdout);
@@ -128,25 +137,24 @@ int main(int argc, char *argv[])
        }
 
        //Command line arguments
-       int opt;
-       while((opt = getopt(argc, argv, "hp:")) != -1){
-               switch(opt){
-                       case 'p':
-                               port = atoi(optarg);
-                               if(port < 1)
-                                       die("Port numbers are > 1\n");
-                               break;
-                       case 'h':
-                               usage(stdout, argv[0]);
-                               exit(EXIT_SUCCESS);
-                       default:
-                               usage(stderr, argv[0]);
-                               exit(EXIT_FAILURE);
+       int opti = 1;
+       while(opti < argc){
+               if(strcmp(argv[opti], "-h") == 0){
+                       usage(stdout, argv[0]);
+                       exit(EXIT_SUCCESS);
+               } else if(strcmp(argv[opti], "-p") == 0 && opti+1<argc){
+                       port = atoi(argv[++opti]);
+                       if(port < 1)
+                               die("Port numbers are > 1\n");
+               } else {
+                       usage(stderr, argv[0]);
+                       exit(EXIT_FAILURE);
                }
-
+               opti++;
        }
 
        open_filedescriptors();
+       write(fd, "\n", 1);
 
        long cyclestart;
        struct task *curtask;
@@ -159,21 +167,24 @@ int main(int argc, char *argv[])
                for(ct = 0; ct<MAXTASKS; ct++){
                        //See whether the task is even in use
                        if((curtask = task_get(ct)) == NULL){
-                               debug("Task %d not implemented\n", ct);
+//                             debug("Task %d not implemented\n", ct);
                                continue;
                        }
                        //See whether the task interval has passed
                        if(cyclestart-curtask->lastrun < curtask->interval){
-                               debug("Task %d not scheduled\n", ct);
+//                             debug("Task %d not scheduled\n", ct);
                                continue;
                        }
 #ifdef DEBUG
                        printf("Current task to run: %d\n", ct);
                        getchar();
 #endif
-                       run_task(curtask);
+                       run_task(curtask, fd);
                }
-               usleep(10);
+               debug("Waiting for 500ms\n");
+               usleep(500000);
+               debug("done waiting\n");
+               write(fd, "\n", 1);
        }
        return 0;
 }