• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.internal.util.test;
18 
19 import android.content.Context;
20 import android.os.Handler;
21 import android.os.Looper;
22 import android.os.Message;
23 import android.os.Messenger;
24 import android.util.Log;
25 
26 import com.android.internal.util.AsyncChannel;
27 
28 import java.util.HashMap;
29 import java.util.Map;
30 import java.util.Set;
31 
32 /**
33  * Provides an interface for the server side implementation of a bidirectional channel as described
34  * in {@link com.android.internal.util.AsyncChannel}.
35  */
36 public class BidirectionalAsyncChannelServer {
37 
38     private static final String TAG = "BidirectionalAsyncChannelServer";
39 
40     // Keeps track of incoming clients, which are identifiable by their messengers.
41     private final Map<Messenger, AsyncChannel> mClients = new HashMap<>();
42 
43     private Messenger mMessenger;
44 
BidirectionalAsyncChannelServer(final Context context, final Looper looper, final Handler messageHandler)45     public BidirectionalAsyncChannelServer(final Context context, final Looper looper,
46             final Handler messageHandler) {
47         Handler handler = new Handler(looper) {
48             @Override
49             public void handleMessage(Message msg) {
50                 AsyncChannel channel = mClients.get(msg.replyTo);
51                 switch (msg.what) {
52                     case AsyncChannel.CMD_CHANNEL_FULL_CONNECTION:
53                         if (channel != null) {
54                             Log.d(TAG, "duplicate client connection: " + msg.sendingUid);
55                             channel.replyToMessage(msg,
56                                     AsyncChannel.CMD_CHANNEL_FULLY_CONNECTED,
57                                     AsyncChannel.STATUS_FULL_CONNECTION_REFUSED_ALREADY_CONNECTED);
58                         } else {
59                             channel = new AsyncChannel();
60                             mClients.put(msg.replyTo, channel);
61                             channel.connected(context, this, msg.replyTo);
62                             channel.replyToMessage(msg, AsyncChannel.CMD_CHANNEL_FULLY_CONNECTED,
63                                     AsyncChannel.STATUS_SUCCESSFUL);
64                         }
65                         break;
66                     case AsyncChannel.CMD_CHANNEL_DISCONNECT:
67                         channel.disconnect();
68                         break;
69 
70                     case AsyncChannel.CMD_CHANNEL_DISCONNECTED:
71                         mClients.remove(msg.replyTo);
72                         break;
73 
74                     default:
75                         messageHandler.handleMessage(msg);
76                         break;
77                 }
78             }
79         };
80         mMessenger = new Messenger(handler);
81     }
82 
getMessenger()83     public Messenger getMessenger() {
84         return mMessenger;
85     }
86 
getClientMessengers()87     public Set<Messenger> getClientMessengers() {
88         return mClients.keySet();
89     }
90 
91 }
92