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 17 package com.android.printservice.recommendation.util; 18 19 import android.annotation.NonNull; 20 import android.net.nsd.NsdManager; 21 import android.net.nsd.NsdServiceInfo; 22 import com.android.internal.annotations.GuardedBy; 23 24 import java.util.LinkedList; 25 26 /** 27 * Nsd resolve requests for the same info cancel each other. Hence this class synchronizes the 28 * resolutions to hide this effect. 29 */ 30 public class NsdResolveQueue { 31 /** Lock for {@link #sInstance} */ 32 private static final Object sLock = new Object(); 33 34 /** Instance of this singleton */ 35 @GuardedBy("sLock") 36 private static NsdResolveQueue sInstance; 37 38 /** Lock for {@link #mResolveRequests} */ 39 private final Object mLock = new Object(); 40 41 /** Current set of registered service info resolve attempts */ 42 @GuardedBy("mLock") 43 private final LinkedList<NsdResolveRequest> mResolveRequests = new LinkedList<>(); 44 45 public static NsdResolveQueue getInstance() { 46 synchronized (sLock) { 47 if (sInstance == null) { 48 sInstance = new NsdResolveQueue(); 49 } 50 51 return sInstance; 52 } 53 } 54 55 /** 56 * Container for a request to resolve a serviceInfo. 57 */ 58 private static class NsdResolveRequest { 59 final @NonNull NsdManager nsdManager; 60 final @NonNull NsdServiceInfo serviceInfo; 61 final @NonNull NsdManager.ResolveListener listener; 62 63 private NsdResolveRequest(@NonNull NsdManager nsdManager, 64 @NonNull NsdServiceInfo serviceInfo, @NonNull NsdManager.ResolveListener listener) { 65 this.nsdManager = nsdManager; 66 this.serviceInfo = serviceInfo; 67 this.listener = listener; 68 } 69 } 70 71 /** 72 * Resolve a serviceInfo or queue the request if there is a request currently in flight. 73 * 74 * @param nsdManager The nsd manager to use 75 * @param serviceInfo The service info to resolve 76 * @param listener The listener to call back once the info is resolved. 77 */ 78 public void resolve(@NonNull NsdManager nsdManager, @NonNull NsdServiceInfo serviceInfo, 79 @NonNull NsdManager.ResolveListener listener) { 80 synchronized (mLock) { 81 mResolveRequests.addLast(new NsdResolveRequest(nsdManager, serviceInfo, 82 new ListenerWrapper(listener))); 83 84 if (mResolveRequests.size() == 1) { 85 resolveNextRequest(); 86 } 87 } 88 } 89 90 /** 91 * Wrapper for a {@link NsdManager.ResolveListener}. Calls the listener and then 92 * {@link #resolveNextRequest()}. 93 */ 94 private class ListenerWrapper implements NsdManager.ResolveListener { 95 private final @NonNull NsdManager.ResolveListener mListener; 96 97 private ListenerWrapper(@NonNull NsdManager.ResolveListener listener) { 98 mListener = listener; 99 } 100 101 @Override 102 public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) { 103 mListener.onResolveFailed(serviceInfo, errorCode); 104 105 synchronized (mLock) { 106 mResolveRequests.pop(); 107 resolveNextRequest(); 108 } 109 } 110 111 @Override 112 public void onServiceResolved(NsdServiceInfo serviceInfo) { 113 mListener.onServiceResolved(serviceInfo); 114 115 synchronized (mLock) { 116 mResolveRequests.pop(); 117 resolveNextRequest(); 118 } 119 } 120 } 121 122 /** 123 * Resolve the next request if there is one. 124 */ 125 private void resolveNextRequest() { 126 if (!mResolveRequests.isEmpty()) { 127 NsdResolveRequest request = mResolveRequests.getFirst(); 128 129 request.nsdManager.resolveService(request.serviceInfo, request.listener); 130 } 131 } 132 133 } 134