diff options
author | Andrzej J. R. Hunt <andrzej@ahunt.org> | 2012-07-12 17:22:12 +0100 |
---|---|---|
committer | Michael Meeks <michael.meeks@suse.com> | 2012-08-06 10:22:55 +0100 |
commit | dbe8d4822724ccdbbd51a94605c270579e88da0a (patch) | |
tree | 6c0c3170d2e353a9da9d67156948670a87c68124 /android | |
parent | 9d7d8eb7a8a7a26e85f5fa8fde9580947bf005ab (diff) |
Changed from JSON to plaintext for Server->Client. Namespace usage cleanup.
Change-Id: I7e1b229b475e476c71ec3a110696942299b25733
Diffstat (limited to 'android')
-rw-r--r-- | android/sdremote/src/org/libreoffice/impressremote/communication/Client.java | 80 | ||||
-rw-r--r-- | android/sdremote/src/org/libreoffice/impressremote/communication/Receiver.java | 77 |
2 files changed, 52 insertions, 105 deletions
diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/Client.java b/android/sdremote/src/org/libreoffice/impressremote/communication/Client.java index 049950a970bf..47321e58b41e 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/communication/Client.java +++ b/android/sdremote/src/org/libreoffice/impressremote/communication/Client.java @@ -1,9 +1,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 org.apache.http.util.ByteArrayBuffer; import org.json.JSONException; @@ -44,69 +47,28 @@ public abstract class Client { } private void listen() { - while (true) { - ByteArrayBuffer aBuffer = new ByteArrayBuffer(0); - int aTemp; - System.out.println("Now listening"); - try { - while ((aTemp = mInputStream.read()) != 0x0a) { - if (aTemp == -1) { - System.out.println("EOF Reached!!!"); - } - System.out.println("Char: " + aTemp); - aBuffer.append((byte) aTemp); - } - } catch (IOException e1) { - // TODO stream couldn't be opened. - e1.printStackTrace(); - } - System.out.println("Escaped the loop!"); - String aLengthString; - try { - aLengthString = new String(aBuffer.toByteArray(), CHARSET); - } catch (Exception e1) { - e1.printStackTrace(); - throw new Error("Specified network encoding [" + CHARSET - + " not available."); - } - - int aLength = Integer.parseInt(aLengthString); - System.out.println("Lenth = " + aLength); - byte[] aCommand = new byte[aLength]; - try { - int readIn = 0; - while (readIn < aLength) { - readIn += mInputStream.read(aCommand, 0, aLength - readIn); -// System.out.println("Read in :" + readIn + " of : " -// + aLength); - } - } catch (IOException e) { - // TODO close and notify that the connection has closed - e.printStackTrace(); - } - String aCommandString; - try { - aCommandString = new String(aCommand, CHARSET); - } catch (UnsupportedEncodingException e) { - throw new Error("Specified network encoding [" + CHARSET - + " not available."); - } - mReceiver.parseCommand(aCommandString); - } - } - - private void parseCommand(String aCommand) { - JSONObject aCommandObject; - String aInstruction; + BufferedReader aReader; try { - aCommandObject = new JSONObject(aCommand); - aInstruction = aCommandObject.getString("command"); - if (aInstruction.equals("slide_changed")) { - // TODO: process and notify + System.out.println("deb:Listening"); + 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) { + System.out.println("deb__:" + aTemp); + aList.add(aTemp); + } + System.out.println("deb:parsing"); + mReceiver.parseCommand(aList); } - } catch (JSONException e) { + } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); + } catch (IOException e1) { + // TODO stream couldn't be opened. + e1.printStackTrace(); } } diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/Receiver.java b/android/sdremote/src/org/libreoffice/impressremote/communication/Receiver.java index a46cd4fc3045..40c491b0badb 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/communication/Receiver.java +++ b/android/sdremote/src/org/libreoffice/impressremote/communication/Receiver.java @@ -8,6 +8,8 @@ */ package org.libreoffice.impressremote.communication; +import java.util.ArrayList; + import org.json.JSONException; import org.json.JSONObject; @@ -25,57 +27,40 @@ public class Receiver { mActivityMessenger = aActivityMessenger; } - public void parseCommand(String aJSONCommandString) { + public void parseCommand(ArrayList<String> aCommand) { if (mActivityMessenger == null) { return; } - try { -// int aPrinted = 0; -// while (aPrinted < aJSONCommandString.length()) { -// if (aPrinted + 100 < aJSONCommandString.length()) -// System.out.println(aJSONCommandString.substring(aPrinted, -// aPrinted + 100)); -// else -// System.out.println(aJSONCommandString.substring(aPrinted)); -// aPrinted += 100; -// } - - JSONObject aJSONCommand = new JSONObject(aJSONCommandString); - String aInstruction = aJSONCommand.getString("command"); - if (aInstruction.equals("slide_updated")) { - int aSlideNumber = aJSONCommand.getInt("slide_number"); - Message aMessage = Message.obtain(null, - CommunicationService.MSG_SLIDE_CHANGED); - Bundle aData = new Bundle(); - aData.putInt("slide_number", aSlideNumber); - aMessage.setData(aData); - try { - mActivityMessenger.send(aMessage); - } catch (RemoteException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } else if (aInstruction.equals("slide_preview")) { - int aSlideNumber = aJSONCommand.getInt("slide_number"); - String aImageString = aJSONCommand.getString("image_preview"); - byte[] aImage = Base64.decode(aImageString, Base64.DEFAULT); - Message aMessage = Message.obtain(null, - CommunicationService.MSG_SLIDE_PREVIEW); - Bundle aData = new Bundle(); - aData.putInt("slide_number", aSlideNumber); - aData.putByteArray("preview_image", aImage); - aMessage.setData(aData); - try { - mActivityMessenger.send(aMessage); - } catch (RemoteException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } + String aInstruction = aCommand.get(0); + if (aInstruction.equals("slide_updated")) { + int aSlideNumber = Integer.parseInt(aCommand.get(1)); + Message aMessage = Message.obtain(null, + CommunicationService.MSG_SLIDE_CHANGED); + Bundle aData = new Bundle(); + aData.putInt("slide_number", aSlideNumber); + aMessage.setData(aData); + try { + mActivityMessenger.send(aMessage); + } catch (RemoteException e) { + // Dead Handler -- i.e. Activity gone. + } + } else if (aInstruction.equals("slide_preview")) { + int aSlideNumber = Integer.parseInt(aCommand.get(1)); + String aImageString = aCommand.get(2); + byte[] aImage = Base64.decode(aImageString, Base64.DEFAULT); + Message aMessage = Message.obtain(null, + CommunicationService.MSG_SLIDE_PREVIEW); + Bundle aData = new Bundle(); + aData.putInt("slide_number", aSlideNumber); + aData.putByteArray("preview_image", aImage); + aMessage.setData(aData); + try { + mActivityMessenger.send(aMessage); + } catch (RemoteException e) { + // TODO Auto-generated catch block + e.printStackTrace(); } - } catch (JSONException e) { - // TODO Auto-generated catch block - e.printStackTrace(); } } |