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