server.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * server.cpp
  3. *
  4. * Created on: Jan 4, 2018
  5. * Author: Philipp Hinz
  6. * based on: https://gist.github.com/suyash/2488ff6996c98a8ee3a84fe3198a6f85
  7. */
  8. #include <arpa/inet.h>
  9. #include <netinet/in.h>
  10. #include <stdbool.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <unistd.h>
  15. #include <pthread.h>
  16. #include "server.h"
  17. #include "logger.h"
  18. #include "hal.h"
  19. #include "coffee.h"
  20. #include "stripe.h"
  21. #include "events.h"
  22. /**
  23. * TCP Uses 2 types of sockets, the connection socket and the listen socket.
  24. * The Goal is to separate the connection phase from the data exchange phase.
  25. * */
  26. int listen_sock;
  27. int sock;
  28. char buffer_ok[] = "OK\n";
  29. /**
  30. * Just sends an ACK message back
  31. */
  32. void sendACK() {
  33. send(sock, buffer_ok, strlen(buffer_ok), 0);
  34. }
  35. /**
  36. * Closes all active connections
  37. */
  38. void serverTerminate(event_t *e) {
  39. logger(V_BASIC, "Server.cpp: Terminating\n");
  40. close(sock);
  41. close(listen_sock);
  42. }
  43. /**
  44. * Main thread to handle networking stuff
  45. * @param *threadid Thread ID
  46. */
  47. void* serverThread(void* threadid) {
  48. sleep(1); // Wait for other components to initialize
  49. // socket address used for the server
  50. struct sockaddr_in server_address;
  51. memset(&server_address, 0, sizeof(server_address));
  52. server_address.sin_family = AF_INET;
  53. // htons: host to network short: transforms a value in host byte
  54. // ordering format to a short value in network byte ordering format
  55. server_address.sin_port = htons(SERVER_PORT);
  56. // htonl: host to network long: same as htons but to long
  57. server_address.sin_addr.s_addr = htonl(INADDR_ANY);
  58. // create a TCP socket, creation returns -1 on failure
  59. if ((listen_sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
  60. logger_error("could not create listen socket\n");
  61. pthread_exit(0);
  62. }
  63. // bind it to listen to the incoming connections on the created server
  64. // address, will return -1 on error
  65. if ((bind(listen_sock, (struct sockaddr *) &server_address,
  66. sizeof(server_address))) < 0) {
  67. logger_error("could not bind socket\n");
  68. pthread_exit(0);
  69. }
  70. int wait_size = 16; // maximum number of waiting clients, after which
  71. // dropping begins
  72. if (listen(listen_sock, wait_size) < 0) {
  73. logger_error("could not open socket for listening\n");
  74. pthread_exit(0);
  75. }
  76. event_subscribe("terminate", &serverTerminate, "server.cpp");
  77. logger(V_BASIC, "Now listening for commands on port %d\n", SERVER_PORT);
  78. // socket address used to store client address
  79. struct sockaddr_in client_address;
  80. unsigned int client_address_len = 0;
  81. // run indefinitely
  82. while (true) {
  83. // open a new socket to transmit data per connection
  84. if ((sock = accept(listen_sock, (struct sockaddr *) &client_address,
  85. &client_address_len)) < 0) {
  86. logger_error("could not open a socket to accept data\n");
  87. pthread_exit(0);
  88. }
  89. int n = 0;
  90. int len = 0, maxlen = 100, lastlen = 0;
  91. char buffer[maxlen];
  92. char cmd[maxlen];
  93. char *pcmd = cmd;
  94. char *pbuffer = buffer;
  95. strncpy(buffer, "", maxlen);
  96. strncpy(cmd, "", maxlen);
  97. logger(V_HAL, "client connected with ip address: %s\n",
  98. inet_ntoa(client_address.sin_addr));
  99. // keep running as long as the client keeps the connection open
  100. while ((n = recv(sock, pbuffer, maxlen, 0)) > 0) {
  101. pbuffer += n;
  102. maxlen -= n;
  103. len += n;
  104. strncpy(cmd, buffer, len - 1);
  105. cmd[len - 1] = '\0';
  106. pcmd = &cmd[lastlen];
  107. logger(V_SPI, "received %d bytes: '%s' (cmd: %s)\n", len, buffer,
  108. pcmd);
  109. if (!strcmp(SERVER_CMD_ON, pcmd)) {
  110. if (getState() == STATE_OFF) {
  111. halSendSignal(SigPowerUp);
  112. sendACK();
  113. }
  114. } else if (!strcmp(SERVER_CMD_OFF, pcmd)) {
  115. if (getState() != STATE_OFF) {
  116. halSendSignal(SigPowerDown);
  117. sendACK();
  118. }
  119. } else if (!strcmp(SERVER_CMD_STATUS, pcmd)) {
  120. char tmp[] = "0\n";
  121. tmp[0] += getState();
  122. send(sock, tmp, sizeof(tmp), 0);
  123. } else if (!strcmp(SERVER_CMD_STRIPEON, pcmd)) {
  124. stripeOn();
  125. sendACK();
  126. } else if (!strcmp(SERVER_CMD_STRIPEOFF, pcmd)) {
  127. stripeOff();
  128. sendACK();
  129. }
  130. lastlen = len;
  131. // echo received content back
  132. //send(sock, buffer, len, 0);
  133. }
  134. close(sock);
  135. }
  136. close(listen_sock);
  137. pthread_exit(EXIT_SUCCESS);
  138. }