-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinder.cc
339 lines (280 loc) · 9.95 KB
/
binder.cc
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#include <cstdlib>
#include <cstring>
#include <unistd.h>
#include <string>
#include <list>
#include <map>
#include <iostream>
#include "segment.h"
#include "message.h"
#include "register_success_message.h"
#include "register_failure_message.h"
#include "register_request_message.h"
#include "loc_success_message.h"
#include "loc_failure_message.h"
#include "loc_request_message.h"
#include "terminate_message.h"
#include "constants.h"
#include "network.h"
#include "helper_functions.h"
using namespace std;
// Global variables for binder
static map<procedure_signature, list<server_info *>, ps_compare> procLocDict;
static list<server_function_info *> roundRobinList;
static list<server_info *> serverList;
static bool isTerminated = false;
// Handles a registration request from the server
void handleRegistrationRequest(RegisterRequestMessage *message, int sock) {
const char *name = message->getName().c_str();
int *argTypes = message->getArgTypes();
string server_identifier = message->getServerIdentifier();
int port = message->getPort();
procedure_signature key(name, argTypes);
int result = SUCCESS_CODE;
try {
// If 'key' doesn't exist in map, add it to the map and round robin
if (procLocDict.find(key) == procLocDict.end()) {
/*
* The purpose of this function is so we can have copy of the argTypes
* that is not the original
*/
int *memArgTypes = copyArgTypes(argTypes);
key = procedure_signature(name, memArgTypes);
procLocDict[key] = list<server_info *>();
// This is bad we shouldn't need a newKey and we should be able to use the key above
// due to &* reasones I made a variable newKey for the 'info' object
procedure_signature *newKey = new procedure_signature(name, memArgTypes);
server_info *entry = new server_info(server_identifier, port, sock);
server_function_info *info = new server_function_info(entry, newKey);
// Adding to roundRobinList if server is not found
roundRobinList.push_back(info);
// Adding to serverList if server is not found
bool serverExist = false;
for (list<server_info *>::iterator it = serverList.begin(); it != serverList.end(); it++) {
if ((*it)->server_identifier == entry->server_identifier &&
(*it)->port == entry->port &&
(*it)->socket == entry->socket) {
serverExist = true;
break;
}
}
if (!serverExist) {
serverList.push_back(entry);
}
} else {
bool sameLoc = false;
list<server_info *> hostList = procLocDict[key];
for (list<server_info *>::iterator it = hostList.begin(); it != hostList.end(); it++) {
if((*it)->server_identifier == server_identifier &&
(*it)->port == port &&
(*it)->socket == sock) {
// If they have the same socket, then must be same server_address/port
// The same procedure signature already exists on the same location
sameLoc = true;
result = WARNING_CODE_DUPLICATED_PROCEDURE;
}
}
if (!sameLoc) { // Same procedure but different socket
server_info *new_msg_loc = new server_info(server_identifier, port, sock);
hostList.push_back(new_msg_loc);
int *newArgTypes = copyArgTypes(argTypes);
procedure_signature *useFulKey = new procedure_signature(name, newArgTypes);
server_function_info *info = new server_function_info(new_msg_loc, useFulKey);
// Adding to roundRobinList if server is not found
roundRobinList.push_back(info);
// Adding to serverList if server is not found
bool serverExist = false;
for (list<server_info *>::iterator it = serverList.begin(); it != serverList.end(); it++) {
if ((*it)->server_identifier == new_msg_loc->server_identifier &&
(*it)->port == new_msg_loc->port &&
(*it)->socket == new_msg_loc->socket) {
serverExist = true;
break;
}
}
if (!serverExist) {
serverList.push_back(new_msg_loc);
}
}
}
} catch (...) {
result = ERROR_CODE_PROCEDURE_REGISTRATION_FAILED;
}
if (result >= 0) {
RegisterSuccessMessage messageToServer = RegisterSuccessMessage(result);
Segment segmentToServer =
Segment(messageToServer.getLength(), MSG_TYPE_REGISTER_SUCCESS,
&messageToServer);
segmentToServer.send(sock);
} else {
RegisterFailureMessage messageToServer = RegisterFailureMessage(result);
Segment segmentToServer =
Segment(messageToServer.getLength(), MSG_TYPE_REGISTER_FAILURE,
&messageToServer);
segmentToServer.send(sock);
}
}
// Handles a location request from the client
void handleLocationRequest(LocRequestMessage *message, int sock) {
bool exist = false;
string serverIdToPushBack;
int portToPushBack;
int socketToPushBack;
for (list<server_function_info *>::iterator it = roundRobinList.begin(); it != roundRobinList.end(); it++){
//If the name are the same and argTypes
if((*it)->ps->name == message->getName() && compareArr((*it)->ps->argTypes, message->getArgTypes() )){
exist = true;
serverIdToPushBack = (*it)->si->server_identifier;
portToPushBack = (*it)->si->port;
socketToPushBack = (*it)->si->socket;
LocSuccessMessage locSuccessMsg = LocSuccessMessage((*it)->si->server_identifier.c_str(), (*it)->si->port);
Segment locSuccessSeg = Segment(locSuccessMsg.getLength(), MSG_TYPE_LOC_SUCCESS, &locSuccessMsg);
locSuccessSeg.send(sock);
break;
}
}
if (exist) {
list<server_function_info *>::iterator i = roundRobinList.begin();
list<server_function_info *> tempList;
while (i != roundRobinList.end()){
if((*i)->si->server_identifier == serverIdToPushBack && (*i)->si->port == portToPushBack && (*i)->si->socket == socketToPushBack){
tempList.push_back(*i);
roundRobinList.erase(i++); // alternatively, i = items.erase(i);
}else{
++i;
}
}
roundRobinList.splice(roundRobinList.end(), tempList);
} else {
LocFailureMessage locFailMsg =
LocFailureMessage(ERROR_CODE_PROCEDURE_NOT_FOUND);
Segment locFailSeg = Segment(locFailMsg.getLength(), MSG_TYPE_LOC_FAILURE, &locFailMsg);
locFailSeg.send(sock);
}
}
// Handles a termination request from the client
void handleTerminationRequest() {
// Informs all the servers to terminate
for (list<server_info *>::const_iterator it = serverList.begin(); it != serverList.end(); it++) {
TerminateMessage messageToServer = TerminateMessage();
Segment segmentToServer =
Segment(messageToServer.getLength(), MSG_TYPE_TERMINATE,
&messageToServer);
segmentToServer.send((*it)->socket);
}
// Signals the binder to terminate
isTerminated = true;
}
// Handles a request from the client/server
void handleRequest(Segment *segment, int socket) {
switch (segment->getType()) {
case MSG_TYPE_REGISTER_REQUEST: {
RegisterRequestMessage *messageFromServer =
dynamic_cast<RegisterRequestMessage *>(segment->getMessage());
handleRegistrationRequest(messageFromServer, socket);
break;
}
case MSG_TYPE_LOC_REQUEST: {
LocRequestMessage *messageFromClient =
dynamic_cast<LocRequestMessage *>(segment->getMessage());
handleLocationRequest(messageFromClient, socket);
break;
}
case MSG_TYPE_TERMINATE: {
handleTerminationRequest();
break;
}
}
}
int main() {
fd_set allSockets;
fd_set readSockets;
/*
* Clears all entries from the all sockets set and the read
* sockets set
*/
FD_ZERO(&allSockets);
FD_ZERO(&readSockets);
/*
* Creates the welcome socket, adds it to the all sockets set and
* sets it as the maximum socket so far
*/
int welcomeSocket = createSocket();
if (welcomeSocket < 0) {
return welcomeSocket;
}
int result = setUpToListen(welcomeSocket);
if (result < 0) {
return result;
}
FD_SET(welcomeSocket, &allSockets);
int maxSocket = welcomeSocket;
/*
* Prints the binder address and the binder port on the binder's
* standard output
*/
string binderIdentifier = getHostAddress();
if (binderIdentifier == "") {
return ERROR_CODE_HOST_ADDRESS_NOT_FOUND;
}
int port = getSocketPort(welcomeSocket);
if (port < 0) {
return port;
}
cout << "BINDER_ADDRESS " << binderIdentifier << endl;
cout << "BINDER_PORT " << port << endl;
while (!isTerminated) {
readSockets = allSockets;
// Checks if some of the sockets are ready to be read from
int result = select(maxSocket + 1, &readSockets, 0, 0, 0);
if (result < 0) {
continue;
}
for (int i = 0; i <= maxSocket; i++) {
if (!FD_ISSET(i, &readSockets)) {
continue;
}
if (i == welcomeSocket) {
/*
* Creates the connection socket when a connection is made
* to the welcome socket
*/
int connectionSocket = acceptConnection(i);
if (connectionSocket < 0) {
continue;
}
// Adds the connection socket to the all sockets set
FD_SET(connectionSocket, &allSockets);
/*
* Sets the connection socket as the maximum socket so far
* if necessary
*/
if (connectionSocket > maxSocket) {
maxSocket = connectionSocket;
}
} else {
/*
* Creates a segment to receive data from the client/server and
* reads into it from the connection socket
*/
Segment *segment = 0;
result = 0;
result = Segment::receive(i, segment);
if (result < 0) {
/*
* Closes the connection socket and removes it from the
* all sockets set
*/
destroySocket(i);
FD_CLR(i, &allSockets);
continue;
}
// Handles a request from the client/server
handleRequest(segment, i);
}
}
}
// Destroys the welcome socket
destroySocket(welcomeSocket);
return SUCCESS_CODE;
}