1e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project/*
2e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project * Copyright (C) 2007 Esmertec AG.
3e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project * Copyright (C) 2007 The Android Open Source Project
4e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project *
5e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
6e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project * you may not use this file except in compliance with the License.
7e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project * You may obtain a copy of the License at
8e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project *
9e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
10e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project *
11e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
12e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
13e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project * See the License for the specific language governing permissions and
15e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project * limitations under the License.
16e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project */
17e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
18e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Projectpackage com.android.im.imps;
19e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
20e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Projectimport com.android.im.engine.ImException;
21e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
22e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project/**
23e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project * The IMPS transport binding is divided into two channels: a mandatory data
24e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project * channel and a conditional CIR channel. All the exchange of CSP primitives is
25e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project * done in the data channel.
26e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project */
27e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Projectabstract class DataChannel {
28e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    protected ImpsConnection mConnection;
29e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    protected PrimitiveParser mParser;
30e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    protected PrimitiveSerializer mSerializer;
31e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    protected long mMinPollMillis;
32e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
33e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    protected DataChannel(ImpsConnection connection) throws ImException {
34e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        mConnection = connection;
35e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    }
36e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
37e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    /**
38e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     * Establishes a data channel with the IMPS server.
39e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     *
40e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     * @throws ImException if an error occur during establishing the data
41e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     *             channel.
42e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     */
43e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    public abstract void connect() throws ImException;
44e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
45e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    /**
46e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     * Suspend the data channel. No data will be sent through the data channel
47e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     * after suspended. It can be recovered from {@link #resume()}.
48e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     */
49e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    public abstract void suspend();
50e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
51e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    /**
52e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     * Resume the suspended data channel.
53e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     *
54e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     * @return <code>true</code> if the channel is resumed successfully;
55e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     *         <code>false</code> if the channel is timeout and a new one must
56e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     *         be established.
57e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     */
58e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    public abstract boolean resume();
59e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
60e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    /**
61e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     * Shutdown the data channel.
62e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     */
63e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    public abstract void shutdown();
64e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
65e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    /**
66e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     * Sends a CSP primitive to the IMPS server through this data channel.
67e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     *
68e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     * @param p the primitive to send.
69e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     */
70e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    public abstract void sendPrimitive(Primitive p);
71e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
72e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    /**
73e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     * Receives a primitive from this data channel, waiting until a primitive
74e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     * from the server arrived or being interrupted.
75e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     *
76e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     * @return the received primitive
77e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     * @throws InterruptedException
78e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     */
79e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    public abstract Primitive receivePrimitive() throws InterruptedException;
80e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
81e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    /**
82e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     * Gets the time when the last primitive was sent to the server through the
83e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     * data channel.
84e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     *
85e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     * @return the time last primitive was sent.
86e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     */
87e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    public abstract long getLastActiveTime();
88e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
89e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    /**
90e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     * Tells if there is any primitive waiting to send.
91e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     *
92e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     * @return <code>true</code> if there is one or more primitives waiting to send.
93e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     */
94e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    public abstract boolean isSendingQueueEmpty();
95e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
96e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    /**
97e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     * Starts the keep alive task. KeepAliveRequest will be sent to the server
98e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     * if no other transaction has occurred during the KeepAliveTime interval.
99e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     */
100e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    public abstract void startKeepAlive(long interval);
101e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
102e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    /**
103e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     * Set the ServerMinPoll value (in seconds) after capability negotiation.
104e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     * The DataChannel <b>MUST NOT</b> send more than 1 PollingRequest within
105e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     * this interval.
106e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     */
107e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    public void setServerMinPoll(long interval)
108e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    {
109e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        mMinPollMillis = interval * 1000;
110e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    }
111e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project}
112