aboutsummaryrefslogtreecommitdiff
path: root/third_party/sl4a/src/main/java/com/google/android/mobly/snippet/rpc/SimpleServer.java
blob: db7255a54f188809962773158757b3169086d5b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
 * Copyright (C) 2016 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.google.android.mobly.snippet.rpc;

import com.google.android.mobly.snippet.util.Log;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import org.json.JSONException;
import org.json.JSONObject;

/** A simple server. */
public abstract class SimpleServer {
    private static int threadIndex = 0;
    private final ConcurrentHashMap<Integer, ConnectionThread> mConnectionThreads =
            new ConcurrentHashMap<>();
    private final List<SimpleServerObserver> mObservers = new ArrayList<>();
    private volatile boolean mStopServer = false;
    private ServerSocket mServer;
    private Thread mServerThread;

    public interface SimpleServerObserver {
        void onConnect();

        void onDisconnect();
    }

    protected abstract void handleConnection(Socket socket) throws Exception;

    protected abstract void handleRPCConnection(
            Socket socket, Integer UID, BufferedReader reader, PrintWriter writer) throws Exception;

    /** Adds an observer. */
    public void addObserver(SimpleServerObserver observer) {
        mObservers.add(observer);
    }

    /** Removes an observer. */
    public void removeObserver(SimpleServerObserver observer) {
        mObservers.remove(observer);
    }

    private void notifyOnConnect() {
        for (SimpleServerObserver observer : mObservers) {
            observer.onConnect();
        }
    }

    private void notifyOnDisconnect() {
        for (SimpleServerObserver observer : mObservers) {
            observer.onDisconnect();
        }
    }

    private final class ConnectionThread extends Thread {
        private final Socket mmSocket;
        private final BufferedReader reader;
        private final PrintWriter writer;
        private final Integer UID;
        private final boolean isRpc;

        private ConnectionThread(
                Socket socket,
                boolean rpc,
                Integer uid,
                BufferedReader reader,
                PrintWriter writer) {
            setName("SimpleServer ConnectionThread " + getId());
            mmSocket = socket;
            this.UID = uid;
            this.reader = reader;
            this.writer = writer;
            this.isRpc = rpc;
        }

        @Override
        public void run() {
            Log.v("Server thread " + getId() + " started.");
            try {
                if (isRpc) {
                    Log.d("Handling RPC connection in " + getId());
                    handleRPCConnection(mmSocket, UID, reader, writer);
                } else {
                    Log.d("Handling Non-RPC connection in " + getId());
                    handleConnection(mmSocket);
                }
            } catch (Exception e) {
                if (!mStopServer) {
                    Log.e("Server error.", e);
                }
            } finally {
                close();
                mConnectionThreads.remove(this.UID);
                notifyOnDisconnect();
                Log.v("Server thread " + getId() + " stopped.");
            }
        }

        private void close() {
            if (mmSocket != null) {
                try {
                    mmSocket.close();
                } catch (IOException e) {
                    Log.e(e.getMessage(), e);
                }
            }
        }
    }

    private InetAddress getPrivateInetAddress() throws UnknownHostException, SocketException {

        InetAddress candidate = null;
        Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
        for (NetworkInterface netint : Collections.list(nets)) {
            if (!netint.isLoopback() || !netint.isUp()) { // Ignore if localhost or not active
                continue;
            }
            Enumeration<InetAddress> addresses = netint.getInetAddresses();
            for (InetAddress address : Collections.list(addresses)) {
                if (address instanceof Inet4Address) {
                    Log.d("local address " + address);
                    return address; // Prefer ipv4
                }
                candidate = address; // Probably an ipv6
            }
        }
        if (candidate != null) {
            return candidate; // return ipv6 address if no suitable ipv6
        }
        return InetAddress.getLocalHost(); // No damn matches. Give up, return local host.
    }

