Class: Reader::JSTP::Engine
- Inherits:
-
Object
- Object
- Reader::JSTP::Engine
- Defined in:
- lib/reader/jstp/engine.rb
Instance Method Summary collapse
-
#initialize(source) ⇒ Engine
constructor
A new instance of Engine.
-
#tcp ⇒ Object
Start the server with the TCP strategy.
-
#websocket ⇒ Object
Start the server with the websocket strategy.
Constructor Details
#initialize(source) ⇒ Engine
Returns a new instance of Engine.
4 5 6 7 |
# File 'lib/reader/jstp/engine.rb', line 4 def initialize source @config = ::JSTP::Configuration.instance @source = source end |
Instance Method Details
#tcp ⇒ Object
Start the server with the TCP strategy
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/reader/jstp/engine.rb', line 41 def tcp @server = TCPServer.new @config.port.inbound @config.logger.info "JSTP node running on TCP mode in #{@config.hostname}:#{@config.port.inbound}" loop { Thread.start(@server.accept) { |client| begin # Opening routine token = UUID.new.generate @source.clients[token] = client if @source.respond_to? :open begin @source.open client, token rescue Exception => e @config.logger.error "On open hook: #{e.class}: #{e.}" @config.logger.debug e.backtrace.to_s end end # Message loop while line = client.gets begin @message = ::JSTP::Dispatch.new line @source.dispatch @message, client rescue Exception => e log_exception e, @message end end # Closing routine client.close @source.clients.delete token if @source.respond_to? :close @source.close client, token end rescue Exception => exception @config.logger.error "Client #{token} is DOWN: #{exception.class}: #{exception.}" @config.logger.debug exception.backtrace.to_s client.close end } } rescue Exception => e @config.logger.fatal "Could not initialize TCP server on port #{@config.port.inbound}" end |
#websocket ⇒ Object
Start the server with the websocket strategy
10 11 12 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 |
# File 'lib/reader/jstp/engine.rb', line 10 def websocket EM.run do EM::WebSocket .start host: "0.0.0.0", port: @config.port.inbound do |server| server.onopen do @source.clients[UUID.new.generate] = server if @source.respond_to? :open @source.open server, @source.clients.key(server) end end server. do || begin @message = ::JSTP::Dispatch.new @source.dispatch(@message, server) rescue Exception => exception log_exception exception, @message end end server.onclose do if @source.respond_to? :close @source.close server, @source.clients.key(server) end end end end end |