diff --git a/messaging/bridge.cc b/messaging/bridge.cc index 2ab751ce2..4a5390caf 100644 --- a/messaging/bridge.cc +++ b/messaging/bridge.cc @@ -24,7 +24,7 @@ void sigpipe_handler(int sig) { static std::vector get_services(std::string whitelist_str, bool zmq_to_msgq) { std::vector service_list; for (const auto& it : services) { - std::string name = it.name; + std::string name = it.second.name; bool in_whitelist = whitelist_str.find(name) != std::string::npos; if (name == "plusFrame" || name == "uiLayoutState" || (zmq_to_msgq && !in_whitelist)) { continue; diff --git a/messaging/impl_msgq.cc b/messaging/impl_msgq.cc index 18e936449..8f2c10a08 100644 --- a/messaging/impl_msgq.cc +++ b/messaging/impl_msgq.cc @@ -17,12 +17,7 @@ void sig_handler(int signal) { } static bool service_exists(std::string path){ - for (const auto& it : services) { - if (it.name == path) { - return true; - } - } - return false; + return services.count(path) > 0; } diff --git a/messaging/impl_zmq.cc b/messaging/impl_zmq.cc index 5355c8110..aa95d94fb 100644 --- a/messaging/impl_zmq.cc +++ b/messaging/impl_zmq.cc @@ -8,17 +8,7 @@ #include "cereal/messaging/impl_zmq.h" static int get_port(std::string endpoint) { - int port = -1; - for (const auto& it : services) { - std::string name = it.name; - if (name == endpoint) { - port = it.port; - break; - } - } - - assert(port >= 0); - return port; + return services.at(endpoint).port; } ZMQContext::ZMQContext() { diff --git a/messaging/socketmaster.cc b/messaging/socketmaster.cc index 50614be36..f0f88dac8 100644 --- a/messaging/socketmaster.cc +++ b/messaging/socketmaster.cc @@ -15,13 +15,6 @@ static inline uint64_t nanos_since_boot() { return t.tv_sec * 1000000000ULL + t.tv_nsec; } -static const service *get_service(const char *name) { - for (const auto &it : services) { - if (strcmp(it.name, name) == 0) return ⁢ - } - return nullptr; -} - static inline bool inList(const std::vector &list, const char *value) { for (auto &v : list) { if (strcmp(value, v) == 0) return true; @@ -61,8 +54,9 @@ SubMaster::SubMaster(const std::vector &service_list, const std::v const char *address, const std::vector &ignore_alive) { poller_ = Poller::create(); for (auto name : service_list) { - const service *serv = get_service(name); - assert(serv != nullptr); + assert(services.count(std::string(name)) > 0); + + service serv = services.at(std::string(name)); SubSocket *socket = SubSocket::create(message_context.context(), name, address ? address : "127.0.0.1", true); assert(socket != 0); bool is_polled = inList(poll, name) || poll.empty(); @@ -70,7 +64,7 @@ SubMaster::SubMaster(const std::vector &service_list, const std::v SubMessage *m = new SubMessage{ .name = name, .socket = socket, - .freq = serv->frequency, + .freq = serv.frequency, .ignore_alive = inList(ignore_alive, name), .allocated_msg_reader = malloc(sizeof(capnp::FlatArrayMessageReader)), .is_polled = is_polled}; @@ -199,7 +193,7 @@ SubMaster::~SubMaster() { PubMaster::PubMaster(const std::vector &service_list) { for (auto name : service_list) { - assert(get_service(name) != nullptr); + assert(services.count(name) > 0); PubSocket *socket = PubSocket::create(message_context.context(), name); assert(socket); sockets_[name] = socket; diff --git a/services.py b/services.py index a7673229e..da99a4094 100755 --- a/services.py +++ b/services.py @@ -105,14 +105,18 @@ def build_header(): h += "/* THIS IS AN AUTOGENERATED FILE, PLEASE EDIT services.py */\n" h += "#ifndef __SERVICES_H\n" h += "#define __SERVICES_H\n" - h += "struct service { char name[0x100]; int port; bool should_log; int frequency; int decimation; };\n" - h += "static struct service services[] = {\n" + + h += "#include \n" + + h += "struct service { std::string name; int port; bool should_log; int frequency; int decimation; };\n" + h += "static std::map services = {\n" for k, v in service_list.items(): should_log = "true" if v.should_log else "false" decimation = -1 if v.decimation is None else v.decimation - h += ' { "%s", %d, %s, %d, %d },\n' % \ - (k, v.port, should_log, v.frequency, decimation) + h += ' { "%s", {"%s", %d, %s, %d, %d}},\n' % \ + (k, k, v.port, should_log, v.frequency, decimation) h += "};\n" + h += "#endif\n" return h