    /**
     * Starts the RPC server bound to the localhost address.
     *
     * @param port the port to bind to or 0 to pick any unused port
     * @throws IOException
     */
    public void startLocal(int port) throws IOException {
        InetAddress address = getPrivateInetAddress();
        mServer = new ServerSocket(port, 5 /* backlog */, address);
        start();
    }

    public int getPort() {
        return mServer.getLocalPort();
    }

    private void start() {
        mServerThread =
                new Thread() {
                    @Override
                    public void run() {
                        while (!mStopServer) {
                            try {
                                Socket sock = mServer.accept();
                                if (!mStopServer) {
                                    startConnectionThread(sock);
                                } else {
                                    sock.close();
                                }
                            } catch (IOException e) {
                                if (!mStopServer) {
                                    Log.e("Failed to accept connection.", e);
                                }
                            } catch (JSONException e) {
                                if (!mStopServer) {
                                    Log.e("Failed to parse request.", e);
                                }
                            }
                        }
                    }
                };
        mServerThread.start();
        Log.v("Bound to " + mServer.getInetAddress());
    }

    private void startConnectionThread(final Socket sock) throws IOException, JSONException {
        BufferedReader reader =
                new BufferedReader(new InputStreamReader(sock.getInputStream()), 8192);
        PrintWriter writer = new PrintWriter(sock.getOutputStream(), true);
        String data;
        if ((data = reader.readLine()) != null) {
            Log.v("Received: " + data);
            JSONObject request = new JSONObject(data);
            if (request.has("cmd") && request.has("uid")) {
                String cmd = request.getString("cmd");
                int uid = request.getInt("uid");
                JSONObject result = new JSONObject();
                if (cmd.equals("initiate")) {
                    Log.d("Initiate a new session");
                    threadIndex += 1;
                    int mUID = threadIndex;
                    ConnectionThread networkThread =
                            new ConnectionThread(sock, true, mUID, reader, writer);
                    mConnectionThreads.put(mUID, networkThread);
                    networkThread.start();
                    notifyOnConnect();
                    result.put("uid", mUID);
                    result.put("status", true);
                    result.put("error", null);
                } else if (cmd.equals("continue")) {
                    Log.d("Continue an existing session");
                    Log.d("keys: " + mConnectionThreads.keySet().toString());
                    if (!mConnectionThreads.containsKey(uid)) {
                        result.put("uid", uid);
                        result.put("status", false);
                        result.put("error", "Session does not exist.");
                    } else {
                        ConnectionThread networkThread =
                                new ConnectionThread(sock, true, uid, reader, writer);
                        mConnectionThreads.put(uid, networkThread);
                        networkThread.start();
                        notifyOnConnect();
                        result.put("uid", uid);
                        result.put("status", true);
                        result.put("error", null);
                    }
                } else {
                    result.put("uid", uid);
                    result.put("status", false);
                    result.put("error", "Unrecognized command.");
                }
                writer.write(result + "\n");
                writer.flush();
                Log.v("Sent: " + result);
            } else {
                ConnectionThread networkThread =
                        new ConnectionThread(sock, false, 0, reader, writer);
                mConnectionThreads.put(0, networkThread);
                networkThread.start();
                notifyOnConnect();
            }
        }
    }

    public void shutdown() throws Exception {
        // Stop listening on the server socket to ensure that
        // beyond this point there are no incoming requests.
        mStopServer = true;
        try {
            mServer.close();
        } catch (IOException e) {
            Log.e("Failed to close server socket.", e);
        }
        // Since the server is not running, the mNetworkThreads set can only
        // shrink from this point onward. We can just stop all of the running helper
        // threads. In the worst case, one of the running threads will already have
        // shut down. Since this is a CopyOnWriteList, we don't have to worry about
        // concurrency issues while iterating over the set of threads.
        for (ConnectionThread connectionThread : mConnectionThreads.values()) {
            connectionThread.close();
        }
        for (SimpleServerObserver observer : mObservers) {
            removeObserver(observer);
        }
    }
}