• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 
17 package com.googlecode.android_scripting.jsonrpc;
18 
19 import com.googlecode.android_scripting.Log;
20 import com.googlecode.android_scripting.SimpleServer;
21 
22 import java.io.BufferedReader;
23 import java.io.InputStreamReader;
24 import java.io.PrintWriter;
25 import java.net.Socket;
26 
27 /**
28  * A JSON RPC server that forwards RPC calls to a specified receiver object.
29  *
30  */
31 public class JsonRpcServer extends SimpleServer {
32     private final RpcReceiverManagerFactory mRpcReceiverManagerFactory;
33 
34     // private final String mHandshake;
35 
36     /**
37      * Construct a {@link JsonRpcServer} connected to the provided {@link RpcReceiverManager}.
38      *
39      * @param managerFactory the {@link RpcReceiverManager} to register with the server
40      * @param handshake the secret handshake required for authorization to use this server
41      */
JsonRpcServer(RpcReceiverManagerFactory managerFactory, String handshake)42     public JsonRpcServer(RpcReceiverManagerFactory managerFactory, String handshake) {
43         // mHandshake = handshake;
44         mRpcReceiverManagerFactory = managerFactory;
45     }
46 
47     @Override
shutdown()48     public void shutdown() {
49         super.shutdown();
50         // Notify all RPC receiving objects. They may have to clean up some of their state.
51         for (RpcReceiverManager manager : mRpcReceiverManagerFactory.getRpcReceiverManagers()
52                 .values()) {
53             manager.shutdown();
54         }
55     }
56 
send(PrintWriter writer, Object result)57     private void send(PrintWriter writer, Object result) {
58         writer.write(result + "\n");
59         writer.flush();
60         Log.v("Sent Response: " + result);
61     }
62 
63     @Override
handleConnection(Socket socket)64     protected void handleConnection(Socket socket) throws Exception {
65         BufferedReader reader =
66                 new BufferedReader(new InputStreamReader(socket.getInputStream()), 8192);
67         PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);
68         String data;
69         JsonRpcHandler jsonRpcHandler = new JsonRpcHandler(mRpcReceiverManagerFactory);
70         while ((data = reader.readLine()) != null) {
71             Log.v("Received Request: " + data);
72             send(writer, jsonRpcHandler.getResponse(data));
73         }
74     }
75 }
76