blob: 773397dfd6eb8fff93740e743a460752c6bf14e1 [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/byteorder.h"
Austin Schuha3e576b2019-05-22 21:22:23 -070010#include "aos/events/shm-event-loop.h"
11#include "aos/init.h"
12#include "aos/logging/logging.h"
13#include "aos/logging/queue_logging.h"
14#include "aos/time/time.h"
Brian Silverman17f503e2015-08-02 18:17:18 -070015#include "y2014/queues/hot_goal.q.h"
16
17int main() {
18 ::aos::InitNRT();
19
Austin Schuha3e576b2019-05-22 21:22:23 -070020 ::aos::ShmEventLoop shm_event_loop;
Brian Silverman17f503e2015-08-02 18:17:18 -070021
Austin Schuha3e576b2019-05-22 21:22:23 -070022 ::aos::Sender<::y2014::HotGoal> hot_goal_sender =
23 shm_event_loop.MakeSender<::y2014::HotGoal>(".y2014.hot_goal");
24
25 uint64_t left_count = 0, right_count = 0;
Brian Silverman17f503e2015-08-02 18:17:18 -070026 int my_socket = -1;
27 while (true) {
28 if (my_socket == -1) {
29 my_socket = socket(AF_INET, SOCK_STREAM, 0);
30 if (my_socket == -1) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070031 AOS_PLOG(WARNING, "socket(AF_INET, SOCK_STREAM, 0) failed");
Brian Silverman17f503e2015-08-02 18:17:18 -070032 continue;
33 } else {
Austin Schuhf257f3c2019-10-27 21:00:43 -070034 AOS_LOG(INFO, "opened socket (is %d)\n", my_socket);
Brian Silverman17f503e2015-08-02 18:17:18 -070035 sockaddr_in address, *sockaddr_pointer;
36 memset(&address, 0, sizeof(address));
37 address.sin_family = AF_INET;
38 address.sin_port = ::aos::hton<uint16_t>(1180);
39 sockaddr *address_pointer;
40 sockaddr_pointer = &address;
41 memcpy(&address_pointer, &sockaddr_pointer, sizeof(void *));
42 if (bind(my_socket, address_pointer, sizeof(address)) == -1) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070043 AOS_PLOG(WARNING, "bind(%d, %p, %zu) failed", my_socket, &address,
44 sizeof(address));
Brian Silverman17f503e2015-08-02 18:17:18 -070045 close(my_socket);
46 my_socket = -1;
47 continue;
48 }
49
50 if (listen(my_socket, 1) == -1) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070051 AOS_PLOG(WARNING, "listen(%d, 1) failed", my_socket);
Brian Silverman17f503e2015-08-02 18:17:18 -070052 close(my_socket);
53 my_socket = -1;
54 continue;
55 }
56 }
57 }
58
59 int connection = accept4(my_socket, nullptr, nullptr, SOCK_NONBLOCK);
60 if (connection == -1) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070061 AOS_PLOG(WARNING, "accept(%d, nullptr, nullptr) failed", my_socket);
Brian Silverman17f503e2015-08-02 18:17:18 -070062 continue;
63 }
Austin Schuhf257f3c2019-10-27 21:00:43 -070064 AOS_LOG(INFO, "accepted (is %d)\n", connection);
Brian Silverman17f503e2015-08-02 18:17:18 -070065
66 while (connection != -1) {
67 fd_set fds;
68 FD_ZERO(&fds);
69 FD_SET(connection, &fds);
Austin Schuhf2a50ba2016-12-24 16:16:26 -080070 struct timeval timeout_timeval;
71 timeout_timeval.tv_sec = 1;
72 timeout_timeval.tv_usec = 0;
Brian Silverman17f503e2015-08-02 18:17:18 -070073 switch (
74 select(connection + 1, &fds, nullptr, nullptr, &timeout_timeval)) {
75 case 1: {
76 uint8_t data;
77 ssize_t read_bytes = read(connection, &data, sizeof(data));
78 if (read_bytes != sizeof(data)) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070079 AOS_LOG(WARNING, "read %zd bytes instead of %zd\n", read_bytes,
80 sizeof(data));
Brian Silverman17f503e2015-08-02 18:17:18 -070081 break;
82 }
83 if (data & 0x01) ++right_count;
84 if (data & 0x02) ++left_count;
Austin Schuha3e576b2019-05-22 21:22:23 -070085 auto message = hot_goal_sender.MakeMessage();
Brian Silverman17f503e2015-08-02 18:17:18 -070086 message->left_count = left_count;
87 message->right_count = right_count;
Austin Schuhf257f3c2019-10-27 21:00:43 -070088 AOS_LOG_STRUCT(DEBUG, "sending", *message);
Brian Silverman17f503e2015-08-02 18:17:18 -070089 message.Send();
90 } break;
91 case 0:
Austin Schuhf257f3c2019-10-27 21:00:43 -070092 AOS_LOG(WARNING, "read on %d timed out\n", connection);
Brian Silverman17f503e2015-08-02 18:17:18 -070093 close(connection);
94 connection = -1;
95 break;
96 default:
Austin Schuhf257f3c2019-10-27 21:00:43 -070097 AOS_PLOG(FATAL, "select(%d, %p, nullptr, nullptr, %p) failed",
98 connection + 1, &fds, &timeout_timeval);
Brian Silverman17f503e2015-08-02 18:17:18 -070099 }
100 }
101 }
102
Austin Schuhf257f3c2019-10-27 21:00:43 -0700103 AOS_LOG(FATAL, "finished???\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700104}