blob: 7bf9fa2611842cb41f05bfa71fed24ec7028e67d [file] [log] [blame]
Brian Silverman17f503e2015-08-02 18:17:18 -07001#include <sys/types.h>
2#include <sys/socket.h>
3#include <errno.h>
4#include <string.h>
5#include <netinet/in.h>
6#include <arpa/inet.h>
7
8#include "aos/common/time.h"
9#include "aos/common/logging/queue_logging.h"
10#include "aos/common/logging/logging.h"
11#include "aos/linux_code/init.h"
12#include "aos/common/byteorder.h"
13
14#include "y2014/queues/hot_goal.q.h"
15
16int main() {
17 ::aos::InitNRT();
18
19 uint64_t left_count, right_count;
Brian Silvermanb601d892015-12-20 18:24:38 -050020 ::y2014::hot_goal.FetchLatest();
21 if (::y2014::hot_goal.get()) {
22 LOG_STRUCT(DEBUG, "starting with", *::y2014::hot_goal);
23 left_count = ::y2014::hot_goal->left_count;
24 right_count = ::y2014::hot_goal->left_count;
Brian Silverman17f503e2015-08-02 18:17:18 -070025 } else {
26 LOG(DEBUG, "no starting message\n");
27 left_count = right_count = 0;
28 }
29
30 int my_socket = -1;
31 while (true) {
32 if (my_socket == -1) {
33 my_socket = socket(AF_INET, SOCK_STREAM, 0);
34 if (my_socket == -1) {
35 PLOG(WARNING, "socket(AF_INET, SOCK_STREAM, 0) failed");
36 continue;
37 } else {
38 LOG(INFO, "opened socket (is %d)\n", my_socket);
39 sockaddr_in address, *sockaddr_pointer;
40 memset(&address, 0, sizeof(address));
41 address.sin_family = AF_INET;
42 address.sin_port = ::aos::hton<uint16_t>(1180);
43 sockaddr *address_pointer;
44 sockaddr_pointer = &address;
45 memcpy(&address_pointer, &sockaddr_pointer, sizeof(void *));
46 if (bind(my_socket, address_pointer, sizeof(address)) == -1) {
47 PLOG(WARNING, "bind(%d, %p, %zu) failed",
48 my_socket, &address, sizeof(address));
49 close(my_socket);
50 my_socket = -1;
51 continue;
52 }
53
54 if (listen(my_socket, 1) == -1) {
55 PLOG(WARNING, "listen(%d, 1) failed", my_socket);
56 close(my_socket);
57 my_socket = -1;
58 continue;
59 }
60 }
61 }
62
63 int connection = accept4(my_socket, nullptr, nullptr, SOCK_NONBLOCK);
64 if (connection == -1) {
65 PLOG(WARNING, "accept(%d, nullptr, nullptr) failed", my_socket);
66 continue;
67 }
68 LOG(INFO, "accepted (is %d)\n", connection);
69
70 while (connection != -1) {
71 fd_set fds;
72 FD_ZERO(&fds);
73 FD_SET(connection, &fds);
74 struct timeval timeout_timeval =
75 ::aos::time::Time::InSeconds(1).ToTimeval();
76 switch (
77 select(connection + 1, &fds, nullptr, nullptr, &timeout_timeval)) {
78 case 1: {
79 uint8_t data;
80 ssize_t read_bytes = read(connection, &data, sizeof(data));
81 if (read_bytes != sizeof(data)) {
82 LOG(WARNING, "read %zd bytes instead of %zd\n", read_bytes,
83 sizeof(data));
84 break;
85 }
86 if (data & 0x01) ++right_count;
87 if (data & 0x02) ++left_count;
Brian Silvermanb601d892015-12-20 18:24:38 -050088 auto message = ::y2014::hot_goal.MakeMessage();
Brian Silverman17f503e2015-08-02 18:17:18 -070089 message->left_count = left_count;
90 message->right_count = right_count;
91 LOG_STRUCT(DEBUG, "sending", *message);
92 message.Send();
93 } break;
94 case 0:
95 LOG(WARNING, "read on %d timed out\n", connection);
96 close(connection);
97 connection = -1;
98 break;
99 default:
100 PLOG(FATAL,
101 "select(%d, %p, nullptr, nullptr, %p) failed",
102 connection + 1, &fds, &timeout_timeval);
103 }
104 }
105 }
106
107 LOG(FATAL, "finished???\n");
108}