Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

return messages to originating socket, begin error handling

  • Loading branch information...
commit 7b9d4724e39850a957eba4b78efaf11644090a25 1 parent 79d9bee
Michael Moon authored

Showing 1 changed file with 48 additions and 5 deletions. Show diff stats Hide diff stats

  1. +48 5 controller/netrap-controller.c
53 controller/netrap-controller.c
@@ -17,6 +17,16 @@
17 17 #include <unistd.h>
18 18 #include <fcntl.h>
19 19
  20 +
  21 +
  22 +#define DEFAULT_PORT "/dev/arduino"
  23 +#define DEFAULT_BAUD 115200
  24 +
  25 +#define DEFAULT_LISTEN_ADDR 0.0.0.0
  26 +#define DEFAULT_LISTEN_PORT 2560
  27 +
  28 +
  29 +
20 30 #define BUFFER_SIZE 1024
21 31 typedef struct {
22 32 unsigned int head;
@@ -404,8 +414,8 @@ int main(int argc, char **argv) {
404 414
405 415 int running = 1;
406 416
407   - char * printer_port = "/dev/arduino";
408   - int printer_baud = 115200;
  417 + char * printer_port = DEFAULT_PORT;
  418 + int printer_baud = DEFAULT_BAUD;
409 419
410 420 printer_socket *printer = new_printer_socket(printer_port, printer_baud);
411 421
@@ -436,6 +446,21 @@ int main(int argc, char **argv) {
436 446 FD_SET(((local_socket *) errorsockets->data[i])->fd, errorselect);
437 447 select(maxfd, readselect, writeselect, errorselect, NULL);
438 448
  449 + for (int i = 0; i < errorsockets->length; i++) {
  450 + local_socket *s = (local_socket *) errorsockets->data[i];
  451 + if (FD_ISSET(s->fd, errorselect)) {
  452 + printf("error on %d: %p\n", s->fd, s);
  453 + switch (s->type) {
  454 + case SOCKTYPE_LOCAL:
  455 + break;
  456 + case SOCKTYPE_PRINTER:
  457 + break;
  458 + case SOCKTYPE_CLIENT:
  459 + break
  460 + }
  461 + }
  462 + }
  463 +
439 464 for (int i = 0; i < readsockets->length; i++) {
440 465 local_socket *s = (local_socket *) readsockets->data[i];
441 466 if (FD_ISSET(s->fd, readselect)) {
@@ -484,7 +509,13 @@ int main(int argc, char **argv) {
484 509 ringbuffer_readline(&sock->rxbuffer, line, BUFFER_SIZE);
485 510 //printf(", got a line: %s", line);
486 511 int r = snprintf(buf, BUFFER_SIZE, "< %s", line);
487   - write(sock->lastmsgsock->fd, buf, r);
  512 + if (sock->lastmsgsock->type == SOCKTYPE_LOCAL)
  513 + write(sock->lastmsgsock->fd, buf, r);
  514 + else if (sock->lastmsgsock->ty[e == SOCKTYPE_CLIENT) {
  515 + ringbuffer_write(((client_socket *) sock->lastmsgsock)->txbuffer, buf, r);
  516 + if (array_indexof(writesockets, sock) == -1)
  517 + array_push(writesockets, sock);
  518 + }
488 519 if (strncmp(line, "ok", 2) == 0) {
489 520 //fprintf(stderr, "got token!");
490 521 if (sock->tokens < sock->maxtoken)
@@ -524,7 +555,13 @@ int main(int argc, char **argv) {
524 555 //printf("write %d", s->fd);
525 556 switch (s->type) {
526 557 case SOCKTYPE_LOCAL:
527   - //printf(" (local)");
  558 + {
  559 + local_socket *sock = (client_socket *) s;
  560 + if (ringbuffer_canread(sock->txbuffer) > 0)
  561 + ringbuffer_readtofd(sock->txbuffer, s->fd);
  562 + if (ringbuffer_canread(sock->txbuffer) == 0)
  563 + array_delete(writesockets, s);
  564 + }
528 565 break;
529 566 case SOCKTYPE_PRINTER:
530 567 {
@@ -543,7 +580,13 @@ int main(int argc, char **argv) {
543 580 }
544 581 break;
545 582 case SOCKTYPE_CLIENT:
546   - //printf(" (client)");
  583 + {
  584 + client_socket *sock = (client_socket *) s;
  585 + if (ringbuffer_canread(sock->txbuffer) > 0)
  586 + ringbuffer_readtofd(sock->txbuffer, s->fd);
  587 + if (ringbuffer_canread(sock->txbuffer) == 0)
  588 + array_delete(writesockets, s);
  589 + }
547 590 break;
548 591 }
549 592

0 comments on commit 7b9d472

Please sign in to comment.
Something went wrong with that request. Please try again.