Skip to content

Commit

Permalink
added comments and specs for the ffi-rzmq related things.
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrik Sundberg committed Oct 7, 2011
1 parent 2f05794 commit 3a13ade
Show file tree
Hide file tree
Showing 10 changed files with 371 additions and 70 deletions.
6 changes: 6 additions & 0 deletions lib/my-ffi-rzmq/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,11 @@ def to_s
copy_out_string
end

# Convenience method to check if a message is of size 0.
#
def empty?
size == 0
end

end # class Message
end # module ZMQ
29 changes: 17 additions & 12 deletions lib/my-ffi-rzmq/multipart_message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ module ZMQ
# The methods to override are: #push(frame), #unwrap, #duplicate
#
class MultipartMessage

include Enumerable

# Create a multipart message.
#
# Can provide an array of message frames to initialize the message with.
Expand All @@ -27,14 +28,6 @@ def initialize(msg_frames = [])
@msg_frames = msg_frames
end

# Add a frame to the message (positioning it at the end of message).
#
def add(frame)
@msg_frames << frame
self
end
alias_method :<<, :add

# Remove the first frame of the message and return it.
#
def pop
Expand Down Expand Up @@ -99,28 +92,40 @@ def to_s
result
end

# Add a frame to the message (positioning it at the end of message).
#
def add(frame)
raise RuntimeError.new("#add on base class not implemented, need to implement in child classes!")
end

# Redirect << operator to #add method
#
def <<(frame)
add(frame)
end

# Add a frame ot the front of the message.
#
# Not implemented in the base class.
#
def push(frame)
raise ZMQ::MessageError.new("#push on base class not implemented, need to implement in child classes!")
raise RuntimeError.new("#push on base class not implemented, need to implement in child classes!")
end

# Unwrap the address of a message.
#
# Not implemented in the base class.
#
def unwrap
raise ZMQ::MessageError.new("#unwrap on base class not implemented, need to implement in child classes!")
raise RuntimeError.new("#unwrap on base class not implemented, need to implement in child classes!")
end

# Create a duplicate of the message.
#
# Not implemented in the base class.
#
def duplicate
raise ZMQ::MessageError.new("#duplicate on base class not implemented, need to implement in child classes!")
raise RuntimeError.new("#duplicate on base class not implemented, need to implement in child classes!")
end

end # class MultipartMessage
Expand Down
36 changes: 27 additions & 9 deletions lib/my-ffi-rzmq/raw_multipart_message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,26 @@ def initialize(msg_frames = [])
super
end

# Add a frame to the message (positioning it at the end of message).
#
# Any string automatically gets wrapped in a ZMQ::Message, ZMQ::Messages
# are added directly.
#
def add(frame)
msg_frame = convert_frame(frame)
@msg_frames << msg_frame
self
end

# Add a frame to the front of the message.
#
# Frame can be either a string or a ZMQ::Message. The method
# wraps any string given in a ZMQ::Message for the user.
#
def push(frame)
msg_frame = case frame
when String
ZMQ::Message.new(frame)
when ZMQ::Message
frame
else
raise ZMQ::MessageError.new("Tried to push a message from of unknown type: #{frame.class}")
end
msg_frame = convert_frame(frame)
@msg_frames.unshift(msg_frame)
self
end

# Remove the address from the front of a message.
Expand All @@ -43,7 +48,7 @@ def push(frame)
#
def unwrap
frame = pop
if @msg_frames.first.size == 0
if @msg_frames.first.empty?
empty = pop
empty.close
end
Expand All @@ -66,5 +71,18 @@ def close
@msg_frames.each {|frame| frame.close}
end

############################ PRIVATE METHODS ########################

def convert_frame(frame)
case frame
when String
ZMQ::Message.new(frame)
when ZMQ::Message
frame
else
raise ArgumentError.new("Tried to push a of unknown type: #{frame.class}")
end
end

end # class MultipartRawMessage
end # module ZMQ
37 changes: 28 additions & 9 deletions lib/my-ffi-rzmq/string_multipart_message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,29 @@ class StringMultipartMessage < ZMQ::MultipartMessage
def initialize(msg_frames = [])
super
end


# Add a frame to the message (positioning it at the end of message).
#
# Any ZMQ::Message automatically gets copied out to a ruby string
# (user still responsible for original ZMQ::Message), strings are
# added directly.
#
def add(frame)
msg_frame = convert_frame(frame)
@msg_frames << msg_frame
self
end

# Add a frame to the front of the message.
#
# Frame can be either a string or a ZMQ::Message. The method
# copies out any string given in a ZMQ::Message (and the user
# is still in charge of properly closing such a message).
#
def push(frame)
msg_frame = case frame
when String
frame
when ZMQ::Message
frame.copy_out_string
else
raise ZMQ::MessageError.new("Tried to push a message from of unknown type: #{frame.class}")
end
msg_frame = convert_frame(frame)
@msg_frames.unshift(msg_frame)
self
end

