Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 1 | #include <arpa/inet.h> |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 2 | #include <netinet/in.h> |
| 3 | #include <sys/socket.h> |
| 4 | #include <sys/types.h> |
Brian Silverman | 97aae26 | 2015-12-25 18:00:34 -0800 | [diff] [blame] | 5 | #include <unistd.h> |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 6 | |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 7 | #include <cerrno> |
| 8 | #include <cstring> |
| 9 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 10 | #include "aos/events/shm_event_loop.h" |
Austin Schuh | a3e576b | 2019-05-22 21:22:23 -0700 | [diff] [blame] | 11 | #include "aos/init.h" |
| 12 | #include "aos/logging/logging.h" |
Austin Schuh | a3e576b | 2019-05-22 21:22:23 -0700 | [diff] [blame] | 13 | #include "aos/time/time.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 14 | #include "y2014/queues/hot_goal_generated.h" |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 15 | |
Austin Schuh | 094d09b | 2020-11-20 23:26:52 -0800 | [diff] [blame] | 16 | int main(int argc, char **argv) { |
| 17 | ::aos::InitGoogle(&argc, &argv); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 18 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 19 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
Austin Schuh | c5fa6d9 | 2022-02-25 14:36:28 -0800 | [diff] [blame] | 20 | aos::configuration::ReadConfig("aos_config.json"); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 21 | |
| 22 | ::aos::ShmEventLoop shm_event_loop(&config.message()); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 23 | |
Austin Schuh | a3e576b | 2019-05-22 21:22:23 -0700 | [diff] [blame] | 24 | ::aos::Sender<::y2014::HotGoal> hot_goal_sender = |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 25 | shm_event_loop.MakeSender<::y2014::HotGoal>("/"); |
Austin Schuh | a3e576b | 2019-05-22 21:22:23 -0700 | [diff] [blame] | 26 | |
| 27 | uint64_t left_count = 0, right_count = 0; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 28 | int my_socket = -1; |
| 29 | while (true) { |
| 30 | if (my_socket == -1) { |
| 31 | my_socket = socket(AF_INET, SOCK_STREAM, 0); |
| 32 | if (my_socket == -1) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 33 | AOS_PLOG(WARNING, "socket(AF_INET, SOCK_STREAM, 0) failed"); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 34 | continue; |
| 35 | } else { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 36 | AOS_LOG(INFO, "opened socket (is %d)\n", my_socket); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 37 | sockaddr_in address, *sockaddr_pointer; |
| 38 | memset(&address, 0, sizeof(address)); |
| 39 | address.sin_family = AF_INET; |
Brian Silverman | b4bfb0c | 2021-01-11 18:25:51 -0800 | [diff] [blame] | 40 | address.sin_port = htons(1180); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 41 | sockaddr *address_pointer; |
| 42 | sockaddr_pointer = &address; |
| 43 | memcpy(&address_pointer, &sockaddr_pointer, sizeof(void *)); |
| 44 | if (bind(my_socket, address_pointer, sizeof(address)) == -1) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 45 | AOS_PLOG(WARNING, "bind(%d, %p, %zu) failed", my_socket, &address, |
| 46 | sizeof(address)); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 47 | close(my_socket); |
| 48 | my_socket = -1; |
| 49 | continue; |
| 50 | } |
| 51 | |
| 52 | if (listen(my_socket, 1) == -1) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 53 | AOS_PLOG(WARNING, "listen(%d, 1) failed", my_socket); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 54 | close(my_socket); |
| 55 | my_socket = -1; |
| 56 | continue; |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | int connection = accept4(my_socket, nullptr, nullptr, SOCK_NONBLOCK); |
| 62 | if (connection == -1) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 63 | AOS_PLOG(WARNING, "accept(%d, nullptr, nullptr) failed", my_socket); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 64 | continue; |
| 65 | } |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 66 | AOS_LOG(INFO, "accepted (is %d)\n", connection); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 67 | |
| 68 | while (connection != -1) { |
| 69 | fd_set fds; |
| 70 | FD_ZERO(&fds); |
| 71 | FD_SET(connection, &fds); |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 72 | struct timeval timeout_timeval; |
| 73 | timeout_timeval.tv_sec = 1; |
| 74 | timeout_timeval.tv_usec = 0; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 75 | switch ( |
| 76 | select(connection + 1, &fds, nullptr, nullptr, &timeout_timeval)) { |
| 77 | case 1: { |
| 78 | uint8_t data; |
| 79 | ssize_t read_bytes = read(connection, &data, sizeof(data)); |
| 80 | if (read_bytes != sizeof(data)) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 81 | AOS_LOG(WARNING, "read %zd bytes instead of %zd\n", read_bytes, |
| 82 | sizeof(data)); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 83 | break; |
| 84 | } |
| 85 | if (data & 0x01) ++right_count; |
| 86 | if (data & 0x02) ++left_count; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 87 | auto builder = hot_goal_sender.MakeBuilder(); |
| 88 | y2014::HotGoal::Builder hot_goal_builder = |
| 89 | builder.MakeBuilder<y2014::HotGoal>(); |
| 90 | hot_goal_builder.add_left_count(left_count); |
| 91 | hot_goal_builder.add_right_count(right_count); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 92 | (void)builder.Send(hot_goal_builder.Finish()); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 93 | } break; |
| 94 | case 0: |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 95 | AOS_LOG(WARNING, "read on %d timed out\n", connection); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 96 | close(connection); |
| 97 | connection = -1; |
| 98 | break; |
| 99 | default: |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 100 | AOS_PLOG(FATAL, "select(%d, %p, nullptr, nullptr, %p) failed", |
| 101 | connection + 1, &fds, &timeout_timeval); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 106 | AOS_LOG(FATAL, "finished???\n"); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 107 | } |