1/********************************************************************
2 * Copyright (c) 2013 - 2014, Pivotal Inc.
3 * All rights reserved.
4 *
5 * Author: Zhanwei Wang
6 ********************************************************************/
7/********************************************************************
8 * 2014 -
9 * open source under Apache License Version 2.0
10 ********************************************************************/
11/**
12 * Licensed to the Apache Software Foundation (ASF) under one
13 * or more contributor license agreements. See the NOTICE file
14 * distributed with this work for additional information
15 * regarding copyright ownership. The ASF licenses this file
16 * to you under the Apache License, Version 2.0 (the
17 * "License"); you may not use this file except in compliance
18 * with the License. You may obtain a copy of the License at
19 *
20 * http://www.apache.org/licenses/LICENSE-2.0
21 *
22 * Unless required by applicable law or agreed to in writing, software
23 * distributed under the License is distributed on an "AS IS" BASIS,
24 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
26 * limitations under the License.
27 */
28#include "Memory.h"
29#include "ProtobufRpcEngine.pb.h"
30#include "RpcCall.h"
31#include "RpcContentWrapper.h"
32#include "RpcHeader.pb.h"
33#include "RpcRemoteCall.h"
34#include "WriteBuffer.h"
35
36#include <google/protobuf/io/coded_stream.h>
37
38#define PING_CALL_ID -4
39
40using namespace google::protobuf::io;
41
42namespace Hdfs {
43namespace Internal {
44
45void RpcRemoteCall::serialize(const RpcProtocolInfo & protocol,
46 WriteBuffer & buffer) {
47 RpcRequestHeaderProto rpcHeader;
48 rpcHeader.set_callid(identity);
49 rpcHeader.set_clientid(clientId);
50 rpcHeader.set_retrycount(-1);
51 rpcHeader.set_rpckind(RPC_PROTOCOL_BUFFER);
52 rpcHeader.set_rpcop(RpcRequestHeaderProto_OperationProto_RPC_FINAL_PACKET);
53 RequestHeaderProto requestHeader;
54 requestHeader.set_methodname(call.getName());
55 requestHeader.set_declaringclassprotocolname(protocol.getProtocol());
56 requestHeader.set_clientprotocolversion(protocol.getVersion());
57 RpcContentWrapper wrapper(&requestHeader, call.getRequest());
58 int rpcHeaderLen = rpcHeader.ByteSize();
59 int size = CodedOutputStream::VarintSize32(rpcHeaderLen) + rpcHeaderLen + wrapper.getLength();
60 buffer.writeBigEndian(size);
61 buffer.writeVarint32(rpcHeaderLen);
62 rpcHeader.SerializeToArray(buffer.alloc(rpcHeaderLen), rpcHeaderLen);
63 wrapper.writeTo(buffer);
64}
65
66std::vector<char> RpcRemoteCall::GetPingRequest(const std::string & clientid) {
67 WriteBuffer buffer;
68 std::vector<char> retval;
69 RpcRequestHeaderProto pingHeader;
70 pingHeader.set_callid(PING_CALL_ID);
71 pingHeader.set_clientid(clientid);
72 pingHeader.set_retrycount(INVALID_RETRY_COUNT);
73 pingHeader.set_rpckind(RpcKindProto::RPC_PROTOCOL_BUFFER);
74 pingHeader.set_rpcop(RpcRequestHeaderProto_OperationProto_RPC_FINAL_PACKET);
75 int rpcHeaderLen = pingHeader.ByteSize();
76 int size = CodedOutputStream::VarintSize32(rpcHeaderLen) + rpcHeaderLen;
77 buffer.writeBigEndian(size);
78 buffer.writeVarint32(rpcHeaderLen);
79 pingHeader.SerializeWithCachedSizesToArray(reinterpret_cast<unsigned char *>(buffer.alloc(pingHeader.ByteSize())));
80 retval.resize(buffer.getDataSize(0));
81 memcpy(&retval[0], buffer.getBuffer(0), retval.size());
82 return retval;
83}
84
85}
86}
87
88