Open Lighting Architecture: common/rpc/RpcChannel.h Source File

| Open Lighting Architecture 0.10.9

Loading...
Searching...
No Matches
RpcChannel.h
1/*
2 * This library is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU Lesser General Public
4 * License as published by the Free Software Foundation; either
5 * version 2.1 of the License, or (at your option) any later version.
6 *
7 * This library is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
11 *
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with this library; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15 *
16 * RpcChannel.h
17 * The RPC Channel
18 * Copyright (C) 2005 Simon Newton
19 */
20
21#ifndef COMMON_RPC_RPCCHANNEL_H_
22#define COMMON_RPC_RPCCHANNEL_H_
23
24#if HAVE_CONFIG_H
25#include <config.h>
26#endif // HAVE_CONFIG_H
27
28#include <stdint.h>
29#include <google/protobuf/service.h>
30#include <ola/Callback.h>
31#include <ola/io/Descriptor.h>
32#include <ola/util/SequenceNumber.h>
33#include <memory>
34
35#include "ola/ExportMap.h"
36
37#include HASH_MAP_H
38
39namespace ola {
40namespace rpc {
41
42class RpcMessage;
43class RpcService;
44
51 class RpcChannel {
52 public :
59 typedef SingleUseCallback1<void, class RpcSession*> CloseCallback;
60
70 RpcChannel(RpcService *service,
71 ola::io::ConnectedDescriptor *descriptor,
72 ExportMap *export_map = NULL);
73
77 ~RpcChannel();
78
83 void SetService(RpcService *service) { m_service = service; }
84
91 bool PendingRPCs() const { return !m_requests.empty(); }
92
96 void DescriptorReady();
97
109 void SetChannelCloseHandler(CloseCallback *callback);
110
114 void CallMethod(const google::protobuf::MethodDescriptor *method,
115 class RpcController *controller,
116 const google::protobuf::Message *request,
117 google::protobuf::Message *response,
118 SingleUseCallback0<void> *done);
119
125 void RequestComplete(class OutstandingRequest *request);
126
131 RpcSession *Session();
132
136 static const unsigned int PROTOCOL_VERSION = 1;
137
138 private:
139 typedef HASH_NAMESPACE::HASH_MAP_CLASS<int, class OutstandingResponse*>
140 ResponseMap;
141
142 std::auto_ptr<RpcSession> m_session;
143 RpcService *m_service; // service to dispatch requests to
144 std::auto_ptr<CloseCallback> m_on_close;
145 // the descriptor to read/write to.
146 class ola::io::ConnectedDescriptor *m_descriptor;
147 SequenceNumber<uint32_t> m_sequence;
148 uint8_t *m_buffer; // buffer for incoming msgs
149 unsigned int m_buffer_size; // size of the buffer
150 unsigned int m_expected_size; // the total size of the current msg
151 unsigned int m_current_size; // the amount of data read for the current msg
152 HASH_NAMESPACE::HASH_MAP_CLASS<int, class OutstandingRequest*> m_requests;
153 ResponseMap m_responses;
154 ExportMap *m_export_map;
155 UIntMap *m_recv_type_map;
156
157 bool SendMsg(RpcMessage *msg);
158 int AllocateMsgBuffer(unsigned int size);
159 int ReadHeader(unsigned int *version, unsigned int *size) const;
160 bool HandleNewMsg(uint8_t *buffer, unsigned int size);
161 void HandleRequest(RpcMessage *msg);
162 void HandleStreamRequest(RpcMessage *msg);
163
164 // server end
165 void SendRequestFailed(class OutstandingRequest *request);
166 void SendNotImplemented(int msg_id);
167 void DeleteOutstandingRequest(class OutstandingRequest *request);
168
169 // client end
170 void HandleResponse(RpcMessage *msg);
171 void HandleFailedResponse(RpcMessage *msg);
172 void HandleCanceledResponse(RpcMessage *msg);
173 void HandleNotImplemented(RpcMessage *msg);
174
175 void HandleChannelClose();
176
177 static const char K_RPC_RECEIVED_TYPE_VAR[];
178 static const char K_RPC_RECEIVED_VAR[];
179 static const char K_RPC_SENT_ERROR_VAR[];
180 static const char K_RPC_SENT_VAR[];
181 static const char *K_RPC_VARIABLES[];
182 static const char STREAMING_NO_RESPONSE[];
183 static const unsigned int INITIAL_BUFFER_SIZE = 1 << 11; // 2k
184 static const unsigned int MAX_BUFFER_SIZE = 1 << 20; // 1M
185};
186} // namespace rpc
187} // namespace ola
188#endif // COMMON_RPC_RPCCHANNEL_H_
Callback.h
ExportMap.h
Export variables on the http server.
ola::ExportMap
A container for the exported variables.
Definition ExportMap.h:324
ola::SequenceNumber
SequenceNumber, this ensures that we increment the sequence number whenever we go to use it.
Definition SequenceNumber.h:30
ola::SingleUseCallback0
A 0 argument callback which deletes itself after it's run.
Definition Callback.h:141
ola::SingleUseCallback1
A 1 argument callback which deletes itself after it's run.
Definition Callback.h:1004
ola::UIntMap
Definition ExportMap.h:242
ola::client::RpcChannel::RpcChannel
RpcChannel(RpcService *service, ola::io::ConnectedDescriptor *descriptor, ExportMap *export_map=NULL)
Create a new RpcChannel.
Definition RpcChannel.cpp:109
ola::io::ConnectedDescriptor
A BidirectionalFileDescriptor that also generates notifications when closed.
Definition Descriptor.h:283
ola::rpc::OutstandingRequest
Definition RpcChannel.cpp:61
ola::rpc::RpcChannel::Session
RpcSession * Session()
Return the RpcSession associated with this channel.
Definition RpcChannel.cpp:276
ola::rpc::RpcChannel::RequestComplete
void RequestComplete(class OutstandingRequest *request)
Invoked by the RPC completion handler when the server side response is ready.
Definition RpcChannel.cpp:259
ola::rpc::RpcChannel::CloseCallback
SingleUseCallback1< void, class RpcSession * > CloseCallback
The callback to run when the channel is closed.
Definition RpcChannel.h:59
ola::rpc::RpcChannel::~RpcChannel
~RpcChannel()
Destructor.
Definition RpcChannel.cpp:138
ola::rpc::RpcChannel::PROTOCOL_VERSION
static const unsigned int PROTOCOL_VERSION
the RPC protocol version.
Definition RpcChannel.h:136
ola::rpc::RpcChannel::CallMethod
void CallMethod(const google::protobuf::MethodDescriptor *method, class RpcController *controller, const google::protobuf::Message *request, google::protobuf::Message *response, SingleUseCallback0< void > *done)
Invoke an RPC method on this channel.
Definition RpcChannel.cpp:205
ola::rpc::RpcChannel::SetChannelCloseHandler
void SetChannelCloseHandler(CloseCallback *callback)
Set the Callback to be run when the channel fails. The callback will be invoked if the descriptor is ...
Definition RpcChannel.cpp:201
ola::rpc::RpcChannel::RpcChannel
RpcChannel(RpcService *service, ola::io::ConnectedDescriptor *descriptor, ExportMap *export_map=NULL)
Create a new RpcChannel.
Definition RpcChannel.cpp:109
ola::rpc::RpcChannel::PendingRPCs
bool PendingRPCs() const
Check if there are any pending RPCs on the channel. Pending RPCs are those where a request has been s...
Definition RpcChannel.h:91
ola::rpc::RpcChannel::DescriptorReady
void DescriptorReady()
Called when new data arrives on the descriptor.
Definition RpcChannel.cpp:142
ola::rpc::RpcChannel::SetService
void SetService(RpcService *service)
Set the Service to use to handle incoming requests.
Definition RpcChannel.h:83
ola::rpc::RpcController
A RpcController object is passed every time an RPC is invoked and is used to indicate the success or ...
Definition RpcController.h:42
ola::rpc::RpcService
Definition RpcService.h:35
ola::rpc::RpcSession
Represents the RPC session between a client and server.
Definition RpcSession.h:45
Descriptor.h
ola::rpc
The RPC (Remote Procedure Call) system.
Definition RpcChannel.cpp:41
ola
The namespace containing all OLA symbols.
Definition Credentials.cpp:44