Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8dd93a9ad045d712cf444de103be4c8919ad5d70 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2011 protos software gmbh (http://www.protos.de).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * CONTRIBUTORS:
 * 		Thomas Schuetz (initial contribution)
 *
 *******************************************************************************/


#include "etMessageService.h"


#include "etLogger.h"
#include "etMSCLogger.h"

void etMessageService_init(etMessageService* self, etUInt8* buffer, etUInt16 maxBlocks, etUInt16 blockSize){
	ET_MSC_LOGGER_SYNC_ENTRY("etMessageService", "init")
	self->messageBuffer.buffer = buffer;
	self->messageBuffer.maxBlocks = maxBlocks;
	self->messageBuffer.blockSize = blockSize;
	etMessageQueue_init(&self->messagePool);
	etMessageQueue_init(&self->messageQueue);

	etMessageService_initMessagePool(self);
	ET_MSC_LOGGER_SYNC_EXIT
}

/*
 * initialize message pool with block buffer
 * all blocks are added to pool
 */
void etMessageService_initMessagePool(etMessageService* self){
	ET_MSC_LOGGER_SYNC_ENTRY("etMessageService", "initMessagePool")
	etInt16 i;

	for (i=0; i<self->messageBuffer.maxBlocks; i++){
		etMessage* block = (etMessage*) &self->messageBuffer.buffer[i*self->messageBuffer.blockSize];
		etMessageQueue_push(&self->messagePool, block);
	}
	ET_MSC_LOGGER_SYNC_EXIT
}

void etMessageService_pushMessage(etMessageService* self, etMessage* msg){
	ET_MSC_LOGGER_SYNC_ENTRY("etMessageService", "pushMessage")
	etMessageQueue_push(&self->messageQueue, msg);
	ET_MSC_LOGGER_SYNC_EXIT
}

etMessage* etMessageService_popMessage(etMessageService* self){
	ET_MSC_LOGGER_SYNC_ENTRY("etMessageService", "popMessage")
	etMessage* msg = etMessageQueue_pop(&self->messageQueue);
	ET_MSC_LOGGER_SYNC_EXIT
	return msg;
}


etMessage* etMessageService_getMessageBuffer(etMessageService* self, etUInt16 size){
	ET_MSC_LOGGER_SYNC_ENTRY("etMessageService", "getMessageBuffer")
	if (size<=self->messageBuffer.blockSize){
		if (self->messagePool.size>0){
			etMessage* msg = etMessageQueue_pop(&self->messagePool);
			ET_MSC_LOGGER_SYNC_EXIT
			return msg;
		}
	}
	ET_MSC_LOGGER_SYNC_EXIT
	return NULL;
}

void etMessageService_returnMessageBuffer(etMessageService* self, etMessage* buffer){
	ET_MSC_LOGGER_SYNC_ENTRY("etMessageService", "returnMessageBuffer")
	etMessageQueue_push(&self->messagePool, buffer);
	ET_MSC_LOGGER_SYNC_EXIT
}

void etMessageService_deliverAllMessages(etMessageService* self){
	ET_MSC_LOGGER_SYNC_ENTRY("etMessageService", "deliverAllMessages")
	while (self->messageQueue.size > 0){
		etMessage* msg = etMessageService_popMessage(self);
		etLogger_logInfoF("Message ID=%d, Address=%d", msg->evtID, msg->address);
		etMessageService_returnMessageBuffer(self, msg);
	}
	ET_MSC_LOGGER_SYNC_EXIT
}

void etMessageService_execute(etMessageService* self){
	ET_MSC_LOGGER_SYNC_ENTRY("etMessageService", "execute")
	etMessageService_deliverAllMessages(self);
	ET_MSC_LOGGER_SYNC_EXIT
}

Back to the top