fix for dirty part - receive fixed

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

fix for dirty part - receive fixed

andrewk
So I found a flaw in reliable mode on receive: if a connection fails, then comes back, then fails again - it returns a NIL instead of retrying the connection.  So I fixed this and cleaned up the work around from my last code send in.

Not a full more-ruby-like refactoring, but it is prettier, and fixes a bug -


Index: /Users/akuklewicz/dev/stomp-ruby/trunk/lib/stomp.rb
===================================================================
--- /Users/akuklewicz/dev/stomp-ruby/trunk/lib/stomp.rb (revision 45)
+++ /Users/akuklewicz/dev/stomp-ruby/trunk/lib/stomp.rb (working copy)
@@ -153,36 +153,29 @@
     end
       
     # Receive a frame, block until the frame is received
-    def __old_receive
-      # The recive my fail so we may need to retry.
+    def receive
+      # The receive may fail so we may need to retry.
       while TRUE
         begin
           s = socket
           return _receive(s)
         rescue
-          @failure = $!;
+          @failure = $!
           raise unless @reliable
-          $stderr.print "receive failed: " + $!;
+          $stderr.print "receive failed: #{$!.message}\n"
         end
       end
     end
     
-    def receive
-      super_result = __old_receive()
-      if super_result.nil? && @reliable
-        $stderr.print "connection.receive returning EOF as nil - resetting connection.\n"
-        @socket = nil
-        super_result = __old_receive()
-      end        
-      return super_result
-    end
-
     private
     def _receive( s )
       line = ' '
       @read_semaphore.synchronize do
         line = s.gets while line =~ /^\s*$/
-        return NIL if line == NIL
+        if line == NIL
+          raise "connection returned nil" if @reliable
+          return line
+        end
         Message.new do |m|
           m.command = line.chomp
           m.headers = {}
patch.diff