Created
February 19, 2011 22:08
-
-
Save bhuga/835421 to your computer and use it in GitHub Desktop.
Demonstrates assertions, server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'ffi-rzmq' | |
#IPC_SOCKET = "ipc:///tmp/assert-demo.zmq" | |
IPC_SOCKET = 'tcp://localhost:5555' | |
@sender_context = ZMQ::Context.new(2) | |
send_socket = @sender_context.socket(ZMQ::REQ) | |
second_send_socket = nil | |
send_socket.connect(IPC_SOCKET) | |
send_socket.setsockopt(ZMQ::LINGER, 0) | |
first_results = use_second_socket = nil | |
loop do | |
socket = use_second_socket ? second_send_socket : send_socket | |
messages = ["query", "some", "stuff"] | |
while !messages.empty? | |
case messages.size | |
when 1 | |
socket.send_string messages.shift | |
else | |
socket.send_string messages.shift, ZMQ::SNDMORE | |
end | |
end | |
# sleep with IPC socket to cause server assertion failure | |
# run without sleep to cause local bad file descriptor failure | |
#sleep 0.01 | |
puts "sent a message" | |
if use_second_socket | |
puts "closing socket" | |
second_send_socket.close | |
end | |
second_send_socket = @sender_context.socket(ZMQ::REQ) | |
second_send_socket.connect(IPC_SOCKET) | |
second_send_socket.setsockopt(ZMQ::LINGER, 0) | |
use_second_socket = true | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'ffi-rzmq' | |
#IPC_SOCKET = "ipc:///tmp/assert-demo.zmq" | |
IPC_SOCKET = 'tcp://*:5555' | |
@context = ZMQ::Context.new(1) | |
@poller = ZMQ::Poller.new | |
socket = @context.socket(ZMQ::REP) | |
socket.bind(IPC_SOCKET) | |
@poller.register(socket, ZMQ::POLLIN) | |
socket.setsockopt(ZMQ::LINGER, 1) | |
count = 0 | |
loop do | |
@poller.poll(:blocking) | |
@poller.readables.each do |socket| | |
strings = [] | |
strings << socket.recv_string | |
strings << socket.recv_string while socket.more_parts? | |
count += 1 | |
while strings.size >= 2 | |
socket.send_string(strings.shift, ZMQ::SNDMORE) | |
end | |
socket.send_string(strings.shift) | |
puts "read #{count} messages" if count % 1000 == 0 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment