Revision af7522fa

View differences:

som/net_helper-ml.c
46 46
// pointers to the msgs to be send
47 47
static uint8_t *sendingBuffer[NH_BUFFER_SIZE];
48 48
// pointers to the received msgs + sender nodeID
49
static uint8_t *receivedBuffer[NH_BUFFER_SIZE][2];
49
struct receivedB {
50
	struct nodeID *id;
51
	int len;
52
	uint8_t *data;
53
};
54
static struct receivedB receivedBuffer[NH_BUFFER_SIZE];
50 55
/**/ static int recv_counter =0;
51 56

  
52 57
/**
......
55 60
 */
56 61
static int next_R() {
57 62
	const int size = 1024;
58
	if (receivedBuffer[rIdx][0]==NULL) {
59
		receivedBuffer[rIdx][0] = malloc(size);
63
	if (receivedBuffer[rIdx].data==NULL) {
64
		receivedBuffer[rIdx].data = malloc(size);
60 65
	}
61 66
	else {
62 67
		int count;
63 68
		for (count=0;count<NH_BUFFER_SIZE;count++) {
64 69
			rIdx = (++rIdx)%NH_BUFFER_SIZE;
65
			if (receivedBuffer[rIdx][0]==NULL)
70
			if (receivedBuffer[rIdx].data==NULL)
66 71
				break;
67 72
		}
68 73
		if (count==NH_BUFFER_SIZE)
69 74
			return -1;
70 75
		else {
71
			receivedBuffer[rIdx][0] = malloc(size);
76
			receivedBuffer[rIdx].data = malloc(size);
72 77
		}
73 78
	}
74
	memset(receivedBuffer[rIdx][0],0,size);
79
	memset(receivedBuffer[rIdx].data,0,size);
75 80
	return rIdx;
76 81
}
77 82

  
......
227 232
		int index = next_R();
228 233
		if (index >=0) {
229 234
		//	receivedBuffer[index][0] = malloc(buflen);
230
			if (receivedBuffer[index][0] == NULL) {
235
			if (receivedBuffer[index].data == NULL) {
231 236
				fprintf(stderr, "Net-helper : memory error while creating a new message buffer \n");
232 237
				return;
233 238
			}
234 239
			// creating a new sender nodedID
235
			receivedBuffer[index][1] = malloc(sizeof(nodeID));
236
			if (receivedBuffer[index][1]==NULL) {
237
				free (receivedBuffer[index][0]);
238
				receivedBuffer[index][0] = NULL;
240
			receivedBuffer[index].id = malloc(sizeof(nodeID));
241
			if (receivedBuffer[index].id==NULL) {
242
				free (receivedBuffer[index].data);
243
				receivedBuffer[index].data = NULL;
239 244
				fprintf(stderr, "Net-helper : memory error while creating a new nodeID. Message from %s is lost.\n", str);
240 245
				return;
241 246
			}
242 247
			else {
243
				memset(receivedBuffer[index][1], 0, sizeof(struct nodeID));
244
				nodeID *remote; remote = (nodeID*)(receivedBuffer[index][1]);
245
				receivedBuffer[index][0] = realloc(receivedBuffer[index][0],buflen+sizeof(int));
246
				memset(receivedBuffer[index][0],0,buflen+sizeof(int));
247
				memcpy(receivedBuffer[index][0],&buflen,sizeof(int));
248
				size_t lenlen = sizeof(int);
249
				memset(receivedBuffer[index].id, 0, sizeof(struct nodeID));
250
				nodeID *remote; remote = receivedBuffer[index].id;
251
				receivedBuffer[index].data = realloc(receivedBuffer[index].data,buflen);
252
				memset(receivedBuffer[index].data,0,buflen);
253
				receivedBuffer[index].len = buflen;
248 254
				//*(receivedBuffer[index][0]) = buflen;
249
				memcpy((receivedBuffer[index][0])+sizeof(int),buffer,buflen);
255
				memcpy(receivedBuffer[index].data,buffer,buflen);
250 256
				  // get the socketID of the sender
251 257
				remote->addr = malloc(SOCKETID_SIZE);
252 258
				if (remote->addr == NULL) {
......
291 297
	int i;
292 298
	for (i=0;i<NH_BUFFER_SIZE;i++) {
293 299
		sendingBuffer[i] = NULL;
294
		receivedBuffer[i][0] = NULL;
300
		receivedBuffer[i].data = NULL;
295 301
	}
296 302

  
297 303
	mlRegisterErrorConnectionCb(&connError_cb);
......
365 371
{
366 372
	int size;
367 373
	// this should never happen... if it does, index handling is faulty...
368
	if (receivedBuffer[rIdx][1]==NULL) {
374
	if (receivedBuffer[rIdx].id==NULL) {
369 375
		fprintf(stderr, "Net-helper : memory error while creating a new nodeID \n");
370 376
		return -1;
371 377
	}
372 378

  
373
	(*remote) = (nodeID*)(receivedBuffer[rIdx][1]);
379
	(*remote) = receivedBuffer[rIdx].id;
374 380
	// retrieve a msg from the buffer
375
	size = *((int*)(receivedBuffer[rIdx][0]));
381
	size = receivedBuffer[rIdx].len;
376 382
	if (size>buffer_size) {
377 383
		fprintf(stderr, "Net-helper : recv_from_peer: buffer too small (size:%d > buffer_size: %d)!\n",size,buffer_size);
378 384
		return -1;
379 385
	}
380
	memcpy(buffer_ptr, (receivedBuffer[rIdx][0])+sizeof(int), size);
381
	free(receivedBuffer[rIdx][0]);
382
	receivedBuffer[rIdx][0] = NULL;
383
	receivedBuffer[rIdx][1] = NULL;
386
	memcpy(buffer_ptr, receivedBuffer[rIdx].data, size);
387
	free(receivedBuffer[rIdx].data);
388
	receivedBuffer[rIdx].data = NULL;
389
	receivedBuffer[rIdx].id = NULL;
384 390

  
385 391
//	fprintf(stderr, "Net-helper : I've got mail!!!\n");
386 392

  
......
392 398

  
393 399
//	fprintf(stderr,"Net-helper : Waiting for data to come...\n");
394 400
	event_base_once(base,-1, EV_TIMEOUT, &t_out_cb, NULL, tout);
395
	while(receivedBuffer[rIdx][0]==NULL && timeoutFired==0) {
401
	while(receivedBuffer[rIdx].data==NULL && timeoutFired==0) {
396 402
	//	event_base_dispatch(base);
397 403
		event_base_loop(base,EVLOOP_ONCE);
398 404
	}
399 405
	timeoutFired = 0;
400 406
//	fprintf(stderr,"Back from eventlib loop.\n");
401 407

  
402
	if (receivedBuffer[rIdx][0]!=NULL)
408
	if (receivedBuffer[rIdx].data!=NULL)
403 409
		return 1;
404 410
	else
405 411
		return 0;

Also available in: Unified diff