Revision a88adc52 net_helper-ml.c

View differences:

net_helper-ml.c
79 79
/**/ static int recv_counter =0; static int snd_counter =0;
80 80

  
81 81

  
82
static struct nodeID *new_node(socketID_handle peer, int conn_id) {
83
	 struct nodeID *res = malloc(sizeof(struct nodeID));
84
	 if (!res)
85
		 return NULL;
86
	 memset(res, 0, sizeof(struct nodeID));
87
	 res->addr = malloc(SOCKETID_SIZE);
88
	 if (res->addr) {
89
		 memset(res->addr, 0, SOCKETID_SIZE);
90
		 memcpy(res->addr, peer ,SOCKETID_SIZE);
91
		 //	remote->addrSize = SOCKETID_SIZE;
92
		 //	remote->addrStringSize = SOCKETID_STRING_SIZE;
93
		 res->connID = conn_id;
94
		 res->refcnt = 1;
95
	 }
96
	 else {
97
		 free (res);
98
		 fprintf(stderr, "Net-helper : memory error while creating a new nodeID \n");
82
static void connReady_cb (int connectionID, void *arg);
83
static struct nodeID *new_node(socketID_handle peer) {
84
	send_params params = {0,0,0,0};
85
	struct nodeID *res = malloc(sizeof(struct nodeID));
86
	if (!res) {
87
		 fprintf(stderr, "Net-helper : memory error\n");
99 88
		 return NULL;
100
	 }
89
	}
90
	memset(res, 0, sizeof(struct nodeID));
91

  
92
	res->addr = malloc(SOCKETID_SIZE);
93
	if (! res->addr) {
94
		free (res);
95
		fprintf(stderr, "Net-helper : memory error while creating a new nodeID \n");
96
		return NULL;
97
	}
98
	memset(res->addr, 0, SOCKETID_SIZE);
99
	memcpy(res->addr, peer ,SOCKETID_SIZE);
101 100

  
102
	 return res;
101
	res->refcnt = 1;
102

  
103
	res->connID = mlOpenConnection(peer, &connReady_cb, NULL, params);
104

  
105
	return res;
103 106
}
104 107

  
105 108

  
106
static struct nodeID *id_lookup(socketID_handle target, int conn_id) {
109
static struct nodeID **id_lookup(socketID_handle target) {
107 110

  
108 111
	int i,here=-1;
109 112
	for (i=0;i<lookup_curr;i++) {
110 113
		if (lookup_array[i] == NULL) {
111
			if (here < 0)
114
			if (here < 0) {
112 115
				here = i;
113
		}
114
		else if (!mlCompareSocketIDs(lookup_array[i]->addr,target)) {
115
			return nodeid_dup(lookup_array[i]);
116
		}
117
		else {
118
			if (lookup_array[i]->refcnt == 1) {
119
				nodeid_free(lookup_array[i]);
120
				lookup_array[i] = NULL;
121
				if (here < 0)
122
					here = i;
123 116
			}
117
		} else if (!mlCompareSocketIDs(lookup_array[i]->addr,target)) {
118
			return &lookup_array[i];
124 119
		}
125 120
	}
126 121

  
127
	if (here >= 0) {
128
		lookup_array[here] = new_node(target,conn_id);
129
		return nodeid_dup(lookup_array[here]);
122
	if (here == -1) {
123
		here = lookup_curr++;
130 124
	}
131 125

  
132
	if (lookup_curr == lookup_max) {
126
	if (lookup_curr > lookup_max) {
133 127
		lookup_max *= 2;
134 128
		lookup_array = realloc(lookup_array,lookup_max*sizeof(struct nodeID*));
135 129
	}
136 130

  
137
	lookup_array[lookup_curr] = new_node(target,conn_id);
138
	return nodeid_dup(lookup_array[lookup_curr++]);
131
	lookup_array[here] = new_node(target);
132
	return &lookup_array[here];
133
}
139 134

  
135
static struct nodeID *id_lookup_dup(socketID_handle target) {
136
	return nodeid_dup(*id_lookup(target));
140 137
}
141 138

  
142 139

  
......
200 197
static void init_myNodeID_cb (socketID_handle local_socketID,int errorstatus) {
201 198
	switch (errorstatus) {
202 199
	case 0:
203
		//
200
		me->addr = malloc(SOCKETID_SIZE);
201
		if (! me->addr) {
202
			fprintf(stderr, "Net-helper : memory error while creating my new nodeID \n");
203
			return;
204
		}
205

  
204 206
		memcpy(me->addr,local_socketID,SOCKETID_SIZE);
205 207
	//	me->addrSize = SOCKETID_SIZE;
206 208
	//	me->addrStringSize = SOCKETID_STRING_SIZE;
......
338 340
				return;
339 341
			}
340 342
			// creating a new sender nodedID
341
			receivedBuffer[index].id = id_lookup(arg->remote_socketID, arg->connectionID);
343
			receivedBuffer[index].id = id_lookup_dup(arg->remote_socketID);
342 344
				receivedBuffer[index].data = realloc(receivedBuffer[index].data,buflen);
343 345
				memset(receivedBuffer[index].data,0,buflen);
344 346
				receivedBuffer[index].len = buflen;
......
363 365
		return NULL;
364 366
	}
365 367
	memset(me,0,sizeof(nodeID));
366
	me->addr = malloc(SOCKETID_SIZE);
367
	if (me->addr == NULL) {
368
		free(me);
369
		return NULL;
370
	}
371
	memset(me->addr,0,SOCKETID_SIZE);
372 368
	me->connID = -10;	// dirty trick to spot later if the ml has called back ...
373 369
	me->refcnt = 1;
374 370

  
......
535 531
}
536 532

  
537 533
struct nodeID *create_node(const char *rem_IP, int rem_port) {
538
	struct nodeID *remote = malloc(sizeof(nodeID));
539
	if (remote == NULL) {
540
		return NULL;
541
	}
542
	remote->addr = getRemoteSocketID(rem_IP, rem_port);
543
	send_params params = {0,0,0,0};
544
	remote->connID = mlOpenConnection(remote->addr,&connReady_cb,NULL, params);
545
	remote->refcnt = 1;
534
	socketID_handle s;
535
	struct nodeID *remote;
536

  
537
	s = getRemoteSocketID(rem_IP, rem_port);
538
	remote = id_lookup_dup(s);
539
	free(s);
540

  
546 541
	return remote;
547 542
}
548 543

  
549 544
// TODO: check why closing the connection is annoying for the ML
550 545
void nodeid_free(struct nodeID *n) {
546
	if (n && (--(n->refcnt) == 1)) {
547
		struct nodeID **npos;
551 548

  
552
//	mlCloseConnection(n->connID);
553
//	mlCloseSocket(n->addr);
554
//	free(n);
555
	if (n && (--(n->refcnt) == 0)) {
556 549
	//	mlCloseConnection(n->connID);
550
		npos = id_lookup(n->addr);
551
		*npos = NULL;
557 552
		mlCloseSocket(n->addr);
558 553
		free(n);
559 554
	}
......
573 568

  
574 569
struct nodeID *nodeid_dup(struct nodeID *s)
575 570
{
576
//  struct nodeID *res;
577
//
578
//  res = malloc(sizeof(struct nodeID));
579
//  if (res != NULL) {
580
//	  res->addr = malloc(SOCKETID_SIZE);
581
//	  if (res->addr != NULL) {
582
//		 memcpy(res->addr, s->addr, SOCKETID_SIZE);
583
//	//	 res->addrSize = SOCKETID_SIZE;
584
//	//	 res->addrStringSize = SOCKETID_STRING_SIZE;
585
//		 res->connID = s->connID;
586
//	  }
587
//	  else {
588
//		free(res);
589
//		res = NULL;
590
//		fprintf(stderr,"Net-helper : Error while duplicating nodeID...\n");
591
//	  }
592
//  }
593
//	return res;
594 571
	s->refcnt++;
595 572
	return s;
596 573
}
......
617 594
  socketID_handle h = (socketID_handle) sid;
618 595
  mlStringToSocketID((char *)b,h);
619 596
  *len = strlen((char*)b) + 1;
620
  return id_lookup(h,-1);
597
  return id_lookup_dup(h);
621 598
}

Also available in: Unified diff