43911bc4bc3e4ee83cbb837e15af0d458283dbb7
2 #include <netinet/in.h>
9 #include <sys/socket.h>
10 #include <sys/types.h>
14 #include "interpret.h"
15 #include "mTaskSymbols.h"
22 #define MSG_GET_TASK 't'
23 #define MSG_DEL_TASK 'd'
24 #define MSG_SDS_SPEC 's'
25 #define MSG_SDS_UPD 'u'
32 void killHandler(int i
)
34 printf("%s caught, Bye...\n", strsignal(i
));
38 bool input_available(int fd
){
45 if (select(fd
+1, &fds
, NULL
, NULL
, &tv
) == -1)
47 return FD_ISSET(fd
, &fds
);
51 if (gettimeofday(&tv1
, NULL
) == -1)
53 return tv1
.tv_sec
*1000 + tv1
.tv_usec
/1000;
56 void read_message(int fd_in
, int fd_out
)
62 debug("Receiving input: %c\n", c
);
65 debug("Receiving an sds\n");
69 debug("Receiving an sds\n");
70 //TODO do something with the return value
74 debug("Receiving a delete task request\n");
78 debug("Receiving a task\n");
79 c
= task_register(fd_in
);
85 debug("iTasks server shut down\n");
88 debug("Unknown message: %X %X?\n", c
, EOF
);
92 void open_filedescriptors()
94 struct sockaddr_in sa
;
96 bzero((char *) &sa
, sizeof(sa
));
97 sa
.sin_family
= AF_INET
;
98 sa
.sin_addr
.s_addr
= INADDR_ANY
;
99 sa
.sin_port
= htons(port
);
101 if((sock_fd
= socket(AF_INET
, SOCK_STREAM
, 0)) == -1)
103 if(bind(sock_fd
, (struct sockaddr
*)&sa
, sizeof(sa
)) == -1)
105 if(listen(sock_fd
, 10) == -1)
108 printf("Listening on %d\n", port
);
110 if((fd
= accept(sock_fd
, (struct sockaddr
*)NULL
, NULL
)) == -1)
114 void usage(FILE *o
, char *arg0
){
119 "-p PORT Custom port number, default: 8123\n"
123 int main(int argc
, char *argv
[])
127 //Register signal handler
128 if(signal(SIGINT
, killHandler
) == SIG_ERR
){
129 die("Couldn't register signal handler...\n");
131 if(signal(SIGTERM
, killHandler
) == SIG_ERR
){
132 die("Couldn't register signal handler...\n");
135 //Command line arguments
137 while((opt
= getopt(argc
, argv
, "hp:")) != -1){
142 die("Port numbers are > 1\n");
145 usage(stdout
, argv
[0]);
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");