OSDN Git Service

Fix service discovery bugs
authorIrfan Sheriff <isheriff@google.com>
Wed, 18 Apr 2012 22:46:00 +0000 (15:46 -0700)
committerIrfan Sheriff <isheriff@google.com>
Thu, 19 Apr 2012 00:40:49 +0000 (17:40 -0700)
- Application does not have transaction id information. go through
 list and remove

- Avoid removing client info while enumerating it

Change-Id: Ie293876756418ed4cd6dc3a903689ee7003bd12d
Signed-off-by: yoshihiko.ikenaga@jp.sony.com
wifi/java/android/net/wifi/p2p/WifiP2pService.java

index 314e33e..6168f0e 100644 (file)
@@ -1500,8 +1500,7 @@ public class WifiP2pService extends IWifiP2pManager.Stub {
             int key;
             WifiP2pServiceRequest req;
             for (int i=0; i < c.mReqList.size(); i++) {
-                key = c.mReqList.keyAt(i);
-                req = c.mReqList.get(key);
+                req = c.mReqList.valueAt(i);
                 if (req != null) {
                     sb.append(req.getSupplicantQuery());
                 }
@@ -1539,7 +1538,10 @@ public class WifiP2pService extends IWifiP2pManager.Stub {
             return false;
         }
 
-        req.setTransactionId(++mServiceTransactionId);
+        ++mServiceTransactionId;
+        //The Wi-Fi p2p spec says transaction id should be non-zero
+        if (mServiceTransactionId == 0) ++mServiceTransactionId;
+        req.setTransactionId(mServiceTransactionId);
         clientInfo.mReqList.put(mServiceTransactionId, req);
 
         if (mServiceDiscReqId == null) {
@@ -1550,13 +1552,23 @@ public class WifiP2pService extends IWifiP2pManager.Stub {
     }
 
     private void removeServiceRequest(Messenger m, WifiP2pServiceRequest req) {
-
         ClientInfo clientInfo = getClientInfo(m, false);
         if (clientInfo == null) {
             return;
         }
 
-        clientInfo.mReqList.remove(req.getTransactionId());
+        //Application does not have transaction id information
+        //go through stored requests to remove
+        boolean removed = false;
+        for (int i=0; i < clientInfo.mReqList.size(); i++) {
+            if (req.equals(clientInfo.mReqList.valueAt(i))) {
+                removed = true;
+                clientInfo.mReqList.removeAt(i);
+                break;
+            }
+        }
+
+        if (!removed) return;
 
         if (clientInfo.mReqList.size() == 0 && clientInfo.mServList.size() == 0) {
             if (DBG) logd("remove client information from framework");
@@ -1670,6 +1682,7 @@ public class WifiP2pService extends IWifiP2pManager.Stub {
                 } catch (RemoteException e) {
                     if (DBG) logd("detect dead channel");
                     clearClientInfo(c.mMessenger);
+                    return;
                 }
             }
         }
@@ -1683,6 +1696,8 @@ public class WifiP2pService extends IWifiP2pManager.Stub {
      * TODO: This can be done better with full async channels.
      */
     private void clearClientDeadChannels() {
+        ArrayList<Messenger> deadClients = new ArrayList<Messenger>();
+
         for (ClientInfo c : mClientInfoList.values()) {
             Message msg = Message.obtain();
             msg.what = WifiP2pManager.PING;
@@ -1693,9 +1708,13 @@ public class WifiP2pService extends IWifiP2pManager.Stub {
                 c.mMessenger.send(msg);
             } catch (RemoteException e) {
                 if (DBG) logd("detect dead channel");
-                clearClientInfo(c.mMessenger);
+                deadClients.add(c.mMessenger);
             }
         }
+
+        for (Messenger m : deadClients) {
+            clearClientInfo(m);
+        }
     }
 
     /**