Class: Writer::JSTP::Dispatch
- Inherits:
-
Discoverer::Pattern
- Object
- Discoverer::Pattern
- Writer::JSTP::Dispatch
- Defined in:
- lib/writer/jstp/dispatch.rb
Instance Attribute Summary collapse
-
#pool ⇒ Object
Returns the value of attribute pool.
Instance Method Summary collapse
-
#initialize(source) ⇒ Dispatch
constructor
A new instance of Dispatch.
- #json ⇒ Object
- #short ⇒ Object
- #string ⇒ Object
-
#tcp(port = nil) ⇒ Object
Dispatch applying the TCP strategy.
-
#websocket(port = nil) ⇒ Object
Dispatch this applying the websocket strategy.
Constructor Details
#initialize(source) ⇒ Dispatch
Returns a new instance of Dispatch.
6 7 8 9 10 |
# File 'lib/writer/jstp/dispatch.rb', line 6 def initialize source super source @config = ::JSTP::Configuration.instance @pool = {} end |
Instance Attribute Details
#pool ⇒ Object
Returns the value of attribute pool.
4 5 6 |
# File 'lib/writer/jstp/dispatch.rb', line 4 def pool @pool end |
Instance Method Details
#json ⇒ Object
64 65 66 |
# File 'lib/writer/jstp/dispatch.rb', line 64 def json JSON.dump @source end |
#short ⇒ Object
101 102 103 104 105 106 107 |
# File 'lib/writer/jstp/dispatch.rb', line 101 def short unless @source.body "#{@source.method} #{@source.resource.join('/')}" else "#{@source.method} #{@source.resource.join('/')}?#{JSON.dump(@source.body)}" end end |
#string ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/writer/jstp/dispatch.rb', line 68 def string response = "#{@source.method} #{@source.resource.join('/')} #{@source.protocol.join('/')}\n" @source.each do |key, value| unless key == "method" || key == "resource" || key == "protocol" || key == "body" if value.is_a? Array response += "#{key}: #{value.join('/')}\n" else if key == 'timestamp' response += "#{key}: #{Time.at(value)}\n" else response += "#{key}: #{value}\n" end end end end unless @source.body.nil? response += "\n" if @source.body.is_a? Hash @source.body.each do |key, value| begin response += "#{key}: #{JSON.dump(value)}\n" rescue JSON::GeneratorError => e response += "#{key}: #{value}\n" end end else response += JSON.dump @source.body end end response end |
#tcp(port = nil) ⇒ Object
Dispatch applying the TCP strategy
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/writer/jstp/dispatch.rb', line 47 def tcp port = nil port = @config.port.outbound if port.nil? host = @source['resource'].first @pool[host] ||= TCPSocket.new host, port if @pool[host].closed? @pool[host] = TCPSocket.new host, port elsif not @pool[host].stat.readable? @pool[host].close @pool[host] = TCPSocket.new host, port end @pool[host].puts @source.to.json @config.logger.debug @source.to_s end |
#websocket(port = nil) ⇒ Object
Dispatch this applying the websocket strategy
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/writer/jstp/dispatch.rb', line 13 def websocket port = nil host = @source["resource"].first path = @source["resource"][1..-1].join('/') if port.nil? connection_uri = "ws://#{host}:#{@config.port.outbound}/#{path}" else connection_uri = "ws://#{host}:#{port}/#{path}" end begin ws = EM::WebSocketClient.connect connection_uri ws.callback do ws.send_msg @source.to.json @config.logger.debug @source.to_s ws.close_connection_after_writing end rescue RuntimeError => e EM.run do ws = EM::WebSocketClient.connect connection_uri ws.callback do ws.send_msg @source.to.json @config.logger.debug @source.to_s ws.close_connection_after_writing end ws.disconnect do EM::stop_event_loop end end end end |