-
Notifications
You must be signed in to change notification settings - Fork 0
/
Member.h
174 lines (152 loc) · 3.74 KB
/
Member.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**********************************
* FILE NAME: Member.h
*
* DESCRIPTION: Definition of all Member related class
**********************************/
#ifndef MEMBER_H_
#define MEMBER_H_
#include "stdincludes.h"
/**
* CLASS NAME: q_elt
*
* DESCRIPTION: Entry in the queue
*/
class q_elt {
public:
void *elt;
int size;
q_elt(void *elt, int size);
};
/**
* CLASS NAME: Address
*
* DESCRIPTION: Class representing the address of a single node
*/
class Address {
public:
char addr[6];
Address() {}
// Copy constructor
Address(const Address &anotherAddress);
// Overloaded = operator
Address& operator =(const Address &anotherAddress);
bool operator ==(const Address &anotherAddress);
Address(string address) {
size_t pos = address.find(":");
int id = stoi(address.substr(0, pos));
short port = (short)stoi(address.substr(pos + 1, address.size()-pos-1));
memcpy(&addr[0], &id, sizeof(int));
memcpy(&addr[4], &port, sizeof(short));
}
Address(int id, short port) {
memcpy(addr, &id, sizeof(int));
memcpy(&addr[4], &port, sizeof(short));
}
string getAddress() {
int id = 0;
short port;
memcpy(&id, &addr[0], sizeof(int));
memcpy(&port, &addr[4], sizeof(short));
return to_string(id) + ":" + to_string(port);
}
int getId() {
return *(int*)addr;
}
int getPort() {
return *(short*)&addr[4];
}
void setId(int id) {
memcpy(addr, &id, sizeof(int));
}
void setPort(short port) {
memcpy(&addr[4], &port, sizeof(short));
}
void init() {
memset(&addr, 0, sizeof(addr));
}
};
enum MemberListEntryState {
ACTIVE,
TENTATIVE,
BAD
};
/**
* CLASS NAME: MemberListEntry
*
* DESCRIPTION: Entry in the membership list
*/
class MemberListEntry {
public:
int id;
short port;
long heartbeat;
long timestamp;
Address address;
MemberListEntryState state;
MemberListEntry(int id, short port, long heartbeat, long timestamp, MemberListEntryState state);
MemberListEntry(int id, short port);
MemberListEntry(): id(0), port(0), heartbeat(0), timestamp(0),address(),state(BAD) {}
MemberListEntry(const MemberListEntry &anotherMLE);
MemberListEntry& operator =(const MemberListEntry &anotherMLE);
bool operator==(const Address& addr);
int getid();
short getport();
long getheartbeat();
long gettimestamp();
void setid(int id);
void setport(short port);
void setheartbeat(long hearbeat);
void settimestamp(long timestamp);
void swapAddr(Address& local, Address& other);
friend void Swap(MemberListEntry& MLE_1, MemberListEntry& MEL_2);
};
/**
* CLASS NAME: Member
*
* DESCRIPTION: Class representing a member in the distributed system
*/
// Declaration and definition here
class Member {
public:
// This member's Address
Address addr;
// boolean indicating if this member is up
bool inited;
// boolean indicating if this member is in the group
bool inGroup;
// boolean indicating if this member has failed
bool bFailed;
// number of my neighbors
int nnb;
// the node's own heartbeat
long heartbeat;
// counter for next ping
int pingCounter;
// counter for ping timeout
int timeOutCounter;
// Membership table
vector<MemberListEntry> memberList;
// My position in the membership table
vector<MemberListEntry>::iterator myPos;
// Queue for failure detection messages
queue<q_elt> mp1q;
size_t traPointer;
/**
* Constructor
*/
Member(): inited(false), inGroup(false), bFailed(false), nnb(0), heartbeat(0), pingCounter(0), timeOutCounter(0), traPointer(1){}
// copy constructor
Member(const Member &anotherMember);
// Assignment operator overloading
Member& operator =(const Member &anotherMember);
virtual ~Member() {}
};
#pragma pack(push)
#pragma pack(1)
class MemberMsg {
public:
Address address;
long memberHeartBeat;
};
#pragma pack(pop)
#endif /* MEMBER_H_ */