summaryrefslogtreecommitdiff
path: root/android/sdremote/src/org/libreoffice/impressremote/communication/Client.java
blob: ea6d4b9f43836be80c0722ffb2668556b5d4fcf3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package org.libreoffice.impressremote.communication;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import org.libreoffice.impressremote.Globals;
import org.libreoffice.impressremote.communication.CommunicationService.State;

import android.content.Intent;
import android.util.Log;

/**
 * Generic Client for the remote control. To implement a Client for a specific
 * transport medium you must provide input and output streams (
 * <code>mInputStream</code> and <code>mOutputStream</code> before calling any
 * methods.
 */
public abstract class Client {

    protected static final String CHARSET = "UTF-8";

    protected InputStream mInputStream;
    protected BufferedReader mReader;
    protected OutputStream mOutputStream;
    protected String mPin = "";
    protected String mName = "";

    private static Client latestInstance = null;

    public abstract void closeConnection();

    public abstract void validating() throws IOException;
    private Receiver mReceiver;

    protected Server mServer;

    protected CommunicationService mCommunicationService;

    protected Client(Server aServer,
                    CommunicationService aCommunicationService,
                    Receiver aReceiver) {
        mServer = aServer;
        mName = aServer.getName();
        mCommunicationService = aCommunicationService;
        mReceiver = aReceiver;
        latestInstance = this;
    }

    protected void startListening() {

        Thread t = new Thread() {
            public void run() {
                listen();
            }

        };
        t.start();
    }

    private final void listen() {
        try {
            while (true) {
                ArrayList<String> aList = new ArrayList<String>();
                String aTemp;
                // read until empty line
                while ((aTemp = mReader.readLine()) != null
                                && aTemp.length() != 0) {
                    aList.add(aTemp);
                }
                if (aTemp == null) {
                    Intent aIntent = new Intent(
                                    mCommunicationService
                                                    .getApplicationContext(),
                                    ReconnectionActivity.class);
                    aIntent.putExtra("server", mServer);
                    aIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    mCommunicationService.getApplicationContext()
                                    .startActivity(aIntent);
                    return;
                }
                mReceiver.parseCommand(aList);
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e1) {
            // TODO stream couldn't be opened.
            e1.printStackTrace();
        } finally {
            onDisconnect();
        }

    }

    public static String getPin() {
        if (latestInstance != null) {
            return latestInstance.mPin;
        } else {
            return "";
        }
    }

    public static String getName() {
        if (latestInstance != null) {
            return latestInstance.mName;
        } else {
            return "";
        }
    }

    /**
     * Send a valid command to the Server.
     */
    public void sendCommand(String command) {
        try {
            mOutputStream.write(command.getBytes(CHARSET));
        } catch (UnsupportedEncodingException e) {
            throw new Error("Specified network encoding [" + CHARSET
                            + " not available.");
        } catch (IOException e) {
            // I.e. connection closed. This will be dealt with by the listening
            // loop.
        }
    }

    /**
     * Called after the Client disconnects. Can be extended to allow for
     * cleaning up bluetooth properties etc.
     */
    protected void onDisconnect() {
    }

}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */