module MMonad::Agent::Macros

class level methods implementing the Agent DSL

Public Instance Methods

exceptions(&handling) click to toggle source

@params handling [Proc] provides the DSL hook for

extending classes to define the error handling behavior
# File lib/m_monad/agent.rb, line 33
def exceptions(&handling)
  @exceptions = handling
end
pattern(interface) click to toggle source

@params pattern [Symbol] describes the 0MQ wrapper

interface for binding to the socket address. Only wrappers
for explicitly supported 0MQ patterns are accepted.

@raises MMonad::PatternNotAllowed

# File lib/m_monad/agent.rb, line 21
def pattern(interface)
  @pattern = MMonad::Pattern.find_agent(interface)
end
process(&handling) click to toggle source

@params procedure [Proc] provides the DSL hook for

extending classes to define the message processing loop
# File lib/m_monad/agent.rb, line 27
def process(&handling)
  @process = handling
end
run() click to toggle source

The `run` class method binds to the specified socket and begins the processing loop. Once called, `run` will block indefinitely. `run` should only be called once and is intended to be thread-safe.

# File lib/m_monad/agent.rb, line 41
def run
  active_socket = @pattern.new(@socket)

  while message = active_socket.receive
    active_socket << process!(message).to_json
  end
end
socket(address) click to toggle source

@params address [String] describes the socket,

including the protocol and networking port, if applicable
# File lib/m_monad/agent.rb, line 13
def socket(address)
  @socket = address
end

Private Instance Methods

process!(message) click to toggle source
# File lib/m_monad/agent.rb, line 55
def process!(message)
  @process.call(unwrap(message))
rescue => exception
  if !defined?(@exceptions)
    unhandled(exception)
  else
    @exceptions.call(exception) || unhandled(exception)
  end
end
unhandled(exception) click to toggle source
# File lib/m_monad/agent.rb, line 65
def unhandled(exception)
  { unhandled_exception: exception }
end
unwrap(message) click to toggle source
# File lib/m_monad/agent.rb, line 51
def unwrap(message)
  JSON.parse(message.to_a.first)
end