| 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/io/input_stream.h" |
|---|
| 33 |
#include "common/io/buffer/io_memory_stream.h" |
|---|
| 34 |
#include "net/rpc/lib/server/rpc_core_types.h" |
|---|
| 35 |
|
|---|
| 36 |
namespace rpc { |
|---|
| 37 |
|
|---|
| 38 |
rpc::Query::Query(const rpc::Transport& transport, |
|---|
| 39 |
uint32 qid, |
|---|
| 40 |
const string& service, |
|---|
| 41 |
const string& method, |
|---|
| 42 |
io::MemoryStream& args, |
|---|
| 43 |
const rpc::Codec& codec, |
|---|
| 44 |
uint32 rid) |
|---|
| 45 |
: transport_(transport), |
|---|
| 46 |
codec_(codec.Clone()), |
|---|
| 47 |
qid_(qid), |
|---|
| 48 |
service_(service), |
|---|
| 49 |
method_(method), |
|---|
| 50 |
args_(), |
|---|
| 51 |
args_decoder_(codec_->CreateDecoder(args_)), |
|---|
| 52 |
args_decoding_initialized_(false), |
|---|
| 53 |
status_(static_cast<rpc::REPLY_STATUS>(-1)), |
|---|
| 54 |
result_(), |
|---|
| 55 |
result_encoder_(codec_->CreateEncoder(result_)), |
|---|
| 56 |
args_array_(), |
|---|
| 57 |
completion_callback_(NULL), |
|---|
| 58 |
rid_(rid) { |
|---|
| 59 |
args_.AppendStream(&args); |
|---|
| 60 |
// always keep a marker on stream start, to be able to rewind the stream |
|---|
| 61 |
args_.MarkerSet(); |
|---|
| 62 |
} |
|---|
| 63 |
|
|---|
| 64 |
rpc::Query::~Query() { |
|---|
| 65 |
delete codec_; |
|---|
| 66 |
codec_ = NULL; |
|---|
| 67 |
delete args_decoder_; |
|---|
| 68 |
args_decoder_ = NULL; |
|---|
| 69 |
delete result_encoder_; |
|---|
| 70 |
result_encoder_ = NULL; |
|---|
| 71 |
while ( !args_array_.empty() ) { |
|---|
| 72 |
rpc::Object* obj = args_array_.back(); |
|---|
| 73 |
args_array_.pop_back(); |
|---|
| 74 |
delete obj; |
|---|
| 75 |
} |
|---|
| 76 |
args_array_.clear(); |
|---|
| 77 |
} |
|---|
| 78 |
|
|---|
| 79 |
io::MemoryStream& rpc::Query::RewindParams() { |
|---|
| 80 |
args_.MarkerRestore(); |
|---|
| 81 |
args_.MarkerSet(); |
|---|
| 82 |
args_decoding_initialized_ = false; |
|---|
| 83 |
return args_; |
|---|
| 84 |
} |
|---|
| 85 |
bool rpc::Query::InitDecodeParams() { |
|---|
| 86 |
CHECK ( !args_decoding_initialized_ ); |
|---|
| 87 |
uint32 item_count; |
|---|
| 88 |
if ( DECODE_RESULT_SUCCESS != args_decoder_->DecodeArrayStart(item_count) ) { |
|---|
| 89 |
return false; |
|---|
| 90 |
} |
|---|
| 91 |
// initialized |
|---|
| 92 |
args_decoding_initialized_ = true; |
|---|
| 93 |
return true; |
|---|
| 94 |
} |
|---|
| 95 |
bool rpc::Query::HasMoreParams() { |
|---|
| 96 |
CHECK ( args_decoding_initialized_ ); |
|---|
| 97 |
bool has_more_items; |
|---|
| 98 |
DECODE_RESULT result = args_decoder_->DecodeArrayContinue(has_more_items); |
|---|
| 99 |
return result == DECODE_RESULT_SUCCESS && has_more_items; |
|---|
| 100 |
} |
|---|
| 101 |
|
|---|
| 102 |
void rpc::Query::AddParam(rpc::Object* obj) { |
|---|
| 103 |
args_array_.push_back(obj); |
|---|
| 104 |
} |
|---|
| 105 |
|
|---|
| 106 |
void rpc::Query::SetCompletionCallback( |
|---|
| 107 |
Callback1<const rpc::Query&>* completion_callback) { |
|---|
| 108 |
CHECK_NULL(completion_callback_); |
|---|
| 109 |
completion_callback_ = completion_callback; |
|---|
| 110 |
CHECK_NOT_NULL(completion_callback_); |
|---|
| 111 |
} |
|---|
| 112 |
|
|---|
| 113 |
string rpc::Query::ToString() const { |
|---|
| 114 |
ostringstream oss; |
|---|
| 115 |
oss << "rpc::Query {" |
|---|
| 116 |
<< " qid=" << qid_ |
|---|
| 117 |
<< " service=" << service_ |
|---|
| 118 |
<< " method=" << method_ |
|---|
| 119 |
<< " codec=" << *codec_ |
|---|
| 120 |
<< " args=" << args_.DebugString() |
|---|
| 121 |
<< " status=" << status_ << "(" << rpc::ReplyStatusName(status_) << ")" |
|---|
| 122 |
<< " result=" << result_.DebugString() |
|---|
| 123 |
<< " rid=" << rid_ |
|---|
| 124 |
<< "}"; |
|---|
| 125 |
return oss.str(); |
|---|
| 126 |
} |
|---|
| 127 |
} |
|---|