-
Notifications
You must be signed in to change notification settings - Fork 0
/
Member.h
126 lines (119 loc) · 3.05 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
/**********************************
* 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));
}
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);
}
void init() {
memset(&addr, 0, sizeof(addr));
}
};
/**
* CLASS NAME: MemberListEntry
*
* DESCRIPTION: Entry in the membership list
*/
class MemberListEntry {
public:
int id;
short port;
long heartbeat;
long timestamp;
MemberListEntry(int id, short port, long heartbeat, long timestamp);
MemberListEntry(int id, short port);
MemberListEntry(): id(0), port(0), heartbeat(0), timestamp(0) {}
MemberListEntry(const MemberListEntry &anotherMLE);
MemberListEntry& operator =(const MemberListEntry &anotherMLE);
int getid();
short getport();
long getheartbeat();
long gettimestamp();
void setid(int id);
void setport(short port);
void setheartbeat(long hearbeat);
void settimestamp(long timestamp);
};
/**
* 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: how many ping sent in the current time slot
int pingCounter;
// counter for ping timeout: send ping
int timeOutCounter;
// Membership table
unordered_map<int, MemberListEntry> memberList;
// falied Membership table
unordered_map<int, MemberListEntry> failedMemberList;
// My position in the membership table
unordered_map<int, MemberListEntry>::iterator myPos;
// Queue for failure detection messages
queue<q_elt> mp1q;
/**
* Constructor
*/
Member(): inited(false), inGroup(false), bFailed(false), nnb(0), heartbeat(0), pingCounter(0), timeOutCounter(0) {}
// copy constructor
Member(const Member &anotherMember);
// Assignment operator overloading
Member& operator =(const Member &anotherMember);
virtual ~Member() {}
};
#endif /* MEMBER_H_ */