root/trunk/whisperlib/net/rpc/lib/server/irpc_async_query_executor.cc

Revision 7, 4.2 kB (checked in by whispercastorg, 2 years ago)

version 0.2.0

Line 
1 // Copyright (c) 2009, Whispersoft s.r.l.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Whispersoft s.r.l. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Author: Cosmin Tudorache
31
32 #include "net/rpc/lib/server/irpc_async_query_executor.h"
33
34 namespace rpc {
35
36 IAsyncQueryExecutor::IAsyncQueryExecutor(uint64 max_concurent_queries)
37   : handlers_(),
38     next_result_handler_id_(0),
39     max_concurent_queries_(max_concurent_queries),
40     concurent_queries_(0),
41     return_result_callback_(
42         NewPermanentCallback(this, &IAsyncQueryExecutor::ReturnResult)),
43     sync_() {
44 }
45 IAsyncQueryExecutor::~IAsyncQueryExecutor() {
46   CHECK(handlers_.empty()) << "#" << handlers_.size() << " handlers pending";
47   CHECK_EQ(concurent_queries_, 0) << "pending queries";
48   delete return_result_callback_;
49   return_result_callback_ = NULL;
50 }
51 void IAsyncQueryExecutor::RegisterResultHandler(IResultHandler& result_handler) {
52   synch::MutexLocker lock(&sync_);
53
54   // generate a new unique resultHandlerID
55   uint64 result_handler_id = (next_result_handler_id_++);
56
57   // set the ID in the handler
58   result_handler.SetResultHandlerID(result_handler_id);
59
60   // insert the [ID, {handler..}] pair in map
61   CHECK(handlers_.insert(make_pair(result_handler_id, &result_handler)).second)
62       << "Duplicate result_handler_id=" << result_handler_id;
63 }
64
65 void IAsyncQueryExecutor::UnregisterResultHandler(
66     IResultHandler& result_handler) {
67   const uint64 result_handler_id = result_handler.GetResultHandlerID();
68   // remove handler from map
69   {
70     synch::MutexLocker lock(&sync_);
71     CHECK(handlers_.erase(result_handler_id))
72       << " result_handler_id=" << result_handler_id;
73   }
74 }
75
76 bool IAsyncQueryExecutor::QueueRPC(rpc::Query* q) {
77   {
78     synch::MutexLocker lock(&sync_);
79
80     // one more query in execution
81     concurent_queries_++;
82
83     // check we have the query's result handler registered
84     #ifdef _DEBUG
85     CHECK(handlers_.find(q->rid()) != handlers_.end());
86     #endif
87
88     if ( concurent_queries_ >= max_concurent_queries_ ) {
89       q->SetCompletionCallback(return_result_callback_);
90       q->Complete(RPC_SERVER_BUSY);
91       return true;
92     }
93   }
94   // pass this query to executor implementation
95   return InternalQueueRPC(q);
96 }
97
98 void IAsyncQueryExecutor::ReturnResult(const rpc::Query& q) {
99   // find the local map entry of the result_handler
100   synch::MutexLocker lock(&sync_);
101
102   // one less query in execution
103   concurent_queries_--;
104
105   MapOfResultHandlers::const_iterator it = handlers_.find(q.rid());
106   if ( it == handlers_.end() ) {
107     LOG_WARNING << "No connection to deliver completed query: " << q;
108     // the connection who launched this query was closed. Ignore the result.
109     return;
110   }
111
112   IResultHandler& handler = *(it->second);
113
114   // Deliver the result.
115   //  (NOTE: we need to keep the map locked, so that the handler cannot be
116   //         unregistered & deleted)
117   handler.HandleRPCResult(q);
118 }
119 }
Note: See TracBrowser for help on using the browser.