-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhej.cpp
354 lines (302 loc) · 10.2 KB
/
hej.cpp
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
#include "CSNode.hpp"
#include "Strings.hpp"
#include <sys/socket.h>
#include <netdb.h>
bool CSNode::doBind (int port) {
//if (hasBinding) return true;
// server address
this->port = port;
struct sockaddr_in address;
address.sin_family = AF_INET;
address.sin_port = htons(port);
address.sin_addr.s_addr = INADDR_ANY;
int addrlen = sizeof(address);
// Creating socket file descriptor
if ((bindSocket = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket");
return false;
}
//do binding to INADDR_ANY
if (bind (bindSocket, (struct sockaddr *)&address, sizeof(address)) < 0) {
perror("bind");
close (bindSocket);
return false;
}
//set reuseaddr
int j;
if(setsockopt(bindSocket, SOL_SOCKET, SO_REUSEADDR, &j, sizeof(int)) < 0 ) {
perror("setsockopt");
close(bindSocket);
return false;
}
//listen
if (listen(bindSocket, 10) < 0) {
close (bindSocket);
perror("listen");
return false;
}
hasBinding = true;
return true;
}
void CSNode::unBind () {
if (hasBinding) close (bindSocket);
hasBinding = false;
}
CSNode::CSConnection *CSNode::waitForIncomming(int port) {
if(!hasBinding) {
doBind(port);
}
CSConnection *connection = new CSConnection;
struct sockaddr_in address;
address.sin_family = AF_INET;
address.sin_port = htons(port);
address.sin_addr.s_addr = INADDR_ANY;
int addrlen = sizeof(address);
if ((connection->connectionSocket = accept(bindSocket, (struct sockaddr *)&address, (socklen_t*)&addrlen)) < 0) {
perror("accept");
return 0;
}
//convert incomming address to presentation text
char addressBuffer[1024];
if (inet_ntop (AF_INET, &address.sin_addr, addressBuffer, sizeof(addressBuffer)) > 0) {
connection->identityString = addressBuffer;
}
return connection;
}
CSNode::CSConnection *CSNode::connectToPeer (const char *address, int port) {
CSConnection *connection = new CSConnection;
if ((connection->connectionSocket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket");
return 0;
}
struct sockaddr_in sa;
// port = 2121;
sa.sin_family = AF_INET;
sa.sin_port = htons(port);
sa.sin_addr.s_addr = INADDR_LOOPBACK;
bool success = true;
struct hostent *he;
he = gethostbyname(address);
if(he == 0) {
perror("gethostbyname");
return 0;
}
char *addr = (char *)&sa.sin_addr;
addr[0] = he->h_addr_list[0][0];
addr[1] = he->h_addr_list[0][1];
addr[2] = he->h_addr_list[0][2];
addr[3] = he->h_addr_list[0][3];
if (connect (connection->connectionSocket, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
perror("connect");
success = false;
}
if (!success) {
close (connection->connectionSocket);
delete connection;
return 0;
}
connection->identityString = address;
return connection;
}
void CSNode::closeConnection (CSNode::CSConnection *connection) {
if (connection) {
close (connection->connectionSocket);
delete connection;
}
}
string CSNode::readSentence (CSConnection *connection, char stopCharacter) { //ETX
string result;
const int Bufsize = 1024;
int bytes; char buffer[1024];
string test = connection->readBuffer.readString();
if (test.length())
return test;
while ((bytes = recv(connection->connectionSocket, buffer, Bufsize, 0)) >= 0) {
if (bytes < 0) {
perror ("recv)");
return string();
}
connection->readBuffer.fill(buffer, bytes);
if (connection->readBuffer.contains ('\3'))
break;
}
return connection->readBuffer.readString();
}
bool CSNode::writeSentence (CSConnection *connection, string sentence) {
Buffer writeBuffer;
writeBuffer.fill ((char *)sentence.c_str(), sentence.length());
writeBuffer.fill ((char *)"\3\0", 2);
string out = writeBuffer.readString('\0');
int bytes = send (connection->connectionSocket, out.c_str(), out.length(), 0);
if (bytes == sentence.length()) return true;
return false;
}
int CSNode::clientPUSH (CSNode::CSConnection *connection, const char *filename)
{
int fd = open (filename, O_RDONLY);
if (fd < 0) {
writeSentence(connection, "0");
perror ("open");
return -1;
}
//calculate file size
int size = lseek (fd, 0, SEEK_END);
lseek (fd, 0, SEEK_SET);
char sizebuf[128];
//send file size as string
sprintf(sizebuf, "%d", size);
writeSentence(connection, sizebuf);
printf("<send file> : %s , size=%s\n", filename, sizebuf);
int bufSize = 4096;
char buffer[bufSize];
int bytes, total = 0;
//this below is mirrored in serverPUSH
while ((bytes = read(fd, buffer, bufSize)) > 0) {
total += bytes;
//first fill the buffer (in case it was already non-empty)
connection->readBuffer.fill(buffer, bytes);
//...then extract from it
while((bytes = connection->readBuffer.readBytes(buffer, bufSize)) > 0) {
send(connection->connectionSocket, buffer, bytes, 0);
}
}
if (bytes < 0) {
perror ("read");
}
if (total == size)
printf("<PUSH> : file sent\n");
else
printf("<PUSH> : incomplete send\n");
close (fd);
return 0;
}
CSNode::CSConnection *CSNode::clientCommand(string command, CSNode::CSConnection *connection = 0) {
astream a(command);
string stripped = a.get('\n');
a.setString(stripped);
vector<string> argv = a.split(' ');
string keyword = argv[0];
cout << "argv[0]: '" << argv[0] << "'\n";
if(!keyword.compare("SERVE")) {
if(argv.size() < 2) {
cout << "Usage : SERVE <port>\n";
} else {
cout << "Server thread started on port " << argv[1] << "....\n";
doBind(atoi(argv[1].c_str()));
server.startThread();
// CSNode::CSConnection *newConnection = waitForIncomming (atoi(argv[1].c_str()));
// if(newConnection) {
// cout << "Gracefully accepted call from " << newConnection->identityString << " :)\n";
// connection = newConnection;
// serverCommand (connection);
// }
}
} else if(!keyword.compare("UNSERVE")) {
cout << "Ending server thread\n";
server.endThread();
} else if(!keyword.compare("CALL")) {
if(argv.size() < 3) {
cout << "Usage : CALL <address> <port>\n";
} else {
if (connection) {
cout << "Please close previous connection to " << connection->identityString << " first. (CALL)\n";
return connection;
}
CSNode::CSConnection *newConnection = connectToPeer(argv[1].c_str(), atoi(argv[2].c_str()));
if (newConnection) {
cout << "Successfully connected to " << argv[1] << "\n";
cout << "Sending credentials to " << argv[1] << "\n";
// -- bla bla --
cout << "Host accepted call.\n";
connection = newConnection;
//node.createServer (connection);
} else cout << "Failed to connect to " << argv[1] << "\n";
}
return connection;
} else if (!keyword.compare("MESSAGE")) {
if(connection) {
writeSentence (connection, stripped);
} else cout << "No connection\n";
} else if (!keyword.compare("CLOSE")) {
if(connection) {
writeSentence(connection, "CLOSE");
closeConnection(connection);
cout << "Connection closed\n";
connection = 0;
} else cout << "Not connected.\n";
} else if (!keyword.compare("EXIT")) {
if (connection) {
writeSentence (connection, "CLOSE");
closeConnection (connection);
}
cout << "Connection closed. Exit.\n";
exit(0);
} else if (!keyword.compare("PUSH")) {
if(connection) {
writeSentence (connection, stripped);
clientPUSH (connection, argv[1].c_str());
} else cout << "No connection\n";
} else if (!keyword.compare("PULL")) {
}
return connection;
}
int CSNode::serverPUSH (CSNode::CSConnection *connection, const char *filename) // PUSH from client
{
//read size from connect
string sizestr = readSentence(connection);
int size = atoi(sizestr.c_str());
if (size == 0) {
printf("<PUSH> : file size 0, abort\n");
return -1;
}
printf("<read file> : %s , size %d\n", filename, size);
//read file to disk
int fd = open (filename, O_CREAT|O_WRONLY|O_TRUNC); //read, write and execute permission
if (fd < 0) {
perror("open");
return -1;
}
int bufSize = 4096;
char buffer[bufSize];
int bytes, total = 0;
while (total < size && (bytes = recv(connection->connectionSocket, buffer, bufSize, 0)) > 0) {
total += bytes;
//first fill the buffer (in case it was already non-empty)
connection->readBuffer.fill(buffer, bytes);
//...then extract from it
while((bytes = connection->readBuffer.readBytes(buffer, bufSize)) > 0) {
write(fd, buffer, bytes);
}
}
if (bytes < 0) {
perror ("recv)");
}
if(total == size)
printf("<PUSH> : success\n");
else
printf("<PUSH> : Odd file size\n");
return 0;
}
void CSNode::serverCommand (CSConnection *connection) {
string message = readSentence(connection);
astream a(message);
vector<string> argv = a.split(' ');
string keyword = argv[0];
if (!keyword.compare("MESSAGE")) {
string output = "<message> : ";
for (int i = 1; i < argv.size(); i++) {
output += argv[i];
if (i < argv.size() - 1) output += " ";
}
cout << output << "\n";
} else if (!keyword.compare("CLOSE")) {
// if remote node is a server, help to close
writeSentence(connection, "CLOSE");
closeConnection (connection);
exit(0); // abandon...
} else if (!keyword.compare("PUSH")) {
serverPUSH(connection, argv[1].c_str());
} else if (!keyword.compare("PULL")) {
}
printf("> "); //reinsert the prompt
}