Class: Netconf::Serial

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

Constant Summary collapse

DEFAULT_BAUD =
9600
DEFAULT_DATABITS =
8
DEFAULT_STOPBITS =
1
DEFAULT_PARITY =
SerialPort::NONE
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) ⇒ Serial

Returns a new instance of Serial

Raises:


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/net/netconf/serial.rb', line 15

def initialize( args_h, &block )
  os_type = args_h[:os_type] || Netconf::DEFAULT_OS_TYPE
  
  raise Netconf::InitError, "Missing 'port' param" unless args_h[:port]
  raise Netconf::InitError, "Missing 'username' param" unless args_h[:username]
  
  @args = args_h.clone
  @args[:prompt] ||= /([%>])\s+$/
    
  # extend this instance with the capabilities of the specific console
  # type; it needs to define #trans_start_netconf session
  # this must be provided! if the caller does not, this will 
  # throw a NameError exception.
  
  extend Netconf::const_get( os_type )::TransSerial        
  
  @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


13
14
15
# File 'lib/net/netconf/serial.rb', line 13

def args
  @args
end

Instance Method Details

#loginObject


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/net/netconf/serial.rb', line 37

def 
  
  begin
    puts
    waitfor(/ogin:/)
  rescue Timeout::Error
    puts
    waitfor(/ogin:/)
  end
  
  puts @args[:username]
  
  waitfor(/assword:/)
  puts @args[:password]
  
  waitfor( @args[:prompt] )
end

#puts(str = nil) ⇒ Object


98
99
100
# File 'lib/net/netconf/serial.rb', line 98

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

#trans_closeObject


82
83
84
85
# File 'lib/net/netconf/serial.rb', line 82

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

#trans_open {|_self| ... } ⇒ Object

:yield: self

Yields:

  • (_self)

Yield Parameters:


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/net/netconf/serial.rb', line 55

def trans_open # :yield: self
  
  baud = @args[:speed] || DEFAULT_BAUD
  data_bits = @args[:bits] || DEFAULT_DATABITS
  stop_bits = @args[:stop] || DEFAULT_STOPBITS
  parity = @args[:parity] || DEFAULT_PARITY
  
  @trans = SerialPort.new( @args[:port], baud, data_bits, stop_bits, parity )            
        
  got = ()      
  yield self if block_given?      
  trans_start_netconf( got )      
  
  self
end

#trans_receiveObject


91
92
93
94
95
96
# File 'lib/net/netconf/serial.rb', line 91

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


71
72
73
74
75
76
# File 'lib/net/netconf/serial.rb', line 71

def trans_receive_hello
  hello_str = trans_receive()
  so_xml = hello_str.index("\n") + 1
  hello_str.slice!(0, so_xml)
  hello_str
end

#trans_send(cmd_str) ⇒ Object


87
88
89
# File 'lib/net/netconf/serial.rb', line 87

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

#trans_send_helloObject


78
79
80
# File 'lib/net/netconf/serial.rb', line 78

def trans_send_hello
  nil
end

#waitfor(this_re = nil) ⇒ Object


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
132
# File 'lib/net/netconf/serial.rb', line 102

def waitfor( this_re = nil )      
  on_re = this_re || @args[:prompt]
  
  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 ) and not 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