Revision 2694dce1

View differences:

som/net_helper-ml.c
4 4
 *  This is free software; see GPL.txt
5 5
 *
6 6
 */
7
#include <netinet/in.h>
8
#include <sys/uio.h>
9
#include "util/udpSocket.h"
10
#include <sys/socket.h>
7 11

  
8 12
#include <event2/event.h>
9 13
#include <arpa/inet.h>
......
12 16
#include <stdio.h>
13 17
#include <string.h>
14 18

  
19

  
15 20
#include "net_helper.h"
16 21
#include "ml.h"
17 22
#include "ml_helpers.h"
......
30 35
typedef struct nodeID {
31 36
	socketID_handle addr;
32 37
	int connID;	// connection associated to this node, -1 if myself
33
	int addrSize;
34
	int addrStringSize;
38
	int refcnt;
39
//	int addrSize;
40
//	int addrStringSize;
35 41
} nodeID;
36 42

  
37 43
typedef struct msgData_cb {
......
109 115
	case 0:
110 116
		//
111 117
		memcpy(me->addr,local_socketID,SOCKETID_SIZE);
112
		me->addrSize = SOCKETID_SIZE;
113
		me->addrStringSize = SOCKETID_STRING_SIZE;
118
	//	me->addrSize = SOCKETID_SIZE;
119
	//	me->addrStringSize = SOCKETID_STRING_SIZE;
114 120
		me->connID = -1;
115
		fprintf(stderr,"Net-helper init : received my own socket: %s.\n",node_addr(me));
121
		me->refcnt = 1;
122
	//	fprintf(stderr,"Net-helper init : received my own socket: %s.\n",node_addr(me));
116 123
		break;
117 124
	case -1:
118 125
		//
119 126
		fprintf(stderr,"Net-helper init : socket error occurred in ml while creating socket\n");
127
		exit(1);
120 128
		break;
121 129
	case 1:
122 130
		//
123 131
		fprintf(stderr,"Net-helper init : NAT traversal failed while creating socket\n");
132
		exit(1);
124 133
		break;
125 134
	case 2:
126 135
	    fprintf(stderr,"Net-helper init : NAT traversal timeout while creating socket\n");
136
	    exit(2);
127 137
	    break;
128 138
	default :	// should never happen
129 139
		//
......
141 151
static void t_out_cb (int socket, short flag, void* arg) {
142 152

  
143 153
	timeoutFired = 1;
144
	fprintf(stderr,"TIMEOUT!!!\n");
154
//	fprintf(stderr,"TIMEOUT!!!\n");
145 155
//	event_base_loopbreak(base);
146 156
}
147 157

  
......
151 161
 * @param arg
152 162
 */
153 163
static void receive_conn_cb(int connectionID, void *arg) {
154
    fprintf(stderr, "Net-helper : remote peer opened the connection %d with arg = %d\n", connectionID,(int)arg);
164
//    fprintf(stderr, "Net-helper : remote peer opened the connection %d with arg = %d\n", connectionID,(int)arg);
155 165

  
156 166
}
157 167

  
......
169 179
	send_Data(connectionID,(char *)(sendingBuffer[p->bIdx]),p->mSize,p->msgType,&params);
170 180
	free(sendingBuffer[p->bIdx]);
171 181
	sendingBuffer[p->bIdx] = NULL;
172
	fprintf(stderr,"Net-helper: Message # %d for connection %d sent!\n ", p->bIdx,connectionID);
182
//	fprintf(stderr,"Net-helper: Message # %d for connection %d sent!\n ", p->bIdx,connectionID);
173 183
	//	event_base_loopbreak(base);
174 184
}
175 185

  
......
201 211
static void recv_data_cb(char *buffer, int buflen, unsigned char msgtype, recv_params *arg) {
202 212
// TODO: lacks a void* arg... moreover: recv_params has a msgtype, but there is also a msgtype explicit argument...
203 213
//	fprintf(stderr, "Net-helper : called back with some news...\n");
204
	char str[SOCKETID_STRING_SIZE]; //str[SOCKETID_STRING_SIZE]=0;
214
	char str[SOCKETID_STRING_SIZE];
205 215
	if (arg->remote_socketID != NULL)
206 216
		socketID_To_String(arg->remote_socketID,str,SOCKETID_STRING_SIZE);
207 217
	else
......
210 220
	    fprintf(stderr, "Net-helper : corrupted message arrived from %s\n",str);
211 221
	}
