Open Lighting Architecture  0.9.1
 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/io/SelectServer.h>
33 #include <ola/util/SequenceNumber.h>
34 #include <memory>
35 
36 #include "ola/ExportMap.h"
37 #include "common/rpc/RpcController.h"
38 
39 #include HASH_MAP_H
40 
41 namespace ola {
42 namespace rpc {
43 
44 class RpcMessage;
45 class RpcService;
46 
48  /*
49  * These are requests on the server end that haven't completed yet.
50  */
51  public:
53  ~OutstandingRequest() {}
54 
55  int id;
56  RpcController *controller;
57  google::protobuf::Message *response;
58 };
59 
66 class RpcChannel {
67  public :
77  RpcChannel(RpcService *service,
78  ola::io::ConnectedDescriptor *descriptor,
79  ExportMap *export_map = NULL);
80 
84  ~RpcChannel();
85 
90  void SetService(RpcService *service) { m_service = service; }
91 
98  bool PendingRPCs() const { return !m_requests.empty(); }
99 
103  void DescriptorReady();
104 
117 
121  void CallMethod(const google::protobuf::MethodDescriptor *method,
122  RpcController *controller,
123  const google::protobuf::Message *request,
124  google::protobuf::Message *response,
126 
132  void RequestComplete(OutstandingRequest *request);
133 
137  static const unsigned int PROTOCOL_VERSION = 1;
138 
139  private:
140  typedef HASH_NAMESPACE::HASH_MAP_CLASS<int, class OutstandingResponse*>
141  ResponseMap;
142 
143  bool SendMsg(RpcMessage *msg);
144  int AllocateMsgBuffer(unsigned int size);
145  int ReadHeader(unsigned int *version, unsigned int *size) const;
146  bool HandleNewMsg(uint8_t *buffer, unsigned int size);
147  void HandleRequest(RpcMessage *msg);
148  void HandleStreamRequest(RpcMessage *msg);
149 
150  // server end
151  void SendRequestFailed(OutstandingRequest *request);
152  void SendNotImplemented(int msg_id);
153  void DeleteOutstandingRequest(OutstandingRequest *request);
154 
155  // client end
156  void HandleResponse(RpcMessage *msg);
157  void HandleFailedResponse(RpcMessage *msg);
158  void HandleCanceledResponse(RpcMessage *msg);
159  void HandleNotImplemented(RpcMessage *msg);
160 
161  void HandleChannelClose();
162 
163  RpcService *m_service; // service to dispatch requests to
164  std::auto_ptr<SingleUseCallback0<void> > m_on_close;
165  // the descriptor to read/write to.
166  class ola::io::ConnectedDescriptor *m_descriptor;
167  SequenceNumber<uint32_t> m_sequence;
168  uint8_t *m_buffer; // buffer for incoming msgs
169  unsigned int m_buffer_size; // size of the buffer
170  unsigned int m_expected_size; // the total size of the current msg
171  unsigned int m_current_size; // the amount of data read for the current msg
172  HASH_NAMESPACE::HASH_MAP_CLASS<int, OutstandingRequest*> m_requests;
173  ResponseMap m_responses;
174  ExportMap *m_export_map;
175  UIntMap *m_recv_type_map;
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_