Skip to content

Commit

Permalink
update celluloid-zmq README to reflect new API
Browse files Browse the repository at this point in the history
  • Loading branch information
bts committed Mar 2, 2012
1 parent a5a0650 commit f4aa3fa
Showing 1 changed file with 53 additions and 35 deletions.
88 changes: 53 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,71 @@ Celluloid::ZMQ
This gem uses the ffi-rzmq library to provide Celluloid actors that can
interact with 0MQ sockets.

Celluloid::ZMQ provides two methods for multiplexing 0MQ sockets with
receiving messages over Celluloid's actor protocol:

* Celluloid::ZMQ#wait_readable(socket): wait until a message is available to
read from the given 0MQ socket
* Celluloid::ZMQ#wait_writeable(socket): waits until there's space in the
given socket's message buffer to send another message
It provides different `Celluloid::ZMQ::Socket`s which can be initialized
then sent `bind` or `connect`. Once bound or connected, the socket can
`read` or `send` depending on whether it's readable or writable.

Example Usage:

require 'celluloid-zmq'
```ruby
require 'celluloid/zmq'

Celluloid::ZMQ.init

ZMQ_CONTEXT = ZMQ::Context.new(1)
class Server
include Celluloid::ZMQ

class MyZmqCell
include Celluloid::ZMQ
def initialize(address)
@socket = PullSocket.new

def initialize(addr)
@socket = ZMQ_CONTEXT.socket(ZMQ::PUSH)
begin
@socket.bind(address)
rescue IOError
@socket.close
raise
end
end

unless ZMQ::Util.resultcode_ok? @socket.connect addr
@socket.close
raise "error connecting to #{addr}: #{ZMQ::Util.error_string}"
end
end
def run
while true; handle_message! @socket.read; end
end

def write(message)
wait_writeable @socket
unless ZMQ::Util.resultcode_ok? @socket.send_string message
raise "error sending 0MQ message: #{ZMQ::Util.error_string}"
end
end
def handle_message(message)
puts "got message: #{message}"
end
end

def read
wait_readable @socket
message = ''
class Client
include Celluloid::ZMQ

rc = @socket.recv_string message
if ZMQ::Util.resultcode_ok? rc
handle_message message
else
raise "error receiving ZMQ string: #{ZMQ::Util.error_string}"
end
end
def initialize(address)
@socket = PushSocket.new

begin
@socket.connect(address)
rescue IOError
@socket.close
raise
end
end

def write(message)
@socket.send(message)

nil
end
end

addr = 'tcp://127.0.0.1:3435'

server = Server.new(addr)
client = Client.new(addr)

server.run!
client.write('hi')
```

Copyright
---------

Copyright (c) 2011 Tony Arcieri. See LICENSE.txt for further details.
Copyright (c) 2011 Tony Arcieri. See LICENSE.txt for further details.

0 comments on commit f4aa3fa

Please sign in to comment.