From a8d7244ed7e71df075160db9c3072a7ed559a7c9 Mon Sep 17 00:00:00 2001 From: Purple Fox Date: Tue, 12 Jul 2011 09:38:19 +0100 Subject: [PATCH] fixed stomp client examples --- .../org/nodex/examples/stomp/StompPerf.java | 2 +- src/examples/ruby/stomp/stomp_client.rb | 3 +- src/examples/ruby/stomp/stomp_perf.rb | 2 +- .../nodex/core/parsetools/RecordParser.java | 38 +++++++++++++------ src/main/ruby/parsetools.rb | 8 ++-- 5 files changed, 34 insertions(+), 19 deletions(-) diff --git a/src/examples/java/org/nodex/examples/stomp/StompPerf.java b/src/examples/java/org/nodex/examples/stomp/StompPerf.java index 705a1f066..54c5aa12d 100644 --- a/src/examples/java/org/nodex/examples/stomp/StompPerf.java +++ b/src/examples/java/org/nodex/examples/stomp/StompPerf.java @@ -19,7 +19,7 @@ public class StompPerf { public static void main(String[] args) throws Exception { - StompClient.connect(8080, new StompConnectHandler() { + StompClient.connect(8181, new StompConnectHandler() { public void onConnect(final StompConnection conn) { final int warmup = 500000; final int numMessages = 1000000; diff --git a/src/examples/ruby/stomp/stomp_client.rb b/src/examples/ruby/stomp/stomp_client.rb index 7f1cde6b5..e151a0e22 100644 --- a/src/examples/ruby/stomp/stomp_client.rb +++ b/src/examples/ruby/stomp/stomp_client.rb @@ -1,12 +1,13 @@ require "stomp" require "buffer" +include Stomp topic = "test-topic" # A very simple example using STOMP to send and consume some messages # Use this with the java STOMP server in src/examples/java/stomp -Stomp::Client.connect(8080) do |conn| +StompClient.connect(8181) do |conn| conn.subscribe(topic) { |headers, body| puts "Got message #{body}" } (1..10).each { |i| conn.send(topic, Buffer.from_str("hello #{i}\n")) } end diff --git a/src/examples/ruby/stomp/stomp_perf.rb b/src/examples/ruby/stomp/stomp_perf.rb index 3de88f6a0..3c73fa87c 100644 --- a/src/examples/ruby/stomp/stomp_perf.rb +++ b/src/examples/ruby/stomp/stomp_perf.rb @@ -7,7 +7,7 @@ num_messages = 1000000; count = 0 start = nil -Client.connect(8080, "localhost") { |conn| +Client.connect(8181, "localhost") { |conn| conn.subscribe("test-topic") { |msg| count += 1 if count == warmup + num_messages diff --git a/src/main/java/org/nodex/core/parsetools/RecordParser.java b/src/main/java/org/nodex/core/parsetools/RecordParser.java index 90a212c0a..8f813fbe7 100644 --- a/src/main/java/org/nodex/core/parsetools/RecordParser.java +++ b/src/main/java/org/nodex/core/parsetools/RecordParser.java @@ -9,6 +9,13 @@ import java.io.UnsupportedEncodingException; * User: tim * Date: 27/06/11 * Time: 17:49 + * + * A RecordParser takes as input a fragmented sequence of Buffers and outputs a record, which is also a Buffer + * Records can be delimited by a sequence of bytes, or can be fixed size. + * The delimiters can be changed or the parser can be switched between fixed size mode or delimited mode + * any time after creation. This enables the parser to be used for spitting out records for protocols which may + * involve a combination of delimited and fixed size records or where the delimiter changes. + * The parser is not character encoding aware, and works with sequences of bytes not characters */ public class RecordParser extends DataHandler { @@ -27,8 +34,23 @@ public class RecordParser extends DataHandler { this.output = output; } - public static RecordParser newDelimited(final String delim, final String enc, final DataHandler output) { - return newDelimited(delimToByteArray(delim, enc), output); + /** + * Helper method to convert a latin-1 String to an array of bytes for use as a delimiter + * Please do not use this for non latin-1 characters + * @param str + * @return The byte[] form of the string + */ + public static byte[] latin1StringToBytes(String str) { + byte[] bytes = new byte[str.length()]; + for (int i = 0; i < str.length(); i++) { + char c = str.charAt(i); + bytes[i] = (byte)(c & 0xFF); + } + return null; + } + + public static RecordParser newDelimited(String delim, DataHandler output) { + return newDelimited(latin1StringToBytes(delim), output); } public static RecordParser newDelimited(byte[] delim, DataHandler output) { @@ -43,16 +65,8 @@ public class RecordParser extends DataHandler { return ls; } - private static byte[] delimToByteArray(String delim, String enc) { - try { - return delim.getBytes(enc); - } catch (UnsupportedEncodingException e) { - throw new RuntimeException("Unsupported encoding " + enc); - } - } - - public void delimitedMode(final String delim, final String enc) { - delimitedMode(delimToByteArray(delim, enc)); + public void delimitedMode(String delim) { + delimitedMode(latin1StringToBytes(delim)); } public void delimitedMode(byte[] delim) { diff --git a/src/main/ruby/parsetools.rb b/src/main/ruby/parsetools.rb index bfea12241..988ac4768 100644 --- a/src/main/ruby/parsetools.rb +++ b/src/main/ruby/parsetools.rb @@ -28,9 +28,9 @@ module ParserTools @java_parser.onData(data._to_java_buffer) end - def RecordParser.new_delimited(delim, enc = "UTF-8", proc = nil, &output_block) + def RecordParser.new_delimited(delim, proc = nil, &output_block) output_block = proc if proc - RecordParser.new(org.nodex.core.parsetools.RecordParser.newDelimited(delim, enc, OutputCallback.new(output_block))) + RecordParser.new(org.nodex.core.parsetools.RecordParser.newDelimited(delim, OutputCallback.new(output_block))) end def RecordParser.new_bytes_delimited(bytes, proc = nil, &output_block) @@ -43,8 +43,8 @@ module ParserTools RecordParser.new(org.nodex.core.parsetools.RecordParser.newFixed(size, OutputCallback.new(output_block))) end - def delimited_mode(delim, enc) - @java_parser.delimitedMode(delim, enc) + def delimited_mode(delim) + @java_parser.delimitedMode(delim) end def bytes_delimited_mode(bytes)