02852efbce945274e620852137084c455acd44a2
3 #include <netinet/in.h>
10 #include <sys/socket.h>
11 #include <sys/types.h>
15 #include "interpret.h"
16 #include "mTaskSymbols.h"
23 #define MSG_GET_TASK 't'
24 #define MSG_DEL_TASK 'd'
25 #define MSG_SDS_SPEC 's'
26 #define MSG_SDS_UPD 'u'
33 void killHandler(int i
)
35 printf("%i caught, Bye...\n", i
);
39 bool input_available(int fd
){
46 if (select(fd
+1, &fds
, NULL
, NULL
, &tv
) == -1)
48 return FD_ISSET(fd
, &fds
);
52 if (gettimeofday(&tv1
, NULL
) == -1)
54 return tv1
.tv_sec
*1000 + tv1
.tv_usec
/1000;
57 void read_message(int fd_in
, int fd_out
)
63 debug("Receiving input: %c\n", c
);
66 debug("Receiving an sds\n");
70 debug("Receiving an sds\n");
71 //TODO do something with the return value
75 debug("Receiving a delete task request\n");
79 debug("Receiving a task\n");
80 c
= task_register(fd_in
);
81 // write(fd_out, &c, 1);
87 // debug("iTasks server shut down\n");
88 // exit(EXIT_SUCCESS);
90 debug("Unknown message: %X\n", c
);
95 void open_filedescriptors()
97 struct sockaddr_in sa
;
99 memset(&sa
, 0, sizeof(sa
));
100 sa
.sin_family
= AF_INET
;
101 sa
.sin_addr
.s_addr
= INADDR_ANY
;
102 sa
.sin_port
= htons(port
);
104 if((sock_fd
= socket(AF_INET
, SOCK_STREAM
, 0)) == -1)
106 if(bind(sock_fd
, (struct sockaddr
*)&sa
, sizeof(sa
)) == -1)
108 if(listen(sock_fd
, 10) == -1)
111 printf("Listening on %d\n", port
);
113 if((fd
= accept(sock_fd
, (struct sockaddr
*)NULL
, NULL
)) == -1)
117 void usage(FILE *o
, char *arg0
){
122 "-p PORT Custom port number, default: 8123\n"
126 int main(int argc
, char *argv
[])
130 //Register signal handler
131 if(signal(SIGINT
, killHandler
) == SIG_ERR
){
132 die("Couldn't register signal handler...\n");
134 if(signal(SIGTERM
, killHandler
) == SIG_ERR
){
135 die("Couldn't register signal handler...\n");
138 //Command line arguments
141 if(strcmp(argv
[opti
], "-h") == 0){
142 usage(stdout
, argv
[0]);
144 } else if(strcmp(argv
[opti
], "-p") == 0 && opti
+1<argc
){
145 port
= atoi(argv
[++opti
]);
147 die("Port numbers are > 1\n");
149 usage(stderr
, argv
[0]);
160 open_filedescriptors();
164 struct task
*curtask
;
166 //Check for new tasks
167 if(input_available(fd
))
168 read_message(fd
, fd
);
170 cyclestart
= millis();
171 for(ct
= 0; ct
<MAXTASKS
; ct
++){
172 //See whether the task is even in use
173 if((curtask
= task_get(ct
)) == NULL
){
174 // debug("Task %d not implemented\n", ct);
177 //See whether the task interval has passed
178 if(cyclestart
-curtask
->lastrun
< curtask
->interval
){
179 // debug("Task %d not scheduled\n", ct);
183 printf("Current task to run: %d\n", ct
);
186 run_task(curtask
, fd
);
188 debug("Waiting for 500ms\n");
190 debug("done waiting\n");