Home | History | Annotate | Download | only in sl4a
      1 /*
      2  * Copyright (C) 2016 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 package com.android.tradefed.util.sl4a;
     17 
     18 import com.android.tradefed.log.LogUtil.CLog;
     19 
     20 import java.io.BufferedReader;
     21 import java.io.IOException;
     22 import java.io.InputStreamReader;
     23 import java.io.PrintWriter;
     24 import java.net.ServerSocket;
     25 import java.net.Socket;
     26 
     27 /**
     28  * Helper class to simulate the device sl4a layer that respond to RPC queries.
     29  */
     30 public class FakeSocketServerHelper extends Thread {
     31     private ServerSocket mSocket;
     32     private boolean mCanceled = false;
     33 
     34     public FakeSocketServerHelper() throws IOException {
     35         setName(getClass().getCanonicalName());
     36         mSocket = new ServerSocket(0);
     37     }
     38 
     39     public int getPort() {
     40         return mSocket.getLocalPort();
     41     }
     42 
     43     public void close() throws IOException {
     44         mCanceled = true;
     45         if (mSocket != null) {
     46             mSocket.close();
     47         }
     48     }
     49 
     50     @Override
     51     public void run() {
     52         Socket client = null;
     53         BufferedReader in = null;
     54         PrintWriter out = null;
     55         try {
     56             client = mSocket.accept();
     57             in = new BufferedReader(
     58                     new InputStreamReader(client.getInputStream()));
     59             out = new PrintWriter(client.getOutputStream(), true);
     60         } catch (IOException e1) {
     61             return;
     62         }
     63         while (!mCanceled) {
     64             try {
     65                 String response = in.readLine();
     66                 if (response != null) {
     67                     if (response.contains("initiate")) {
     68                         out.print("{\"uid\":1,\"status\":true}");
     69                         out.print('\n');
     70                         out.flush();
     71                     } else if (response.contains("getBoolean")) {
     72                         out.print("{\"id\":2,\"result\":true,\"error\":null}");
     73                         out.print('\n');
     74                         out.flush();
     75                     }
     76                 }
     77             } catch (IOException e) {
     78                 CLog.e(e);
     79                 mCanceled = true;
     80             }
     81         }
     82     }
     83 }
     84