Class: MarilynRPC::Gentleman

Inherits:
Object
  • Object
show all
Defined in:
lib/marilyn-rpc/gentleman.rb

Overview

The gentleman is a proxy onject that should help to create async responses on the server (service) side. There are two ways to use the gentleman. See the examples.

The Gentleman has to be returned by the service method.

Examples:

Use the gentleman als passed block

MarilynRPC::Gentleman.proxy do |helper|
  EM.system('ls', &helper)

  lambda do |output,status|
    output if status.exitstatus == 0 
  end
end

Use the gentleman for responses that are objects

conn = EM::Protocols::HttpClient2.connect 'google.com', 80
req = conn.get('/')
MarilynRPC::Gentleman.new(conn) do |response|
  response.content
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(deferable = nil, &callback) ⇒ Gentleman

create a new proxy object using a deferable or the passed block.

Parameters:

  • deferable (EventMachine::Deferrable) (defaults to: nil)


32
33
34
35
36
37
38
39
40
41
# File 'lib/marilyn-rpc/gentleman.rb', line 32

def initialize(deferable = nil, &callback)
  @callback = callback
  if deferable
    unless deferable.respond_to? :callback
      raise ArgumentError.new("Wrong type, expected object that responds to #callback!")
    end
    gentleman = self
    deferable.callback { |*args| gentleman.handle(*args) }
  end
end

Instance Attribute Details

#callbackProc

the callback that will be called when the deferable was successful

Returns:

  • (Proc)

    the current value of callback



27
28
29
# File 'lib/marilyn-rpc/gentleman.rb', line 27

def callback
  @callback
end

#connectionObject

the connection where the response should be send

Returns:

  • (Object)

    the current value of connection



27
28
29
# File 'lib/marilyn-rpc/gentleman.rb', line 27

def connection
  @connection
end

#tagObject

the tag that should be used for the response

Returns:

  • (Object)

    the current value of tag



27
28
29
# File 'lib/marilyn-rpc/gentleman.rb', line 27

def tag
  @tag
end

Class Method Details

.proxy(&block) ⇒ Object

Creates a anonymous Gentleman proxy where the helper is exposed to, be able to use the Gentleman in situations where only a callback can be passed.



45
46
47
48
49
# File 'lib/marilyn-rpc/gentleman.rb', line 45

def self.proxy(&block)
  gentleman = MarilynRPC::Gentleman.new
  gentleman.callback = block.call(gentleman.helper)
  gentleman
end

Instance Method Details

#handle(*args) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The handler that will send the response to the remote system

Parameters:

  • args (Object)

    the arguments that should be handled by the callback, the reponse of the callback will be send as result



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/marilyn-rpc/gentleman.rb', line 55

def handle(*args)
  mail = MarilynRPC::CallResponseMail.new(self.tag, self.callback.call(*args))
  data = MarilynRPC::Envelope.new(mail.encode, 
                                  MarilynRPC::CallResponseMail::TYPE).encode
  connection.send_data(data)
rescue Exception => exception
  mail = MarilynRPC::ExceptionMail.new(self.tag, exception)
  data = MarilynRPC::Envelope.new(mail.encode,
                                  MarilynRPC::ExceptionMail::TYPE).encode
  connection.send_data(data)
end

#helperObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The helper that will be called by the deferable to call #handle later



70
71
72
73
# File 'lib/marilyn-rpc/gentleman.rb', line 70

def helper
  gentleman = self
  lambda { |*args| gentleman.handle(*args) }
end