(file) Return to MessageQueue.h CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Common

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

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2