reSIProcate
SIP stack, Dialog Usage Manager and related libraries
Loading...
Searching...
No Matches
AsyncProcessor.hxx
Go to the documentation of this file.
1#if !defined(RESIP_ASYNC_PROCESSOR_HXX)
2#define RESIP_ASYNC_PROCESSOR_HXX
3
4#include <iosfwd>
5#include <vector>
6#include "repro/Processor.hxx"
9
10namespace repro
11{
12class AsyncProcessorMessage;
13
14// This class is used to create Processors that need to accomplish blocking
15// tasks, such as database access. The request/event is delivered to this
16// Processor, like any other via the virtual method:
17// virtual processor_action_t process(RequestContext &)
18//
19// When a blocking task is required, it can be queued up for the thread pool
20// of workers, that are implemented in the constructor provided asyncDispatcher, by
21// constructing a new AsyncProcessorMessage and calling mAsyncDispatcher->post
22// AsyncProcessorMessage implementations should contain any data needed by the
23// threads and contain any members required to hold the blocking functions
24// return data or results.
25//
26// When a worker thread picks up the message it will call the virtual method:
27// virtual void asyncProcess(AsyncProcessorMessage* msg)
28// The implementation of this method should perform the actual blocking function
29// calls - ie. database access call. If there is any data to return it should be
30// set in the provided AsyncProcessorMessage before returning from asyncProcess.
31//
32// The dispatcher will then queue this message up to be sent back to the
33// AsyncProcessor, via the virtual method:
34// virtual processor_action_t process(RequestContext &)
35// This virtual method will need to examine the event type to see if the event is
36// a new request or an asynchronous result message (AsyncProcessorMessage).
37//
38
39/* Example:
40class MyAsyncProcessorAsyncMessage : public AsyncProcessorMessage
41{
42public:
43 MyAsyncProcessorAsyncMessage(AsyncProcessor& proc,
44 const resip::Data& tid,
45 resip::TransactionUser* passedtu):
46 AsyncProcessorMessage(proc,tid,passedtu) { }
47
48 virtual EncodeStream& encode(EncodeStream& strm) const
49 {
50 strm << "MyAsyncProcessorAsyncMessage(tid="<<mTid<<")"; return strm;
51 }
52
53 Data mDataRequiredToCallBlockingFunction;
54 Data mDataReturnedFromBlockingFunction;
55};
56
57class MyAsyncProcessor : public AsyncProcessor
58{
59 public:
60 MyAsyncProcessor(ProxyConfig& config, resip::Dispatcher* asyncDispatcher) :
61 AsyncProcessor("MyAsyncProcessor", asyncDispatcher) {}
62 ~MyAsyncProcessor() {}
63
64 // Processor virtual method
65 virtual processor_action_t process(RequestContext &rc)
66 {
67 Message *message = rc.getCurrentEvent();
68
69 MyAsyncProcessorAsyncMessage *async = dynamic_cast<MyAsyncProcessorAsyncMessage*>(message);
70 if (async)
71 {
72 // Async Function is complete - do something with results and continue
73 InfoLog(<< "Async function is complete, results are: " << async->mDataReturnedFromBlockingFunction);
74 return Continue;
75 }
76 else
77 {
78 // Control enters here when request arrives and is passed through process chain
79 // Dispatch async request to worker thread pool
80 MyAsyncProcessorAsyncMessage* async = new MyAsyncProcessorAsyncMessage(*this, rc.getTransactionId(), &rc.getProxy());
81 async->mDataRequiredToCallBlockingFunction = "foo";
82 mAsyncDispatcher->post(std::unique_ptr<ApplicationMessage>(async));
83 return WaitingForEvent;
84 }
85 }
86
87 // Virtual method called from WorkerThreads - return true to queue to stack when complete,
88 // false when no response is required
89 virtual bool asyncProcess(AsyncProcessorMessage* msg)
90 {
91 MyAsyncProcessorAsyncMessage* async = dynamic_cast<MyAsyncProcessorAsyncMessage*>(msg);
92 if(async)
93 {
94 // Running inside a worker thread here, do blocking work here
95 // set any results in MyAsyncProcessorAsyncMessage and return control to Dispatcher
96 // that will queue this message back to this processor.
97 async->mDataReturnedFromBlockingFunction = "bar";
98 }
99 }
100
101 private:
102};
103*/
104
105
107{
108 public:
112 virtual ~AsyncProcessor() {}
113
114 // Processor virtual method
116
117 // Virtual method called from WorkerThreads
118 // return true to queue to stack when complete, false when no response is required
120
121 protected:
123};
124
125}
126
127
128#endif
129
130/* ====================================================================
131 * The Vovida Software License, Version 1.0
132 *
133 * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved.
134 *
135 * Redistribution and use in source and binary forms, with or without
136 * modification, are permitted provided that the following conditions
137 * are met:
138 *
139 * 1. Redistributions of source code must retain the above copyright
140 * notice, this list of conditions and the following disclaimer.
141 *
142 * 2. Redistributions in binary form must reproduce the above copyright
143 * notice, this list of conditions and the following disclaimer in
144 * the documentation and/or other materials provided with the
145 * distribution.
146 *
147 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
148 * and "Vovida Open Communication Application Library (VOCAL)" must
149 * not be used to endorse or promote products derived from this
150 * software without prior written permission. For written
151 * permission, please contact vocal@vovida.org.
152 *
153 * 4. Products derived from this software may not be called "VOCAL", nor
154 * may "VOCAL" appear in their name, without prior written
155 * permission of Vovida Networks, Inc.
156 *
157 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
158 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
159 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
160 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
161 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
162 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
163 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
164 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
165 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
166 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
167 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
168 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
169 * DAMAGE.
170 *
171 * ====================================================================
172 *
173 * This software consists of voluntary contributions made by Vovida
174 * Networks, Inc. and many individuals on behalf of Vovida Networks,
175 * Inc. For more information on Vovida Networks, Inc., please see
176 * <http://www.vovida.org/>.
177 *
178 */
virtual bool asyncProcess(AsyncProcessorMessage *msg)=0
AsyncProcessor(const resip::Data &name, resip::Dispatcher *asyncDispatcher, ChainType type=NO_TYPE)
resip::Dispatcher * mAsyncDispatcher
virtual processor_action_t process(RequestContext &)=0
An alternative to std::string, encapsulates an arbitrary buffer of bytes.
Definition Data.hxx:90
A generic thread-bank class.
This Class is used to implement a primitive form of RPC using loose XML formatting.
void list()
Definition userAdmin.cxx:66