infrastructuur voor het lezen van taken
authorMart Lubbers <mart@martlubbers.net>
Fri, 18 Nov 2016 18:43:24 +0000 (19:43 +0100)
committerMart Lubbers <mart@martlubbers.net>
Fri, 18 Nov 2016 18:43:24 +0000 (19:43 +0100)
int/int.c

index 6ef8cc4..2bf2cef 100644 (file)
--- a/int/int.c
+++ b/int/int.c
@@ -24,6 +24,7 @@
 #define die(s, ...) {fprintf(stderr, s, ##__VA_ARGS__); exit(1);}
 
 char tasks[MAXTASKS][MAXTASKSIZE] = {0};
+bool tasks_used[MAXTASKS] = {0};
 
 void killHandler(int i)
 {
@@ -44,15 +45,58 @@ bool input_available(int fd){
        return FD_ISSET(fd, &fds);
 }
 
-int main(void)
+void read_message(int fd_in, int fd_out)
 {
+       //Find next task
        uint8_t c;
-//     char taskindex = 0;
-//     int pl, sp, pc;
-//
-       int fd_in = 0;
-       int next_free_new_task = 0;
-       uint16_t tasklen = 0;
+       uint16_t tasklen;
+       uint8_t ct;
+
+       for(ct = 0; ct<MAXTASKS; ct++)
+               if(!tasks_used[ct])
+                       break;
+       if(ct == MAXTASKS)
+               die("Trying to add too much tasks...\n");
+
+       debug("Receiving input for task %d\n", ct);
+       read(fd_in, &c, 1);
+       if((char) c == 's') {
+               debug("Receiving an sds\n");
+       } else if((char) c == 't'){
+               read(fd_in, &c, 1);
+               tasklen = 256*c;
+               read(fd_in, &c, 1);
+               tasklen += c;
+               if(tasklen > MAXTASKSIZE)
+                       die("Task is too long: %d\n", tasklen);
+               for(int i = 0; i<tasklen; i++){
+                       debug("Read %d\n", i);
+                       read(fd_in, tasks[ct]+i, 1);
+                       debug("t[][%i]: %d\n", i,
+                               tasks[ct][i]);
+               }
+               //Return the task number for later removal
+               write(fd_out, (void *)&ct, 1);
+               debug("Received a task of length %d\n", tasklen);
+               tasks_used[ct] = true;
+       } else {
+               die("Unknown message: %c?\n", c);
+       }
+}
+
+void run_task(char *program)
+{
+
+       (void) program;
+}
+
+int main(void)
+{
+       int fd_in, fd_out;
+       int ct;
+
+       fd_in = fileno(stdin);
+       fd_out = fileno(stdout);
 
        //Register signal handler
        if(signal(SIGINT, killHandler) == SIG_ERR){
@@ -63,32 +107,21 @@ int main(void)
        }
 
        while(true){
+               //Check for new tasks
                if(input_available(fd_in)){
-                       printf("Receiving input\n");
-                       read(fd_in, &c, 1);
-                       if((char) c == 's'){
-                               debug("Receiving an sds\n");
-                       } else if((char) c == 't'){
-                               read(fd_in, &c, 1);
-                               tasklen = 256*c;
-                               read(fd_in, &c, 1);
-                               tasklen += c;
-                               if(tasklen > MAXTASKSIZE){
-                                       die("Task is too long: %d\n", tasklen);
-                               }
-                               for(int i = 0; i<tasklen; i++){
-                                       debug("Read %d\n", i);
-                                       read(fd_in, tasks[next_free_new_task]+i, 1);
-                                       read(fd_in, tasks[next_free_new_task]+i, 1);
-                                       debug("t[][%i]: %d\n", i, tasks[next_free_new_task][i]);
-                               }
-                               debug("Receiving a task of length %d\n", tasklen);
-                       } else {
-                               die("Unknown message: %c?\n", c);
-                       }
+                       read_message(fd_in, fd_out);
                        exit(1);
                }
-               usleep(1);
+               //Run tasks
+               for(ct = 0; ct<MAXTASKS; ct++){
+                       if(!tasks_used[ct]){
+                               debug("Task %d not implemented\n", ct);
+                               continue;
+                       }
+                       debug("Going to execute task %d\n", ct);
+                       run_task(tasks[ct]);
+               }
+               usleep(10);
        }
 
 /*     //Read program