diff options
author | Andrzej J.R. Hunt <andrzej@ahunt.org> | 2012-08-13 15:13:50 +0200 |
---|---|---|
committer | Andrzej J.R. Hunt <andrzej@ahunt.org> | 2012-08-13 21:41:43 +0200 |
commit | cf3de756eac964ce073872996956daa0e0840ff8 (patch) | |
tree | ccfb40cdb95f738047eab5f88e880937a751b9b6 /android | |
parent | 61f92ee500c7757e65a3f1944af973c4914c717e (diff) |
Pairing implemented client side.
Change-Id: I678e038470824affa2fe4d792e9a5defbd77807a
Diffstat (limited to 'android')
3 files changed, 135 insertions, 100 deletions
diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/Client.java b/android/sdremote/src/org/libreoffice/impressremote/communication/Client.java index bb76df9aef6b..2864b9db90b6 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/communication/Client.java +++ b/android/sdremote/src/org/libreoffice/impressremote/communication/Client.java @@ -11,11 +11,12 @@ package org.libreoffice.impressremote.communication; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; -import java.io.InputStreamReader; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.util.ArrayList; +import android.content.Context; + /** * Generic Client for the remote control. To implement a Client for a specific * transport medium you must provide input and output streams ( @@ -26,70 +27,74 @@ import java.util.ArrayList; */ public abstract class Client { - private static final String CHARSET = "UTF-8"; - - protected InputStream mInputStream; - protected OutputStream mOutputStream; - - public abstract void closeConnection(); - - private Receiver mReceiver; - - public void setReceiver(Receiver aReceiver) { - mReceiver = aReceiver; - } - - protected void startListening() { - - Thread t = new Thread() { - public void run() { - listen(); - }; - - }; - t.start(); - } - - private void listen() { - BufferedReader aReader; - try { - aReader = new BufferedReader(new InputStreamReader(mInputStream, - CHARSET)); - while (true) { - ArrayList<String> aList = new ArrayList<String>(); - String aTemp; - // read until empty line - while ((aTemp = aReader.readLine()).length() != 0) { - aList.add(aTemp); - } - mReceiver.parseCommand(aList); - } - } catch (UnsupportedEncodingException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IOException e1) { - // TODO stream couldn't be opened. - e1.printStackTrace(); - } - - } - - /** - * Send a valid JSON string to the server. - * - * @param command - * Must be a valid JSON string. - */ - 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) { - // TODO Notify that stream has closed. - } - } + protected static final String CHARSET = "UTF-8"; + + protected InputStream mInputStream; + protected BufferedReader mReader; + protected OutputStream mOutputStream; + + public abstract void closeConnection(); + + private Receiver mReceiver; + + protected Context mContext; + + public Client(Context aContext) { + mContext = aContext; + } + + public void setReceiver(Receiver aReceiver) { + mReceiver = aReceiver; + } + + protected void startListening() { + + Thread t = new Thread() { + public void run() { + listen(); + }; + + }; + t.start(); + } + + private void listen() { + try { + while (true) { + ArrayList<String> aList = new ArrayList<String>(); + String aTemp; + // read until empty line + while ((aTemp = mReader.readLine()).length() != 0) { + aList.add(aTemp); + } + mReceiver.parseCommand(aList); + } + } catch (UnsupportedEncodingException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e1) { + // TODO stream couldn't be opened. + e1.printStackTrace(); + } + + } + + /** + * Send a valid JSON string to the server. + * + * @param command + * Must be a valid JSON string. + */ + 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) { + // TODO Notify that stream has closed. + } + } } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
\ No newline at end of file diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java b/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java index c8ecace40b03..56036841a867 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java +++ b/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java @@ -50,7 +50,8 @@ public class CommunicationService extends Service implements Runnable { if (mStateDesired == State.CONNECTED) { switch (mServerDesired.getProtocol()) { case NETWORK: - mClient = new NetworkClient(mServerDesired.getAddress()); + mClient = new NetworkClient( + mServerDesired.getAddress(), this); mTransmitter = new Transmitter(mClient); mClient.setReceiver(mReceiver); break; diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/NetworkClient.java b/android/sdremote/src/org/libreoffice/impressremote/communication/NetworkClient.java index 9c1f163fab42..f76e14e3c1e7 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/communication/NetworkClient.java +++ b/android/sdremote/src/org/libreoffice/impressremote/communication/NetworkClient.java @@ -8,11 +8,17 @@ */ package org.libreoffice.impressremote.communication; +import java.io.BufferedReader; import java.io.IOException; +import java.io.InputStreamReader; import java.net.Socket; import java.net.UnknownHostException; +import java.util.Random; +import android.content.Context; +import android.content.Intent; import android.os.StrictMode; +import android.support.v4.content.LocalBroadcastManager; /** * Standard Network client. Connects to a server using Sockets. @@ -21,40 +27,63 @@ import android.os.StrictMode; */ public class NetworkClient extends Client { - private static final int PORT = 1599; - - private Socket mSocket; - - public NetworkClient(String ipAddress) { - // FIXME: eventually networking will be fully threaded. - StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() - .permitAll().build(); - StrictMode.setThreadPolicy(policy); - try { - mSocket = new Socket(ipAddress, PORT); - mInputStream = mSocket.getInputStream(); - mOutputStream = mSocket.getOutputStream(); - startListening(); - } catch (UnknownHostException e) { - // TODO Tell the user we have a problem - e.printStackTrace(); - } catch (IOException e) { - // TODO As above - e.printStackTrace(); - } - - } - - @Override - public void closeConnection() { - try { - if (mSocket != null) - mSocket.close(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } + private static final int PORT = 1599; + + private Socket mSocket; + + public NetworkClient(String ipAddress, Context aContext) { + super(aContext); + // FIXME: eventually networking will be fully threaded. + StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() + .permitAll().build(); + StrictMode.setThreadPolicy(policy); + try { + mSocket = new Socket(ipAddress, PORT); + mInputStream = mSocket.getInputStream(); + mReader = new BufferedReader(new InputStreamReader(mInputStream, + CHARSET)); + mOutputStream = mSocket.getOutputStream(); + // Pairing. + Random aRandom = new Random(); + String aPin = "" + aRandom.nextInt(10000); + Intent aIntent = new Intent( + CommunicationService.MSG_PAIRING_STARTED); + aIntent.putExtra("PIN", aPin); + LocalBroadcastManager.getInstance(mContext).sendBroadcast(aIntent); + // Wait until we get the appropriate string back... + String aTemp = mReader.readLine(); + if (!aTemp.equals("LO_SERVER_SERVER_PAIRED")) { + return; + } else { + aIntent = new Intent( + CommunicationService.MSG_PAIRING_SUCCESSFUL); + LocalBroadcastManager.getInstance(mContext).sendBroadcast( + aIntent); + } + while ((aTemp = mReader.readLine()).length() != 0) { + // Get rid of extra lines + } + startListening(); + } catch (UnknownHostException e) { + // TODO Tell the user we have a problem + e.printStackTrace(); + } catch (IOException e) { + // TODO As above + e.printStackTrace(); + } + + } + + @Override + public void closeConnection() { + try { + if (mSocket != null) + mSocket.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
\ No newline at end of file |