1 /*****************************************************************
3 | Neptune - Simple Message Queue
5 | Copyright (c) 2002-2008, Axiomatic Systems, LLC.
8 | Redistribution and use in source and binary forms, with or without
9 | modification, are permitted provided that the following conditions are met:
10 | * Redistributions of source code must retain the above copyright
11 | notice, this list of conditions and the following disclaimer.
12 | * Redistributions in binary form must reproduce the above copyright
13 | notice, this list of conditions and the following disclaimer in the
14 | documentation and/or other materials provided with the distribution.
15 | * Neither the name of Axiomatic Systems nor the
16 | names of its contributors may be used to endorse or promote products
17 | derived from this software without specific prior written permission.
19 | THIS SOFTWARE IS PROVIDED BY AXIOMATIC SYSTEMS ''AS IS'' AND ANY
20 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 | DISCLAIMED. IN NO EVENT SHALL AXIOMATIC SYSTEMS BE LIABLE FOR ANY
23 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 ****************************************************************/
32 /*----------------------------------------------------------------------
34 +---------------------------------------------------------------------*/
35 #include "NptSimpleMessageQueue.h"
37 #include "NptLogging.h"
39 /*----------------------------------------------------------------------
41 +---------------------------------------------------------------------*/
42 NPT_SET_LOCAL_LOGGER("neptune.message-queue")
44 /*----------------------------------------------------------------------
45 | NPT_SimpleMessageCapsule
46 +---------------------------------------------------------------------*/
47 struct NPT_SimpleMessageCapsule
49 NPT_SimpleMessageCapsule(NPT_Message
* message
,
50 NPT_MessageHandler
* handler
);
51 ~NPT_SimpleMessageCapsule();
52 NPT_Message
* m_Message
;
53 NPT_MessageHandler
* m_Handler
;
54 NPT_MessageHandlerProxy
* m_Proxy
;
57 /*----------------------------------------------------------------------
58 | NPT_SimpleMessageCapsule::NPT_SimpleMessageCapsule
59 +---------------------------------------------------------------------*/
60 NPT_SimpleMessageCapsule::NPT_SimpleMessageCapsule(NPT_Message
* message
,
61 NPT_MessageHandler
* handler
) :
64 m_Proxy(NPT_DYNAMIC_CAST(NPT_MessageHandlerProxy
, handler
))
66 if (m_Proxy
) m_Proxy
->AddReference();
69 /*----------------------------------------------------------------------
70 | NPT_SimpleMessageCapsule::~NPT_SimpleMessageCapsule
71 +---------------------------------------------------------------------*/
72 NPT_SimpleMessageCapsule::~NPT_SimpleMessageCapsule()
74 if (m_Proxy
) m_Proxy
->Release();
77 /*----------------------------------------------------------------------
78 | NPT_SimpleMessageQueue::NPT_SimpleMessageQueue
79 +---------------------------------------------------------------------*/
80 NPT_SimpleMessageQueue::NPT_SimpleMessageQueue()
84 /*----------------------------------------------------------------------
85 | NPT_SimpleMessageQueue::~NPT_SimpleMessageQueue
86 +---------------------------------------------------------------------*/
87 NPT_SimpleMessageQueue::~NPT_SimpleMessageQueue()
93 /*----------------------------------------------------------------------
94 | NPT_SimpleMessageQueue::QueueMessage
95 +---------------------------------------------------------------------*/
97 NPT_SimpleMessageQueue::QueueMessage(NPT_Message
* message
,
98 NPT_MessageHandler
* handler
)
100 // push the message on the queue, with the handler reference
101 NPT_SimpleMessageCapsule
* capsule
= new NPT_SimpleMessageCapsule(message
, handler
);
102 NPT_Result result
= m_Queue
.Push(capsule
);
103 if (NPT_FAILED(result
)) delete capsule
;
107 /*----------------------------------------------------------------------
108 | NPT_SimpleMessageQueue::PumpMessage
109 +---------------------------------------------------------------------*/
111 NPT_SimpleMessageQueue::PumpMessage(NPT_Timeout timeout
/* = NPT_TIMEOUT_INFINITE */)
113 NPT_SimpleMessageCapsule
* capsule
;
115 NPT_LOG_FINEST_1("popping message from queue, timeout=%d", timeout
);
116 NPT_Result result
= m_Queue
.Pop(capsule
, timeout
);
117 if (NPT_SUCCEEDED(result
) && capsule
) {
118 if (capsule
->m_Handler
&& capsule
->m_Message
) {
119 result
= capsule
->m_Handler
->HandleMessage(capsule
->m_Message
);
121 delete capsule
->m_Message
;
124 NPT_LOG_FINEST_1("m_Queue.Pop() returned %d", result
);