[muscle] Re: Another Java MUSCLE client patch

  • From: Lior Okman <lior.okman@xxxxxxxxxxxxxxxxxx>
  • To: Jeremy Friesner <jaf@xxxxxxxxxxxx>
  • Date: Fri, 12 Jan 2007 01:00:57 +0200

I'm looking at the zip file you sent me a link to, and

1. The HISTORY.txt file is wrong - only the flatten() method is faster,
since unflatten() didn't call flattenedSize() to begin with.
2. You should probably also note that one of the changes (the one where
Enumerations turned to Iterators) cause the new Message object to lose
backwards compatibility - any code calling Message.fieldNames() needs to
be updated. Unfortunately, return values can't be overloaded.
3. Another cleanup patch to the Message class, to use the
fieldItemSize() method, instead of hardcoding the field sizes in
flatten() and unflatten() for MessageFields.
4. Removed an extra call to flattenedSize() in JZLibMessageIOGateway.
5. Also, a patch to the java's build.xml file, to update the version
number that is written in the manifest.mf file when the build.xml file
is used to generate a muscle.jar file.

Regards,
Lior

Jeremy Friesner wrote:
> Hi Lior,
>
> On Thursday 11 January 2007 12:49, Lior Okman wrote:
>   
>> I hope I'm not too late to have this patch included in 3.25...
>>     
>
> Nope, it's not too late... although actually enough has changed that it's now 
> going to be called the 3.30 release instead of the 3.25 release.  I'll 
> probably wait until things have settled down a bit before releasing it, but 
> in the meantime, here is an unofficial snapshot of the current muscle SVN 
> repository (including the patches from your latest email):
>
>    http://www.lcscanada.com/muscle/muscle3.30b.zip
>
>   
>> 1. The flattenedSize() method is only called once for each message being
>> flattened.
>>     
>
> Very cool :^)
>
>   
>> 2. The various IOGateways are turned public again. The reasoning is that
>> you should use the factory if you don't know what you need, but if you
>> do know what you need, you should be able to directly create it.
>> 3. A few minor cleanups to the previous StringBuffer() patch - no need
>> for this type of code, since boolean's toString() is "true" or "false".
>>     boolean[] array;
>>     ...
>>     ret.append((array[i]) ? "true " : "false ");
>>     
>
> Okay, those make sense.
>
>   
>> 4. The Message.whatString() was fixed to not return a \0 character in
>> case the int being passed was equal to zero. This would cut short data
>> being displayed with the toString() methods.
>>     
>
> I took this a bit further, and now it checks each byte individually... that 
> way it will also work on cases where some of the bytes are zero but not all 
> of them (e.g. w==1):
>
>    public static String whatString(int w)
>    {
>       byte [] temp = new byte[4];
>       temp[0] = (byte)((w >> 24) & 0xFF); if (temp[0] == 0) temp[0] = '0';
>       temp[1] = (byte)((w >> 16) & 0xFF); if (temp[1] == 0) temp[1] = '0';
>       temp[2] = (byte)((w >> 8)  & 0xFF); if (temp[2] == 0) temp[2] = '0';
>       temp[3] = (byte)((w >> 0)  & 0xFF); if (temp[3] == 0) temp[3] = '0';
>       return new String(temp);
>    }
>
> -Jeremy
>
>   

