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