Class: Cod::Service
- Inherits:
-
Object
- Object
- Cod::Service
- Defined in:
- lib/cod/service.rb
Overview
Cod::Service abstracts the pattern where you send a request to a central location (with possibly multiple workers handling requests) and receive an answer. It solves problems related to timeouts, getting your answer and not any kind of answer, etc...
Synopsis
On the server end:
service = server_channel.service
service.one { |request| :answer }
# On the client end:
service = answer_channel.client(server_channel)
# asynchronous, no answer
service.notify [:a, :request] # => nil
# has an answer:
service.call [:a, :request] # => :answer
Depending on the setup of the channels, this class can be used to implement intra- and interprocess communication, very close to RPC. There are two ways to build on this:
-
Using method_missing, implement real RPC on top. This is usually rather simple (since Cod does a lot of work), see github.com/kschiess/zack for an example of this.
-
Using the 'case' gem, implement servers in an (erlang) actor like fashion.
Direct Known Subclasses
Defined Under Namespace
Classes: Client
Instance Method Summary collapse
-
#initialize(channel) ⇒ Service
constructor
A new instance of Service.
-
#one {|Object| ... } ⇒ Object
Waits until a request arrives on the service channel.
Constructor Details
#initialize(channel) ⇒ Service
Returns a new instance of Service.
32 33 34 |
# File 'lib/cod/service.rb', line 32 def initialize(channel) @channel = channel end |
Instance Method Details
#one {|Object| ... } ⇒ Object
Waits until a request arrives on the service channel. Then reads that request and hands it to the block given. The block return value will be returned to the service client.
Use Cod::Client to perform the service call. This will keep track of messages sent and answers received and a couple of other things.
46 47 48 49 50 |
# File 'lib/cod/service.rb', line 46 def one rq, answer_chan = @channel.get res = yield(rq) answer_chan.put res if answer_chan end |