sonos

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 1c867082966d8df814c710a8618db9070809ba67
parent 624e99c2d5175af9b486a8d0c51b78f0306f6de4
Author: Brian Swetland <swetland@frotz.net>
Date:   Sun, 24 Jul 2011 12:50:15 -0700

debugging goodness - xml pretty-printer and more trace options

Signed-off-by: Brian Swetland <swetland@frotz.net>

Diffstat:
M.gitignore | 1+
Mnet/frotz/sonos/SoapRPC.java | 13++++++++++---
Mnet/frotz/sonos/Sonos.java | 14+++++++++++---
Mnet/frotz/sonos/XML.java | 118++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------
Mnet/frotz/sonos/app.java | 4+++-
5 files changed, 127 insertions(+), 23 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -1,3 +1,4 @@ *.class *.jar .gitignore +.* diff --git a/net/frotz/sonos/SoapRPC.java b/net/frotz/sonos/SoapRPC.java @@ -23,7 +23,9 @@ import java.io.OutputStream; import java.nio.ByteBuffer; class SoapRPC { - public boolean trace; + public boolean trace_io; + public boolean trace_reply; + InetAddress addr; int port; ByteBuffer reply; @@ -55,7 +57,7 @@ class SoapRPC { } reply.limit(off); s.close(); - if (trace) { + if (trace_io) { System.out.println("--------- reply -----------"); System.out.println(new String(buf, 0, off)); } @@ -93,7 +95,7 @@ class SoapRPC { sb.append("\"\r\n\r\n"); sb.append(msg); - if (trace) { + if (trace_io) { System.out.println("--------- message -----------"); System.out.println(sb); } @@ -104,6 +106,11 @@ class SoapRPC { xml.init(reply); try { + if (trace_reply) { + System.out.println("--------- reply -----------"); + xml.print(System.out,64); + xml.rewind(); + } xml.open("s:Envelope"); xml.open("s:Body"); return xml; diff --git a/net/frotz/sonos/Sonos.java b/net/frotz/sonos/Sonos.java @@ -17,6 +17,7 @@ package net.frotz.sonos; public class Sonos { + boolean trace_browse; SoapRPC.Endpoint xport; SoapRPC.Endpoint media; SoapRPC.Endpoint render; @@ -35,9 +36,11 @@ public class Sonos { "RenderingControl:1", "/MediaRenderer/RenderingControl/Control"); } - public void debug(boolean x) { - rpc.trace = x; - } + + public void trace_io(boolean x) { rpc.trace_io = x; } + public void trace_reply(boolean x) { rpc.trace_reply = x; } + public void trace_browse(boolean x) { trace_browse = x; } + public void volume() { rpc.call(render,"GetVolume", "<InstanceID>0</InstanceID>"+ @@ -116,6 +119,11 @@ public class Sonos { //System.out.println(tmp); result.init(tmp); + if (trace_browse) { + System.out.println("--------- list -----------"); + result.print(System.out,1024); + result.rewind(); + } System.err.println("Count = " + xml.read("NumberReturned")); System.err.println("Total = " + xml.read("TotalMatches")); System.err.println("UpdID = " + xml.read("UpdateID")); diff --git a/net/frotz/sonos/XML.java b/net/frotz/sonos/XML.java @@ -26,6 +26,8 @@ import java.nio.CharBuffer; import java.util.regex.Pattern; import java.util.regex.Matcher; +import java.io.PrintStream; + // TODO: &apos; -> ' public class XML { @@ -76,44 +78,54 @@ public class XML { nextTag(); System.err.println("XML reset, "+buf.length()+" bytes."); } + public void rewind() { + offset = 0; + nextTag(); + } + + /* modifies the sequence in-place, escaping basic entities */ public XML.Sequence unescape(XML.Sequence s) { + s.count = unescape(s, s.data, s.offset) - s.offset; + return s; + } + + /* copies the sequence into a char[] + offset, escaping basic entities */ + public int unescape(XML.Sequence s, char[] out, int off) { + char[] in = s.data; int n = s.offset; int max = n + s.count; - char data[] = s.data; - int out = n; while (n < max) { - char c = data[n++]; + char c = in[n++]; if (c != '&') { - data[out++] = c; + out[off++] = c; continue; } int e = n; while (n < max) { - if (data[n++] != ';') + if (in[n++] != ';') continue; - switch(data[e]) { + switch(in[e]) { case 'l': // lt - data[out++] = '<'; + out[off++] = '<'; break; case 'g': // gt - data[out++] = '>'; + out[off++] = '>'; break; case 'q': // quot - data[out++] = '"'; + out[off++] = '"'; break; case 'a': // amp | apos - if (data[e+1] == 'm') - data[out++] = '&'; - else if (data[e+1] == 'p') - data[out++] = '\''; + if (in[e+1] == 'm') + out[off++] = '&'; + else if (in[e+1] == 'p') + out[off++] = '\''; break; } break; } } - s.count = out - s.offset; - return s; + return off; } public XML.Sequence getAttr(String name) { @@ -139,6 +151,64 @@ public class XML { return null; } + /* set sequence to the text between the end of the current tag + * and the beginning of the next tag. + */ + public XML.Sequence getText() { + char[] data = xml; + int n; + tmp.data = data; + n = tmp.offset = mTag.end(); + try { + for (;;) { + if (data[n] == '<') + break; + n++; + } + tmp.count = n - tmp.offset; + } catch (ArrayIndexOutOfBoundsException x) { + tmp.count = 0; + } + return tmp; + } + + public void print(PrintStream out, int max) { + char[] buf = new char[max]; + print(out, max, 0, buf); + } + void print(PrintStream out, int max, int indent, char[] buf) { + XML.Sequence s; + int n; + if (!isOpen) { + out.println("ERROR"); + return; + } + for (n = 0; n < indent; n++) + out.print(" "); + out.print(str()); + s = getText(); + nextTag(); + if (isOpen) { + out.print("\n"); + do { + print(out, max, indent + 2, buf); + } while (isOpen); + for (n = 0; n < indent; n++) + out.print(" "); + out.println(str()); + } else { + if (s.count > max) { + s.count = max; + n = unescape(s, buf, 0); + out.println("" + new String(buf, 0, n) + "..." + str()); + } else { + n = unescape(s, buf, 0); + out.println("" + new String(buf, 0, n) + str()); + } + } + nextTag(); + } + public boolean more() { return isOpen; } @@ -285,7 +355,23 @@ public class XML { s.init(data, offset, offset + count); return s; } - + void trim() { + while (count > 0) { + char c = data[offset]; + if ((c==' ')||(c=='\r')||(c=='\n')||(c=='\t')) { + offset++; + count--; + continue; + } + c = data[offset + count - 1]; + if ((c==' ')||(c=='\r')||(c=='\n')||(c=='\t')) { + count--; + continue; + } + break; + } + } + /* CharSequence interface */ public int length() { return count; diff --git a/net/frotz/sonos/app.java b/net/frotz/sonos/app.java @@ -21,7 +21,9 @@ public class app { Sonos sonos = new Sonos(new byte[] { 10, 0, 0, (byte) 199}); String cmd = args[0]; -// sonos.debug(true); + //sonos.trace_io(true); + sonos.trace_reply(true); + sonos.trace_browse(true); if (cmd.equals("play")) { sonos.play();