-
Notifications
You must be signed in to change notification settings - Fork 8
/
client.hpp
132 lines (102 loc) · 3.56 KB
/
client.hpp
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#ifndef __CLIENT_H__
#define __CLIENT_H__
#define NOT_AN_IP_TOKEN "dummy"
#include <iostream>
#include <memory>
#include <string>
#include <cstdlib>
#include <queue>
#include <regex>
#include <grpc++/grpc++.h>
#include "node.grpc.pb.h"
#include "encoding_helpers.hpp"
#include "processor.hpp"
class block;
struct transaction;
using grpc::Channel;
using grpc::ClientContext;
using grpc::Status;
using onevote::Empty;
using onevote::BlockMsg;
using onevote::TransactionMsg;
using onevote::AddrRequest;
using onevote::AddrResponse;
using onevote::TransactionRequest;
using onevote::BlockRequest;
using onevote::Miner;
/*
Implements a specific gRPC client that connects with Servers running
the Miner::Service
*/
class SinglePeerClient {
public:
// Initializes the SinglePeerClient which requires a gRPC channel an address to connect to
// and the peer's own client.
SinglePeerClient(std::shared_ptr<Channel> channel, std::string my_addr, std::string addr);
// Broadcasts a block to the peer
Status BroadcastBlock(block* block);
// Broadcasts a transaction to the peer
Status BroadcastTransaction(transaction* transaction);
// Asks for a block from the peer, returns NULL if they don't return
// or if they don't have the plock
block* GetBlock(char* block_hash);
// Returns the address of the peer
std::string* peerAddr();
// Gets all of the peers that the peer has
AddrResponse GetAddr();
// Sends a heartbeat to the peer, returns true if its
// alive
bool GetHeartbeat();
private:
// The client's IP_ADDRESS:PORT
std::string my_addr_;
// The peer's IP_ADDRESS:PORT
std::string addr_;
// Used for gRPC
std::unique_ptr<Miner::Stub> stub_;
};
/*
This class defined a communications Client API that is consumed by a processor.
It implements a wrapper around a list of SinglePeerClients.
*/
class Client {
public:
// Initializes the Client, it must know its own address and an initial
// peer to make a connection to. It will then bootstrap from that peer.
Client(std::string own_address, std::string first_peer);
// Sends a block to all the peers in its network
void BroadcastBlock(block* block);
// Sends a transaction to all the peers in its network
void BroadcastTransaction(transaction* transaction);
// Sends a heartbeat to all peers in its network and returns
// the number of peers that are still alive.
int checkHeartbeats();
// Asks peers in its network for their peers and then
// adds them to client's peer network. Returns the number
// of peers actve in its network.
int bootstrapPeers();
// Returns a peers list to be consumed by server
std::list<std::string*>* getPeersList();
// Adds a new peer to client's neetwork and ensures
// invariants.
void addNewPeer(std::string addr);
// Asks each of its peers in the network for
// a specific block until it finds it. Returns NULL
// otherwise
block* getBlock(char* block_hash);
private:
// The first peer that the client connects to
std::string first_peer_;
// The client's own address, needed when sending
// GetAddr requests
std::string my_address_;
// List of peers that this is connected ot
// AddNewPeer will ensure that there are no duplicates
// and that each peer has a valid IP.
std::list<SinglePeerClient*> peer_clients_;
// Callback to be used for checkHeartbeats
bool successHearbeat(const SinglePeerClient*& peer_client);
// Used to make logs
void clientLog(std::string message);
};
#endif