fixed MacAddress::parse declaration
[public/netxms.git] / include / nxmbapi.h
1 /*
2 ** NetXMS - Network Management System
3 ** NetXMS Message Bus API
4 ** Copyright (C) 2009-2015 Victor Kirhenshtein
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
42 /**
43 * Message class
44 */
45 class LIBNXMB_EXPORTABLE NXMBMessage
46 {
47 protected:
48 TCHAR *m_type;
49 TCHAR *m_senderId;
50
51 public:
52 NXMBMessage();
53 NXMBMessage(const TCHAR *type, const TCHAR *senderId);
54 virtual ~NXMBMessage();
55
56 const TCHAR *getType() const { return m_type; }
57 const TCHAR *getSenderId() const { return m_senderId; }
58 };
59
60 /**
61 * Subscriber class
62 */
63 class LIBNXMB_EXPORTABLE NXMBSubscriber
64 {
65 protected:
66 TCHAR *m_id;
67
68 public:
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
78 /**
79 * Abstract message filter class
80 */
81 class LIBNXMB_EXPORTABLE NXMBFilter
82 {
83 public:
84 NXMBFilter();
85 virtual ~NXMBFilter();
86
87 virtual bool isAllowed(NXMBMessage &msg);
88 virtual bool isOwnedByDispatcher();
89 };
90
91 /**
92 * Message filter which accept messages of specific type(s)
93 */
94 class LIBNXMB_EXPORTABLE NXMBTypeFilter : public NXMBFilter
95 {
96 protected:
97 StringMap m_types;
98
99 public:
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
109 /**
110 * Call handler
111 */
112 typedef bool (* NXMBCallHandler)(const TCHAR *, const void *, void *);
113
114 /**
115 * String map template for holding objects as values
116 */
117 class CallHandlerMap : public StringMapBase
118 {
119 public:
120 CallHandlerMap() : StringMapBase(false) { }
121
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 };
125
126 /**
127 * Message dispatcher class
128 */
129 class LIBNXMB_EXPORTABLE NXMBDispatcher
130 {
131 private:
132 Queue *m_queue;
133 int m_numSubscribers;
134 NXMBSubscriber **m_subscribers;
135 NXMBFilter **m_filters;
136 MUTEX m_subscriberListAccess;
137 THREAD m_workerThreadHandle;
138 CallHandlerMap *m_callHandlers;
139 MUTEX m_callHandlerAccess;
140 CONDITION m_stopCondition;
141
142 void workerThread();
143 static THREAD_RESULT THREAD_CALL workerThreadStarter(void *);
144
145 public:
146 NXMBDispatcher();
147 ~NXMBDispatcher();
148
149 void postMessage(NXMBMessage *msg);
150 bool call(const TCHAR *callName, const void *input, void *output);
151
152 void addSubscriber(NXMBSubscriber *subscriber, NXMBFilter *filter);
153 void removeSubscriber(const TCHAR *id);
154
155 void addCallHandler(const TCHAR *callName, NXMBCallHandler handler);
156 void removeCallHandler(const TCHAR *callName);
157
158 static NXMBDispatcher *getInstance();
159 };
160
161 #endif /* _nxmbapi_h_ */