change log updated
[public/netxms.git] / include / nxmbapi.h
CommitLineData
ccc34207
VK
1/*
2** NetXMS - Network Management System
3** NetXMS Message Bus API
e02cc28e 4** Copyright (C) 2009-2015 Victor Kirhenshtein
ccc34207
VK
5**
6** This program is free software; you can redistribute it and/or modify
7** it under the terms of the GNU General Public License as published by
8** the Free Software Foundation; either version 2 of the License, or
9** (at your option) any later version.
10**
11** This program is distributed in the hope that it will be useful,
12** but WITHOUT ANY WARRANTY; without even the implied warranty of
13** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14** GNU General Public License for more details.
15**
16** You should have received a copy of the GNU General Public License
17** along with this program; if not, write to the Free Software
18** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19**
20** File: nxmbapi.h
21**
22**/
23
24#ifndef _nxmbapi_h_
25#define _nxmbapi_h_
26
27#ifdef _WIN32
28#ifdef LIBNXMB_EXPORTS
29#define LIBNXMB_EXPORTABLE __declspec(dllexport)
30#else
31#define LIBNXMB_EXPORTABLE __declspec(dllimport)
32#endif
33#else /* _WIN32 */
34#define LIBNXMB_EXPORTABLE
35#endif
36
37#include <nms_common.h>
38#include <nms_util.h>
39#include <nms_threads.h>
40#include <nxqueue.h>
41
e02cc28e
VK
42/**
43 * Message class
44 */
ccc34207
VK
45class LIBNXMB_EXPORTABLE NXMBMessage
46{
47protected:
48 TCHAR *m_type;
49 TCHAR *m_senderId;
50
51public:
52 NXMBMessage();
53 NXMBMessage(const TCHAR *type, const TCHAR *senderId);
54 virtual ~NXMBMessage();
55
9069da06
AK
56 const TCHAR *getType() const { return m_type; }
57 const TCHAR *getSenderId() const { return m_senderId; }
ccc34207
VK
58};
59
e02cc28e
VK
60/**
61 * Subscriber class
62 */
ccc34207
VK
63class LIBNXMB_EXPORTABLE NXMBSubscriber
64{
65protected:
66 TCHAR *m_id;
67
68public:
69 NXMBSubscriber(const TCHAR *id);
70 virtual ~NXMBSubscriber();
71
72 const TCHAR *getId() { return CHECK_NULL(m_id); }
73
74 virtual void messageHandler(NXMBMessage &msg);
75 virtual bool isOwnedByDispatcher();
76};
77
e02cc28e
VK
78/**
79 * Abstract message filter class
80 */
ccc34207
VK
81class LIBNXMB_EXPORTABLE NXMBFilter
82{
83public:
84 NXMBFilter();
85 virtual ~NXMBFilter();
86
87 virtual bool isAllowed(NXMBMessage &msg);
88 virtual bool isOwnedByDispatcher();
89};
90
e02cc28e
VK
91/**
92 * Message filter which accept messages of specific type(s)
93 */
ccc34207
VK
94class LIBNXMB_EXPORTABLE NXMBTypeFilter : public NXMBFilter
95{
96protected:
97 StringMap m_types;
98
99public:
100 NXMBTypeFilter();
101 virtual ~NXMBTypeFilter();
102
103 virtual bool isAllowed(NXMBMessage &msg);
104
105 void addMessageType(const TCHAR *type);
106 void removeMessageType(const TCHAR *type);
107};
108
e02cc28e
VK
109/**
110 * Call handler
111 */
112typedef bool (* NXMBCallHandler)(const TCHAR *, const void *, void *);
113
114/**
115 * String map template for holding objects as values
116 */
117class CallHandlerMap : public StringMapBase
118{
119public:
120 CallHandlerMap() : StringMapBase(false) { }
ccc34207 121
e02cc28e
VK
122 void set(const TCHAR *name, NXMBCallHandler handler) { setObject((TCHAR *)name, (void *)handler, false); }
123 NXMBCallHandler get(const TCHAR *name) { return (NXMBCallHandler)getObject(name); }
124};
ccc34207 125
e02cc28e
VK
126/**
127 * Message dispatcher class
128 */
ccc34207
VK
129class LIBNXMB_EXPORTABLE NXMBDispatcher
130{
ccc34207 131private:
ccc34207
VK
132 Queue *m_queue;
133 int m_numSubscribers;
134 NXMBSubscriber **m_subscribers;
135 NXMBFilter **m_filters;
136 MUTEX m_subscriberListAccess;
137 THREAD m_workerThreadHandle;
e02cc28e
VK
138 CallHandlerMap *m_callHandlers;
139 MUTEX m_callHandlerAccess;
2516b6e3 140 CONDITION m_stopCondition;
ccc34207
VK
141
142 void workerThread();
2516b6e3 143 static THREAD_RESULT THREAD_CALL workerThreadStarter(void *);
ccc34207
VK
144
145public:
146 NXMBDispatcher();
2516b6e3 147 ~NXMBDispatcher();
ccc34207
VK
148
149 void postMessage(NXMBMessage *msg);
e02cc28e 150 bool call(const TCHAR *callName, const void *input, void *output);
ccc34207
VK
151
152 void addSubscriber(NXMBSubscriber *subscriber, NXMBFilter *filter);
153 void removeSubscriber(const TCHAR *id);
154
e02cc28e
VK
155 void addCallHandler(const TCHAR *callName, NXMBCallHandler handler);
156 void removeCallHandler(const TCHAR *callName);
157
2516b6e3 158 static NXMBDispatcher *getInstance();
ccc34207
VK
159};
160
ccc34207 161#endif /* _nxmbapi_h_ */