-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
195 lines (154 loc) · 5.87 KB
/
utils.cpp
File metadata and controls
195 lines (154 loc) · 5.87 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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <netdb.h> // Add this include for addrinfo
#include <unistd.h> // Add this include for close
#include <random> // Add this include for std::random_device
#include <chrono> // Add this include for std::chrono
#include "utils.h"
using namespace std;
int setupTCPSocket(char *port){
struct addrinfo hints, *res, *tmp;
int socketfd, status;
memset(&hints, 0, sizeof(hints));
hints.ai_socktype = SOCK_STREAM;
hints.ai_family = AF_INET;
hints.ai_flags = AI_PASSIVE;
if((status = getaddrinfo(NULL, port, &hints, &res)) != 0){
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
}
tmp = res;
while(tmp){
if((socketfd = socket(tmp->ai_family, tmp->ai_socktype, tmp->ai_protocol)) == -1){
perror("server: socket");
tmp = tmp->ai_next;
continue;
}
int opt = 1;
setsockopt(socketfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
if(::bind(socketfd, tmp->ai_addr, tmp->ai_addrlen) == -1){
perror("server: bind");
close(socketfd);
tmp = tmp->ai_next;
continue;
}
break;
}
freeaddrinfo(res);
if(tmp == NULL){
fprintf(stderr, "server: failed to bind tcp\n");
exit(1);
}
cout<<"Socket binded\n";
return socketfd;
}
int setupSocket(char *port) {
struct addrinfo hints, *res, *tmp;
int socketfd, status;
memset(&hints, 0, sizeof(hints));
hints.ai_socktype = SOCK_DGRAM;
hints.ai_family = AF_INET;
hints.ai_flags = AI_PASSIVE;
if((status = getaddrinfo(NULL, port, &hints, &res)) != 0){
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
}
tmp = res;
while(tmp){
if((socketfd = socket(tmp->ai_family, tmp->ai_socktype, tmp->ai_protocol)) == -1){
perror("server: socket");
tmp = tmp->ai_next;
continue;
}
if(::bind(socketfd, tmp->ai_addr, tmp->ai_addrlen) == -1){
perror("server: bind");
close(socketfd);
tmp = tmp->ai_next;
continue;
}
break;
}
freeaddrinfo(res);
if(tmp == NULL){
fprintf(stderr, "server: failed to bind udp\n");
exit(1);
}
cout<<"Socket binded\n";
return socketfd;
}
struct sockaddr_in getAddrFromNode(Node node){
// node.printNode();
struct addrinfo hints, *res;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_DGRAM;
// cout<<node.getNodeName()<<endl;
int status = getaddrinfo(node.getNodeName(), node.getPort(), &hints, &res);
if (status != 0) {
std::cerr << "Error resolving hostname: "<<node.getNodeName()<<" :" << gai_strerror(status) << std::endl;
exit(1);
}
struct sockaddr_in addr = *(struct sockaddr_in *)res->ai_addr;
freeaddrinfo(res);
return addr;
}
struct sockaddr_in getTCPAddrFromNode(Node node){
// node.printNode();
struct addrinfo hints, *res;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
// cout<<node.getNodeName()<<endl;
int status = getaddrinfo(node.getNodeName(), node.getTcpPort(), &hints, &res);
if (status != 0) {
std::cerr << "Error resolving hostname: "<<node.getNodeName()<<" :" << gai_strerror(status) << std::endl;
exit(1);
}
struct sockaddr_in addr = *(struct sockaddr_in *)res->ai_addr;
freeaddrinfo(res);
return addr;
}
bool withProbability(double probability) {
std::random_device rd; // Non-deterministic random number generator
std::mt19937 gen(rd()); // Seed the generator
std::uniform_real_distribution<> dis(0.0, 1.0); // Uniform distribution between 0 and 1
return dis(gen) < probability; // Perform action if random number is less than the probability
}
// unsigned long long generateHash(const std::string& str, int n) {
// unsigned long long hash = 0;
// const int p = 31; // A small prime number
// const int m = 1e9 + 9; // A large prime modulus
// unsigned long long p_pow = 1; // p^0
// for (char c : str) {
// hash = (hash + (c - 'a' + 1) * p_pow) % m;
// p_pow = (p_pow * p) % m; // Increment p^i
// }
// return hash%(int)pow(2, n);
// }
char* getCurrentFullTimestamp() {
char* timestamp = new char[24];
time_t now = std::time(nullptr);
tm* localTime = localtime(&now);
auto now_ms = std::chrono::system_clock::now();
auto duration = now_ms.time_since_epoch();
auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count() % 1000;
std::strftime(timestamp, 20, "%Y-%m-%d %H:%M:%S", localTime);
std::snprintf(timestamp + 19, 5, ".%03d", static_cast<int>(millis));
return timestamp;
}
std::string getCurrentTSinEpoch() {
auto now = std::chrono::system_clock::now();
auto now_ms = std::chrono::time_point_cast<std::chrono::milliseconds>(now);
auto epoch = now_ms.time_since_epoch();
auto value = std::chrono::duration_cast<std::chrono::milliseconds>(epoch);
return std::to_string(value.count());
}
int differenceWithCurrentEpoch(string ts){
auto now = std::chrono::system_clock::now();
auto now_ms = std::chrono::time_point_cast<std::chrono::milliseconds>(now);
auto epoch = now_ms.time_since_epoch();
auto value = std::chrono::duration_cast<std::chrono::milliseconds>(epoch);
return value.count() - stoll(ts);
}
int hashFunction(string key, int ringSize) {
// Simple hash function for consistent hashing
hash<string> hashFunc;
size_t hashValue = hashFunc(key);
return hashValue % ringSize;
}