Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6afd3524c7def32ee700d569534c40cd589ecde3 (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
96
97
98
99
100
101
102
103
/*******************************************************************************
 * Copyright (c) 2012 Draeger Medical GmbH (http://www.draeger.com).
 * 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:
 * 		Peter Karlitschek (initial contribution)
 *
 *******************************************************************************/

#ifndef MESSAGESERVICE_H_
#define MESSAGESERVICE_H_

#include "common/messaging/IMessageService.h"
#include "common/messaging/Message.h"
#include "common/messaging/Address.h"
#include "common/messaging/StaticMessageMemory.h"
#include "common/messaging/MessageDispatcher.h"
#include "etDatatypes.h"
#include "osal/etMutex.h"
#include "osal/etSema.h"
#include "osal/etThread.h"
#include "osal/etTimer.h"

namespace etRuntime {

class MessageService: public RTObject, public IMessageService {

public:

	enum ExecMode {
		POLLED, BLOCKED, MIXED
	};

	MessageService(IRTObject* parent, IMessageService::ExecMode mode, int node, int thread, const String& name, IMessageMemory* memory, int priority = 0);
	MessageService(IRTObject* parent, IMessageService::ExecMode mode, etTime interval, int node, int thread, const String& name, IMessageMemory* memory, int priority = 0);
	virtual ~MessageService();

	void run();

	virtual void start();
	virtual void terminate();

	virtual Address getFreeAddress();

	virtual void freeAddress(const Address& addr);

	virtual void addMessageReceiver(IMessageReceiver& receiver);
	virtual void removeMessageReceiver(IMessageReceiver& receiver);

	virtual void addPollingMessageReceiver(IMessageReceiver& receiver);
	virtual void removePollingMessageReceiver(IMessageReceiver& receiver);
	virtual void receive(const Message* msg);

	Message* getMessageBuffer(int size);
	void returnMessageBuffer(const Message* buffer);

	const Address& getAddress(void) const { return m_address; }
	String toString() const;

protected:

	long getLastMessageTimestamp() const {
		return m_lastMessageTimestamp;
	}

	void pollingTask();

private:
	// static functions for c calls
	static void run(void* self) {
		static_cast<MessageService*>(self)->run();
	}

	static void pollingTask(void* self) {
		static_cast<MessageService*>(self)->pollingTask();
	}

	etBool m_running;
	IMessageService::ExecMode m_execMode;
	long m_lastMessageTimestamp;

	etMutex m_mutex;
	etSema m_executionSemaphore;
	etThread m_thread;
	etTimer m_timer;

	Address m_address;
	MessageSeQueue m_messageQueue;
	MessageDispatcher m_messageDispatcher;
	IMessageMemory* m_messageMemory;

	void MessageService_init(etTime interval, int priority); // common ctor

	MessageService();
	MessageService(MessageService const&);
	MessageService& operator=(MessageService const&);
};

} /* namespace etRuntime */
#endif /* MESSAGESERVICE_H_ */

Back to the top