Class: KineticRuby::Server
- Inherits:
-
Object
- Object
- KineticRuby::Server
- Defined in:
- lib/kinetic_server.rb
Instance Attribute Summary collapse
-
#connected ⇒ Object
readonly
Returns the value of attribute connected.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
Instance Method Summary collapse
-
#initialize(port = DEFAULT_KINETIC_PORT, log_level = Logger::LOG_LEVEL_INFO) ⇒ Server
constructor
A new instance of Server.
- #report_buffer(buf) ⇒ Object
- #shutdown ⇒ Object
- #start ⇒ Object
Constructor Details
#initialize(port = DEFAULT_KINETIC_PORT, log_level = Logger::LOG_LEVEL_INFO) ⇒ Server
Returns a new instance of Server.
14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/kinetic_server.rb', line 14 def initialize(port = DEFAULT_KINETIC_PORT, log_level=Logger::LOG_LEVEL_INFO) @host = 'localhost' @port ||= DEFAULT_KINETIC_PORT raise "Invalid Kinetic test server port specified (port: #{port})" if !@port || @port < 0 @logger = Logger.new(log_level) @proto = Proto.new(@logger) @server = nil @worker = nil @clients = [] @logger.log "Kinetic Ruby test device server started!" end |
Instance Attribute Details
#connected ⇒ Object (readonly)
Returns the value of attribute connected.
12 13 14 |
# File 'lib/kinetic_server.rb', line 12 def connected @connected end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
12 13 14 |
# File 'lib/kinetic_server.rb', line 12 def host @host end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
12 13 14 |
# File 'lib/kinetic_server.rb', line 12 def port @port end |
Instance Method Details
#report_buffer(buf) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/kinetic_server.rb', line 30 def report_buffer(buf) bytes = buf.bytes row_len = 16 @logger.log "Raw Data (length=#{buf.count}):" while !bytes.empty? row_len = bytes.count >= row_len ? row_len : bytes.count @logger.log " row_len: #{row_len}" row = bytes.slice!(row_len) @logger.log " row: #{row.inspect}" msg = " " row.each do |b| msg += sprintf("0x%02X", b) end @logger.log msg end report end |
#shutdown ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/kinetic_server.rb', line 133 def shutdown return if @server.nil? @logger.log "Kinetic Test Server: shutting down..." if @worker @worker.exit @worker.join(2) @worker = nil end if @server @server.close @server = nil end @logger.log "Kinetic Test Server: shutdown complete" end |
#start ⇒ Object
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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/kinetic_server.rb', line 48 def start return unless @server.nil? @server = TCPServer.new(@host, @port) # Setup handler for signaled shutdown (via ctrl+c) trap("INT") do @logger.log "Kinetic Test Server: INT triggered Kintic Test Server shutdown" shutdown end # Create worker thread for test server to run in so we can continue @worker = Thread.new do @logger.log "Kinetic Test Server: Listening for Kinetic clients..." loop do client = nil begin client, client_info = @server.accept rescue Exception => e @logger.log "Kinetic Test Server: EXCEPTION during accept!\n" + " #{e.inspect}\n" + " #{e.message}\n #{e.backtrace.join(" \n")}" next if client.nil? end next if client.nil? @logger.log "Kinetic Test Server: Connected to #{client.inspect}" request = '' data = nil pdu = nil disconnect = false while !disconnect begin data = client.recv(1024) rescue IO::WaitReadable @logger.log("IO:WaitReadable"); IO.select([client]) retry rescue Exception => e @logger.logv "Kinetic Test Server: EXCEPTION during receive!\n" + " #{e.inspect}\n" + " #{e.message}\n #{e.backtrace.join(" \n")}" disconnect = true next end if (data.nil? || data.empty?) @logger.log "Kinetic Test Server: Client #{client.inspect} disconnected!" disconnect = true next end # Incrementally parse PDU until complete if request[0] == KineticRuby::Proto::VERSION_PREFIX && request.length >= 9 pdu ||= PDU.new(@Logger) pdu.update(request) end # Otherwise, handle custom test requests if pdu.nil? request_match = request.match(/^read\((\d+)\)/) if request_match len = request_match[1].to_i response = 'G'*len @logger.log "Kinetic Test Server: Responding to 'read(#{len})' w/ '#{response}'" client.write response request = '' elsif request =~ /^readProto()/ response = @proto.test_encode @logger.log "Kinetic Test Server: Responding to 'read(#{len})' w/ dummy @protobuf (#{response.length} bytes)" client.write response request = '' end end @logger.log "Kinetic Test Server: Client #{client.inspect} disconnected!" client.close @logger.log "Kinetic Test Server: Client connection shutdown successfully" end end end end |