cod comes with a number of built-in special channels. For example, it can talk directly to beanstalkd.
Look at this as an example of how easily cod can be extended to speak protocol X. And also, use these because they make cod useful!
Beanstalkd channels work like other cod channels at their base:
# Connects to beanstalkd on localhost:11300
# per default.
beanstalk = Cod.beanstalk("my_tube")
beanstalk.put :foo
beanstalk.get # => :foo
The symbol :foo
is transformed into a message (using
serialisation) and put into the tube named ‘my_tube’. It
then waits for someone to come a long and ask beanstalkd for a message. Which
happens in #get
.
Here’s a more extended example:
beanstalk.try_get do |msg, ctl|
case msg
when 1
ctl.release
when 2
ctl.bury
when 3
# Exit block without call to ctl - normal
# reserve/delete cycle that also happens
# in #get
else
# Raising an exception will release the
# message for someone else to fetch.
fail "Unknown message!"
end
end
Please also have a look at Cod::Beanstalk::Channel::Control
(ctl
above)-
almost all commands beanstalkd allows on messages are allowed on this object.
A lot of server implementations have an inetd mode where you launch the
server binary and communicate via stdin/stdout with it. cod captures that
pattern in Cod.process
:
proc = Cod.process('uname', Cod::LineSerializer.new)
uname = proc.channel
uname.get # => "Darwin"
# Wait for the process to terminate
proc.wait
Note that we use the line serializer here. This makes cod expect and speak the unix convention where each element of output is terminated by a “\n”.
Note that you can also implement such ‘stdin/stdout’ servers using cod – but only if you are careful not to use these streams for other things:
stdio = Cod.stdio(Cod::LineSerializer.new)
stdio.put 'test'
This will output
test
followed by a newline.
Be sure to also check out Cod.bidir_pipe
and the examples in the
source tree (examples/) that illustrate some of these concepts.
Cod is the clay in your hands. Go build something.