Class: SDN::Client
- Inherits:
-
Object
- Object
- SDN::Client
- Defined in:
- lib/sdn/client.rb
Constant Summary collapse
- WAIT_TIME =
0.25
Instance Method Summary collapse
- #ensure(message) ⇒ Object
-
#initialize(port) ⇒ Client
constructor
A new instance of Client.
- #receive(timeout = nil) ⇒ Object
- #send(message) ⇒ Object
- #transact(message) ⇒ Object
Constructor Details
#initialize(port) ⇒ Client
Returns a new instance of Client.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/sdn/client.rb', line 5 def initialize(port) uri = URI.parse(port) @io = if uri.scheme == "tcp" require 'socket' TCPSocket.new(uri.host, uri.port) elsif uri.scheme == "telnet" || uri.scheme == "rfc2217" require 'net/telnet/rfc2217' Net::Telnet::RFC2217.new(host: uri.host, port: uri.port || 23, baud: 4800, data_bits: 8, parity: :odd, stop_bits: 1) elsif port == "/dev/ptmx" require 'pty' io, slave = PTY.open puts "Slave PTY available at #{slave.path}" io else require 'ccutrer-serialport' CCutrer::SerialPort.new(port, baud: 4800, data_bits: 8, parity: :odd, stop_bits: 1) end @buffer = "" end |
Instance Method Details
#ensure(message) ⇒ Object
40 41 42 43 44 45 46 47 |
# File 'lib/sdn/client.rb', line 40 def ensure() loop do = transact() next if .empty? next unless .class.expected_response?(.first) return .first end end |
#receive(timeout = nil) ⇒ Object
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 |
# File 'lib/sdn/client.rb', line 51 def receive(timeout = nil) = [] loop do , bytes_read = Message.parse(@buffer.bytes) # discard how much we read @buffer = @buffer[bytes_read..-1] if bytes_read unless break unless .empty? # one EOF is just serial ports saying they have no data; # several EOFs in a row is the file is dead and gone eofs = 0 begin block = @io.read_nonblock(64 * 1024) SDN.logger.debug "read #{block.unpack("H*").first.gsub(/\h{2}/, "\\0 ")}" @buffer.concat(block) next rescue IO::WaitReadable, EOFError => e if e.is_a?(EOFError) eofs += 1 else eofs = 0 end raise if eofs == 5 wait = @buffer.empty? ? timeout : WAIT_TIME if @io.wait_readable(wait).nil? # timed out; just discard everything SDN.logger.debug "discarding #{@buffer.unpack("H*").first.gsub(/\h{2}/, "\\0 ")} due to timeout" @buffer = "" return if timeout end retry end next end if block_given? yield else << end end end |
#send(message) ⇒ Object
30 31 32 |
# File 'lib/sdn/client.rb', line 30 def send() @io.write(.serialize) end |
#transact(message) ⇒ Object
34 35 36 37 38 |
# File 'lib/sdn/client.rb', line 34 def transact() .ack_requested = true send() receive(1) end |