WPILibC++  unspecified
RpcServer.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) FIRST 2015. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #ifndef NT_RPCSERVER_H_
9 #define NT_RPCSERVER_H_
10 
11 #include "llvm/DenseMap.h"
12 
13 #include "CallbackManager.h"
14 #include "Handle.h"
15 #include "IRpcServer.h"
16 #include "Log.h"
17 
18 namespace nt {
19 
20 namespace impl {
21 
22 typedef std::pair<unsigned int, unsigned int> RpcIdPair;
23 
24 struct RpcNotifierData : public RpcAnswer {
25  RpcNotifierData(NT_Entry entry_, NT_RpcCall call_, StringRef name_,
26  StringRef params_, const ConnectionInfo& conn_,
27  IRpcServer::SendResponseFunc send_response_)
28  : RpcAnswer{entry_, call_, name_, params_, conn_},
29  send_response{send_response_} {}
30 
31  IRpcServer::SendResponseFunc send_response;
32 };
33 
34 using RpcListenerData =
36 
38  : public CallbackThread<RpcServerThread, RpcAnswer, RpcListenerData,
39  RpcNotifierData> {
40  public:
41  RpcServerThread(int inst, wpi::Logger& logger)
42  : m_inst(inst), m_logger(logger) {}
43 
44  bool Matches(const RpcListenerData& listener, const RpcNotifierData& data) {
45  return !data.name.empty() && data.send_response;
46  }
47 
48  void SetListener(RpcNotifierData* data, unsigned int listener_uid) {
49  unsigned int local_id = Handle{data->entry}.GetIndex();
50  unsigned int call_uid = Handle{data->call}.GetIndex();
51  RpcIdPair lookup_uid{local_id, call_uid};
52  m_response_map.insert(std::make_pair(lookup_uid, data->send_response));
53  }
54 
55  void DoCallback(std::function<void(const RpcAnswer& call)> callback,
56  const RpcNotifierData& data) {
57  DEBUG4("rpc calling " << data.name);
58  unsigned int local_id = Handle{data.entry}.GetIndex();
59  unsigned int call_uid = Handle{data.call}.GetIndex();
60  RpcIdPair lookup_uid{local_id, call_uid};
61  callback(data);
62  {
63  std::lock_guard<std::mutex> lock(m_mutex);
64  auto i = m_response_map.find(lookup_uid);
65  if (i != m_response_map.end()) {
66  // post an empty response and erase it
67  (i->getSecond())("");
68  m_response_map.erase(i);
69  }
70  }
71  }
72 
73  int m_inst;
74  wpi::Logger& m_logger;
76 };
77 
78 } // namespace impl
79 
80 class RpcServer : public IRpcServer,
81  public CallbackManager<RpcServer, impl::RpcServerThread> {
82  friend class RpcServerTest;
83  friend class CallbackManager<RpcServer, impl::RpcServerThread>;
84 
85  public:
86  RpcServer(int inst, wpi::Logger& logger);
87 
88  void Start();
89 
90  unsigned int Add(std::function<void(const RpcAnswer& answer)> callback);
91  unsigned int AddPolled(unsigned int poller_uid);
92  void RemoveRpc(unsigned int rpc_uid) override;
93 
94  void ProcessRpc(unsigned int local_id, unsigned int call_uid, StringRef name,
95  StringRef params, const ConnectionInfo& conn,
96  SendResponseFunc send_response,
97  unsigned int rpc_uid) override;
98 
99  void PostRpcResponse(unsigned int local_id, unsigned int call_uid,
100  llvm::StringRef result);
101 
102  private:
103  int m_inst;
104  wpi::Logger& m_logger;
105 };
106 
107 } // namespace nt
108 
109 #endif // NT_RPCSERVER_H_
NetworkTables Connection Information.
Definition: ntcore_cpp.h:57
Definition: DenseMap.h:585
Definition: CallbackManager.h:52
NetworkTables Remote Procedure Call (Server Side)
Definition: ntcore_cpp.h:120
NT_Entry entry
Entry handle.
Definition: ntcore_cpp.h:128
NT_RpcCall call
Call handle.
Definition: ntcore_cpp.h:131
Definition: RpcServer.h:37
void PostRpcResponse(NT_Entry entry, NT_RpcCall call, StringRef result)
Post RPC response (return value) for a polled RPC.
Definition: ntcore_cpp.cpp:561
Definition: Handle.h:20
Definition: IEntryNotifier.h:15
Definition: CallbackManager.h:166
Definition: RpcServer.h:24
Definition: Logger.h:30
std::string name
Entry name.
Definition: ntcore_cpp.h:134
Definition: IRpcServer.h:18
Definition: RpcServer.h:80
Definition: CallbackManager.h:28
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:42