-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
235 lines (209 loc) · 7.05 KB
/
client.js
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
/**
* Created by gags on 4/10/15.
*/
var inquirer = require('inquirer'),
io = require('socket.io-client'),
fs = require('fs'),
path = require('path'),
ip = require('ip'),
ss = require('socket.io-stream'),
Server = require('socket.io'),
HashTable = require('hashtable'),
hashtable = new HashTable(),
ConsistentHashing = require('consistent-hashing'),
constants = require('./constants'),
ioServer = new Server();
var argv = require('optimist')
.usage('Usage: $0 -c [CONFIG] -p [PORT]')
.demand(['c', 'p'])
.alias('c', 'config')
.describe('c', 'Config file with list of ip and port pair identifying peers')
.alias('p', 'port')
.describe('p', 'Port to run peer on')
.argv;
var peers = validateConfig(argv.config),
peersList = new ConsistentHashing(peers),
logEnabled = false;
// NOTE: Validate config file
if (!peers) {
console.log("Please enter valid IP address and port separated by a space in config file ! : [IP_ADDRESS] [PORT] => ", argv.config);
process.exit();
}
//
// listOperations();
//
// // NOTE: List the operations supported by DHT
// function listOperations() {
// var requestForOperation = [{
// type: "list",
// name: "operation",
// message: "Please select the operation you would like to perform : ",
// choices: [constants.PUT, constants.GET, constants.DELETE]
// }];
//
// inquirer.prompt(requestForOperation, function( response ) {
// requestKey(response.operation);
// });
// }
//
// // NOTE: request for 'key' to be used
// function requestKey(operation) {
// var requestForKey = [{
// type: "input",
// name: "key",
// message: "Please enter the key : ",
// validate: function (input) {
// if (input === undefined || input === '' || input.trim() === '') {
// console.log("Please enter proper key !");
// return false;
// } else {
// return true;
// }
// }
// }];
//
// inquirer.prompt(requestForKey, function( response ) {
// // NOTE: Only PUT operation requires 'value'; GET and DELETE requires only the 'key'
// if (operation === constants.PUT) {
// requestValue(operation, response.key);
// } else {
// delegateOperationToPeer(response.key, operation, { key: response.key, value: response.value });
// //listOperations();
// }
// });
// }
//
// // NOTE: request for 'value' to be stored (for PUT)
// function requestValue(operation, key) {
// var requestForValue = [{
// type: "input",
// name: "value",
// message: "Please enter the value : ",
// validate: function (input) {
// if (input === undefined || input === '' || input.trim() === '') {
// console.log("Please enter proper value !");
// return false;
// } else {
// return true;
// }
// }
// }];
//
// inquirer.prompt(requestForValue, function( response ) {
// delegateOperationToPeer(key, operation, { key: key, value: response.value });
// //listOperations();
// });
// }
// NOTE: perform specific operation based on 'operation' using the 'key' and 'value'
function performOperation(operation, key, value) {
var status = constants.NOOP;
switch (operation) {
case constants.PUT:
status = putValue(key, value);
break;
case constants.GET:
status = getValue(key);
break;
case constants.DELETE:
status = deleteKey(key);
break;
default:
logServerMessage("ERROR: SOMETHING WENT TERRIBLY WRONG !");
}
logServerMessage(operation + " : Status => " + status);
return status;
}
function putValue(key, value) {
var status = hashtable.put(key, value);
return status ? true : false;
}
function getValue(key) {
var value = hashtable.get(key);
return value ? value : null;
}
function deleteKey(key) {
return hashtable.remove(key);
}
// NOTE: Find target peer using ConsistentHashing
// function findTargetPeer(key) {
// return peersList.getNode(key);
// }
var sockets = new HashTable();
// function delegateOperationToPeer(key, operation, operation_params) {
// var socket_address;
// var peerID = findTargetPeer(key);
//
// if (validateAddress(peerID)) {
// socket_address = "http://" + peerID.split(" ").join(":");
// } else {
// logClientMessage("ERROR : SOMETHING TERRIBLY WENT WRONG WHILE CONNECTING TO PEER !");
// process.exit();
// }
//
// var socket;
//
// // NOTE: Check if the socket is already established and re-use if yes, else create a new connection
// if (sockets.has(peerID)) {
// socket = sockets.get(peerID);
// socket.emit('operation', { operation: operation, params: operation_params });
// } else {
// socket = io(socket_address);
// console.log("Connecting to peer : ", socket_address);
//
// socket.on('op_status', function (response) {
// logClientMessage(operation + " : Status => " + response.status);
// listOperations();
// });
//
// socket.on('connect', function () {
// logClientMessage("Connected to Peer Server !");
// socket.emit('operation', { operation: operation, params: operation_params });
// });
//
// sockets.put(peerID, socket);
// }
// }
// NOTE: DHT Peer Server
ioServer.on('connect', function (socket) {
logServerMessage("Connected with Peer Client : " + socket.handshake.address);
socket.on('operation', function (response) {
var status = performOperation(response.operation, response.params.key, response.params.value);
socket.emit('op_status', { operation: response.operation, status: status, timestamp: response.timestamp });
});
});
// NOTE: validate the config file for correct peer addresses
function validateConfig(fileName) {
var peers = fs.readFileSync(fileName).toString().split('\n');
peers = peers.filter(function (peer, i){
return peer.length > 0;
});
var invalidPeers = peers.filter(function (peer, i) {
return !validateAddress(peer);
});
return invalidPeers.length > 0 ? false : peers;
}
// NOTE: check if address is valid (ip:port)
function validateAddress(entry) {
var ip_port = entry.split(" ");
var blocks = ip_port[0].split(".");
if (ip_port.length < 2)
return false;
if(blocks.length === 4) {
return blocks.every(function(block) {
return parseInt(block,10) >=0 && parseInt(block,10) <= 255;
});
}
return false;
}
// NOTE: log client message
function logClientMessage(message) {
if (logEnabled)
console.log("[Client] : ", message);
}
// NOTE: log server message
function logServerMessage(message) {
if (logEnabled)
console.log("[Server] : ", message);
}
ioServer.listen(argv.port);
console.log("\n Server running at : " + ip.address() + ":" + argv.port);