Module: Cod
- Defined in:
- lib/cod.rb,
lib/cod/pipe.rb,
lib/cod/iopair.rb,
lib/cod/select.rb,
lib/cod/channel.rb,
lib/cod/process.rb,
lib/cod/service.rb,
lib/cod/bidir_pipe.rb,
lib/cod/tcp_client.rb,
lib/cod/tcp_server.rb,
lib/cod/work_queue.rb,
lib/cod/select_group.rb,
lib/cod/line_serializer.rb,
lib/cod/simple_serializer.rb,
lib/cod/protocol_buffers_serializer.rb
Overview
The core concept of Cod are ‘channels’. (see Channel::Base) You can create such channels on top of the various transport layers. Once you have such a channel, you #put messages into it and you #get messages out of it. Messages are retrieved in FIFO manner.
channel.put :test1
channel.put :test2
channel.get # => :test1
Cod also brings a few abstractions layered on top of channels: You can use channels to present ‘services’ (Cod::Service) to the network: A service is a simple one or two way RPC call. (one way = asynchronous)
client = channel.client
client.notify [:foo, :bar]
Cod channels are serializable whereever possible. If you want to tell somebody where to write his answers and/or questions to, send him the channel! This is really powerful and used extensively in constructing the higher order primitives.
server.put [:some_request, my_channel]
# Server will receive my_channel and be able to contact us there.
All Cod channels have a serializer. If you don’t specify your own serializer, they will use Marshal.dump and Marshal.load. (see SimpleSerializer) This allows to send Ruby objects and not just strings by default. If you want to, you can of course go back to very strict wire formats, see ProtocolBuffersSerializer or LineSerializer for an example of that.
line_protocol_channel = Cod.pipe(Cod::LineSerializer.new)
line_protocol_channel.put 'some_string'
The goal of Cod is that you have to know only very few things about the network (the various transports) to be able to construct complex things. It also translates cryptic OS errors into plain text messages where it can’t just handle them. This should give you a clear place to look at if things go wrong. Note that this can only be ever as good as the sum of situations Cod has been tested in. Contribute your observations and we’ll come up with a way of dealing with most of the tricky stuff!
Types of channels in this version
- Cod.pipe
-
Transports via
IO.pipe
- Cod.tcp
-
Transports via TCP (client)
- Cod.tcp_server
-
Transports via TCP (as a server)
- Cod.stdio
-
Connects to $stdin and $stdout (
IO.pipe
) - Cod.process
-
Spawn a child process and connects to that process’ $stdin and $stdout (
IO.pipe
) - Cod.beanstalk
-
Transports via a tube on beanstalkd
Defined Under Namespace
Modules: Beanstalk Classes: BidirPipe, Channel, ConnectionLost, ExclusiveSection, IOPair, LineSerializer, Pipe, Process, ProtocolBuffersSerializer, ReadOnlyChannel, Select, SelectGroup, Service, SimpleSerializer, TcpClient, TcpServer, WorkQueue, WriteOnlyChannel
Class Method Summary collapse
-
.beanstalk(tube_name, server = 'localhost:11300') ⇒ Cod::Beanstalk::Channel
Creates a channel based on the beanstalkd messaging queue.
-
.bidir_pipe(serializer = nil) ⇒ Cod::BidirPipe
Creates two channels based on Cod.pipe (unidirectional IO.pipe) and links things up so that you communication is bidirectional.
-
.pipe(serializer = nil) ⇒ Cod::Pipe
Creates a pipe connection that is visible to this process and its children.
-
.process(command, serializer = nil) ⇒ Cod::Process
Runs a command via Process.spawn, then links a channel to the commands stdout and stdin.
-
.select(timeout, groups) ⇒ Hash, ...
A shortcurt for constructing a Select.
-
.stdio(serializer = nil) ⇒ Cod::Pipe
Links a process’ stdin and stdout up with a pipe.
-
.tcp(destination, serializer = nil) ⇒ Cod::TcpClient
Creates a tcp connection to the destination and returns a channel for it.
-
.tcp_server(bind_to, serializer = nil) ⇒ Cod::TcpServer
Creates a tcp listener on bind_to and returns a channel for it.
Class Method Details
.beanstalk(tube_name, server = 'localhost:11300') ⇒ Cod::Beanstalk::Channel
Creates a channel based on the beanstalkd messaging queue.
115 116 117 |
# File 'lib/cod.rb', line 115 def beanstalk(tube_name, server=nil) Cod::Beanstalk::Channel.new(tube_name, server||'localhost:11300') end |
.bidir_pipe(serializer = nil) ⇒ Cod::BidirPipe
Creates two channels based on pipe (unidirectional IO.pipe) and links things up so that you communication is bidirectional. Writes go to #out and reads come from #in.
76 77 78 |
# File 'lib/cod.rb', line 76 def bidir_pipe(serializer=nil, pipe_pair=nil) Cod::BidirPipe.new(serializer, pipe_pair) end |
.pipe(serializer = nil) ⇒ Cod::Pipe
Creates a pipe connection that is visible to this process and its children.
63 64 65 |
# File 'lib/cod.rb', line 63 def pipe(serializer=nil) Cod::Pipe.new(serializer) end |
.process(command, serializer = nil) ⇒ Cod::Process
Runs a command via Process.spawn, then links a channel to the commands stdout and stdin.
128 129 130 |
# File 'lib/cod.rb', line 128 def process(command, serializer=nil) Cod::Process.new(command, serializer) end |
.select(timeout, groups) ⇒ Hash, ...
A shortcurt for constructing a Select. See Cod::Select#do for more information.
9 10 11 12 |
# File 'lib/cod/select.rb', line 9 def select(timeout, groups) # TODO create an overload without the timeout Select.new(timeout, groups).do end |
.stdio(serializer = nil) ⇒ Cod::Pipe
Links a process’ stdin and stdout up with a pipe. This means that the pipes #put method will print to stdout, and the #get method will read from stdin.
140 141 142 |
# File 'lib/cod.rb', line 140 def stdio(serializer=nil) Cod::Pipe.new(serializer, [$stdin, $stdout]) end |
.tcp(destination, serializer = nil) ⇒ Cod::TcpClient
Creates a tcp connection to the destination and returns a channel for it.
87 88 89 90 91 |
# File 'lib/cod.rb', line 87 def tcp(destination, serializer=nil) Cod::TcpClient.new( destination, serializer || SimpleSerializer.new) end |
.tcp_server(bind_to, serializer = nil) ⇒ Cod::TcpServer
Creates a tcp listener on bind_to and returns a channel for it.
100 101 102 103 104 |
# File 'lib/cod.rb', line 100 def tcp_server(bind_to, serializer=nil) Cod::TcpServer.new( bind_to, serializer || SimpleSerializer.new) end |