-
Notifications
You must be signed in to change notification settings - Fork 0
/
dsm.cc
388 lines (314 loc) · 10.3 KB
/
dsm.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <map>
#include <queue>
#include <cstdlib>
#include <cstdio>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>
#include "messages.h"
#include "config.h"
#include "socket.h"
#include "thread.h"
#include "mutual_exclusion.h"
using namespace std;
// function definitions
int main(int argc, char ** argv);
void * thread_conn_handler( void * arg );
int readByte(int addr);
void writeByte(int addr, int value);
void processQueue();
void printMemory();
// NODE variables / properties
int nodeId;
bool shouldQuit = false;
extern int MESSAGES_SENT;
extern int MUTEX_MESSAGES_SENT;
pthread_mutex_t waitForToken = PTHREAD_MUTEX_INITIALIZER;
// mutex protecting the commandQueue
pthread_mutex_t commandMutex = PTHREAD_MUTEX_INITIALIZER;
// a queue of commands to be executed once a lock is acquired
queue< vector<int> > commandQueue;
// a data "cache" for unmodified bytes (memory_address -> value)
map<int, int> unmodified;
// a data "cache" for modified bytes (memory_address -> value)
map<int, int> modified;
// the bytes that I own (memory_address -> value)
map<int, int> myBytes;
// the bytes that others own (memory_address -> port_number)
map<int, int> otherBytes;
vector<int> nodeList, portList; // keep track of node, port
int server, port; // keep track of socket and port this nodeId owns
// not part of the mutual exclusion, just a flag telling us if we have it or not
bool hasToken = false;
/**
* Main function
*/
int main(int argc, char ** argv){
if( argc != 2){
cerr << "Usage " << argv[0] << " <nodeId>" << endl;
return EXIT_FAILURE;
}
pthread_mutex_lock( &waitForToken );
nodeId = atoi(argv[1]);
cout << "Started DSM node with id=" << nodeId << endl;
// read membership.conf and determine what port to run on (including all other nodes)
readMembershipConfig(nodeList, portList);
port = findPort(nodeId, nodeList, portList); // find the port that this node is running on
// read in
map<int, int> allBytes = readFileMap("memory_map.conf");
map<int, int>::iterator it;
for ( it=allBytes.begin() ; it != allBytes.end(); it++ ){
int addr = it->first;
int id = it->second;
if( id == nodeId ){
myBytes[addr] = 0;
} else {
otherBytes[addr] = findPort(id, nodeList, portList);
}
}
if (port == -99)
{
printf("[Error]: No port found for id:%i .\n",nodeId);
return EXIT_FAILURE;
}
// setup a server connection to run on that port
server = setup_server(port,&port);
// attempt to setup connection to all nodes
for (unsigned int i = 0; i < portList.size(); ++i)
{
if (nodeList[i] != nodeId) // for each node that is not this node
{
int clientSocket = 2; // 2 means failed to connect
while (clientSocket == 2)
{
clientSocket = setup_client("localhost", portList[i]);
sendint(clientSocket, PING);
// wait 100 millis for node to come up
if (clientSocket == 2) usleep(100 *1000);
}
}
}
mutual_exclusion_init(nodeList, portList, nodeId);
socklen_t sin_size;
struct sockaddr_storage their_addr;
char s[INET6_ADDRSTRLEN];
// I'm wondering why we don't need to accept()?? but the socket is connected
while( !shouldQuit )
{
// accept() socket operation on non socket????
sin_size = sizeof their_addr;
int new_fd = accept(server, (struct sockaddr *)&their_addr, &sin_size);
if (new_fd == -1)
{
perror("accept");
continue;
}
inet_ntop(their_addr.ss_family, get_in_addr((struct sockaddr *)&their_addr), s, sizeof s);
//cout << "Node " << nodeId << " got connection from " << s << endl;
// prepare argument for thread
int * arg = new int(new_fd);
startDetachedThread(thread_conn_handler, arg);
}
return 0;
}
void * thread_conn_handler(void * arg){
int socket = *(int *)arg;
free(arg);
int message = readint( socket );
if( message == ACQUIRE_LOCK){
cout << "Node " << nodeId << " got ACQUIRE_LOCK message" << endl;
lock();
pthread_mutex_unlock( &waitForToken );
hasToken = true;
cout << "Node " << nodeId << " has acquired lock " << endl;
processQueue();
}
else if( message == RELEASE_LOCK){
cout << "Node " << nodeId << " got RELEASE_LOCK message" << endl;
// don't release a token without actuall having it!
pthread_mutex_lock( &waitForToken );
processQueue();
hasToken = false;
// Update the actual bytes with our cache by sending messages
map<int, int>::iterator it;
for ( it=modified.begin() ; it != modified.end(); it++ )
{
int memAddr = it->first;
int value = it->second;
int port = otherBytes[memAddr];
int socket = setup_client("localhost", port);
sendint(socket, WRITE);
sendint(socket, memAddr);
sendint(socket, value);
close(socket);
}
// clear "cache" maps
modified.erase( modified.begin(), modified.end() );
// we don't need to send messages to modify the actual unmodifed bytes
unmodified.erase( unmodified.begin(), unmodified.end() );
cout << "Node " << nodeId << " has released lock" << endl;
unlock();
}
else if( message == ADD){
cout << "Node " << nodeId << " got ADD message" << endl;
int totalsize = readint(socket);
vector<int> command;
command.push_back( ADD );
for (int i =0 ; i < totalsize ; ++i)
{
command.push_back( readint(socket) );
}
// add this command to the queue
pthread_mutex_lock( &commandMutex );
commandQueue.push( command );
pthread_mutex_unlock( &commandMutex );
if( hasToken ) processQueue();
}
else if( message == PRINT){
cout << "Node " << nodeId << " got PRINT message" << endl;
int memLoc = readint(socket);
vector<int> command;
command.push_back( PRINT );
command.push_back( memLoc );
// add this command to the queue
pthread_mutex_lock( &commandMutex );
commandQueue.push( command );
pthread_mutex_unlock( &commandMutex );
if( hasToken ) processQueue();
}
else if( message == QUIT ){
vector<int> command;
command.push_back( QUIT );
command.push_back( socket );
// add this command to the queue
pthread_mutex_lock( &commandMutex );
commandQueue.push( command );
pthread_mutex_unlock( &commandMutex );
// all we have to do is quit
if( commandQueue.size() == 1 ) processQueue();
// break early so that the socket isn't closed
return NULL;
}
else if( message == READ){
int memLoc = readint(socket);
int value = readByte( memLoc );
sendint(socket, value);
}
else if( message == WRITE){
int memLoc = readint(socket); // where to store (memAddr)
int value= readint(socket); // what to store (value)
writeByte(memLoc, value);
}
else if( message == QUIT){
cout << "Node " << nodeId << " got QUIT message" << endl;
}
else if( message == TOKEN ){
tokenReceived();
}
else if( message == PING){
}
else{
cout << "Unrecognized message" << endl;
}
close(socket);
return NULL;
}
// abstracts writing to a byte, updates the local copies as necessary
void writeByte(int addr, int value){
map<int, int>::iterator it;
// do I have it?
it = myBytes.find(addr);
if( it != myBytes.end() ){
myBytes[addr] = value;
return;
}
// have I cached it, if so, get it out of the friggin cache
it = unmodified.find(addr);
if( it != unmodified.end() ){
unmodified.erase( it );
}
// otherwise, let's just cache the value for now
modified[addr] = value;
}
// abstracts reading a byte. Grabs it from its local copy if possible, or a cached
// version. As a last resort, it sends a socket connection to read the value
int readByte(int addr){
map<int, int>::iterator it;
// do I have it?
it = myBytes.find(addr);
if( it != myBytes.end() ){
return it->second;
}
// have I cached it?
it = unmodified.find(addr);
if( it != unmodified.end() ){
return it->second;
}
// have I modified it?
it = modified.find(addr);
if( it != modified.end() ){
return it->second;
}
// we have to look it up :(
it = otherBytes.find(addr);
if( it != otherBytes.end() ){
int port = it->second;
int socket = setup_client("localhost", port);
sendint(socket, READ);
sendint(socket, addr);
int value = readint(socket);
unmodified[addr] = value;
close(socket);
return value;
}
return -1;
}
void processQueue(){
pthread_mutex_lock( &commandMutex );
while( !commandQueue.empty() ){
vector<int> params = commandQueue.front();
int command = params[0];
if( command == PRINT ){
int addr = params[1];
int value = readByte( addr );
cout << "Node " << nodeId << " printing " << value << " at memory location " << addr << endl;
} else if (command == ADD){
cout << "Node " << nodeId << " is performing ADD" << endl;
int destinationAddr = params[1];
int value = params[ params.size() - 1];
for (unsigned int i=2;i<params.size()-1; ++i)
{
value += readByte( params[i] );
}
writeByte( destinationAddr, value );
} else if( command == QUIT ){
int socket = params[1];
sendint(socket, MUTEX_MESSAGES_SENT);
sendint(socket, MESSAGES_SENT);
shouldQuit = true;
}
// actually remove that command from the queue
commandQueue.pop();
}
pthread_mutex_unlock( &commandMutex );
}
void printMemory(){
cout << "memory for node " << nodeId << endl;
map<int, int>::iterator it;
for ( it=myBytes.begin() ; it != myBytes.end(); it++ )
{
int memAddr = it->first;
int value = it->second;
cout << "memory[" << memAddr << "] = " << value << endl;
}
}