212 222
	else {
213
		fprintf(stderr, "Net-helper : message arrived from %s\n",str);
223
	//	fprintf(stderr, "Net-helper : message arrived from %s\n",str);
214 224
		// buffering the received message only if possible, otherwise ignore it...
215 225
		int index = next_R();
216 226
		if (index >=0) {
......
228 238
				return;
229 239
			}
230 240
			else {
241
				memset(receivedBuffer[index][1], 0, sizeof(struct nodeID));
231 242
				nodeID *remote; remote = (nodeID*)(receivedBuffer[index][1]);
232
				memcpy(receivedBuffer[index][0],buffer,buflen);
243
				receivedBuffer[index][0] = realloc(receivedBuffer[index][0],buflen+sizeof(int));
244
				*(receivedBuffer[index][0]) = buflen;
245
				memcpy((receivedBuffer[index][0])+sizeof(int),buffer,buflen);
233 246
				  // get the socketID of the sender
234 247
				remote->addr = malloc(SOCKETID_SIZE);
235 248
				if (remote->addr == NULL) {
......
238 251
					  return;
239 252
				}
240 253
				else {
254
					memset(remote->addr, 0, SOCKETID_SIZE);
241 255
					memcpy(remote->addr, arg->remote_socketID ,SOCKETID_SIZE);
242
					remote->addrSize = SOCKETID_SIZE;
243
					remote->addrStringSize = SOCKETID_STRING_SIZE;
256
				//	remote->addrSize = SOCKETID_SIZE;
257
				//	remote->addrStringSize = SOCKETID_STRING_SIZE;
244 258
					remote->connID = arg->connectionID;
259
					remote->refcnt = 1;
245 260
				}
246 261
			}
247 262
		}
......
266 281
	if (me == NULL) {
267 282
		return NULL;
268 283
	}
284
	memset(me,0,sizeof(nodeID));
269 285
	me->addr = malloc(SOCKETID_SIZE);
270 286
	if (me->addr == NULL) {
271 287
		free(me);
272 288
		return NULL;
273 289
	}
274
	me->addrSize = 0;	// dirty trick to spot later if the ml has called back ...
290
	memset(me->addr,0,SOCKETID_SIZE);
291
	me->connID = -10;	// dirty trick to spot later if the ml has called back ...
292
	me->refcnt = 1;
275 293

  
276 294
	int i;
277 295
	for (i=0;i<NH_BUFFER_SIZE;i++) {
......
285 303
		register_Recv_data_cb(&recv_data_cb,msgtypes[i]);
286 304
	}
287 305
	init_messaging_layer(1,tout,port,IPaddr,0,NULL,&init_myNodeID_cb,base);
306
	while (me->connID<-1) {
307
	//	event_base_once(base,-1, EV_TIMEOUT, &t_out_cb, NULL, &tout);
308
		event_base_loop(base,EVLOOP_ONCE);
309
	}
310
	timeoutFired = 0;
288 311
//	fprintf(stderr,"Net-helper init : back from init!\n");
289
//	register_recv_localsocketID_cb(init_myNodeID_cb);
312

  
290 313
	return me;
291 314
}
292 315

  
......
297 320
 * @param to
298 321
 * @param buffer_ptr
299 322
 * @param buffer_size
300
 * @return The number of sent bytes or -1 if a connection error occurred.
323
 * @return The dimension of the buffer or -1 if a connection error occurred.
301 324
 */
