am cb1254f1: am 20caa09c: am 21384faa: am 694f219c: Merge "NsdService does not clean up after exiting clients"
* commit 'cb1254f1abc44b82e2a9da0d9a73230eb46e129e': NsdService does not clean up after exiting clients
This commit is contained in:
@@ -142,25 +142,40 @@ public class NsdService extends INsdManager.Stub {
|
|||||||
class DefaultState extends State {
|
class DefaultState extends State {
|
||||||
@Override
|
@Override
|
||||||
public boolean processMessage(Message msg) {
|
public boolean processMessage(Message msg) {
|
||||||
|
ClientInfo cInfo = null;
|
||||||
switch (msg.what) {
|
switch (msg.what) {
|
||||||
case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED:
|
case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED:
|
||||||
if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) {
|
if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) {
|
||||||
AsyncChannel c = (AsyncChannel) msg.obj;
|
AsyncChannel c = (AsyncChannel) msg.obj;
|
||||||
if (DBG) Slog.d(TAG, "New client listening to asynchronous messages");
|
if (DBG) Slog.d(TAG, "New client listening to asynchronous messages");
|
||||||
c.sendMessage(AsyncChannel.CMD_CHANNEL_FULLY_CONNECTED);
|
c.sendMessage(AsyncChannel.CMD_CHANNEL_FULLY_CONNECTED);
|
||||||
ClientInfo cInfo = new ClientInfo(c, msg.replyTo);
|
cInfo = new ClientInfo(c, msg.replyTo);
|
||||||
mClients.put(msg.replyTo, cInfo);
|
mClients.put(msg.replyTo, cInfo);
|
||||||
} else {
|
} else {
|
||||||
Slog.e(TAG, "Client connection failure, error=" + msg.arg1);
|
Slog.e(TAG, "Client connection failure, error=" + msg.arg1);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case AsyncChannel.CMD_CHANNEL_DISCONNECTED:
|
case AsyncChannel.CMD_CHANNEL_DISCONNECTED:
|
||||||
if (msg.arg1 == AsyncChannel.STATUS_SEND_UNSUCCESSFUL) {
|
switch (msg.arg1) {
|
||||||
Slog.e(TAG, "Send failed, client connection lost");
|
case AsyncChannel.STATUS_SEND_UNSUCCESSFUL:
|
||||||
} else {
|
Slog.e(TAG, "Send failed, client connection lost");
|
||||||
if (DBG) Slog.d(TAG, "Client connection lost with reason: " + msg.arg1);
|
break;
|
||||||
|
case AsyncChannel.STATUS_REMOTE_DISCONNECTION:
|
||||||
|
if (DBG) Slog.d(TAG, "Client disconnected");
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
if (DBG) Slog.d(TAG, "Client connection lost with reason: " + msg.arg1);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
cInfo = mClients.get(msg.replyTo);
|
||||||
|
if (cInfo != null) {
|
||||||
|
cInfo.expungeAllRequests();
|
||||||
|
mClients.remove(msg.replyTo);
|
||||||
|
}
|
||||||
|
//Last client
|
||||||
|
if (mClients.size() == 0) {
|
||||||
|
stopMDnsDaemon();
|
||||||
}
|
}
|
||||||
mClients.remove(msg.replyTo);
|
|
||||||
break;
|
break;
|
||||||
case AsyncChannel.CMD_CHANNEL_FULL_CONNECTION:
|
case AsyncChannel.CMD_CHANNEL_FULL_CONNECTION:
|
||||||
AsyncChannel ac = new AsyncChannel();
|
AsyncChannel ac = new AsyncChannel();
|
||||||
@@ -238,13 +253,15 @@ public class NsdService extends INsdManager.Stub {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void storeRequestMap(int clientId, int globalId, ClientInfo clientInfo) {
|
private void storeRequestMap(int clientId, int globalId, ClientInfo clientInfo, int what) {
|
||||||
clientInfo.mClientIds.put(clientId, globalId);
|
clientInfo.mClientIds.put(clientId, globalId);
|
||||||
|
clientInfo.mClientRequests.put(clientId, what);
|
||||||
mIdToClientInfoMap.put(globalId, clientInfo);
|
mIdToClientInfoMap.put(globalId, clientInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void removeRequestMap(int clientId, int globalId, ClientInfo clientInfo) {
|
private void removeRequestMap(int clientId, int globalId, ClientInfo clientInfo) {
|
||||||
clientInfo.mClientIds.remove(clientId);
|
clientInfo.mClientIds.remove(clientId);
|
||||||
|
clientInfo.mClientRequests.remove(clientId);
|
||||||
mIdToClientInfoMap.remove(globalId);
|
mIdToClientInfoMap.remove(globalId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -264,10 +281,6 @@ public class NsdService extends INsdManager.Stub {
|
|||||||
result = NOT_HANDLED;
|
result = NOT_HANDLED;
|
||||||
break;
|
break;
|
||||||
case AsyncChannel.CMD_CHANNEL_DISCONNECTED:
|
case AsyncChannel.CMD_CHANNEL_DISCONNECTED:
|
||||||
//Last client
|
|
||||||
if (mClients.size() == 1) {
|
|
||||||
stopMDnsDaemon();
|
|
||||||
}
|
|
||||||
result = NOT_HANDLED;
|
result = NOT_HANDLED;
|
||||||
break;
|
break;
|
||||||
case NsdManager.DISABLE:
|
case NsdManager.DISABLE:
|
||||||
@@ -291,7 +304,7 @@ public class NsdService extends INsdManager.Stub {
|
|||||||
Slog.d(TAG, "Discover " + msg.arg2 + " " + id +
|
Slog.d(TAG, "Discover " + msg.arg2 + " " + id +
|
||||||
servInfo.getServiceType());
|
servInfo.getServiceType());
|
||||||
}
|
}
|
||||||
storeRequestMap(msg.arg2, id, clientInfo);
|
storeRequestMap(msg.arg2, id, clientInfo, msg.what);
|
||||||
replyToMessage(msg, NsdManager.DISCOVER_SERVICES_STARTED, servInfo);
|
replyToMessage(msg, NsdManager.DISCOVER_SERVICES_STARTED, servInfo);
|
||||||
} else {
|
} else {
|
||||||
stopServiceDiscovery(id);
|
stopServiceDiscovery(id);
|
||||||
@@ -330,7 +343,7 @@ public class NsdService extends INsdManager.Stub {
|
|||||||
id = getUniqueId();
|
id = getUniqueId();
|
||||||
if (registerService(id, (NsdServiceInfo) msg.obj)) {
|
if (registerService(id, (NsdServiceInfo) msg.obj)) {
|
||||||
if (DBG) Slog.d(TAG, "Register " + msg.arg2 + " " + id);
|
if (DBG) Slog.d(TAG, "Register " + msg.arg2 + " " + id);
|
||||||
storeRequestMap(msg.arg2, id, clientInfo);
|
storeRequestMap(msg.arg2, id, clientInfo, msg.what);
|
||||||
// Return success after mDns reports success
|
// Return success after mDns reports success
|
||||||
} else {
|
} else {
|
||||||
unregisterService(id);
|
unregisterService(id);
|
||||||
@@ -371,7 +384,7 @@ public class NsdService extends INsdManager.Stub {
|
|||||||
id = getUniqueId();
|
id = getUniqueId();
|
||||||
if (resolveService(id, servInfo)) {
|
if (resolveService(id, servInfo)) {
|
||||||
clientInfo.mResolvedService = new NsdServiceInfo();
|
clientInfo.mResolvedService = new NsdServiceInfo();
|
||||||
storeRequestMap(msg.arg2, id, clientInfo);
|
storeRequestMap(msg.arg2, id, clientInfo, msg.what);
|
||||||
} else {
|
} else {
|
||||||
replyToMessage(msg, NsdManager.RESOLVE_SERVICE_FAILED,
|
replyToMessage(msg, NsdManager.RESOLVE_SERVICE_FAILED,
|
||||||
NsdManager.FAILURE_INTERNAL_ERROR);
|
NsdManager.FAILURE_INTERNAL_ERROR);
|
||||||
@@ -477,7 +490,7 @@ public class NsdService extends INsdManager.Stub {
|
|||||||
|
|
||||||
int id2 = getUniqueId();
|
int id2 = getUniqueId();
|
||||||
if (getAddrInfo(id2, cooked[3])) {
|
if (getAddrInfo(id2, cooked[3])) {
|
||||||
storeRequestMap(clientId, id2, clientInfo);
|
storeRequestMap(clientId, id2, clientInfo, NsdManager.RESOLVE_SERVICE);
|
||||||
} else {
|
} else {
|
||||||
clientInfo.mChannel.sendMessage(NsdManager.RESOLVE_SERVICE_FAILED,
|
clientInfo.mChannel.sendMessage(NsdManager.RESOLVE_SERVICE_FAILED,
|
||||||
NsdManager.FAILURE_INTERNAL_ERROR, clientId);
|
NsdManager.FAILURE_INTERNAL_ERROR, clientId);
|
||||||
@@ -821,6 +834,9 @@ public class NsdService extends INsdManager.Stub {
|
|||||||
/* A map from client id to unique id sent to mDns */
|
/* A map from client id to unique id sent to mDns */
|
||||||
private SparseArray<Integer> mClientIds = new SparseArray<Integer>();
|
private SparseArray<Integer> mClientIds = new SparseArray<Integer>();
|
||||||
|
|
||||||
|
/* A map from client id to the type of the request we had received */
|
||||||
|
private SparseArray<Integer> mClientRequests = new SparseArray<Integer>();
|
||||||
|
|
||||||
private ClientInfo(AsyncChannel c, Messenger m) {
|
private ClientInfo(AsyncChannel c, Messenger m) {
|
||||||
mChannel = c;
|
mChannel = c;
|
||||||
mMessenger = m;
|
mMessenger = m;
|
||||||
@@ -834,10 +850,41 @@ public class NsdService extends INsdManager.Stub {
|
|||||||
sb.append("mMessenger ").append(mMessenger).append("\n");
|
sb.append("mMessenger ").append(mMessenger).append("\n");
|
||||||
sb.append("mResolvedService ").append(mResolvedService).append("\n");
|
sb.append("mResolvedService ").append(mResolvedService).append("\n");
|
||||||
for(int i = 0; i< mClientIds.size(); i++) {
|
for(int i = 0; i< mClientIds.size(); i++) {
|
||||||
sb.append("clientId ").append(mClientIds.keyAt(i));
|
int clientID = mClientIds.keyAt(i);
|
||||||
sb.append(" mDnsId ").append(mClientIds.valueAt(i)).append("\n");
|
sb.append("clientId ").append(clientID).
|
||||||
|
append(" mDnsId ").append(mClientIds.valueAt(i)).
|
||||||
|
append(" type ").append(mClientRequests.get(clientID)).append("\n");
|
||||||
}
|
}
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Remove any pending requests from the global map when we get rid of a client,
|
||||||
|
// and send cancellations to the daemon.
|
||||||
|
private void expungeAllRequests() {
|
||||||
|
int globalId, clientId, i;
|
||||||
|
for (i = 0; i < mClientIds.size(); i++) {
|
||||||
|
clientId = mClientIds.keyAt(i);
|
||||||
|
globalId = mClientIds.valueAt(i);
|
||||||
|
mIdToClientInfoMap.remove(globalId);
|
||||||
|
if (DBG) Slog.d(TAG, "Terminating client-ID " + clientId +
|
||||||
|
" global-ID " + globalId + " type " + mClientRequests.get(clientId));
|
||||||
|
switch (mClientRequests.get(clientId)) {
|
||||||
|
case NsdManager.DISCOVER_SERVICES:
|
||||||
|
stopServiceDiscovery(globalId);
|
||||||
|
break;
|
||||||
|
case NsdManager.RESOLVE_SERVICE:
|
||||||
|
stopResolveService(globalId);
|
||||||
|
break;
|
||||||
|
case NsdManager.REGISTER_SERVICE:
|
||||||
|
unregisterService(globalId);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
mClientIds.clear();
|
||||||
|
mClientRequests.clear();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user