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 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 9 | #include "aos/time/time.h" |
| 10 | #include "aos/logging/queue_logging.h" |
| 11 | #include "aos/logging/logging.h" |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 12 | #include "aos/linux_code/init.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 13 | #include "aos/byteorder.h" |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 14 | |
| 15 | #include "y2014/queues/hot_goal.q.h" |
| 16 | |
| 17 | int main() { |
| 18 | ::aos::InitNRT(); |
| 19 | |
| 20 | uint64_t left_count, right_count; |
Brian Silverman | b601d89 | 2015-12-20 18:24:38 -0500 | [diff] [blame] | 21 | ::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 Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 26 | } 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 Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 75 | struct timeval timeout_timeval; |
| 76 | timeout_timeval.tv_sec = 1; |
| 77 | timeout_timeval.tv_usec = 0; |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 78 | 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 Silverman | b601d89 | 2015-12-20 18:24:38 -0500 | [diff] [blame] | 90 | auto message = ::y2014::hot_goal.MakeMessage(); |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 91 | 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 | } |