Module: XKoon::Processor::ClassMethods

Defined in:
lib/xkoon/processor.rb

Overview

Class Methods: These are injected into the user’s processor implementation when this module is included.

Instance Method Summary collapse

Instance Method Details

#error_wrapper(p, h, m) ⇒ Array

Wrap in Error: Yields the given block, rescuing exceptions and throwing them back to the peer.

Parameters:

  • p (Hash)

    RxIO Peer (Client / Server) Hash

  • h (Module)

    Handler Module

  • m (Hash)

    Message Hash

Returns:

  • (Array)

    An two-element Array containing a boolean representing success and the block’s result [failed, output]



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/xkoon/processor.rb', line 50

def error_wrapper p, h, m
  failed = false
  out = nil
  begin
    out = yield
  rescue Exception => e
    send_error p, h, m, e
    failed = true
  end

  [failed, out]
end

#handle_response(p, h, m) ⇒ Object

Handle Response: Called whenever a response is sent back from the remote peer.

Parameters:

  • p (Hash)

    RxIO Peer (Client / Server) Hash

  • h (Module)

    Handler Module

  • m (Hash)

    Message Hash



40
41
42
# File 'lib/xkoon/processor.rb', line 40

def handle_response p, h, m
  # NoOp - Provide your implementation
end

#send_error(p, h, req, error) ⇒ Object

Respond with Error: Sends an error message to the Peer.

Parameters:

  • p (Hash)

    RxIO Peer (Client / Server) Hash

  • h (Module)

    Handler Module

  • req (Hash)

    Original request from Peer

  • error (Object)

    Anything representing an error



69
70
71
# File 'lib/xkoon/processor.rb', line 69

def send_error p, h, req, error
  send_response p, h, req, { error: error }, false
end

#send_request(p, h, r, m) ⇒ Object

Send Request: Sends a request to the Peer.

Parameters:

  • p (Hash)

    RxIO Peer (Client / Server) Hash

  • h (Module)

    Handler Module

  • r (Symbol)

    Request method

  • m (Hash)

    Message Hash



31
32
33
# File 'lib/xkoon/processor.rb', line 31

def send_request p, h, r, m
  h.send_request p, r, m
end

#send_response(p, h, req, res = {}, success = true) ⇒ Object

Send Generic Response Message: Sends a response message to the Peer.

Parameters:

  • p (Hash)

    RxIO Peer (Client / Server) Hash

  • h (Module)

    Handler Module

  • req (Hash)

    Original request from Peer

  • res (Object) (defaults to: {})

    Anything representing the result of the Peer’s request

  • success (Object) (defaults to: true)

    Boolean indicating success for the Peer’s request



80
81
82
# File 'lib/xkoon/processor.rb', line 80

def send_response p, h, req, res = {}, success = true
  h.send_msg p, { success: success, req: req, res: res, sent: Time.now.to_i }.to_json
end