-
Notifications
You must be signed in to change notification settings - Fork 0
/
CUnit.h
87 lines (67 loc) · 2.46 KB
/
CUnit.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#ifndef RING_CUNIT_H
#define RING_CUNIT_H
#include "EDirection.h"
#include "loggers.h"
#include <string>
#include <thread>
#include <mutex>
#include <grpcpp/grpcpp.h>
#include "unit.grpc.pb.h"
namespace RING
{
class CUnit : public ring::Unit::Service
{
public:
enum class EMessageType
{
ShareNodeId,
LeaderElected
};
struct SNeighbour
{
unsigned m_nodeId;
std::string m_addr;
};
using TCallback = std::function<void(EMessageType, unsigned)>;
CUnit(unsigned p_nodeId,
const std::shared_ptr<CLoggerBase>& p_logger,
const std::string& p_skeletonAddress,
const SNeighbour& p_neighbour,
const TCallback& p_callback)
: m_nodeId(p_nodeId)
, m_skeletonAddress(p_skeletonAddress)
, m_neighbour(p_neighbour)
, m_callback(p_callback)
, m_logger(p_logger)
{}
~CUnit();
// send messag to neighbour stub
void SendNodeId(unsigned p_nodeId);
// receives message from neighbour stub
grpc::Status sendNodeId(grpc::ServerContext* p_context,
const ring::sendNodeIdRequest* p_request,
ring::sendNodeIdResponse* p_reply) override;
// send messag to neighbour stub
void SendLeaderElected(unsigned p_leaderId);
// receives message from neighbour stub
grpc::Status sendLeaderElected(grpc::ServerContext* p_context,
const ring::sendLeaderElectedRequest* p_request,
ring::sendLeaderElectedResponse* p_reply) override;
void CreateSkeleton();
// creating a single thread serving as skeleton
void StartSkeleton();
void StartStub();
private:
std::shared_ptr<CLoggerBase> m_logger;
const unsigned m_nodeId;
const std::string m_skeletonAddress;
const SNeighbour m_neighbour;
bool m_initialized = false;
const TCallback m_callback;
std::shared_ptr<grpc::Channel> m_channel;
std::unique_ptr<ring::Unit::Stub> m_stub;
std::unique_ptr<grpc::Server> m_skeleton;
std::unique_ptr<std::thread> m_thread;
};
}
#endif