-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.cpp
More file actions
86 lines (66 loc) · 1.85 KB
/
utils.cpp
File metadata and controls
86 lines (66 loc) · 1.85 KB
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
#include <stdlib.h>
#include <sstream>
#include <iostream>
#include "dotenv.h"
#include "constants.hpp"
#include "utils.hpp"
#include "replicaservice_types.h"
#include "ReplicaService.h"
Entry
getEmptyLogEntry() {
Entry emptyLogEntry;
emptyLogEntry.type = EntryType::EMPTY_ENTRY;
emptyLogEntry.key = "";
emptyLogEntry.value = "";
emptyLogEntry.term = -1;
emptyLogEntry.clientIdentifier = "";
emptyLogEntry.requestIdentifier = std::numeric_limits<int>::max();
return emptyLogEntry;
}
unsigned int
getElectionTimeout() {
unsigned int minTimeMS = atoi(dotenv::env[MIN_ELECTION_TIMEOUT_ENV_VAR_NAME].c_str());
unsigned int maxTimeMS = atoi(dotenv::env[MAX_ELECTION_TIMEOUT_ENV_VAR_NAME].c_str());
std::cout << minTimeMS << "\n";
std::cout << maxTimeMS << "\n";
srand(time(0));
return (rand() % (maxTimeMS - minTimeMS)) + minTimeMS;
}
std::vector<ID>
getMemberIDs(const std::vector<std::string>& socketAddrs) {
std::vector<ID> membership;
for(const std::string& addr : socketAddrs) {
std::stringstream ss(addr);
std::string host;
std::string portStr;
getline(ss, host, ':');
getline(ss, portStr, ':');
ID id;
id.hostname = host;
id.port = atoi(portStr.c_str());
membership.push_back(id);
}
return membership;
}
ID
getNullID() {
ID nullID;
nullID.hostname = "";
nullID.port = 0;
return nullID;
}
bool
isANullID(const ID& id) {
return id.hostname == "" && id.port == 0;
}
bool
areAMajorityGreaterThanOrEqual(std::vector<int> numLst, int num) {
unsigned int numForMajority = (numLst.size() / 2) + 1;
unsigned int numGreaterThanOrEqual = 0;
for(const int& currNum : numLst) {
if(currNum >= num) {
++numGreaterThanOrEqual;
}
}
return numGreaterThanOrEqual >= numForMajority;
}