mirror of https://github.com/commaai/cereal.git
Unbridge - for republishing msgs sent from your laptop to openpilot (#160)
* duplicate bridge * unbridge! * some fixes * combine bridge * one if * rm unbridge * add --unbridge arg * ip and unbridge arg parsing. segfaults if not given any ip value * this is better than a python cli interface for now * where did those tabs come from? * bridge --unbridge is a little cumbersome to type... ...and only republish test messages * more clear * update * only check --ip * assume reverse if anything specified as ip * refactor * better ordering * order imports * use a whitelist * combine into get_services
This commit is contained in:
parent
cde8667d3b
commit
127edd520f
|
@ -1,59 +1,74 @@
|
||||||
#include <iostream>
|
#include <algorithm>
|
||||||
#include <string>
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <csignal>
|
#include <csignal>
|
||||||
|
#include <iostream>
|
||||||
#include <map>
|
#include <map>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
typedef void (*sighandler_t)(int sig);
|
typedef void (*sighandler_t)(int sig);
|
||||||
|
|
||||||
#include "services.h"
|
|
||||||
|
|
||||||
#include "impl_msgq.h"
|
#include "impl_msgq.h"
|
||||||
#include "impl_zmq.h"
|
#include "impl_zmq.h"
|
||||||
|
#include "services.h"
|
||||||
|
|
||||||
void sigpipe_handler(int sig) {
|
void sigpipe_handler(int sig) {
|
||||||
assert(sig == SIGPIPE);
|
assert(sig == SIGPIPE);
|
||||||
std::cout << "SIGPIPE received" << std::endl;
|
std::cout << "SIGPIPE received" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::vector<std::string> get_services() {
|
static std::vector<std::string> get_services(std::string whitelist_str, bool zmq_to_msgq) {
|
||||||
std::vector<std::string> name_list;
|
std::vector<std::string> service_list;
|
||||||
|
|
||||||
for (const auto& it : services) {
|
for (const auto& it : services) {
|
||||||
std::string name = it.name;
|
std::string name = it.name;
|
||||||
if (name == "plusFrame" || name == "uiLayoutState") continue;
|
bool in_whitelist = whitelist_str.find(name) != std::string::npos;
|
||||||
name_list.push_back(name);
|
if (name == "plusFrame" || name == "uiLayoutState" || (zmq_to_msgq && !in_whitelist)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
service_list.push_back(name);
|
||||||
}
|
}
|
||||||
|
return service_list;
|
||||||
return name_list;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int main(int argc, char** argv) {
|
||||||
int main(void){
|
|
||||||
signal(SIGPIPE, (sighandler_t)sigpipe_handler);
|
signal(SIGPIPE, (sighandler_t)sigpipe_handler);
|
||||||
|
|
||||||
auto endpoints = get_services();
|
bool zmq_to_msgq = argc > 2;
|
||||||
|
std::string ip = zmq_to_msgq ? argv[1] : "127.0.0.1";
|
||||||
|
std::string whitelist_str = zmq_to_msgq ? std::string(argv[2]) : "";
|
||||||
|
|
||||||
std::map<SubSocket*, PubSocket*> sub2pub;
|
Poller *poller;
|
||||||
|
Context *pub_context;
|
||||||
Context *zmq_context = new ZMQContext();
|
Context *sub_context;
|
||||||
Context *msgq_context = new MSGQContext();
|
if (zmq_to_msgq) { // republishes zmq debugging messages as msgq
|
||||||
Poller *poller = new MSGQPoller();
|
poller = new ZMQPoller();
|
||||||
|
pub_context = new MSGQContext();
|
||||||
for (auto endpoint: endpoints){
|
sub_context = new ZMQContext();
|
||||||
SubSocket * msgq_sock = new MSGQSubSocket();
|
} else {
|
||||||
msgq_sock->connect(msgq_context, endpoint, "127.0.0.1", false);
|
poller = new MSGQPoller();
|
||||||
poller->registerSocket(msgq_sock);
|
pub_context = new ZMQContext();
|
||||||
|
sub_context = new MSGQContext();
|
||||||
PubSocket * zmq_sock = new ZMQPubSocket();
|
|
||||||
zmq_sock->connect(zmq_context, endpoint);
|
|
||||||
|
|
||||||
sub2pub[msgq_sock] = zmq_sock;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::map<SubSocket*, PubSocket*> sub2pub;
|
||||||
|
for (auto endpoint: get_services(whitelist_str, zmq_to_msgq)) {
|
||||||
|
PubSocket * pub_sock;
|
||||||
|
SubSocket * sub_sock;
|
||||||
|
if (zmq_to_msgq) {
|
||||||
|
pub_sock = new MSGQPubSocket();
|
||||||
|
sub_sock = new ZMQSubSocket();
|
||||||
|
} else {
|
||||||
|
pub_sock = new ZMQPubSocket();
|
||||||
|
sub_sock = new MSGQSubSocket();
|
||||||
|
}
|
||||||
|
pub_sock->connect(pub_context, endpoint);
|
||||||
|
sub_sock->connect(sub_context, endpoint, ip, false);
|
||||||
|
|
||||||
while (true){
|
poller->registerSocket(sub_sock);
|
||||||
for (auto sub_sock : poller->poll(100)){
|
sub2pub[sub_sock] = pub_sock;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
for (auto sub_sock : poller->poll(100)) {
|
||||||
Message * msg = sub_sock->receive();
|
Message * msg = sub_sock->receive();
|
||||||
if (msg == NULL) continue;
|
if (msg == NULL) continue;
|
||||||
sub2pub[sub_sock]->sendMessage(msg);
|
sub2pub[sub_sock]->sendMessage(msg);
|
||||||
|
|
Loading…
Reference in New Issue