Change GetEntryTypeFunc to std::function.

Also implement it as a member function of Dispatcher.
This commit is contained in:
Peter Johnson
2015-07-19 23:17:14 -07:00
parent c9ca2f902e
commit c9260ea785
3 changed files with 24 additions and 8 deletions

View File

@@ -16,11 +16,6 @@ using namespace nt;
std::unique_ptr<Dispatcher> 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<std::mutex> lock(m_user_mutex);
m_connections.push_back(std::move(conn));
}
NT_Type Dispatcher::GetEntryType(unsigned int id) const {
std::lock_guard<std::mutex> 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();
}

View File

@@ -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<std::shared_ptr<StorageEntry>> m_idmap;
// Global instance
static std::unique_ptr<Dispatcher> m_instance;
};

View File

@@ -8,6 +8,7 @@
#ifndef NT_MESSAGE_H_
#define NT_MESSAGE_H_
#include <functional>
#include <memory>
#include <string>
@@ -38,7 +39,7 @@ class Message {
kExecuteRpc = 0x20,
kRpcResponse = 0x21
};
typedef NT_Type (*GetEntryTypeFunc)(unsigned int id);
typedef std::function<NT_Type(unsigned int id)> GetEntryTypeFunc;
Message() : m_type(kUnknown), m_id(0), m_flags(0), m_seq_num_uid(0) {}
Message(MsgType type, const private_init&)