302 325
int send_to_peer(const struct nodeID *from, struct nodeID *to, const uint8_t *buffer_ptr, int buffer_size)
303 326
{
......
317 340
		free(sendingBuffer[current]);
318 341
		sendingBuffer[current] = NULL;
319 342
		fprintf(stderr,"Net-helper: Couldn't get a connection ID to send msg %d.\n ", p->bIdx);
343
		free(p);
344
		return -1;
320 345
	}
321 346
	else {
322
		fprintf(stderr,"Net-helper: Got a connection ID to send msg %d to %s.\n ",
323
			current,node_addr(to));
347
	//	fprintf(stderr,"Net-helper: Got a connection ID to send msg %d to %s.\n ",current,node_addr(to));
324 348
	//		struct timeval tout = {0,500};
325 349
	//		event_base_once(base,0, EV_TIMEOUT, &t_out_cb, NULL, &tout);
326 350
		while (sendingBuffer[current] != NULL)
327 351
			event_base_loop(base,EVLOOP_ONCE);//  EVLOOP_NONBLOCK
328 352
//		fprintf(stderr,"Net-helper: Back from eventlib loop with status %d.\n", ok);
353
		free(p);
354
		return buffer_size;
329 355
	}
330
	free(p);
331
	return to->connID;
332 356

  
333 357
}
334 358

  
......
351 375

  
352 376
	(*remote) = (nodeID*)(receivedBuffer[rIdx][1]);
353 377
	// retrieve a msg from the buffer
354
	memcpy(buffer_ptr, receivedBuffer[rIdx][0], buffer_size);
355

  
378
	memcpy(buffer_ptr, (receivedBuffer[rIdx][0])+sizeof(int), buffer_size);
379
	int size = (int)(*(receivedBuffer[rIdx][0]));
356 380
	free(receivedBuffer[rIdx][0]);
357 381
	receivedBuffer[rIdx][0] = NULL;
358 382
	receivedBuffer[rIdx][1] = NULL;
359 383

  
360 384
//	fprintf(stderr, "Net-helper : I've got mail!!!\n");
361 385

  
362
	return buffer_size;
386
	return size;
363 387
}
364 388

  
365 389

  
......
393 417
//		free(remote);
394 418
//		return NULL;
395 419
//	}
396
	remote->addrSize = SOCKETID_SIZE;
397
	remote->addrStringSize = SOCKETID_STRING_SIZE;
420
//	remote->addrSize = SOCKETID_SIZE;
421
//	remote->addrStringSize = SOCKETID_STRING_SIZE;
398 422
	remote->addr = getRemoteSocketID(rem_IP, rem_port);
399 423
	remote->connID = open_Connection(remote->addr,&connReady_cb,NULL);
424
	remote->refcnt = 1;
400 425
	return remote;
