Open Lighting Architecture  0.9.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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
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_