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
);
87 // debug("iTasks server shut down\n");
88 // exit(EXIT_SUCCESS);
90 debug("Unknown message: %X\n", c
);
94 void open_filedescriptors()
96 struct sockaddr_in sa
;
98 bzero((char *) &sa
, sizeof(sa
));
99 sa
.sin_family
= AF_INET
;
100 sa
.sin_addr
.s_addr
= INADDR_ANY
;
101 sa
.sin_port
= htons(port
);
103 if((sock_fd
= socket(AF_INET
, SOCK_STREAM
, 0)) == -1)
105 if(bind(sock_fd
, (struct sockaddr
*)&sa
, sizeof(sa
)) == -1)
107 if(listen(sock_fd
, 10) == -1)
110 printf("Listening on %d\n", port
);
112 if((fd
= accept(sock_fd
, (struct sockaddr
*)NULL
, NULL
)) == -1)
116 void usage(FILE *o
, char *arg0
){
121 "-p PORT Custom port number, default: 8123\n"
125 int main(int argc
, char *argv
[])
129 //Register signal handler
130 if(signal(SIGINT
, killHandler
) == SIG_ERR
){
131 die("Couldn't register signal handler...\n");
133 if(signal(SIGTERM
, killHandler
) == SIG_ERR
){
134 die("Couldn't register signal handler...\n");
137 //Command line arguments
140 if(strcmp(argv
[opti
], "-h") == 0){
141 usage(stdout
, argv
[0]);
143 } else if(strcmp(argv
[opti
], "-p") == 0 && opti
+1<argc
){
144 port
= atoi(argv
[++opti
]);
146 die("Port numbers are > 1\n");
148 usage(stderr
, argv
[0]);
154 open_filedescriptors();
158 struct task
*curtask
;
160 //Check for new tasks
161 if(input_available(fd
))
162 read_message(fd
, fd
);
164 cyclestart
= millis();
165 for(ct
= 0; ct
<MAXTASKS
; ct
++){
166 //See whether the task is even in use
167 if((curtask
= task_get(ct
)) == NULL
){
168 // debug("Task %d not implemented\n", ct);
171 //See whether the task interval has passed
172 if(cyclestart
-curtask
->lastrun
< curtask
->interval
){
173 // debug("Task %d not scheduled\n", ct);
177 printf("Current task to run: %d\n", ct
);
180 run_task(curtask
, fd
);
182 debug("Waiting for 500ms\n");
184 debug("done waiting\n");