401 426
}
402 427

  
403
void delete_node(struct nodeID *n) {
404 428
// TODO: check why closing the connection is annoying for the ML
429
void delete_node(struct nodeID *n) {
430

  
405 431
//	close_Connection(n->connID);
406
	close_Socket(n->addr);
407
	free(n);
432
//	close_Socket(n->addr);
433
//	free(n);
434
	if (n && (--(n->refcnt) == 0)) {
435
	//	close_Connection(n->connID);
436
		close_Socket(n->addr);
437
		free(n);
438
	}
408 439
}
409 440

  
410 441

  
......
419 450
	  return "";
420 451
}
421 452

  
422
struct nodeID *nodeid_dup(const struct nodeID *s)
453
struct nodeID *nodeid_dup(struct nodeID *s)
423 454
{
424
  struct nodeID *res;
425

  
426
  res = malloc(sizeof(struct nodeID));
427
  if (res != NULL) {
428
	  res->addr = malloc(SOCKETID_SIZE);
429
	  if (res->addr != NULL) {
430
		 memcpy(res->addr, s->addr, SOCKETID_SIZE);
431
		 res->addrSize = SOCKETID_SIZE;
432
		 res->addrStringSize = SOCKETID_STRING_SIZE;
433
		 res->connID = s->connID;
434
	  }
435
	  else {
436
		free(res);
437
		res = NULL;
438
		fprintf(stderr,"Net-helper : Error while duplicating nodeID...\n");
439
	  }
440
  }
441

  
442
  return res;
455
//  struct nodeID *res;
456
//
457
//  res = malloc(sizeof(struct nodeID));
458
//  if (res != NULL) {
459
//	  res->addr = malloc(SOCKETID_SIZE);
460
//	  if (res->addr != NULL) {
461
//		 memcpy(res->addr, s->addr, SOCKETID_SIZE);
462
//	//	 res->addrSize = SOCKETID_SIZE;
463
//	//	 res->addrStringSize = SOCKETID_STRING_SIZE;
464
//		 res->connID = s->connID;
465
//	  }
466
//	  else {
467
//		free(res);
468
//		res = NULL;
469
//		fprintf(stderr,"Net-helper : Error while duplicating nodeID...\n");
470
//	  }
471
//  }
472
//	return res;
473
	s->refcnt++;
474
	return s;
443 475
}
444 476

  
445 477
int nodeid_equal(const struct nodeID *s1, const struct nodeID *s2)
446 478
{
447
  return (memcmp(&s1->addr, &s2->addr, SOCKETID_SIZE) == 0);
479
	return (compare_socketIDs(s1->addr,s2->addr) == 0);
448 480
}
449 481

  
450 482
int nodeid_dump(uint8_t *b, const struct nodeID *s)
451 483
{
452 484
  socketID_To_String(s->addr,(char *)b,SOCKETID_STRING_SIZE);
453
  fprintf(stderr,"Dumping nodeID : ho scritto %s (%d bytes)\n",b, strlen((char *)b));
454
//  return SOCKETID_STRING_SIZE;
485
  //fprintf(stderr,"Dumping nodeID : ho scritto %s (%d bytes)\n",b, strlen((char *)b));
455 486
  return strlen((char *)b);
456 487

  
457
//	memcpy(b, s->addr, SOCKETID_SIZE);
458
//	return SOCKETID_SIZE;
488
//	memcpy(b, s->addr,SOCKETID_SIZE);//sizeof(struct sockaddr_in6)*2
489
//	return SOCKETID_SIZE;//sizeof(struct sockaddr_in6)*2;
459 490

  
460 491
}
461 492

  
......
464 495
  struct nodeID *res;
465 496
  res = malloc(sizeof(struct nodeID));
466 497
  if (res != NULL) {
498
	  memset(res,0,sizeof(struct nodeID));
467 499
	  res->addr = malloc(SOCKETID_SIZE);
468 500
	  if (res->addr != NULL) {
469
		  // memcpy(res->addr, b, SOCKETID_SIZE);
501
		  memset(res->addr,0,SOCKETID_SIZE);
502
		  //memcpy(res->addr, b, SOCKETID_SIZE);
503
		  //*len = SOCKETID_SIZE;
470 504
		  *len = strlen((char*)b);
471
//		  char str[(*len)+1]; str[(*len)]= '\0';
472
//		  snprintf(str,(*len),"%s",(char *)b);
473
		  string_To_SocketID((char *)b/*str*/,res->addr);
474
		  res->addrSize = SOCKETID_SIZE;
475
		  res->addrStringSize = SOCKETID_STRING_SIZE;
505
		  string_To_SocketID((char *)b,res->addr);
506
	//	  fprintf(stderr,"Node undumped : %s\n",node_addr(res));
507
	//	  res->addrSize = SOCKETID_SIZE;
508
	//	  res->addrStringSize = SOCKETID_STRING_SIZE;
476 509
		  res->connID = -1;
510
		  res->refcnt = 1;
477 511
	  }
478 512
	  else {
479 513
		  free(res);
480 514
		  res = NULL;
515
		  // TODO: what about *len in this case???
481 516
		  fprintf(stderr,"Net-helper : Error while 'undumping' nodeID...\n");
482 517
	  }
483 518
  }
484
//  *len = SOCKETID_STRING_SIZE; // SOCKETID_SIZE; //sizeof(struct nodeID);
519

  
485 520

  
486 521
  return res;
487 522
}

Also available in: Unified diff