From c9260ea7853fac6b58ffa277eb2028e40b955c66 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Sun, 19 Jul 2015 23:17:14 -0700 Subject: [PATCH] Change GetEntryTypeFunc to std::function. Also implement it as a member function of Dispatcher. --- src/Dispatcher.cpp | 21 ++++++++++++++------- src/Dispatcher.h | 8 ++++++++ src/Message.h | 3 ++- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/Dispatcher.cpp b/src/Dispatcher.cpp index bb79e27126..ba8e466fe9 100644 --- a/src/Dispatcher.cpp +++ b/src/Dispatcher.cpp @@ -16,11 +16,6 @@ using namespace nt; std::unique_ptr Dispatcher::m_instance; -static NT_Type GetEntryType(unsigned int id) { - // TODO - return NT_UNASSIGNED; -} - Dispatcher::Dispatcher() : m_server(false), m_active(false), @@ -145,7 +140,9 @@ void Dispatcher::ServerThreadMain(const char* listen_address, // add to connections list Connection conn; - conn.net.reset(new NetworkConnection(std::move(stream), GetEntryType)); + conn.net.reset(new NetworkConnection( + std::move(stream), + [this](unsigned int id) { return GetEntryType(id); })); conn.net->Start(); AddConnection(std::move(conn)); } @@ -171,7 +168,9 @@ void Dispatcher::ClientThreadMain(const char* server_name, unsigned int port) { DEBUG("client connected"); Connection conn; - conn.net.reset(new NetworkConnection(std::move(stream), GetEntryType)); + conn.net.reset(new NetworkConnection( + std::move(stream), + [this](unsigned int id) { return GetEntryType(id); })); conn.net->set_proto_rev(proto_rev); conn.net->Start(); @@ -262,3 +261,11 @@ void Dispatcher::AddConnection(Connection&& conn) { std::lock_guard lock(m_user_mutex); m_connections.push_back(std::move(conn)); } + +NT_Type Dispatcher::GetEntryType(unsigned int id) const { + std::lock_guard lock(m_idmap_mutex); + if (id >= m_idmap.size()) return NT_UNASSIGNED; + auto value = m_idmap[id]->value(); + if (!value) return NT_UNASSIGNED; + return value->type(); +} diff --git a/src/Dispatcher.h b/src/Dispatcher.h index c37fe00531..0ea194adff 100644 --- a/src/Dispatcher.h +++ b/src/Dispatcher.h @@ -19,6 +19,7 @@ #include "llvm/StringRef.h" #include "NetworkConnection.h" +#include "Storage.h" class TCPAcceptor; @@ -53,6 +54,8 @@ class Dispatcher { void ClientReconnect(); + NT_Type GetEntryType(unsigned int id) const; + struct Connection { enum State { }; @@ -89,6 +92,11 @@ class Dispatcher { std::condition_variable m_reconnect_cv; bool m_do_reconnect; + // Map from integer id to storage entry. Id is 16-bit, so just use a vector. + mutable std::mutex m_idmap_mutex; + std::vector> m_idmap; + + // Global instance static std::unique_ptr m_instance; }; diff --git a/src/Message.h b/src/Message.h index e3fcc78235..57bf620b05 100644 --- a/src/Message.h +++ b/src/Message.h @@ -8,6 +8,7 @@ #ifndef NT_MESSAGE_H_ #define NT_MESSAGE_H_ +#include #include #include @@ -38,7 +39,7 @@ class Message { kExecuteRpc = 0x20, kRpcResponse = 0x21 }; - typedef NT_Type (*GetEntryTypeFunc)(unsigned int id); + typedef std::function GetEntryTypeFunc; Message() : m_type(kUnknown), m_id(0), m_flags(0), m_seq_num_uid(0) {} Message(MsgType type, const private_init&)