--- Message.java        2007-01-12 00:12:58.000000000 +0200
+++ src/com/lcs/muscle/message/Message.java     2007-01-12 00:43:18.000000000 
+0200
@@ -1493,6 +1496,7 @@
       
       public void flatten(ByteBuffer out) throws IOException
       {
+        int flattenedItemSize = flattenedItemSize();
          switch(typeCode())
          {
             case B_BOOL_TYPE:  
@@ -1513,7 +1517,7 @@
             {
                short [] array = (short[]) _payload;
                out.asShortBuffer().put(array, 0, _numItems);
-               out.position(out.position()+array.length*2);
+               out.position(out.position()+array.length*flattenedItemSize);
             }
             break;
 
@@ -1521,7 +1525,7 @@
             { 
                float [] array = (float[]) _payload;
                out.asFloatBuffer().put(array, 0, _numItems);
-               out.position(out.position()+array.length*4);
+               out.position(out.position()+array.length*flattenedItemSize);
             }
             break;
 
@@ -1529,7 +1533,7 @@
             { 
                int [] array = (int[]) _payload;
                out.asIntBuffer().put(array, 0, _numItems);
-               out.position(out.position()+array.length*4);
+               out.position(out.position()+array.length*flattenedItemSize);
             }
             break;
 
@@ -1537,7 +1541,7 @@
             { 
                long [] array = (long[]) _payload;
                out.asLongBuffer().put(array, 0, _numItems);
-               out.position(out.position()+array.length*8);
+               out.position(out.position()+array.length*flattenedItemSize);
             }
             break;
 
@@ -1545,7 +1549,7 @@
             { 
                double [] array = (double[]) _payload;
                out.asDoubleBuffer().put(array, 0, _numItems);
-               out.position(out.position()+array.length*8);
+               out.position(out.position()+array.length*flattenedItemSize);
             }
             break;
 
@@ -1640,7 +1645,7 @@
              { 
                 short [] array = new short[_numItems];
                 in.asShortBuffer().get(array);
-                in.position(in.position()+array.length*2);
+                in.position(in.position()+array.length*flattenedItemSize);
                 _payload = array;
              }
              break;
@@ -1649,7 +1654,7 @@
              { 
                 float [] array = new float[_numItems];
                 in.asFloatBuffer().get(array);
-                in.position(in.position()+array.length*4);
+                in.position(in.position()+array.length*flattenedItemSize);
                 _payload = array;
              }
              break;
@@ -1658,7 +1663,7 @@
              { 
                 int [] array = new int[_numItems];
                 in.asIntBuffer().get(array);
-                in.position(in.position()+array.length*4);
+                in.position(in.position()+array.length*flattenedItemSize);
                 _payload = array;
              }
              break;
@@ -1667,7 +1672,7 @@
              { 
                 long [] array = new long[_numItems];
                 in.asLongBuffer().get(array);
-                in.position(in.position()+array.length*8);
+                in.position(in.position()+array.length*flattenedItemSize);
                 _payload = array;
              }
              break;
@@ -1676,7 +1681,7 @@
              {
                 double [] array = new double[_numItems];
                 in.asDoubleBuffer().get(array);
-                in.position(in.position()+array.length*8);
+                in.position(in.position()+array.length*flattenedItemSize);
                 _payload = array;
              }
              break;

--- ../../muscle3.30b-orig/java/build.xml       2006-03-03 20:12:52.000000000 
+0200
+++ build.xml   2007-01-12 00:33:20.000000000 +0200
@@ -21,7 +21,7 @@
                    <attribute name="Built-By" value="${user.name}"/>
                    <section name="common">
                      <attribute name="Specification-Title" value="Muscle"/>
-                     <attribute name="Specification-Version" value="3.12"/>
+                     <attribute name="Specification-Version" value="3.30"/>
                      <attribute name="Specification-Vendor" value="Level 
Control Systems"/>
                    </section>
          </manifest>
@@ -36,7 +36,7 @@
                    <attribute name="Built-By" value="${user.name}"/>
                    <section name="common">
                      <attribute name="Specification-Title" value="Muscle"/>
-                     <attribute name="Specification-Version" value="3.12"/>
+                     <attribute name="Specification-Version" value="3.30"/>
                      <attribute name="Specification-Vendor" value="Level 
Control Systems"/>
                    </section>
          </manifest>
--- 
../../muscle3.30b-orig/java/com/lcs/muscle/iogateway/JZLibMessageIOGateway.java 
    2007-01-12 00:12:58.000000000 +0200
+++ com/lcs/muscle/iogateway/JZLibMessageIOGateway.java 2007-01-12 
00:55:17.000000000 +0200
@@ -203,7 +203,7 @@
       int flatSize = msg.flattenedSize();
       if ((_outgoingEncoding <= MUSCLE_MESSAGE_DEFAULT_ENCODING)||(flatSize <= 
32)) return super.flattenMessage(msg);
 
-      prepareBuffers(msg);
+      prepareBuffers(msg, flatSize);
       ByteBuffer buffer = ByteBuffer.allocate(_outgoing.limit());
       buffer.put(_outgoing);
       buffer.flip();
@@ -227,7 +227,7 @@
          super.flattenMessage(out, msg);
          return;
       }
-      prepareBuffers(msg);
+      prepareBuffers(msg, flatSize);
       if (out instanceof SelectableChannel) 
       {
          SelectableChannel sc = (SelectableChannel) out;
@@ -254,10 +254,8 @@
       } else out.write(_outgoing);
    }
 
-   private void prepareBuffers(Message msg) throws IOException 
+   private void prepareBuffers(Message msg, int flatSize) throws IOException 
    {
-      int flatSize = msg.flattenedSize();
-
       // Else, we need to compress the message.
       boolean independent = false;
       if (_deflateStream == null) 

Other related posts: