Module: XKoon::Handler
- Extended by:
- RxIO::IOFilters::BinDelim
- Defined in:
- lib/xkoon/handler.rb
Overview
Handler Module: Provides a generic two-way implementation of the xKoon Protocol.
Class Method Summary collapse
-
.handle_msg(peer, msg) ⇒ Object
Handle Message: Parses incomning messages and forwards them to the processor.
-
.on_drop(peer) ⇒ Object
On Drop: RxIO Service Interface Callback - Triggered each time a peer is dropped.
-
.on_join(peer) ⇒ Object
On Join: RxIO Service Interface Callback - Triggered each time a peer is connected.
-
.process_msg(peer, m) ⇒ Object
Process Message: Verifies message structure and forwards to processor.
-
.send_request(peer, req, msg) ⇒ Object
Send Request: Sends a request to the Peer.
Class Method Details
.handle_msg(peer, msg) ⇒ Object
Handle Message: Parses incomning messages and forwards them to the processor.
64 65 66 67 68 |
# File 'lib/xkoon/handler.rb', line 64 def self.handle_msg peer, msg # Process Chomped Message as JSON and Deep-Symbolize Keys process_msg peer, JSON.parse(msg.chomp).try(:sym_keys) end |
.on_drop(peer) ⇒ Object
On Drop: RxIO Service Interface Callback - Triggered each time a peer is dropped. Forwards notifications to Processor.
51 52 53 54 55 56 57 58 |
# File 'lib/xkoon/handler.rb', line 51 def self.on_drop peer # Acquire Local Endpoint's Processor proc = peer[:local].processor # Forward Drop Notification to Processor proc.on_drop peer, self if proc.respond_to? :on_drop end |
.on_join(peer) ⇒ Object
On Join: RxIO Service Interface Callback - Triggered each time a peer is connected. Forwards notifications to Processor.
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/xkoon/handler.rb', line 36 def self.on_join peer # Acquire Local Endpoint's Processor proc = peer[:local].processor # Inject Proxy Generator Method into Peer peer.define_singleton_method(:to_proxy) { Proxy.for_peer peer } # Forward Join Notification to Processor proc.on_join peer, self if proc.respond_to? :on_join end |
.process_msg(peer, m) ⇒ Object
Process Message: Verifies message structure and forwards to processor.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/xkoon/handler.rb', line 74 def self.process_msg peer, m # Acquire Local Endpoint's Processor proc = peer[:local].processor # Check Request Present in Message raise "Missing method in request" unless m[:req].try(:to_s).try :nstr # Handle Responses return proc.handle_response peer, self, m if m[:req].is_a? Hash # Check Request Valid in Message raise "Invalid method [#{m[:req]}] in request" unless proc.respond_to? "process_#{m[:req]}".to_sym # Forward Message to Local Endpoint's Processor proc.send "process_#{m[:req]}".to_sym, peer, self, m end |
.send_request(peer, req, msg) ⇒ Object
Send Request: Sends a request to the Peer.
29 30 31 |
# File 'lib/xkoon/handler.rb', line 29 def self.send_request peer, req, msg send_msg peer, msg.merge(req: req, sent: Time.now.to_i).to_json end |