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/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, making channels look like a communication pipe most of the time.

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)

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.

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 for an example of that.

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 handles reconnection and reliability for you. 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!

See Also:

Defined Under Namespace

Modules: Beanstalk Classes: BidirPipe, Channel, ConnectionLost, ExclusiveSection, IOPair, Pipe, Process, ProtocolBuffersSerializer, ReadOnlyChannel, Select, SelectGroup, Service, SimpleSerializer, TcpClient, TcpServer, WorkQueue, WriteOnlyChannel

Class Method Summary collapse

Class Method Details

.beanstalk(tube_name, server = nil) ⇒ Object

Creates a channel based on the beanstalkd messaging queue.



83
84
85
# File 'lib/cod.rb', line 83

def beanstalk(tube_name, server=nil)
  Cod::Beanstalk::Channel.new(tube_name, server||'localhost:11300')
end

.bidir_pipe(serializer = nil, pipe_pair = nil) ⇒ Object

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.

See Also:



52
53
54
# File 'lib/cod.rb', line 52

def bidir_pipe(serializer=nil, pipe_pair=nil)
  Cod::BidirPipe.new(serializer, pipe_pair)
end

.pipe(serializer = nil, pipe_pair = nil) ⇒ Object

Creates a pipe connection that is visible to this process and its children.

See Also:



41
42
43
# File 'lib/cod.rb', line 41

def pipe(serializer=nil, pipe_pair=nil)
  Cod::Pipe.new(serializer)
end

.process(command, serializer = nil) ⇒ Object

Runs a command via Process.spawn, then links a channel to the commands stdout and stdin. Returns the commands pid and the channel.

Example:

pid, channel = Cod.process('cat')

See Also:



96
97
98
# File 'lib/cod.rb', line 96

def process(command, serializer=nil)
  Cod::Process.new(command, serializer)
end

.select(timeout, groups) ⇒ Object



2
3
4
# File 'lib/cod/select.rb', line 2

def select(timeout, groups)
  Select.new(timeout, groups).do
end

.stdio(serializer = nil) ⇒ Object

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.

See Also:



107
108
109
# File 'lib/cod.rb', line 107

def stdio(serializer=nil)
  Cod::Pipe.new(serializer, [$stdin, $stdout])
end

.tcp(destination, serializer = nil) ⇒ Object

Creates a tcp connection to the destination and returns a channel for it.

See Also:



61
62
63
64
65
# File 'lib/cod.rb', line 61

def tcp(destination, serializer=nil)
  Cod::TcpClient.new(
    destination, 
    serializer || SimpleSerializer.new)
end

.tcp_server(bind_to, serializer = nil) ⇒ Object

Creates a tcp listener on bind_to and returns a channel for it.

See Also:



72
73
74
75
76
# File 'lib/cod.rb', line 72

def tcp_server(bind_to, serializer=nil)
  Cod::TcpServer.new(
    bind_to, 
    serializer || SimpleSerializer.new)
end