1 /* 2 * Copyright (C) 2019 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.net; 18 19 import android.annotation.NonNull; 20 import android.content.Context; 21 import android.util.Log; 22 23 import com.android.internal.annotations.VisibleForTesting; 24 25 import java.util.concurrent.CompletableFuture; 26 import java.util.concurrent.ExecutionException; 27 import java.util.concurrent.atomic.AtomicReference; 28 import java.util.function.Consumer; 29 30 /** 31 * Manager class used to communicate with the ip memory store service in the network stack, 32 * which is running in a separate module. 33 * @hide 34 */ 35 public class IpMemoryStore extends IpMemoryStoreClient { 36 private static final String TAG = IpMemoryStore.class.getSimpleName(); 37 @NonNull private final CompletableFuture<IIpMemoryStore> mService; 38 @NonNull private final AtomicReference<CompletableFuture<IIpMemoryStore>> mTailNode; 39 IpMemoryStore(@onNull final Context context)40 public IpMemoryStore(@NonNull final Context context) { 41 super(context); 42 mService = new CompletableFuture<>(); 43 mTailNode = new AtomicReference<CompletableFuture<IIpMemoryStore>>(mService); 44 getNetworkStackClient().fetchIpMemoryStore( 45 new IIpMemoryStoreCallbacks.Stub() { 46 @Override 47 public void onIpMemoryStoreFetched(@NonNull final IIpMemoryStore memoryStore) { 48 mService.complete(memoryStore); 49 } 50 51 @Override 52 public int getInterfaceVersion() { 53 return this.VERSION; 54 } 55 }); 56 } 57 58 /* 59 * If the IpMemoryStore is ready, this function will run the request synchronously. 60 * Otherwise, it will enqueue the requests for execution immediately after the 61 * service becomes ready. The requests are guaranteed to be executed in the order 62 * they are sumbitted. 63 */ 64 @Override runWhenServiceReady(Consumer<IIpMemoryStore> cb)65 protected void runWhenServiceReady(Consumer<IIpMemoryStore> cb) throws ExecutionException { 66 mTailNode.getAndUpdate(future -> future.handle((store, exception) -> { 67 if (exception != null) { 68 // this should never happens since we also catch the exception below 69 Log.wtf(TAG, "Error fetching IpMemoryStore", exception); 70 return store; 71 } 72 73 try { 74 cb.accept(store); 75 } catch (Exception e) { 76 Log.wtf(TAG, "Exception occured: " + e.getMessage()); 77 } 78 return store; 79 })); 80 } 81 82 @VisibleForTesting getNetworkStackClient()83 protected NetworkStackClient getNetworkStackClient() { 84 return NetworkStackClient.getInstance(); 85 } 86 87 /** Gets an instance of the memory store */ 88 @NonNull getMemoryStore(final Context context)89 public static IpMemoryStore getMemoryStore(final Context context) { 90 return new IpMemoryStore(context); 91 } 92 } 93