Open Lighting Architecture  0.10.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 
39 namespace ola {
40 namespace rpc {
41 
42 class RpcMessage;
43 class RpcService;
44 
51 class RpcChannel {
52  public :
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,
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_
A 0 arg, single use callback that returns void.
Definition: Callback.h:157
void SetService(RpcService *service)
Set the Service to use to handle incoming requests.
Definition: RpcChannel.h:83
Represents the RPC session between a client and server.
Definition: RpcSession.h:45
SingleUseCallback1< void, class RpcSession * > CloseCallback
The callback to run when the channel is closed.
Definition: RpcChannel.h:59
RpcSession * Session()
Return the RpcSession associated with this channel.
Definition: RpcChannel.cpp:276
void RequestComplete(class OutstandingRequest *request)
Invoked by the RPC completion handler when the server side response is ready.
Definition: RpcChannel.cpp:259
A container for the exported variables.
Definition: ExportMap.h:324
A BidirectionalFileDescriptor that also generates notifications when closed.
Definition: Descriptor.h:282
Export variables on the http server.
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
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
Definition: ExportMap.h:242
Definition: RpcChannel.cpp:61
~RpcChannel()
Destructor.
Definition: RpcChannel.cpp:138
The RPC channel used to communicate between the client and the server. This implementation runs over ...
Definition: RpcChannel.h:51
RpcChannel(RpcService *service, ola::io::ConnectedDescriptor *descriptor, ExportMap *export_map=NULL)
Create a new RpcChannel.
Definition: RpcChannel.cpp:109
A 1 argument callback which deletes itself after it's run.
Definition: Callback.h:1004
void DescriptorReady()
Called when new data arrives on the descriptor.
Definition: RpcChannel.cpp:142
The namespace containing all OLA symbols.
Definition: Credentials.cpp:44
A RpcController object is passed every time an RPC is invoked and is used to indicate the success or ...
Definition: RpcController.h:42
static const unsigned int PROTOCOL_VERSION
the RPC protocol version.
Definition: RpcChannel.h:136
Definition: RpcService.h:35
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