1 karl 1.33 //%2005////////////////////////////////////////////////////////////////////////
|
2 mike 1.4 //
|
3 karl 1.32 // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
4 // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
5 // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
|
6 karl 1.30 // IBM Corp.; EMC Corporation, The Open Group.
|
7 karl 1.32 // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
8 // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
|
9 karl 1.33 // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
10 // EMC Corporation; VERITAS Software Corporation; The Open Group.
|
11 mike 1.4 //
12 // Permission is hereby granted, free of charge, to any person obtaining a copy
|
13 chip 1.11 // of this software and associated documentation files (the "Software"), to
14 // deal in the Software without restriction, including without limitation the
15 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
16 mike 1.4 // sell copies of the Software, and to permit persons to whom the Software is
17 // furnished to do so, subject to the following conditions:
|
18 kumpf 1.26 //
|
19 chip 1.11 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
|
20 mike 1.4 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
21 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
|
22 chip 1.11 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
23 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
24 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
25 mike 1.4 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28 //==============================================================================
29 //
30 // Author: Mike Brasher (mbrasher@bmc.com)
31 //
32 // Modified By:
33 //
34 //%/////////////////////////////////////////////////////////////////////////////
35
36 #ifndef Pegasus_MessageQueue_h
37 #define Pegasus_MessageQueue_h
38
39 #include <Pegasus/Common/Config.h>
40 #include <Pegasus/Common/Message.h>
|
41 kumpf 1.28 #include <Pegasus/Common/InternalException.h>
|
42 chip 1.11 #include <Pegasus/Common/IPC.h>
43 #include <Pegasus/Common/Thread.h>
|
44 kumpf 1.27 #include <Pegasus/Common/Linkage.h>
|
45 mike 1.4
46 PEGASUS_NAMESPACE_BEGIN
47
48 /** The MessageQueue class represents a queue abstraction and is used by
49 modules to exchange messages. Methods are provided for enqueuing,
50 dequeuing, removing, iterating messages. Some methods are virtual and
51 may be overriden but subclasses to modify the behavior.
|
52 mike 1.14
53 <h1>A Word about Queue Ids</h1>
54
55 You may pass a specific queue id to the MessageQueue() constructor. The
56 default is to call MessageQueue::getNextQueueId() to obtain one. Only pass
57 queue ids generated by calling MessageQueue::getNextQueueId() to this
58 constructor. Otherwise, you might end up with two queues with the same
59 queue id.
60
61 A technique we encourage, is to declare global queue ids like this:
62
|
63 mday 1.16 <pre>
64 extern const Uint32 GROCERY_QUEUE_ID;
65 </pre>
|
66 mike 1.14
67 And then define them like this:
68
|
69 mday 1.16 <pre>
70 const Uint32 GROCERY_QUEUE_ID = MessageQueue::getNextQueueId();
71 </pre>
|
72 mike 1.14
73 And then pass them to the constructor of MessageQueue (from the derived
74 class). In this way you will secure a unique constant identifier by which
75 you may refer to a queue later on.
76
77 <h1>A Word about using the find() Methods</h1>
78
79 There are two find() methods. One that takes a queue id and one that
80 takes a name. The time complexity of the former is O(1); whereas, the
81 time complexity of the latter is O(n). Therefore, use the queue id form
82 since it is more efficient.
|
83 mike 1.4 */
84 class PEGASUS_COMMON_LINKAGE MessageQueue
85 {
|
86 mday 1.16 public:
|
87 mike 1.4
|
88 mday 1.16 /** This constructor places this object on a queue table which is
89 maintained by this class. Each message queue has a queue-id (which
90 may be obtained by calling getQueueId()). The queue-id may be passed
91 to lookupQueue() to obtain a pointer to the corresponding queue).
92
93 @param queueId the queue id to be used by this object. ONLY PASS IN
94 QUEUE IDS WHICH WERE GENERATED USING MessageQueue::getNextQueueId().
95 Otherwise, you might end up with more than one queue with the same
96 queue id.
97 */
98 MessageQueue(
|
99 kumpf 1.25 const char *name,
|
100 mday 1.16 Boolean async = false,
101 Uint32 queueId = MessageQueue::getNextQueueId());
|
102 mday 1.23
|
103 mday 1.16 /** Removes this queue from the queue table. */
104 virtual ~MessageQueue();
105
106 /** Enques a message (places it at the back of the queue).
107 @param message pointer to message to be enqueued.
|
108 kumpf 1.31 @exception NullPointer exception if message parameter is null.
109 @exception IPCException if socket call has an error
|
110 mday 1.16 */
|
111 kumpf 1.31 virtual void enqueue(Message* message);
|
112 mday 1.16
113
114 /** allows a caller to determine if this message queue is asynchronous or
115 not .
116 */
|
117 mday 1.21 virtual Boolean isAsync(void) { return _async; }
|
118 mday 1.13
|
119 mday 1.20
|
120 mday 1.16 /** Dequeues a message (removes it from the front of the queue).
121 @return pointer to message or zero if queue is empty.
122 */
|
123 mday 1.22 virtual Message* dequeue() throw(IPCException);
|
124 mday 1.16
125 /** Removes the given message from the queue.
126 @param message to be removed.
127 @exception throws NullPointer if message parameter is null.
128 @exception throws NoSuchMessageOnQueue is message paramter is not
129 on this queue.
130 */
|
131 mday 1.22 virtual void remove(Message* message) throw(IPCException);
|
132 mday 1.16
133 /** Find the message with the given type.
134 @parameter type type of message to be found.
135 @return pointer to message if found; null otherwise.
136 */
|
137 mday 1.22 virtual Message* findByType(Uint32 type) throw(IPCException);
|
138 mday 1.16
139 /** Const version of findByType(). */
|
140 mday 1.22 virtual const Message* findByType(Uint32 type) const throw(IPCException);
|
141 mday 1.16
142 /** Find the message with the given key.
143 @parameter key key of message to be found.
144 @return pointer to message if found; null otherwise.
145 */
|
146 mday 1.22 virtual Message* findByKey(Uint32 key) throw(IPCException);
|
147 mday 1.16
148 /** Const version of findByKey(). */
|
149 mday 1.22 virtual const Message* findByKey(Uint32 key) const throw(IPCException);
|
150 mday 1.16
151 /** Finds the messages with the given type and key.
152 @param type type of message to be found.
153 @param type key of message to be found.
154 @return pointer to message if found; null otherwise.
155 */
|
156 mday 1.22 virtual Message* find(Uint32 type, Uint32 key) throw(IPCException);
|
157 mday 1.16
158 /** Const version of find(). */
|
159 mday 1.22 virtual const Message* find(Uint32 type, Uint32 key) const throw(IPCException);
|
160 mday 1.16
161 /** Returns pointer to front message. */
162 Message* front() { return _front; }
163
164 /** Const version of front(). */
165 const Message* front() const { return _front; }
166
167 /** Returns pointer to back message. */
168 Message* back() { return _back; }
169
170 /** Const version of back(). */
171 const Message* back() const { return _back; }
172
173 /** Returns true if there are no messages on the queue. */
174 Boolean isEmpty() const { return _front == 0; }
175
176 /** Returns the number of messages on the queue. */
177 Uint32 getCount() const { return _count; }
178
179 /** Retrieve the queue id for this queue. */
180 Uint32 getQueueId() const { return _queueId; }
181 mday 1.16
|
182 mday 1.23 Uint32 get_capabilities(void) const
183 {
184 return _capabilities;
185 }
186
187
|
188 mday 1.16 /** Prints the contents of this queue by calling the print() method
189 of each message.
190 @param os stream onto which the output is placed.
191 */
192 void print(PEGASUS_STD(ostream)& os) const throw(IPCException);
193
194 /** Lock this queue. */
195 virtual void lock() throw(IPCException);
196
197 /** Unlock this queue. */
198 virtual void unlock();
199
200 /** Provide a string name for this queue to be used by the print method.
201 */
|
202 kumpf 1.25 const char* getQueueName() const;
|
203 mday 1.16
204 /** This method is called after a message has been enqueued. This default
205 implementation does nothing. Derived classes may override this to
206 take some action each time a message is enqueued (for example, this
207 method could handle the incoming message in the thread of the caller
208 of enqueue()).
209 */
|
210 mday 1.24 virtual void handleEnqueue() ;
|
211 mday 1.16
212 /** This method <b>may</b> be called prior to enqueueing an message.
213 the message queue can inform the caller that it does not want
214 to handle the message by returning false **/
215
216 virtual Boolean messageOK(const Message *msg) { return true ;}
|
217 mday 1.17
|
218 mday 1.16 /** Lookup a message queue from a queue id. Note this is an O(1) operation.
219 */
220 static MessageQueue* lookup(Uint32 queueId) throw(IPCException);
221
222 /** Lookup a message given a queue name. NOte this is an O(N) operation.
223 */
224 static MessageQueue* lookup(const char *name) throw(IPCException);
225
226 /** Get the next available queue id. It always returns a non-zero
227 queue id an monotonically increases and finally wraps (to one)
228 after reaching the maximum unsigned 32 bit integer.
229 */
|
230 mike 1.19 static Uint32 getNextQueueId() throw(IPCException);
|
231 mday 1.16
|
232 mday 1.29
233
|
234 mday 1.16 protected:
|
235 mday 1.29 static void remove_myself(Uint32);
236
|
237 mday 1.16 Mutex _mut;
238 Uint32 _queueId;
239 char *_name;
|
240 mday 1.23 Uint32 _capabilities;
241
|
242 mday 1.16 private:
243
244
245 Uint32 _count;
246 Message* _front;
247 Message* _back;
248 Boolean _async;
|
249 mday 1.12
|
250 mike 1.4 };
251
|
252 chip 1.11 inline const Message* MessageQueue::findByType(Uint32 type) const
|
253 mday 1.16 throw(IPCException)
|
254 mike 1.4 {
|
255 mday 1.16 return ((MessageQueue*)this)->findByType(type);
|
256 mike 1.4 }
257
258 inline const Message* MessageQueue::findByKey(Uint32 key) const
|
259 mday 1.16 throw(IPCException)
|
260 mike 1.4 {
|
261 mday 1.16 return ((MessageQueue*)this)->findByKey(key);
|
262 mike 1.4 }
263
264 inline const Message* MessageQueue::find(Uint32 type, Uint32 key) const
|
265 mday 1.16 throw(IPCException)
|
266 mike 1.4 {
|
267 mday 1.16 return ((MessageQueue*)this)->find(type, key);
|
268 mike 1.4 }
269
270 class NoSuchMessageOnQueue : public Exception
271 {
|
272 mday 1.16 public:
273 NoSuchMessageOnQueue() : Exception("No such message on this queue") { }
|
274 mike 1.4 };
275
276 PEGASUS_NAMESPACE_END
277
278 #endif /* Pegasus_MessageQueue_h */
|