886170e32f524b94b9d0f27c11222273226d8a5d
3 #include <netinet/in.h>
11 #include <sys/socket.h>
12 #include <sys/types.h>
16 #include "interpret.h"
17 #include "mTaskSymbols.h"
24 #define MSG_GET_TASK 't'
25 #define MSG_DEL_TASK 'd'
26 #define MSG_SDS_SPEC 's'
27 #define MSG_SDS_UPD 'u'
34 void killHandler(int i
)
36 printf("%i caught, Bye...\n", i
);
40 bool input_available(int fd
){
47 if (select(fd
+1, &fds
, NULL
, NULL
, &tv
) == -1)
49 return FD_ISSET(fd
, &fds
);
53 if (gettimeofday(&tv1
, NULL
) == -1)
55 return tv1
.tv_sec
*1000 + tv1
.tv_usec
/1000;
58 void read_message(int fd_in
, int fd_out
)
64 debug("Receiving input: %c\n", c
);
67 debug("Receiving an sds\n");
71 debug("Receiving an sds\n");
72 //TODO do something with the return value
76 debug("Receiving a delete task request\n");
80 debug("Receiving a task\n");
81 c
= task_register(fd_in
);
82 // write(fd_out, &c, 1);
88 // debug("iTasks server shut down\n");
89 // exit(EXIT_SUCCESS);
91 debug("Unknown message: %X\n", c
);
96 void open_filedescriptors()
98 struct sockaddr_in sa
;
100 bzero((char *) &sa
, sizeof(sa
));
101 sa
.sin_family
= AF_INET
;
102 sa
.sin_addr
.s_addr
= INADDR_ANY
;
103 sa
.sin_port
= htons(port
);
105 if((sock_fd
= socket(AF_INET
, SOCK_STREAM
, 0)) == -1)
107 if(bind(sock_fd
, (struct sockaddr
*)&sa
, sizeof(sa
)) == -1)
109 if(listen(sock_fd
, 10) == -1)
112 printf("Listening on %d\n", port
);
114 if((fd
= accept(sock_fd
, (struct sockaddr
*)NULL
, NULL
)) == -1)
118 void usage(FILE *o
, char *arg0
){
123 "-p PORT Custom port number, default: 8123\n"
127 int main(int argc
, char *argv
[])
131 //Register signal handler
132 if(signal(SIGINT
, killHandler
) == SIG_ERR
){
133 die("Couldn't register signal handler...\n");
135 if(signal(SIGTERM
, killHandler
) == SIG_ERR
){
136 die("Couldn't register signal handler...\n");
139 //Command line arguments
142 if(strcmp(argv
[opti
], "-h") == 0){
143 usage(stdout
, argv
[0]);
145 } else if(strcmp(argv
[opti
], "-p") == 0 && opti
+1<argc
){
146 port
= atoi(argv
[++opti
]);
148 die("Port numbers are > 1\n");
150 usage(stderr
, argv
[0]);
156 open_filedescriptors();
160 struct task
*curtask
;
162 //Check for new tasks
163 if(input_available(fd
))
164 read_message(fd
, fd
);
166 cyclestart
= millis();
167 for(ct
= 0; ct
<MAXTASKS
; ct
++){
168 //See whether the task is even in use
169 if((curtask
= task_get(ct
)) == NULL
){
170 // debug("Task %d not implemented\n", ct);
173 //See whether the task interval has passed
174 if(cyclestart
-curtask
->lastrun
< curtask
->interval
){
175 // debug("Task %d not scheduled\n", ct);
179 printf("Current task to run: %d\n", ct
);
182 run_task(curtask
, fd
);
184 debug("Waiting for 500ms\n");
186 debug("done waiting\n");