blob: 0b99edb7350aa4bef7cc4f94f07410f27af92d10 [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
John Park33858a32018-09-28 23:05:48 -07009#include "aos/time/time.h"
10#include "aos/logging/queue_logging.h"
11#include "aos/logging/logging.h"
John Park398c74a2018-10-20 21:17:39 -070012#include "aos/init.h"
John Park33858a32018-09-28 23:05:48 -070013#include "aos/byteorder.h"
Brian Silverman17f503e2015-08-02 18:17:18 -070014
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);
Austin Schuhf2a50ba2016-12-24 16:16:26 -080075 struct timeval timeout_timeval;
76 timeout_timeval.tv_sec = 1;
77 timeout_timeval.tv_usec = 0;
Brian Silverman17f503e2015-08-02 18:17:18 -070078 switch (
79 select(connection + 1, &fds, nullptr, nullptr, &timeout_timeval)) {
80 case 1: {
81 uint8_t data;
82 ssize_t read_bytes = read(connection, &data, sizeof(data));
83 if (read_bytes != sizeof(data)) {
84 LOG(WARNING, "read %zd bytes instead of %zd\n", read_bytes,
85 sizeof(data));
86 break;
87 }
88 if (data & 0x01) ++right_count;
89 if (data & 0x02) ++left_count;
Brian Silvermanb601d892015-12-20 18:24:38 -050090 auto message = ::y2014::hot_goal.MakeMessage();
Brian Silverman17f503e2015-08-02 18:17:18 -070091 message->left_count = left_count;
92 message->right_count = right_count;
93 LOG_STRUCT(DEBUG, "sending", *message);
94 message.Send();
95 } break;
96 case 0:
97 LOG(WARNING, "read on %d timed out\n", connection);
98 close(connection);
99 connection = -1;
100 break;
101 default:
102 PLOG(FATAL,
103 "select(%d, %p, nullptr, nullptr, %p) failed",
104 connection + 1, &fds, &timeout_timeval);
105 }
106 }
107 }
108
109 LOG(FATAL, "finished???\n");
110}