1 //
2 // Copyright (C) 2015 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 #ifndef PROXY_RPC_SERVER_H
17 #define PROXY_RPC_SERVER_H
18 
19 #include <errno.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <time.h>
23 #include <unistd.h>
24 
25 #include <iostream>
26 
27 #include <base/logging.h>
28 #include <base/callback.h>
29 
30 #include "XmlRpc.h"
31 
32 #include "proxy_shill_wifi_client.h"
33 
34 typedef const base::Callback<XmlRpc::XmlRpcValue(
35     XmlRpc::XmlRpcValue, ProxyShillWifiClient*)> RpcServerMethodHandler;
36 
37 class ProxyRpcServer;
38 class ProxyRpcServerMethod : public XmlRpc::XmlRpcServerMethod {
39  public:
40   ProxyRpcServerMethod(const std::string& method_name,
41                        const RpcServerMethodHandler& handler,
42                        ProxyShillWifiClient* shill_wifi_client,
43                        ProxyRpcServer* server);
44   // This is the function signature exposed by the XmlRpc++ library
45   // that we depend on and hence the non-const references.
46   void execute(XmlRpc::XmlRpcValue& params_in, XmlRpc::XmlRpcValue& value_out);
47   std::string help(void);
48 
49  private:
50   RpcServerMethodHandler handler_;
51   // RPC server methods hold a copy of the raw pointer to the instance of
52   // the |ShillWifiClient| owned by the RPC server.
53   ProxyShillWifiClient* shill_wifi_client_;
54 
55   DISALLOW_COPY_AND_ASSIGN(ProxyRpcServerMethod);
56 };
57 
58 class ProxyRpcServer : public XmlRpc::XmlRpcServer {
59  public:
60   ProxyRpcServer(int server_port,
61                  std::unique_ptr<ProxyShillWifiClient> shill_wifi_client);
62   void Run();
63   void RegisterRpcMethod(const std::string& method_name,
64                          const RpcServerMethodHandler& handler);
65 
66  private:
67   int server_port_;
68   // RPC server owns the only instance of the |ShillWifiClient| used.
69   std::unique_ptr<ProxyShillWifiClient> shill_wifi_client_;
70   // Instances of the various methods registered with the server.
71   std::vector<std::unique_ptr<ProxyRpcServerMethod>> methods_;
72 
73   DISALLOW_COPY_AND_ASSIGN(ProxyRpcServer);
74 };
75 
76 #endif // PROXY_RPC_SERVER_H
77