-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNode.h
76 lines (65 loc) · 1.72 KB
/
Node.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
#include "Chord.h"
#include <string>
#include <protocol/TBinaryProtocol.h>
#include <server/TSimpleServer.h>
#include <transport/TServerSocket.h>
#include <transport/TSocket.h>
#include <transport/TBufferTransports.h>
#include <iostream>
#include <stdio.h>
#include <vector>
#include <map>
#include <utility>
using namespace std;
using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::server;
using boost::shared_ptr;
using namespace ::mp2;
class Node {
public:
int id;
int port;
int start;
ChordClient* connection;
Node(int, int);
~Node();
void notify(int,int);
void current_pred(predecessor&);
void open_connection();
void close_connection();
pthread_mutex_t* m_mutex;
private:
shared_ptr<TSocket> socket;
shared_ptr<TTransport> transport;
shared_ptr<TProtocol> protocol;
};
Node::Node(int id, int port){
this->id = id;
this->port = port;
this->socket = shared_ptr<TSocket>(new TSocket("localhost", port));
this->transport = shared_ptr<TTransport>(new TBufferedTransport(socket));
this->protocol = shared_ptr<TProtocol>(new TBinaryProtocol(transport));
this->connection = new ChordClient(protocol);
}
void Node::notify(int new_id, int new_port){
open_connection();
this->connection->notify(new_id, new_port);
close_connection();
}
void Node::current_pred(predecessor& _return){
open_connection();
this->connection->current_pred(_return);
close_connection();
}
void Node::open_connection(){
this->transport->open();
}
void Node::close_connection(){
this->transport->close();
}
//how do destructors work again? haha
Node::~Node(){
delete this->connection;
}