WPILibC++  unspecified
RpcServer.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2015-2018 FIRST. 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 NTCORE_RPCSERVER_H_
9 #define NTCORE_RPCSERVER_H_
10 
11 #include <utility>
12 
13 #include <wpi/DenseMap.h>
14 #include <wpi/mutex.h>
15 
16 #include "CallbackManager.h"
17 #include "Handle.h"
18 #include "IRpcServer.h"
19 #include "Log.h"
20 
21 namespace nt {
22 
23 namespace impl {
24 
25 typedef std::pair<unsigned int, unsigned int> RpcIdPair;
26 
27 struct RpcNotifierData : public RpcAnswer {
28  RpcNotifierData(NT_Entry entry_, NT_RpcCall call_, StringRef name_,
29  StringRef params_, const ConnectionInfo& conn_,
30  IRpcServer::SendResponseFunc send_response_)
31  : RpcAnswer{entry_, call_, name_, params_, conn_},
32  send_response{send_response_} {}
33 
34  IRpcServer::SendResponseFunc send_response;
35 };
36 
37 using RpcListenerData =
39 
41  : public CallbackThread<RpcServerThread, RpcAnswer, RpcListenerData,
42  RpcNotifierData> {
43  public:
44  RpcServerThread(int inst, wpi::Logger& logger)
45  : m_inst(inst), m_logger(logger) {}
46 
47  bool Matches(const RpcListenerData& listener, const RpcNotifierData& data) {
48  return !data.name.empty() && data.send_response;
49  }
50 
51  void SetListener(RpcNotifierData* data, unsigned int listener_uid) {
52  unsigned int local_id = Handle{data->entry}.GetIndex();
53  unsigned int call_uid = Handle{data->call}.GetIndex();
54  RpcIdPair lookup_uid{local_id, call_uid};
55  m_response_map.insert(std::make_pair(lookup_uid, data->send_response));
56  }
57 
58  void DoCallback(std::function<void(const RpcAnswer& call)> callback,
59  const RpcNotifierData& data) {
60  DEBUG4("rpc calling " << data.name);
61  unsigned int local_id = Handle{data.entry}.GetIndex();
62  unsigned int call_uid = Handle{data.call}.GetIndex();
63  RpcIdPair lookup_uid{local_id, call_uid};
64  callback(data);
65  {
66  std::lock_guard<wpi::mutex> lock(m_mutex);
67  auto i = m_response_map.find(lookup_uid);
68  if (i != m_response_map.end()) {
69  // post an empty response and erase it
70  (i->getSecond())("");
71  m_response_map.erase(i);
72  }
73  }
74  }
75 
76  int m_inst;
77  wpi::Logger& m_logger;
79 };
80 
81 } // namespace impl
82 
83 class RpcServer : public IRpcServer,
84  public CallbackManager<RpcServer, impl::RpcServerThread> {
85  friend class RpcServerTest;
86  friend class CallbackManager<RpcServer, impl::RpcServerThread>;
87 
88  public:
89  RpcServer(int inst, wpi::Logger& logger);
90 
91  void Start();
92 
93  unsigned int Add(std::function<void(const RpcAnswer& answer)> callback);
94  unsigned int AddPolled(unsigned int poller_uid);
95  void RemoveRpc(unsigned int rpc_uid) override;
96 
97  void ProcessRpc(unsigned int local_id, unsigned int call_uid, StringRef name,
98  StringRef params, const ConnectionInfo& conn,
99  SendResponseFunc send_response,
100  unsigned int rpc_uid) override;
101 
102  bool PostRpcResponse(unsigned int local_id, unsigned int call_uid,
103  wpi::StringRef result);
104 
105  private:
106  int m_inst;
107  wpi::Logger& m_logger;
108 };
109 
110 } // namespace nt
111 
112 #endif // NTCORE_RPCSERVER_H_
NetworkTables Connection Information.
Definition: ntcore_cpp.h:62
Definition: CallbackManager.h:54
NetworkTables Remote Procedure Call (Server Side)
Definition: ntcore_cpp.h:125
Definition: DenseMap.h:633
NT_Entry entry
Entry handle.
Definition: ntcore_cpp.h:133
NT_RpcCall call
Call handle.
Definition: ntcore_cpp.h:136
Definition: RpcServer.h:40
Definition: Handle.h:20
Definition: IStorage.h:21
bool PostRpcResponse(NT_Entry entry, NT_RpcCall call, StringRef result)
Post RPC response (return value) for a polled RPC.
Definition: ntcore_cpp.cpp:563
Definition: CallbackManager.h:168
Definition: RpcServer.h:27
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
Definition: Logger.h:30
std::string name
Entry name.
Definition: ntcore_cpp.h:139
Definition: IRpcServer.h:18
Definition: RpcServer.h:83
Definition: CallbackManager.h:30