John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 1 | #include "aos/network/team_number.h" |
Brian Silverman | 431500a | 2013-10-28 19:50:15 -0700 | [diff] [blame] | 2 | |
Jim Ostrowski | 5d5a44f | 2020-06-24 19:10:15 -0700 | [diff] [blame] | 3 | #include <netinet/in.h> |
Jim Ostrowski | 5d5a44f | 2020-06-24 19:10:15 -0700 | [diff] [blame] | 4 | #include <unistd.h> |
Austin Schuh | be2456e | 2015-02-16 02:58:00 -0800 | [diff] [blame] | 5 | |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame^] | 6 | #include <cinttypes> |
| 7 | #include <cstdlib> |
| 8 | |
Austin Schuh | 4c84abb | 2021-06-20 18:16:32 -0700 | [diff] [blame] | 9 | #include "absl/strings/numbers.h" |
Brian Silverman | 431500a | 2013-10-28 19:50:15 -0700 | [diff] [blame] | 10 | |
Jim Ostrowski | 5d5a44f | 2020-06-24 19:10:15 -0700 | [diff] [blame] | 11 | DEFINE_string( |
| 12 | override_hostname, "", |
Jim Ostrowski | 2192ddb | 2020-06-24 19:07:31 -0700 | [diff] [blame] | 13 | "If set, this forces the hostname of this node to be the provided " |
| 14 | "hostname."); |
Jim Ostrowski | 8565b40 | 2020-02-29 20:26:53 -0800 | [diff] [blame] | 15 | |
Brian Silverman | 431500a | 2013-10-28 19:50:15 -0700 | [diff] [blame] | 16 | namespace aos { |
| 17 | namespace network { |
Brian Silverman | 3dfbfb1 | 2020-02-17 20:35:18 -0800 | [diff] [blame] | 18 | namespace team_number_internal { |
| 19 | |
Austin Schuh | 4c84abb | 2021-06-20 18:16:32 -0700 | [diff] [blame] | 20 | std::optional<uint16_t> ParseRoborioTeamNumber( |
| 21 | const std::string_view hostname) { |
Austin Schuh | be2456e | 2015-02-16 02:58:00 -0800 | [diff] [blame] | 22 | for (size_t i = 0; i < hostname.size(); i++) { |
| 23 | if (hostname[i] == '-') { |
Austin Schuh | 4c84abb | 2021-06-20 18:16:32 -0700 | [diff] [blame] | 24 | const std::string_view num_as_s = |
Austin Schuh | df5591e | 2015-12-19 22:36:50 -0800 | [diff] [blame] | 25 | hostname[hostname.size() - 1] == 'C' |
| 26 | ? hostname.substr(i + 1, hostname.size() - 5 - i) |
| 27 | : hostname.substr(i + 1); |
Austin Schuh | be2456e | 2015-02-16 02:58:00 -0800 | [diff] [blame] | 28 | |
| 29 | int num; |
Austin Schuh | 4c84abb | 2021-06-20 18:16:32 -0700 | [diff] [blame] | 30 | if (!absl::SimpleAtoi(num_as_s, &num)) { |
Brian Silverman | 3dfbfb1 | 2020-02-17 20:35:18 -0800 | [diff] [blame] | 31 | return std::nullopt; |
Austin Schuh | be2456e | 2015-02-16 02:58:00 -0800 | [diff] [blame] | 32 | } |
| 33 | if (hostname.substr(0, i) == "roboRIO" && |
| 34 | std::to_string(num) == num_as_s) { |
Brian Silverman | 3dfbfb1 | 2020-02-17 20:35:18 -0800 | [diff] [blame] | 35 | return num; |
Austin Schuh | be2456e | 2015-02-16 02:58:00 -0800 | [diff] [blame] | 36 | } |
Brian Silverman | 3dfbfb1 | 2020-02-17 20:35:18 -0800 | [diff] [blame] | 37 | return std::nullopt; |
Austin Schuh | be2456e | 2015-02-16 02:58:00 -0800 | [diff] [blame] | 38 | } |
| 39 | } |
Brian Silverman | 3dfbfb1 | 2020-02-17 20:35:18 -0800 | [diff] [blame] | 40 | return std::nullopt; |
Austin Schuh | be2456e | 2015-02-16 02:58:00 -0800 | [diff] [blame] | 41 | } |
Brian Silverman | 3dfbfb1 | 2020-02-17 20:35:18 -0800 | [diff] [blame] | 42 | |
Austin Schuh | 4c84abb | 2021-06-20 18:16:32 -0700 | [diff] [blame] | 43 | std::optional<uint16_t> ParsePiTeamNumber(const std::string_view hostname) { |
Brian Silverman | 5f06ed2 | 2020-02-17 21:49:42 -0800 | [diff] [blame] | 44 | if (hostname.substr(0, 3) != "pi-") { |
| 45 | return std::nullopt; |
| 46 | } |
| 47 | size_t first_separator = hostname.find('-'); |
| 48 | if (first_separator == hostname.npos || |
| 49 | first_separator >= hostname.size() - 2) { |
| 50 | return std::nullopt; |
| 51 | } |
| 52 | ++first_separator; |
| 53 | const size_t second_separator = hostname.find('-', first_separator); |
| 54 | if (second_separator == hostname.npos) { |
| 55 | return std::nullopt; |
| 56 | } |
Austin Schuh | 4c84abb | 2021-06-20 18:16:32 -0700 | [diff] [blame] | 57 | const std::string_view number_string = |
Brian Silverman | 5f06ed2 | 2020-02-17 21:49:42 -0800 | [diff] [blame] | 58 | hostname.substr(first_separator, second_separator - first_separator); |
| 59 | int number; |
Austin Schuh | 4c84abb | 2021-06-20 18:16:32 -0700 | [diff] [blame] | 60 | if (!absl::SimpleAtoi(number_string, &number)) { |
Brian Silverman | 5f06ed2 | 2020-02-17 21:49:42 -0800 | [diff] [blame] | 61 | return std::nullopt; |
| 62 | } |
| 63 | return number; |
| 64 | } |
| 65 | |
Brian Silverman | 3dfbfb1 | 2020-02-17 20:35:18 -0800 | [diff] [blame] | 66 | } // namespace team_number_internal |
Austin Schuh | df5591e | 2015-12-19 22:36:50 -0800 | [diff] [blame] | 67 | |
| 68 | namespace { |
| 69 | |
| 70 | uint16_t override_team; |
| 71 | |
Brian Silverman | 3dfbfb1 | 2020-02-17 20:35:18 -0800 | [diff] [blame] | 72 | uint16_t DoGetTeamNumber() { |
John Park | a19d1a9 | 2019-11-06 18:28:14 -0800 | [diff] [blame] | 73 | if (override_team != 0) { |
Jim Ostrowski | 5d5a44f | 2020-06-24 19:10:15 -0700 | [diff] [blame] | 74 | return override_team; |
John Park | a19d1a9 | 2019-11-06 18:28:14 -0800 | [diff] [blame] | 75 | } |
Brian Silverman | 8a6dac9 | 2015-02-21 20:08:24 -0500 | [diff] [blame] | 76 | |
| 77 | const char *override_number = getenv("AOS_TEAM_NUMBER"); |
| 78 | if (override_number != nullptr) { |
Austin Schuh | 4c84abb | 2021-06-20 18:16:32 -0700 | [diff] [blame] | 79 | uint32_t result; |
| 80 | if (!absl::SimpleAtoi(override_number, &result)) { |
Brian Silverman | 3dfbfb1 | 2020-02-17 20:35:18 -0800 | [diff] [blame] | 81 | LOG(FATAL) << "Error parsing AOS_TEAM_NUMBER: " << override_number; |
Brian Silverman | 8a6dac9 | 2015-02-21 20:08:24 -0500 | [diff] [blame] | 82 | } |
Brian Silverman | 3dfbfb1 | 2020-02-17 20:35:18 -0800 | [diff] [blame] | 83 | LOG(WARNING) |
| 84 | << "Team number overriden by AOS_TEAM_NUMBER environment variable to " |
| 85 | << result; |
| 86 | return result; |
Austin Schuh | be2456e | 2015-02-16 02:58:00 -0800 | [diff] [blame] | 87 | } |
Brian Silverman | 3dfbfb1 | 2020-02-17 20:35:18 -0800 | [diff] [blame] | 88 | const auto hostname = GetHostname(); |
| 89 | { |
| 90 | const auto result = team_number_internal::ParseRoborioTeamNumber(hostname); |
| 91 | if (result) { |
| 92 | LOG(INFO) << "roboRIO hostname team number is: " << *result; |
| 93 | return *result; |
| 94 | } |
| 95 | } |
Brian Silverman | 5f06ed2 | 2020-02-17 21:49:42 -0800 | [diff] [blame] | 96 | { |
| 97 | const auto result = team_number_internal::ParsePiTeamNumber(hostname); |
| 98 | if (result) { |
| 99 | LOG(INFO) << "Pi hostname team number is: " << *result; |
| 100 | return *result; |
| 101 | } |
| 102 | } |
Brian Silverman | 3dfbfb1 | 2020-02-17 20:35:18 -0800 | [diff] [blame] | 103 | LOG(FATAL) << "Failed to parse a team number from hostname: " << hostname; |
Brian Silverman | 431500a | 2013-10-28 19:50:15 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | } // namespace |
| 107 | |
Austin Schuh | 288479d | 2019-12-18 19:47:52 -0800 | [diff] [blame] | 108 | ::std::string GetHostname() { |
Jim Ostrowski | 8565b40 | 2020-02-29 20:26:53 -0800 | [diff] [blame] | 109 | if (FLAGS_override_hostname.empty()) { |
| 110 | char buf[256]; |
| 111 | buf[sizeof(buf) - 1] = '\0'; |
| 112 | PCHECK(gethostname(buf, sizeof(buf) - 1) == 0); |
| 113 | return buf; |
| 114 | } else { |
| 115 | return FLAGS_override_hostname; |
| 116 | } |
Austin Schuh | 288479d | 2019-12-18 19:47:52 -0800 | [diff] [blame] | 117 | } |
| 118 | |
Brian Silverman | 431500a | 2013-10-28 19:50:15 -0700 | [diff] [blame] | 119 | uint16_t GetTeamNumber() { |
Brian Silverman | 3dfbfb1 | 2020-02-17 20:35:18 -0800 | [diff] [blame] | 120 | const static uint16_t result = DoGetTeamNumber(); |
John Park | a19d1a9 | 2019-11-06 18:28:14 -0800 | [diff] [blame] | 121 | return result; |
Brian Silverman | 431500a | 2013-10-28 19:50:15 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Brian Silverman | de0a53a | 2014-02-10 22:27:44 -0800 | [diff] [blame] | 124 | void OverrideTeamNumber(uint16_t team) { override_team = team; } |
| 125 | |
Austin Schuh | 4c84abb | 2021-06-20 18:16:32 -0700 | [diff] [blame] | 126 | std::optional<uint16_t> ParsePiNumber(const std::string_view hostname) { |
Austin Schuh | 1f2996e | 2020-03-15 23:09:00 -0700 | [diff] [blame] | 127 | if (hostname.substr(0, 3) != "pi-") { |
| 128 | return std::nullopt; |
| 129 | } |
| 130 | size_t first_separator = hostname.find('-'); |
| 131 | if (first_separator == hostname.npos || |
| 132 | first_separator >= hostname.size() - 2) { |
| 133 | return std::nullopt; |
| 134 | } |
| 135 | ++first_separator; |
| 136 | const size_t second_separator = hostname.find('-', first_separator); |
| 137 | if (second_separator == hostname.npos) { |
| 138 | return std::nullopt; |
| 139 | } |
Austin Schuh | 4c84abb | 2021-06-20 18:16:32 -0700 | [diff] [blame] | 140 | const std::string_view number_string = hostname.substr( |
Jim Ostrowski | 5d5a44f | 2020-06-24 19:10:15 -0700 | [diff] [blame] | 141 | second_separator + 1, hostname.size() - second_separator - 1); |
Austin Schuh | 1f2996e | 2020-03-15 23:09:00 -0700 | [diff] [blame] | 142 | if (number_string.size() == 0) { |
| 143 | return std::nullopt; |
| 144 | } |
| 145 | |
| 146 | int number; |
Austin Schuh | 4c84abb | 2021-06-20 18:16:32 -0700 | [diff] [blame] | 147 | if (!absl::SimpleAtoi(number_string, &number)) { |
Austin Schuh | 1f2996e | 2020-03-15 23:09:00 -0700 | [diff] [blame] | 148 | return std::nullopt; |
| 149 | } |
| 150 | return number; |
| 151 | } |
| 152 | |
Brian Silverman | 431500a | 2013-10-28 19:50:15 -0700 | [diff] [blame] | 153 | } // namespace network |
| 154 | } // namespace aos |