pvAccessCPP  7.1.1
remote.h
1 /**
2  * Copyright - See the COPYRIGHT that is included with this distribution.
3  * pvAccessCPP is distributed subject to a Software License Agreement found
4  * in file LICENSE that is included with this distribution.
5  */
6 
7 #ifndef REMOTE_H_
8 #define REMOTE_H_
9 
10 #ifdef epicsExportSharedSymbols
11 # define remoteEpicsExportSharedSymbols
12 # undef epicsExportSharedSymbols
13 #endif
14 
15 #include <map>
16 #include <string>
17 
18 #include <osiSock.h>
19 
20 #include <pv/serialize.h>
21 #include <pv/pvType.h>
22 #include <pv/byteBuffer.h>
23 #include <pv/timer.h>
24 #include <pv/pvData.h>
25 #include <pv/sharedPtr.h>
26 
27 #ifdef remoteEpicsExportSharedSymbols
28 # define epicsExportSharedSymbols
29 # undef remoteEpicsExportSharedSymbols
30 #endif
31 
32 #include <pv/pvaConstants.h>
33 #include <pv/configuration.h>
34 #include <pv/fairQueue.h>
35 #include <pv/pvaDefs.h>
36 
37 /// TODO only here because of the Lockable
38 #include <pv/pvAccess.h>
39 
40 namespace epics {
41 namespace pvAccess {
42 
43 class TransportRegistry;
44 class ClientChannelImpl;
45 
46 enum QoS {
47  /**
48  * Default behavior.
49  */
50  QOS_DEFAULT = 0x00,
51  /**
52  * Require reply (acknowledgment for reliable operation).
53  */
55  /**
56  * Best-effort option (no reply).
57  */
59  /**
60  * Process option.
61  */
62  QOS_PROCESS = 0x04,
63  /**
64  * Initialize option.
65  */
66  QOS_INIT = 0x08,
67  /**
68  * Destroy option.
69  */
70  QOS_DESTROY = 0x10,
71  /**
72  * Share data option.
73  */
74  QOS_SHARE = 0x20,
75  /**
76  * Get.
77  */
78  QOS_GET = 0x40,
79  /**
80  * Get-put.
81  */
82  QOS_GET_PUT = 0x80
83 };
84 
85 enum ApplicationCommands {
86  CMD_BEACON = 0,
87  CMD_CONNECTION_VALIDATION = 1,
88  CMD_ECHO = 2,
89  CMD_SEARCH = 3,
90  CMD_SEARCH_RESPONSE = 4,
91  CMD_AUTHNZ = 5,
92  CMD_ACL_CHANGE = 6,
93  CMD_CREATE_CHANNEL = 7,
94  CMD_DESTROY_CHANNEL = 8,
95  CMD_CONNECTION_VALIDATED = 9,
96  CMD_GET = 10,
97  CMD_PUT = 11,
98  CMD_PUT_GET = 12,
99  CMD_MONITOR = 13,
100  CMD_ARRAY = 14,
101  CMD_DESTROY_REQUEST = 15,
102  CMD_PROCESS = 16,
103  CMD_GET_FIELD = 17,
104  CMD_MESSAGE = 18,
105  CMD_MULTIPLE_DATA = 19,
106  CMD_RPC = 20,
107  CMD_CANCEL_REQUEST = 21,
108  CMD_ORIGIN_TAG = 22
109 };
110 
111 enum ControlCommands {
112  CMD_SET_MARKER = 0,
113  CMD_ACK_MARKER = 1,
114  CMD_SET_ENDIANESS = 2
115 };
116 
117 /**
118  * Interface defining transport send control.
119  */
120 class TransportSendControl : public epics::pvData::SerializableControl {
121 public:
122  POINTER_DEFINITIONS(TransportSendControl);
123 
124  virtual ~TransportSendControl() {}
125 
126  virtual void startMessage(epics::pvData::int8 command, std::size_t ensureCapacity, epics::pvData::int32 payloadSize = 0) = 0;
127  virtual void endMessage() = 0;
128 
129  virtual void flush(bool lastMessageCompleted) = 0;
130 
131  virtual void setRecipient(osiSockAddr const & sendTo) = 0;
132 };
133 
134 /**
135  * Interface defining transport sender (instance sending data over transport).
136  */
137 class TransportSender : public Lockable, public fair_queue<TransportSender>::entry {
138 public:
139  POINTER_DEFINITIONS(TransportSender);
140 
141  TransportSender() :bytesTX(0u), bytesRX(0u) {}
142  virtual ~TransportSender() {}
143 
144  /**
145  * Called by transport.
146  * By this call transport gives callee ownership over the buffer.
147  * Calls on <code>TransportSendControl</code> instance must be made from
148  * calling thread. Moreover, ownership is valid only for the time of call
149  * of this method.
150  * NOTE: these limitations allow efficient implementation.
151  */
152  virtual void send(epics::pvData::ByteBuffer* buffer, TransportSendControl* control) = 0;
153 
154  size_t bytesTX;
155  size_t bytesRX;
156 };
157 
158 class ClientChannelImpl;
159 class SecuritySession;
160 
161 /**
162  * Interface defining transport (connection).
163  */
164 class epicsShareClass Transport : public epics::pvData::DeserializableControl {
165 public:
167 
168  static size_t num_instances;
169 
170  Transport();
171  virtual ~Transport();
172 
173  /**
174  * Acquires transport.
175  * @param client client (channel) acquiring the transport
176  * @return <code>true</code> if transport was granted, <code>false</code> otherwise.
177  */
178  virtual bool acquire(std::tr1::shared_ptr<ClientChannelImpl> const & client) = 0;
179 
180  /**
181  * Releases transport.
182  * @param client client (channel) releasing the transport
183  */
184  virtual void release(pvAccessID clientId) = 0;
185 
186  /**
187  * Get protocol type (tcp, udp, ssl, etc.).
188  * @return protocol type.
189  */
190  virtual std::string getType() const = 0;
191 
192  virtual const osiSockAddr& getRemoteAddress() const = 0;
193 
194  virtual const std::string& getRemoteName() const = 0;
195 
196  // TODO getContext?
197 
198  /**
199  * Get receive buffer size.
200  * @return receive buffer size.
201  */
202  virtual std::size_t getReceiveBufferSize() const = 0;
203 
204  /**
205  * Transport priority.
206  * @return protocol priority.
207  */
208  virtual epics::pvData::int16 getPriority() const = 0;
209 
210  /**
211  * Set remote transport receive buffer size.
212  * @param receiveBufferSize receive buffer size.
213  */
215 
216  /**
217  * Set remote transport socket receive buffer size.
218  * @param socketReceiveBufferSize remote socket receive buffer size.
219  */
221 
222  /**
223  * Set byte order.
224  * @param byteOrder byte order to set.
225  */
226  // TODO enum
227  virtual void setByteOrder(int byteOrder) = 0;
228 
229  /**
230  * Enqueue send request.
231  * @param sender
232  */
233  virtual void enqueueSendRequest(TransportSender::shared_pointer const & sender) = 0;
234 
235  /**
236  * Flush send queue (sent messages).
237  */
238  virtual void flushSendQueue() = 0;
239 
240  /**
241  * Notify transport that it is has been verified.
242  * @param status vefification status;
243  */
244  virtual void verified(epics::pvData::Status const & status) = 0;
245 
246  /**
247  * Waits (if needed) until transport is verified, i.e. verified() method is being called.
248  * @param timeoutMs timeout to wait for verification, infinite if 0.
249  */
250  virtual bool verify(epics::pvData::int32 timeoutMs) = 0;
251 
252  /**
253  * Close transport.
254  */
255  virtual void close() = 0;
256 
257  //! Call after close() to wait for any worker threads to exit
258  virtual void waitJoin() {}
259 
260  /**
261  * Check connection status.
262  * @return <code>true</code> if connected.
263  */
264  virtual bool isClosed() = 0;
265 
266  /**
267  * Pass data to the active security plug-in session.
268  * @param data the data (any data), can be <code>null</code>.
269  */
270  virtual void authNZMessage(epics::pvData::PVStructure::shared_pointer const & data) = 0;
271 
274 };
275 
276 class Channel;
277 class SecurityPlugin;
278 class AuthenticationRegistry;
279 
280 /**
281  * Not public IF, used by Transports, etc.
282  */
283 class Context {
284 public:
285  POINTER_DEFINITIONS(Context);
286 
287  virtual ~Context() {}
288 
289  virtual epics::pvData::Timer::shared_pointer getTimer() = 0;
290 
291  virtual TransportRegistry* getTransportRegistry() = 0;
292 
293 
294 
295 
296  virtual Configuration::const_shared_pointer getConfiguration() = 0;
297 
298  ///
299  /// due to ClientContextImpl
300  ///
301 
302  virtual void newServerDetected() = 0;
303 
304  virtual std::tr1::shared_ptr<Channel> getChannel(pvAccessID id) = 0;
305  virtual Transport::shared_pointer getSearchTransport() = 0;
306 };
307 
308 /**
309  * Interface defining response handler.
310  */
311 class ResponseHandler {
312 public:
313  POINTER_DEFINITIONS(ResponseHandler);
314 
315  static size_t num_instances;
316 
317  ResponseHandler(Context* context, const std::string& description);
318  virtual ~ResponseHandler();
319 
320  /**
321  * Handle response.
322  * @param[in] responseFrom remote address of the responder, <code>0</code> if unknown.
323  * @param[in] transport response source transport.
324  * @param[in] version message version.
325  * @param[in] payloadSize size of this message data available in the <code>payloadBuffer</code>.
326  * @param[in] payloadBuffer message payload data.
327  * Note that this might not be the only message in the buffer.
328  * Code must not manipulate buffer.
329  */
330  virtual void
331  handleResponse(osiSockAddr* responseFrom, Transport::shared_pointer const & transport,
334 
335 protected:
336  /**
337  * Response hanlder description.
338  */
340 
341  /**
342  * Debug flag.
343  */
345 };
346 
347 /**
348  * A request that expects an response.
349  * Responses identified by its I/O ID.
350  */
351 class ResponseRequest : public TransportSender {
352 public:
353  POINTER_DEFINITIONS(ResponseRequest);
354 
355  virtual ~ResponseRequest() {}
356 
357  /**
358  * Get I/O ID.
359  * @return ioid
360  */
361  virtual pvAccessID getIOID() const = 0;
362 
363  /**
364  * Timeout notification.
365  */
366  virtual void timeout() = 0;
367 
368  /**
369  * Cancel response request (always to be called to complete/destroy).
370  */
371  virtual void cancel() = 0;
372 
373  /**
374  * Report status to clients (e.g. disconnected).
375  * @param status to report.
376  */
377  virtual void reportStatus(Channel::ConnectionState status) = 0;
378 
379  /**
380  * used by MessageHandler and reportChannelStateChange().
381  *
382  * May return NULL
383  */
385 
386  /**
387  * Notification response.
388  * @param transport
389  * @param version
390  * @param payloadBuffer
391  */
393 
394 };
395 
396 
397 }
398 }
399 
400 #endif /* REMOTE_H_ */
virtual pvAccessID getIOID() const =0
Get I/O ID.
virtual void timeout()=0
Timeout notification.
Interface defining transport (connection).
Definition: remote.h:164
Process option.
Definition: remote.h:62
Require reply (acknowledgment for reliable operation).
Definition: remote.h:54
std::string _description
Response hanlder description.
Definition: remote.h:339
Destroy option.
Definition: remote.h:70
virtual std::tr1::shared_ptr< ChannelBaseRequester > getRequester()=0
used by MessageHandler and reportChannelStateChange().
Best-effort option (no reply).
Definition: remote.h:58
Initialize option.
Definition: remote.h:66
virtual void authNZMessage(epics::pvData::PVStructure::shared_pointer const &data)=0
Pass data to the active security plug-in session.
epics::pvData::int32 _debugLevel
Debug flag.
Definition: remote.h:344
virtual void send(epics::pvData::ByteBuffer *buffer, TransportSendControl *control)=0
Called by transport.
Default behavior.
Definition: remote.h:50
virtual void response(Transport::shared_pointer const &transport, epics::pvData::int8 version, epics::pvData::ByteBuffer *payloadBuffer)=0
Notification response.
virtual void newServerDetected()=0
due to ClientContextImpl
Share data option.
Definition: remote.h:74
virtual void cancel()=0
Cancel response request (always to be called to complete/destroy).
virtual void handleResponse(osiSockAddr *responseFrom, Transport::shared_pointer const &transport, epics::pvData::int8 version, epics::pvData::int8 command, std::size_t payloadSize, epics::pvData::ByteBuffer *payloadBuffer)
Handle response.
virtual void reportStatus(Channel::ConnectionState status)=0
Report status to clients (e.g.