Class: SDN::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/sdn/client.rb

Constant Summary collapse

WAIT_TIME =
0.25

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(port) ⇒ Client

Returns a new instance of Client.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/sdn/client.rb', line 9

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 Attribute Details

#trace=(value) ⇒ Object (writeonly)

Sets the attribute trace

Parameters:

  • value

    the value to set the attribute trace to.



7
8
9
# File 'lib/sdn/client.rb', line 7

def trace=(value)
  @trace = value
end

Instance Method Details

#ensure(message) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/sdn/client.rb', line 49

def ensure(message)
  loop do
    messages = transact(message)
    next if messages.empty?
    next unless message.class.expected_response?(messages.first)

    return messages.first
  end
end

#receive(timeout = nil) ⇒ Object



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
# File 'lib/sdn/client.rb', line 61

def receive(timeout = nil)
  messages = []

  loop do
    message, bytes_read = Message.parse(@buffer.bytes)
    # discard how much we read
    @buffer = @buffer[bytes_read..-1] if bytes_read
    unless message
      break unless messages.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.unpack1("H*").gsub(/\h{2}/, "\\0 ")}") if trace?
        @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
          unless @buffer.empty?
            SDN.logger.debug "Discarding #{@buffer.unpack1("H*").gsub(/\h{2}/, "\\0 ")} due to timeout"
          end
          @buffer = +""
          return messages if timeout
        end

        retry
      end
      next
    end

    SDN.logger.debug("Received message #{message.inspect}")
    if block_given?
      yield message
    else
      messages << message
    end
  end

  messages
end

#send(message) ⇒ Object



38
39
40
41
# File 'lib/sdn/client.rb', line 38

def send(message)
  SDN.logger.debug("Sending #{message.inspect}")
  @io.write(message.serialize)
end

#trace?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/sdn/client.rb', line 34

def trace?
  @trace
end

#transact(message) ⇒ Object



43
44
45
46
47
# File 'lib/sdn/client.rb', line 43

def transact(message)
  message.ack_requested = true
  send(message)
  receive(1)
end