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