| | |
| | | #include "bh_util.h" |
| | | #include "defs.h" |
| | | #include "msg.h" |
| | | #include <chrono> |
| | | #include <condition_variable> |
| | | |
| | | using namespace bhome_msg; |
| | | using namespace bhome_shm; |
| | | |
| | | namespace |
| | | ShmSocket::ShmSocket(Shm &shm, const MQId id, const int len) : |
| | | run_(false), mq_(id, shm, len) |
| | | { |
| | | |
| | | } // namespace |
| | | |
| | | //TODO maybe change to base class, each type is a sub class. |
| | | |
| | | ShmSocket::ShmSocket(Type type, bhome_shm::SharedMemory &shm) : |
| | | shm_(shm), type_(type), run_(false) |
| | | { |
| | | switch (type) { |
| | | case eSockBus: mq_.reset(new Queue(kBHBusQueueId, shm_, 1000)); break; |
| | | case eSockRequest: mq_.reset(new Queue(shm_, 12)); break; |
| | | case eSockReply: mq_.reset(new Queue(shm_, 64)); break; |
| | | case eSockSubscribe: mq_.reset(new Queue(shm_, 64)); break; |
| | | case eSockPublish: break; // no recv mq needed |
| | | default: break; |
| | | } |
| | | Start(); |
| | | } |
| | | |
| | | ShmSocket::ShmSocket(Type type) : |
| | | ShmSocket(type, BHomeShm()) {} |
| | | ShmSocket::ShmSocket(Shm &shm, const bool create_or_else_find, const MQId id, const int len) : |
| | | run_(false), mq_(id, create_or_else_find, shm, len) |
| | | { |
| | | Start(); |
| | | } |
| | | ShmSocket::ShmSocket(bhome_shm::SharedMemory &shm, const int len) : |
| | | run_(false), mq_(shm, len) |
| | | { |
| | | Start(); |
| | | } |
| | | |
| | | ShmSocket::~ShmSocket() |
| | | { |
| | | Stop(); |
| | | } |
| | | |
| | | bool ShmSocket::Publish(const std::string &topic, const void *data, const size_t size, const int timeout_ms) |
| | | bool ShmSocket::Start(const RawRecvCB &onData, const IdleCB &onIdle, int nworker) |
| | | { |
| | | if (type_ != eSockPublish) { |
| | | return false; |
| | | } |
| | | assert(!mq_); |
| | | try { |
| | | MsgI imsg; |
| | | if (!imsg.MakeRC(shm_, MakePub(topic, data, size))) { |
| | | return false; |
| | | } |
| | | DEFER1(imsg.Release(shm_)); |
| | | return Queue::Send(shm_, kBHBusQueueId, imsg, timeout_ms); |
| | | } catch (...) { |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | bool ShmSocket::Subscribe(const std::vector<std::string> &topics, const int timeout_ms) |
| | | { |
| | | if (type_ != eSockSubscribe) { |
| | | return false; |
| | | } |
| | | assert(mq_); |
| | | try { |
| | | return mq_->Send(kBHBusQueueId, MakeSub(mq_->Id(), topics), timeout_ms); |
| | | } catch (...) { |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | bool ShmSocket::StartRaw(const RecvRawCB &onData, int nworker) |
| | | { |
| | | auto CanRecv = [this]() { |
| | | switch (type_) { |
| | | case eSockRequest: |
| | | case eSockReply: |
| | | case eSockBus: |
| | | case eSockSubscribe: |
| | | return true; |
| | | default: |
| | | return false; |
| | | } |
| | | }; |
| | | if (!CanRecv()) { |
| | | return false; |
| | | } |
| | | std::lock_guard<std::mutex> lock(mutex_); |
| | | |
| | | StopNoLock(); |
| | | auto RecvProc = [this, onData]() { |
| | | while (run_) { |
| | | try { |
| | | MsgI imsg; |
| | | DEFER1(imsg.Release(shm_)); |
| | | if (mq_->Recv(imsg, 100)) { onData(imsg); } |
| | | } catch (...) { |
| | | auto ioProc = [this, onData, onIdle]() { |
| | | auto DoSend = [this]() { return send_buffer_.TrySend(mq()); }; |
| | | auto DoRecv = [=] { |
| | | // do not recv if no cb is set. |
| | | if (!onData) { |
| | | return false; |
| | | } |
| | | auto onMsg = [&](MsgI &imsg) { |
| | | DEFER1(imsg.Release()); |
| | | onData(*this, imsg); |
| | | }; |
| | | MsgI imsg; |
| | | return mq().TryRecv(imsg) ? (onMsg(imsg), true) : false; |
| | | }; |
| | | |
| | | try { |
| | | bool more_to_send = DoSend(); |
| | | bool more_to_recv = DoRecv(); |
| | | if (onIdle) { onIdle(*this); } |
| | | if (!more_to_send && !more_to_recv) { |
| | | robust::QuickSleep(); |
| | | } |
| | | } catch (...) { |
| | | } |
| | | }; |
| | | |
| | | std::lock_guard<std::mutex> lock(mutex_); |
| | | StopNoLock(); |
| | | |
| | | run_.store(true); |
| | | for (int i = 0; i < nworker; ++i) { |
| | | workers_.emplace_back(RecvProc); |
| | | workers_.emplace_back([this, ioProc]() { while (run_) { ioProc(); } }); |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | bool ShmSocket::Start(const RecvCB &onData, int nworker) |
| | | bool ShmSocket::Start(int nworker, const RecvCB &onData, const IdleCB &onIdle) |
| | | { |
| | | return StartRaw([this, onData](MsgI &imsg) { BHMsg m; if (imsg.Unpack(m)) { onData(m); } }, nworker); |
| | | } |
| | | auto ioProc = [this, onData, onIdle]() { |
| | | auto DoSend = [this]() { return send_buffer_.TrySend(mq()); }; |
| | | auto DoRecv = [=] { |
| | | auto onRecvWithPerMsgCB = [this, onData](ShmSocket &socket, MsgI &imsg, BHMsgHead &head) { |
| | | RecvCB cb; |
| | | if (per_msg_cbs_->Pick(head.msg_id(), cb)) { |
| | | cb(socket, imsg, head); |
| | | } else if (onData) { |
| | | onData(socket, imsg, head); |
| | | } |
| | | }; |
| | | |
| | | bool ShmSocket::StartAsync(int nworker) |
| | | { |
| | | auto AsyncRecvProc = [this](BHMsg &msg) { |
| | | auto Find = [&](RecvCB &cb) { |
| | | std::lock_guard<std::mutex> lock(mutex_); |
| | | const std::string &msgid = msg.msg_id(); |
| | | auto pos = async_cbs_.find(msgid); |
| | | if (pos != async_cbs_.end()) { |
| | | cb.swap(pos->second); |
| | | async_cbs_.erase(pos); |
| | | return true; |
| | | } else { |
| | | // do not recv if no cb is set. |
| | | if (!onData && per_msg_cbs_->empty()) { |
| | | return false; |
| | | } |
| | | auto onMsg = [&](MsgI &imsg) { |
| | | DEFER1(imsg.Release()); |
| | | BHMsgHead head; |
| | | if (imsg.ParseHead(head)) { |
| | | onRecvWithPerMsgCB(*this, imsg, head); |
| | | } |
| | | }; |
| | | MsgI imsg; |
| | | return mq().TryRecv(imsg) ? (onMsg(imsg), true) : false; |
| | | }; |
| | | |
| | | RecvCB cb; |
| | | if (Find(cb) && cb) { |
| | | cb(msg); |
| | | try { |
| | | bool more_to_send = DoSend(); |
| | | bool more_to_recv = DoRecv(); |
| | | if (onIdle) { onIdle(*this); } |
| | | if (!more_to_send && !more_to_recv) { |
| | | robust::QuickSleep(); |
| | | } |
| | | } catch (...) { |
| | | } |
| | | }; |
| | | |
| | | return Start(AsyncRecvProc, nworker); |
| | | std::lock_guard<std::mutex> lock(mutex_); |
| | | StopNoLock(); |
| | | |
| | | run_.store(true); |
| | | for (int i = 0; i < nworker; ++i) { |
| | | workers_.emplace_back([this, ioProc]() { while (run_) { ioProc(); } }); |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | bool ShmSocket::Stop() |
| | |
| | | w.join(); |
| | | } |
| | | } |
| | | workers_.clear(); |
| | | return true; |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | bool ShmSocket::AsyncRequest(const void *remote, const void *pmsg, const int timeout_ms, const RecvCB &cb) |
| | | //maybe reimplment, using async cbs? |
| | | bool ShmSocket::SyncRecv(bhome_msg::MsgI &msg, bhome_msg::BHMsgHead &head, const int timeout_ms) |
| | | { |
| | | if (type_ != eSockRequest) { |
| | | return false; |
| | | } |
| | | assert(remote && pmsg && !mq_); |
| | | try { |
| | | const BHMsg &msg = *static_cast<const BHMsg *>(pmsg); |
| | | auto RegisterCB = [&]() { |
| | | std::lock_guard<std::mutex> lock(mutex_); |
| | | async_cbs_.emplace(msg.msg_id(), cb); |
| | | }; |
| | | |
| | | return mq_->Send(*static_cast<const MQId *>(remote), msg, timeout_ms, RegisterCB); |
| | | } catch (...) { |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | bool ShmSocket::SyncRequest(const void *remote, const void *msg, void *result, const int timeout_ms) |
| | | { |
| | | struct State { |
| | | std::mutex mutex; |
| | | std::condition_variable cv; |
| | | bool canceled = false; |
| | | }; |
| | | |
| | | try { |
| | | std::shared_ptr<State> st(new State); |
| | | auto OnRecv = [=](BHMsg &msg) { |
| | | std::unique_lock<std::mutex> lk(st->mutex); |
| | | if (!st->canceled) { |
| | | static_cast<BHMsg *>(result)->Swap(&msg); |
| | | st->cv.notify_one(); |
| | | } |
| | | }; |
| | | |
| | | std::unique_lock<std::mutex> lk(st->mutex); |
| | | auto end = std::chrono::steady_clock::now() + std::chrono::milliseconds(timeout_ms); |
| | | if (AsyncRequest(remote, msg, timeout_ms, OnRecv) && st->cv.wait_until(lk, end) == std::cv_status::no_timeout) { |
| | | // std::lock_guard<std::mutex> lock(mutex_); // seems no need to lock mutex_. |
| | | bool got = (timeout_ms == 0) ? mq().TryRecv(msg) : mq().Recv(msg, timeout_ms); |
| | | if (got) { |
| | | if (msg.ParseHead(head)) { |
| | | return true; |
| | | } else { |
| | | st->canceled = true; |
| | | return false; |
| | | } |
| | | } catch (...) { |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | bool ShmSocket::QueryRPCTopic(const std::string &topic, bhome::msg::BHAddress &addr, const int timeout_ms) |
| | | { |
| | | BHMsg result; |
| | | const BHMsg &msg = MakeQueryTopic(topic); |
| | | if (SyncRequest(&kBHTopicRPCId, &msg, &result, timeout_ms)) { |
| | | if (result.type() == kMsgTypeQueryTopicReply) { |
| | | DataQueryTopicReply reply; |
| | | if (reply.ParseFromString(result.body())) { |
| | | addr = reply.address(); |
| | | return !addr.mq_id().empty(); |
| | | } |
| | | msg.Release(); |
| | | } |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | bool ShmSocket::RequestRPC(const std::string &topic, const void *data, const size_t size, const int timeout_ms, const RequestResultCB &cb) |
| | | { |
| | | auto Call = [&](const void *remote) { |
| | | const BHMsg &msg(MakeRequest(mq_->Id(), topic, data, size)); |
| | | auto onRecv = [cb](BHMsg &msg) { |
| | | if (msg.type() == kMsgTypeReply) { |
| | | DataReply reply; |
| | | if (reply.ParseFromString(msg.body())) { |
| | | cb(reply.data().data(), reply.data().size()); |
| | | } |
| | | } |
| | | }; |
| | | return AsyncRequest(remote, &msg, timeout_ms, onRecv); |
| | | }; |
| | | |
| | | try { |
| | | BHAddress addr; |
| | | if (QueryRPCTopic(topic, addr, timeout_ms)) { |
| | | return Call(addr.mq_id().data()); |
| | | } |
| | | } catch (...) { |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | bool ShmSocket::RequestRPC(const std::string &topic, const void *data, const size_t size, const int timeout_ms, std::string &out) |
| | | { |
| | | try { |
| | | BHAddress addr; |
| | | if (QueryRPCTopic(topic, addr, timeout_ms)) { |
| | | const BHMsg &msg(MakeRequest(mq_->Id(), topic, data, size)); |
| | | BHMsg reply; |
| | | if (SyncRequest(addr.mq_id().data(), &msg, &reply, timeout_ms) && reply.type() == kMsgTypeReply) { |
| | | DataReply dr; |
| | | if (dr.ParseFromString(msg.body())) { |
| | | dr.mutable_data()->swap(out); |
| | | return true; |
| | | } |
| | | } |
| | | } |
| | | } catch (...) { |
| | | return false; |
| | | } |
| | | } |