• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 android.aconfigd;
18 
19 import android.net.LocalSocket;
20 import android.net.LocalSocketAddress;
21 import android.util.Slog;
22 
23 import java.io.DataInputStream;
24 import java.io.DataOutputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 
28 /** @hide */
29 public class AconfigdClientSocketImpl implements AconfigdClientSocket {
30 
31     private static String TAG = "AconfigdClientSocketImpl";
32 
33     private LocalSocket mLocalSocket;
34     private LocalSocketAddress mLocalSocketAddress;
35 
AconfigdClientSocketImpl(LocalSocketAddress address)36     public AconfigdClientSocketImpl(LocalSocketAddress address) {
37         mLocalSocket = new LocalSocket();
38         mLocalSocketAddress = address;
39     }
40 
AconfigdClientSocketImpl()41     AconfigdClientSocketImpl() {
42         this(new LocalSocketAddress(
43                      "aconfigd_system",
44                      LocalSocketAddress.Namespace.RESERVED));
45     }
46 
47     /**
48      * Connect the socket
49      *
50      * @hide
51      */
connect()52     public void connect() throws IOException {
53         if (mLocalSocket.isConnected()) return;
54         mLocalSocket.connect(mLocalSocketAddress);
55     }
56 
57     /**
58      * send request
59      *
60      * @param requests stream of requests
61      * @hide
62      */
63     @Override
send(byte[] requests)64     public InputStream send(byte[] requests) {
65         if (!mLocalSocket.isConnected()) {
66             try {
67                 connect();
68             } catch (IOException e) {
69                 Slog.e(TAG, "fail to connect to the server socket", e);
70                 return null;
71             }
72         }
73 
74         DataInputStream inputStream = null;
75         DataOutputStream outputStream = null;
76         try {
77             inputStream = new DataInputStream(mLocalSocket.getInputStream());
78             outputStream = new DataOutputStream(mLocalSocket.getOutputStream());
79         } catch (IOException ioe) {
80             Slog.e(TAG, "failed to get local socket iostreams", ioe);
81             return null;
82         }
83 
84         // send requests
85         try {
86             outputStream.writeInt(requests.length);
87             outputStream.write(requests);
88             Slog.i(TAG, "flag requests sent to aconfigd");
89         } catch (IOException ioe) {
90             Slog.e(TAG, "failed to send requests to aconfigd", ioe);
91             return null;
92         }
93 
94         // read return
95         try {
96             int num_bytes = inputStream.readInt();
97             Slog.i(TAG, "received " + num_bytes + " bytes back from aconfigd");
98             return mLocalSocket.getInputStream();
99         } catch (IOException ioe) {
100             Slog.e(TAG, "failed to read requests return from aconfigd", ioe);
101             return null;
102         }
103     }
104 
105     /**
106      * Close the local socket
107      *
108      * @hide
109      */
110     @Override
close()111     public void close() {
112         try {
113             mLocalSocket.shutdownInput();
114             mLocalSocket.shutdownOutput();
115             mLocalSocket.close();
116         } catch (IOException e) {
117             Slog.e(TAG, "failed to close socket", e);
118         }
119     }
120 
121 }
122