Skip to content

Commit

Permalink
make services a map (commaai#520)
Browse files Browse the repository at this point in the history
  • Loading branch information
adeebshihadeh authored Aug 23, 2023
1 parent 736a4cc commit fa580de
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 33 deletions.
2 changes: 1 addition & 1 deletion messaging/bridge.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void sigpipe_handler(int sig) {
static std::vector<std::string> get_services(std::string whitelist_str, bool zmq_to_msgq) {
std::vector<std::string> 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;
Expand Down
7 changes: 1 addition & 6 deletions messaging/impl_msgq.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}


Expand Down
12 changes: 1 addition & 11 deletions messaging/impl_zmq.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
16 changes: 5 additions & 11 deletions messaging/socketmaster.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 &it;
}
return nullptr;
}

static inline bool inList(const std::vector<const char *> &list, const char *value) {
for (auto &v : list) {
if (strcmp(value, v) == 0) return true;
Expand Down Expand Up @@ -61,16 +54,17 @@ SubMaster::SubMaster(const std::vector<const char *> &service_list, const std::v
const char *address, const std::vector<const char *> &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();
if (is_polled) poller_->registerSocket(socket);
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};
Expand Down Expand Up @@ -199,7 +193,7 @@ SubMaster::~SubMaster() {

PubMaster::PubMaster(const std::vector<const char *> &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;
Expand Down
12 changes: 8 additions & 4 deletions services.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <map>\n"

h += "struct service { std::string name; int port; bool should_log; int frequency; int decimation; };\n"
h += "static std::map<std::string, service> 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

Expand Down

0 comments on commit fa580de

Please sign in to comment.