| 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 "common/base/callback.h" |
|---|
| 33 |
#include "net/rpc/lib/client/rpc_service_wrapper.h" |
|---|
| 34 |
|
|---|
| 35 |
namespace rpc { |
|---|
| 36 |
|
|---|
| 37 |
ServiceWrapper::ServiceWrapper(IClientConnection& rpc_connection, |
|---|
| 38 |
const string& service_class_name, |
|---|
| 39 |
const string& service_instance_name) |
|---|
| 40 |
: rpc_connection_(rpc_connection), |
|---|
| 41 |
service_class_name_(service_class_name), |
|---|
| 42 |
service_instance_name_(service_instance_name), |
|---|
| 43 |
call_timeout_(5000), |
|---|
| 44 |
result_callbacks_(), |
|---|
| 45 |
sync_() { |
|---|
| 46 |
} |
|---|
| 47 |
|
|---|
| 48 |
ServiceWrapper::~ServiceWrapper() { |
|---|
| 49 |
CancelAllCalls(); |
|---|
| 50 |
} |
|---|
| 51 |
|
|---|
| 52 |
void ServiceWrapper::SetCallTimeout(uint32 timeout) { |
|---|
| 53 |
call_timeout_ = timeout; |
|---|
| 54 |
LOG_INFO << "CallTimeout modified to: " << call_timeout_ << " ms"; |
|---|
| 55 |
} |
|---|
| 56 |
|
|---|
| 57 |
uint32 ServiceWrapper::GetCallTimeout() const { |
|---|
| 58 |
return call_timeout_; |
|---|
| 59 |
} |
|---|
| 60 |
|
|---|
| 61 |
const std::string& ServiceWrapper::GetServiceName() const { |
|---|
| 62 |
return service_instance_name_; |
|---|
| 63 |
} |
|---|
| 64 |
const std::string& ServiceWrapper::GetServiceClassName() const { |
|---|
| 65 |
return service_class_name_; |
|---|
| 66 |
} |
|---|
| 67 |
|
|---|
| 68 |
Codec& ServiceWrapper::GetCodec() const { |
|---|
| 69 |
return rpc_connection_.GetCodec(); |
|---|
| 70 |
} |
|---|
| 71 |
|
|---|
| 72 |
void ServiceWrapper::CancelCall(CALL_ID call_id) { |
|---|
| 73 |
Callback * result_handler = PopResultCallbackSync(call_id); |
|---|
| 74 |
if ( result_handler == NULL ) { |
|---|
| 75 |
return; |
|---|
| 76 |
} |
|---|
| 77 |
delete result_handler; |
|---|
| 78 |
const uint32 qid = MakeQid(call_id); |
|---|
| 79 |
// WARNING: do NOT call rpc_connection_.CancelQuery with sync_ locked ! |
|---|
| 80 |
// Because CancelQuery synchronizes on selector, and the selector |
|---|
| 81 |
// may be trying to deliver a response on HandleCallResult witch |
|---|
| 82 |
// also locks sync_. |
|---|
| 83 |
rpc_connection_.CancelQuery(qid); |
|---|
| 84 |
} |
|---|
| 85 |
|
|---|
| 86 |
void ServiceWrapper::CancelAllCalls() { |
|---|
| 87 |
// NOTE: do NOT use rpc_connection_.CancelAllQueries() because the |
|---|
| 88 |
// rpc_connection may be shared between multiple RPCServiceWrappers. |
|---|
| 89 |
|
|---|
| 90 |
// Make a temporary map or ResultHandlers, because we don't want to |
|---|
| 91 |
// call rpc_connection_.CancelQuery with sync_ locked. |
|---|
| 92 |
ResultCallbackMap tmp; |
|---|
| 93 |
{ |
|---|
| 94 |
synch::MutexLocker lock(&sync_); |
|---|
| 95 |
tmp = result_callbacks_; |
|---|
| 96 |
result_callbacks_.clear(); |
|---|
| 97 |
} |
|---|
| 98 |
|
|---|
| 99 |
for ( ResultCallbackMap::iterator it = tmp.begin(); it != tmp.end(); ++it) { |
|---|
| 100 |
CALL_ID call_id = it->first; |
|---|
| 101 |
Callback * result_handler = it->second; |
|---|
| 102 |
const uint32 qid = MakeQid(call_id); |
|---|
| 103 |
delete result_handler; |
|---|
| 104 |
rpc_connection_.CancelQuery(qid); |
|---|
| 105 |
} |
|---|
| 106 |
} |
|---|
| 107 |
} |
|---|