| 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 |
#ifndef __NET_RPC_LIB_RPC_SERVER_IRPC_ASYNC_QUERY_EXECUTOR_H__ |
|---|
| 33 |
#define __NET_RPC_LIB_RPC_SERVER_IRPC_ASYNC_QUERY_EXECUTOR_H__ |
|---|
| 34 |
|
|---|
| 35 |
#include <map> |
|---|
| 36 |
#include <whisperlib/common/base/types.h> |
|---|
| 37 |
#include <whisperlib/common/sync/mutex.h> |
|---|
| 38 |
#include <whisperlib/net/rpc/lib/server/rpc_core_types.h> |
|---|
| 39 |
#include <whisperlib/net/rpc/lib/server/irpc_result_handler.h> |
|---|
| 40 |
|
|---|
| 41 |
// Interface for rpc::Query executor. The execution layer implements |
|---|
| 42 |
// this interface to receive queries from the rpc::ServerConnection. |
|---|
| 43 |
|
|---|
| 44 |
namespace rpc { |
|---|
| 45 |
|
|---|
| 46 |
class IAsyncQueryExecutor { |
|---|
| 47 |
public: |
|---|
| 48 |
IAsyncQueryExecutor(uint64 max_concurent_queries); |
|---|
| 49 |
virtual ~IAsyncQueryExecutor(); |
|---|
| 50 |
|
|---|
| 51 |
// Registers the given result handler. |
|---|
| 52 |
// From now on the handler can receive results. |
|---|
| 53 |
void RegisterResultHandler(IResultHandler& resultHandler); |
|---|
| 54 |
|
|---|
| 55 |
// Removes the given result handler. |
|---|
| 56 |
// The handler will no longer receive results |
|---|
| 57 |
// (pending queries results will be ignored). |
|---|
| 58 |
void UnregisterResultHandler(IResultHandler& resultHandler); |
|---|
| 59 |
|
|---|
| 60 |
// Queue the given query for asynchronous execution. |
|---|
| 61 |
// The result will go to the corresponding result handler. |
|---|
| 62 |
// input: |
|---|
| 63 |
// q: the query to be executed. Dynamically allocated, will be automatically |
|---|
| 64 |
// deleted. |
|---|
| 65 |
// returns: |
|---|
| 66 |
// Should always return true. If an error happens the query should be |
|---|
| 67 |
// completed with an error suitable status. |
|---|
| 68 |
bool QueueRPC(rpc::Query * q); |
|---|
| 69 |
|
|---|
| 70 |
protected: |
|---|
| 71 |
// Implementation specific, asynchronously executes the given query. |
|---|
| 72 |
// When the execution finishes, the result is passed to ReturnResult(..). |
|---|
| 73 |
// input: |
|---|
| 74 |
// q: the query to be executed. Dynamically allocated, will be automatically |
|---|
| 75 |
// deleted. |
|---|
| 76 |
// returns: |
|---|
| 77 |
// Should always return true. If an error happens the query should be |
|---|
| 78 |
// completed with an error suitable status. |
|---|
| 79 |
virtual bool InternalQueueRPC(rpc::Query * q) = 0; |
|---|
| 80 |
|
|---|
| 81 |
// Called by the superclass to send back the result of a query to |
|---|
| 82 |
// the a specific IResultHandler. If that handler unregistered in |
|---|
| 83 |
// the meanwhile, the result is discarded. |
|---|
| 84 |
// input: |
|---|
| 85 |
// q: executed query, containing the result |
|---|
| 86 |
void ReturnResult(const rpc::Query& q); |
|---|
| 87 |
|
|---|
| 88 |
private: |
|---|
| 89 |
// map resultHandlerID -> IResultHandler* |
|---|
| 90 |
typedef map<uint64, IResultHandler*> MapOfResultHandlers; |
|---|
| 91 |
MapOfResultHandlers handlers_; |
|---|
| 92 |
|
|---|
| 93 |
// used to generate unique result handler id s |
|---|
| 94 |
uint64 next_result_handler_id_; |
|---|
| 95 |
|
|---|
| 96 |
// The maximum number of parallel executing queries. |
|---|
| 97 |
// Beyond this number we reject new queries. |
|---|
| 98 |
const uint64 max_concurent_queries_; |
|---|
| 99 |
// the number of queries in execution at any given moment |
|---|
| 100 |
uint64 concurent_queries_; |
|---|
| 101 |
|
|---|
| 102 |
// permanent callback to ReturnResult |
|---|
| 103 |
Callback1<const rpc::Query&>* return_result_callback_; |
|---|
| 104 |
|
|---|
| 105 |
// synchronize access to handlers_ |
|---|
| 106 |
synch::Mutex sync_; |
|---|
| 107 |
|
|---|
| 108 |
DISALLOW_EVIL_CONSTRUCTORS(IAsyncQueryExecutor); |
|---|
| 109 |
}; |
|---|
| 110 |
} |
|---|
| 111 |
#endif // __NET_RPC_LIB_RPC_SERVER_IRPC_ASYNC_QUERY_EXECUTOR_H__ |
|---|