Class: Netconf::IOProc

Inherits:
Transport show all
Defined in:
lib/net/netconf/ioproc.rb

Constant Summary collapse

DEFAULT_RDBLKSZ =
(1024 * 1024)

Instance Attribute Summary collapse

Attributes inherited from Transport

#capabilities, #rpc, #session_id, #state, #timeout, #waitio

Instance Method Summary collapse

Methods inherited from Transport

#close, #closed?, #has_capability?, #open, #open?, #rpc_exec, #send_and_receive

Constructor Details

#initialize(args_h = {}, &block) ⇒ IOProc

Returns a new instance of IOProc.


7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/net/netconf/ioproc.rb', line 7

def initialize(args_h = {}, &block)
  os_type = args_h[:os_type] || Netconf::DEFAULT_OS_TYPE

  @args = args_h.clone

  # an OS specific implementation must exist to support this transport type
  extend Netconf.const_get(os_type)::IOProc

  @trans_timeout = @args[:timeout] || Netconf::DEFAULT_TIMEOUT
  @trans_waitio = @args[:waitio] || Netconf::DEFAULT_WAITIO

  super(&block)
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args


5
6
7
# File 'lib/net/netconf/ioproc.rb', line 5

def args
  @args
end

Instance Method Details

#puts(str = nil) ⇒ Object


50
51
52
# File 'lib/net/netconf/ioproc.rb', line 50

def puts(str = nil)
  @trans.puts(str)
end

#trans_closeObject


34
35
36
37
# File 'lib/net/netconf/ioproc.rb', line 34

def trans_close
  @trans.write Netconf::RPC::MSG_CLOSE_SESSION
  @trans.close
end

#trans_openObject

the OS specific transport must implement this method


22
23
24
# File 'lib/net/netconf/ioproc.rb', line 22

def trans_open # :yield: self
  raise 'Unsupported IOProc'
end

#trans_receiveObject


43
44
45
46
47
48
# File 'lib/net/netconf/ioproc.rb', line 43

def trans_receive
  got = waitfor(Netconf::RPC::MSG_END_RE)
  msg_end = got.rindex(Netconf::RPC::MSG_END)
  got[msg_end..-1] = ''
  got
end

#trans_receive_helloObject


26
27
28
# File 'lib/net/netconf/ioproc.rb', line 26

def trans_receive_hello
  trans_receive
end

#trans_send(cmd_str) ⇒ Object


39
40
41
# File 'lib/net/netconf/ioproc.rb', line 39

def trans_send(cmd_str)
  @trans.write(cmd_str)
end

#trans_send_helloObject


30
31
32
# File 'lib/net/netconf/ioproc.rb', line 30

def trans_send_hello
  nil
end

#waitfor(on_re) ⇒ Object


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
# File 'lib/net/netconf/ioproc.rb', line 54

def waitfor(on_re)
  time_out = @trans_timeout
  wait_io = @trans_waitio

  time_out = nil if time_out == false
  done = false
  rx_buf = ''

  until rx_buf.match(on_re) && !IO.select([@trans], nil, nil, wait_io)

    unless IO.select([@trans], nil, nil, time_out)
      raise TimeoutError, 'Netconf IO timed out while waiting for more data'
    end

    begin

      rx_some = @trans.readpartial(DEFAULT_RDBLKSZ)

      rx_buf += rx_some
      break if rx_buf.match(on_re)

    rescue EOFError # End of file reached
      rx_buf = nil if rx_buf == ''
      break # out of outer 'until' loop
    end

  end
  rx_buf
end