# Remove the address from the front of a message.
Expand All @@ -49,6 +55,19 @@ def duplicate
dup_frames = @msg_frames.map {|frame| frame.dup}
ZMQ::StringMultipartMessage.new(dup_frames)
end

############################ PRIVATE METHODS #######################

def convert_frame(frame)
case frame
when String
frame
when ZMQ::Message
frame.copy_out_string
else
raise ArgumentError.new("Tried to push a message from of unknown type: #{frame.class}")
end
end

end # class StringMultipartMessage
end # module ZMQ
23 changes: 19 additions & 4 deletions spec/my-ffi-rzmq/message_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
ZMQ::Util.stub(:strhex).and_return("ok")
msg.strhex.should == "ok"
ensure
msg.close
msg.close unless msg.nil?
end
end

Expand All @@ -20,8 +20,8 @@
msg2 = msg.duplicate
msg2.copy_out_string.should == msg.copy_out_string
ensure
msg.close
msg2.close
msg.close unless msg.nil?
msg2.close unless msg2.nil?
end
end

Expand All @@ -30,7 +30,22 @@
msg = ZMQ::Message.new("foobar")
msg.to_s.should match(/foobar/)
ensure
msg.close
msg.close unless msg.nil?
end
end

it "should know if it is empty or not" do
begin
msg = ZMQ::Message.new
msg.should be_empty
msg2 = ZMQ::Message.new("")
msg2.should be_empty
msg3 = ZMQ::Message.new("foo")
msg3.should_not be_empty
ensure
msg.close unless msg.nil?
msg2.close unless msg.nil?
msg3.close unless msg.nil?
end
end

Expand Down
102 changes: 100 additions & 2 deletions spec/my-ffi-rzmq/raw_multipart_message_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,108 @@
require 'spec_helper'
require 'shared_examples'
require 'shared_multipart_message_examples'
require 'my-ffi-rzmq/message'
require 'my-ffi-rzmq/raw_multipart_message'

describe ZMQ::RawMultipartMessage do

after(:each) do
subject.close
end

it_behaves_like "ZMQ::MultipartMessage"

it "should be possible to add string frames" do
subject.size.should == 0
subject.add "foo"
subject.size.should == 1
subject.last.class.should == ZMQ::Message
subject.last.to_s.should == "foo"
subject.add "bar"
subject.size.should == 2
subject.last.class.should == ZMQ::Message
subject.last.to_s.should == "bar"
end

it "should be possible to add ZMQ::Message frames" do
subject.size.should == 0
subject.add ZMQ::Message.new("foo")
subject.size.should == 1
subject.last.class.should == ZMQ::Message
subject.last.to_s.should == "foo"
subject.add ZMQ::Message.new("bar")
subject.size.should == 2
subject.last.class.should == ZMQ::Message
subject.last.to_s.should == "bar"
end

it "should be possible to push string frames" do
subject.size.should == 0
subject.push "foo"
subject.size.should == 1
subject.first.class.should == ZMQ::Message
subject.first.to_s.should == "foo"
subject.push "bar"
subject.size.should == 2
subject.first.class.should == ZMQ::Message
subject.first.to_s.should == "bar"
end

it "should be possible to push ZMQ::Message frames" do
subject.size.should == 0
subject.push ZMQ::Message.new("foo")
subject.size.should == 1
subject.first.class.should == ZMQ::Message
subject.first.to_s.should == "foo"
subject.push ZMQ::Message.new("bar")
subject.size.should == 2
subject.first.class.should == ZMQ::Message
subject.first.to_s.should == "bar"
end

it "should correctly unwrap messages when a blank frame exists" do
begin
empty_frame = ZMQ::Message.new("")
# make sure it's closed, will prob cause test to leak but no big deal
closed = false
empty_frame.should_receive(:close) do
closed = true
end
subject.push "foo"
subject.push empty_frame
subject.push "bar"
result = subject.unwrap
result.to_s.should == "bar"
subject.size.should == 1
subject.first.to_s.should == "foo"
closed.should be_true
ensure
result.close unless result.nil?
end
end

it "should correctly unwrap messages when a blank frame does not exists" do
begin
subject.push "foo"
subject.push "bar"
result = subject.unwrap
result.to_s.should == "bar"
subject.size.should == 1
subject.first.to_s.should == "foo"
ensure
result.close unless result.nil?
end
end

it "should close it's frames when closed" do
msg1 = ZMQ::Message.new("foo")
msg2 = ZMQ::Message.new("bar")
msg1_closed = false
msg2_closed =false
msg1.stub(:close) {msg1_closed = true}
msg2.stub(:close) {msg2_closed = true}
subject << msg1 << msg2
subject.close
msg1_closed.should be_true
msg2_closed.should be_true
end
end

Loading

0 comments on commit 3a13ade

Please sign in to comment.