pvAccessCPP  7.1.6
rpcClient.h
1 /**
2  * Copyright - See the COPYRIGHT that is included with this distribution.
3  * EPICS pvAccessCPP is distributed subject to a Software License Agreement
4  * found in file LICENSE that is included with this distribution.
5  */
6 
7 #ifndef RPCCLIENT_H
8 #define RPCCLIENT_H
9 
10 #include <string>
11 
12 #ifdef epicsExportSharedSymbols
13 # define rpcClientEpicsExportSharedSymbols
14 # undef epicsExportSharedSymbols
15 #endif
16 #include <pv/pvData.h>
17 #include <pv/valueBuilder.h>
18 #ifdef rpcClientEpicsExportSharedSymbols
19 # define epicsExportSharedSymbols
20 # undef rpcClientEpicsExportSharedSymbols
21 #endif
22 
23 #include <pv/rpcService.h>
24 
25 #include <shareLib.h>
26 
27 #define RPCCLIENT_DEFAULT_TIMEOUT 5.0
28 
29 namespace epics
30 {
31 
32 namespace pvAccess
33 {
34 
35 /**
36  * RPCClient is an interface class that is used by a service client.
37  *
38  */
39 class epicsShareClass RPCClient
40 {
41 public:
42  POINTER_DEFINITIONS(RPCClient);
43 
44  /**
45  * Create a RPCClient.
46  *
47  * @param serviceName the service name
48  * @param pvRequest the pvRequest for the ChannelRPC
49  * @return the RPCClient interface
50  */
51  static shared_pointer create(const std::string & serviceName,
52  epics::pvData::PVStructure::shared_pointer const & pvRequest = epics::pvData::PVStructure::shared_pointer());
53 
54  RPCClient(const std::string & serviceName,
55  epics::pvData::PVStructure::shared_pointer const & pvRequest,
56  const ChannelProvider::shared_pointer& provider = ChannelProvider::shared_pointer(),
57  const std::string& address = std::string());
58 
59  ~RPCClient() {destroy();}
60 
61 
62 
63  /**
64  * Destroy this instance (i.e. release resources).
65  */
66  void destroy();
67 
68  /**
69  * Connect to the server.
70  * The method blocks until the connection is made or a timeout occurs.
71  * It is the same as calling issueConnect and then waitConnect.
72  * @param timeout Timeout in seconds to wait, 0 means forever.
73  * @returns (false,true) If (not connected, is connected).
74  * If false then connect must be reissued.
75  */
76  bool connect(double timeout = RPCCLIENT_DEFAULT_TIMEOUT);
77 
78  /**
79  * Issue a connect request and return immediately.
80  * waitConnect must be called to complete the request.
81  */
82  void issueConnect();
83 
84  /**
85  * Wait for the connect request to complete.
86  * @param timeout timeout in seconds to wait, 0 means forever.
87  * @returns (false,true) If (not connected, is connected).
88  * If false then connect must be reissued.
89  */
90  bool waitConnect(double timeout = RPCCLIENT_DEFAULT_TIMEOUT);
91 
92  /**
93  * Sends a request and wait for the response or until timeout occurs.
94  * This method will also wait for client to connect, if necessary.
95  *
96  * @param pvArgument the argument for the rpc
97  * @param timeout the time in seconds to wait for the response, 0 means forever.
98  * @param lastRequest If true an automatic destroy is made.
99  * @return request response.
100  * @throws RPCRequestException exception thrown on error or timeout.
101  */
102  epics::pvData::PVStructure::shared_pointer request(
103  epics::pvData::PVStructure::shared_pointer const & pvArgument,
104  double timeout = RPCCLIENT_DEFAULT_TIMEOUT,
105  bool lastRequest = false);
106 
107  /**
108  * Issue a channelRPC request and return immediately.
109  * waitRequest must be called to complete the request.
110  * @param pvAgument The argument to pass to the server.
111  * @param lastRequest If true an automatic destroy is made.
112  * @throws std::runtime_error excption thrown on any runtime error condition (e.g. no connection made).
113  */
114  void issueRequest(
115  epics::pvData::PVStructure::shared_pointer const & pvArgument,
116  bool lastRequest = false);
117 
118  /**
119  * Wait for the request to complete.
120  * @param timeout the time in seconds to wait for the reponse.
121  * @return request response.
122  * @throws RPCRequestException exception thrown on error or timeout.
123  */
124  epics::pvData::PVStructure::shared_pointer waitResponse(double timeout = RPCCLIENT_DEFAULT_TIMEOUT);
125 
126 private:
127 
128  const std::string m_serviceName;
129  ChannelProvider::shared_pointer m_provider;
130  Channel::shared_pointer m_channel;
131  ChannelRPC::shared_pointer m_rpc;
132  const epics::pvData::PVStructure::shared_pointer m_pvRequest;
133 
134  struct RPCRequester;
135  std::tr1::shared_ptr<RPCRequester> m_rpc_requester;
136 
137  RPCClient(const RPCClient&);
138  RPCClient& operator=(const RPCClient&);
139 };
140 
141 }
142 
143 }
144 
145 #endif
#define RPCCLIENT_DEFAULT_TIMEOUT
Copyright - See the COPYRIGHT that is included with this distribution.
Definition: rpcClient.h:27