-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNode.nc
373 lines (318 loc) · 12 KB
/
Node.nc
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
/*
* ANDES Lab - University of California, Merced
* This class provides the basic functions of a network node.
*
* @author UCM ANDES Lab
* @date 2013/09/03
*
*/
#include <Timer.h>
#include "includes/command.h"
#include "includes/packet.h"
#include "includes/CommandMsg.h"
#include "includes/sendInfo.h"
#include "includes/channels.h"
#include "includes/socket.h"
module Node{
uses interface Boot;
uses interface SplitControl as AMControl;
uses interface Receive;
uses interface SimpleSend as Sender;
uses interface CommandHandler;
uses interface NeighborDiscovery; //Added
uses interface SimpleSend as FSender;
uses interface SimpleSend as RSender;
uses interface Hashmap<Route> as RoutingTable;
uses interface List<Route> as RouteTable; //not using this -delete later
uses interface Routing;
uses interface Transport;
uses interface Timer<TMilli> as clientTimer;
uses interface Timer<TMilli> as serverTimer;
uses interface Timer<TMilli> as timeWait;
uses interface List<socket_t> as acceptedSockets;
}
implementation{
pack sendPackage;
uint16_t transferGlobal;
uint16_t dataWritten = 1;
// Prototypes
void makePack(pack *Package, uint16_t src, uint16_t dest, uint16_t TTL, uint16_t Protocol, uint16_t seq, uint8_t *payload, uint8_t length);
event void Boot.booted(){
dbg(GENERAL_CHANNEL, "Booted\n");
call AMControl.start();
call Routing.initializeTable();
call NeighborDiscovery.start();
call Routing.start();
}
event void AMControl.startDone(error_t err){
if(err == SUCCESS){
dbg(GENERAL_CHANNEL, "Radio On\n");
}else{
//Retry until successful
call AMControl.start();
}
}
event void AMControl.stopDone(error_t err){}
event message_t* Receive.receive(message_t* msg, void* payload, uint8_t len){
dbg(GENERAL_CHANNEL, "Packet Received\n");
if(len==sizeof(pack)){
pack* myMsg=(pack*) payload;
dbg(GENERAL_CHANNEL, "Package Payload: %s\n", myMsg->payload);
return msg;
}
dbg(GENERAL_CHANNEL, "Unknown Packet Type %d\n", len);
return msg;
}
event void CommandHandler.ping(uint16_t destination, uint8_t *payload){ //runs only once
dbg(GENERAL_CHANNEL, "PING EVENT \n"); //node x is trying to send to node y (TOS_NODE_ID to destination)
if(call RoutingTable.contains(destination))
{
makePack(&sendPackage, TOS_NODE_ID, destination, 20, PROTOCOL_LINKEDLIST, 0, payload, PACKET_MAX_PAYLOAD_SIZE);
// call Sender.send(sendPackage, AM_BROADCAST_ADDR); //destination needs to be AM_BROADCAST_ADDR (everywhere) Note- note sure if we still need this after implementing flooding
dbg(GENERAL_CHANNEL, "Routing packet from %d to %d\n", TOS_NODE_ID, destination);
call RSender.send(sendPackage, destination); //Starting flooding when protocol ping is called
}
else
{
makePack(&sendPackage, TOS_NODE_ID, destination, 20, PROTOCOL_PING, 0, payload, PACKET_MAX_PAYLOAD_SIZE);
// call Sender.send(sendPackage, AM_BROADCAST_ADDR); //destination needs to be AM_BROADCAST_ADDR (everywhere) Note- note sure if we still need this after implementing flooding
dbg(GENERAL_CHANNEL, "There is no route, flooding packet from node %d to %d\n", TOS_NODE_ID, destination);
call FSender.send(sendPackage, AM_BROADCAST_ADDR); //Starting flooding when protocol ping is called
}
}
event void CommandHandler.printNeighbors(){
call NeighborDiscovery.print();
}
event void CommandHandler.printRouteTable(){
call Routing.print();
}
event void CommandHandler.printLinkState(){}
event void CommandHandler.printDistanceVector(){}
socket_addr_t serverSocketAddress;
socket_t fd;
event void CommandHandler.setTestServer(uint16_t port){
dbg(TRANSPORT_CHANNEL, "Initiating server at node %d and binding it to port %d\n", TOS_NODE_ID, port);
fd = call Transport.socket();
serverSocketAddress.addr = TOS_NODE_ID;
serverSocketAddress.port = port;
if(call Transport.bind(fd, &serverSocketAddress) == SUCCESS)
{
dbg(TRANSPORT_CHANNEL, "Server binding succesful!\n");
}
else{
dbg(TRANSPORT_CHANNEL, "Server binding failed\n");
}
if(call Transport.listen(fd) == SUCCESS)
{
dbg(TRANSPORT_CHANNEL, "Server listening...\n");
}
else{
dbg(TRANSPORT_CHANNEL, "Server state listening failed\n");
}
// call serverTimer.startPeriodic(3000);
}
event void serverTimer.fired()
{
socket_t newFd = call Transport.accept(fd);
socket_t readFd;
uint8_t i = 0;
bool check = FALSE;
uint16_t dataRead = 0;
uint16_t bufflen = 5;
uint8_t readBuff[SOCKET_BUFFER_SIZE];
if(newFd != NULL)
{
for(i = 0; i<call acceptedSockets.size(); i++){
if(fd == call acceptedSockets.get(i)){
check = TRUE;
}
}
if(check == FALSE){
call acceptedSockets.pushback(fd);
}
}
if(call Transport.checkConnection(fd) == SUCCESS)
{
for(i = 0; i < SOCKET_BUFFER_SIZE; i++){ //prepare buffer
readBuff[i] = 0;
}
for(i = 0; i < call acceptedSockets.size(); i++){ //get accepted sockets, read, and print
readFd = call acceptedSockets.get(i);
dataRead = call Transport.read(readFd, readBuff, bufflen);
call Transport.sendAck(fd);
}
}
}
socket_addr_t clientSocketAddress;
event void CommandHandler.setTestClient(uint16_t dest, uint16_t srcPort, uint16_t destPort, uint16_t transfer){
dbg(TRANSPORT_CHANNEL, "Initiating client at node %d and binding it to port %d\n", TOS_NODE_ID, srcPort);
// dbg(TRANSPORT_CHANNEL, "Testing client as dest %d srcPort %d destPort %d transfer %d\n", dest, srcPort, destPort, transfer);
fd = call Transport.socket();
//setting up src info
clientSocketAddress.addr = TOS_NODE_ID;
clientSocketAddress.port = srcPort;
//setting up dest info
serverSocketAddress.addr = dest;
serverSocketAddress.port = destPort;
//if connection succesful start timer
dbg(TRANSPORT_CHANNEL, "Creating connection with server %d at port %d\n", dest, destPort);
if(call Transport.bind(fd, &clientSocketAddress) == SUCCESS)
{
dbg(TRANSPORT_CHANNEL, "Client binding succesful!\n");
}
else{
dbg(TRANSPORT_CHANNEL, "Client binding failed\n");
}
if(call Transport.connect(fd, &serverSocketAddress) == SUCCESS)
{
dbg(TRANSPORT_CHANNEL, "Server and client connection started successfully...\n");
// call clientTimer.startPeriodic(4000); //periodically write buffer
transferGlobal = transfer;
}
else
{
dbg(TRANSPORT_CHANNEL, "Connection failed\n");
}
}
event void clientTimer.fired()
{
uint16_t i = 0;
uint16_t bufferWritten;
uint16_t sendBuff;
uint8_t writeBuff[transferGlobal];
sendBuff = 0;
if(transferGlobal != NULL)
{
if(call Transport.checkConnection(fd) == SUCCESS){
for(i = 0; i < transferGlobal; i++) // sending 16 bit unsigned integers from 0 to transfer
{
sendBuff = dataWritten+i;
writeBuff[i] = sendBuff;
// dbg(TRANSPORT_CHANNEL, "Data written in buffer is %d \n", writeBuff[i]);
}
bufferWritten = call Transport.write(fd, writeBuff, transferGlobal);
// dbg(TRANSPORT_CHANNEL, "Data written so far %d \n", dataWritten);
dbg(TRANSPORT_CHANNEL, "transfer status %d \n", transferGlobal);
}
}
if(transferGlobal == NULL)
{
// Start teardown
dbg(TRANSPORT_CHANNEL, "Data writing COMPLETED !!!\n");
call clientTimer.stop();
call Transport.close(fd);
call Transport.close(fd); //Has to be called twice according to documentation
}
else if(call Transport.sendBuffer(fd) == SUCCESS)
{
dataWritten += bufferWritten;
transferGlobal = transferGlobal - bufferWritten;
// dbg(TRANSPORT_CHANNEL, "bufferWritten status %d \n", bufferWritten);
}
else{
dbg(TRANSPORT_CHANNEL, "SendBuffer failed... Resend \n");
// call clientTimer.startPeriodic(6000);
}
}
event void CommandHandler.ClientClosed(uint16_t addr, uint16_t dest, uint16_t srcPort, uint16_t destPort){
dbg(TRANSPORT_CHANNEL, "Closing client with destination %d and destination port %d\n",dest, destPort);
call Transport.close(fd);
call clientTimer.stop();
}
char* dataGlobal;
size_t lenGlobal;
event void timeWait.fired(){
if(call RoutingTable.contains(1))
{
dbg(TRANSPORT_CHANNEL, "dataGlobal: %s \n", dataGlobal);
call Transport.write(fd, dataGlobal, strlen(dataGlobal));
call Transport.sendBuffer(fd);
}
}
event void CommandHandler.hello(uint16_t clientport, char* username){
uint16_t i = 0;
size_t len = strlen(username);
char* user = malloc(len+1);
lenGlobal = len;
user[i] = 'h';
for(i = 1; i < len+1; i++){
user[i] = username[i-1];
}
user[i] = '\0';
dataGlobal = user; //appending h command to username
fd = call Transport.socket();
//setting up src info
clientSocketAddress.addr = TOS_NODE_ID;
clientSocketAddress.port = clientport;
//setting up dest info
serverSocketAddress.addr = 1;
serverSocketAddress.port = 41;
//if connection succesful, start timer
dbg(TRANSPORT_CHANNEL, "Creating connection with server 1 at port 41\n");
if(call Transport.bind(fd, &clientSocketAddress) == SUCCESS)
{
dbg(TRANSPORT_CHANNEL, "Client binding succesful!\n");
}
else{
dbg(TRANSPORT_CHANNEL, "Client binding failed\n");
}
if(call Transport.connect(fd, &serverSocketAddress) == SUCCESS)
{
dbg(TRANSPORT_CHANNEL, "Server and client connection started successfully...\n");
call timeWait.startOneShotAt(call timeWait.getNow(), 3000);
}
else
{
dbg(TRANSPORT_CHANNEL, "Connection failed\n");
}
}
event void CommandHandler.Msg(char* msg){
uint16_t i = 0;
size_t len = strlen(msg);
char* newData = malloc(len+1);
newData[i] = 'm';
for(i = 1; i < len+1; i++){
newData[i] = msg[i-1];
}
newData[i] = '\0';
dataGlobal = newData;
call timeWait.startOneShotAt(call timeWait.getNow(), 0);
}
event void CommandHandler.whisper(char *username, char* msg){
// dbg(TRANSPORT_CHANNEL, "Say %s to %s\n", msg, username);
uint16_t i = 0;
size_t len2 = strlen(msg);
size_t len1 = strlen(username);
size_t len = len1+len2;
char* whisperData = malloc(len1+len2+2);
whisperData[i] = 'w';
for(i = 1; i < len1+1; i++){
whisperData[i] = username[i-1];
}
for(i = len1+1; i<len+1; i++){
whisperData[i] = msg[i-1];
}
whisperData[i] = '\0';
dataGlobal = whisperData;
// dataGlobal[i] = '\0';
// dbg(TRANSPORT_CHANNEL, "Sending %s\n", newData);
call timeWait.startOneShotAt(call timeWait.getNow(), 0);
}
event void CommandHandler.listusr(){
char* lCommand = malloc(1);
lCommand[0] = 'l';
lCommand[1] = '\0';
dataGlobal = lCommand;
call timeWait.startOneShotAt(call timeWait.getNow(), 0);
}
event void CommandHandler.setAppServer(){}
event void CommandHandler.setAppClient(){}
void makePack(pack *Package, uint16_t src, uint16_t dest, uint16_t TTL, uint16_t protocol, uint16_t seq, uint8_t* payload, uint8_t length){
Package->src = src;
Package->dest = dest;
Package->TTL = TTL;
Package->seq = seq;
Package->protocol = protocol;
memcpy(Package->payload, payload, length);
}
}