Class: NetConfGen::Handler::Base

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

Overview

Base handler contains the common methods for real handlers.

Direct Known Subclasses

RWSimple

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Base

Initialize the handler.

Options:

- :logger  => logger object (e.g. a Logger instance)
- :timeout => used while waiting for next DATA/ACK packets (default: 5s)

All given options are saved in @opts.

Parameters:

  • opts (Hash) (defaults to: {})

    Options



190
191
192
193
194
# File 'lib/netconfgen/netconfgen.rb', line 190

def initialize(opts = {})
  @logger = opts[:logger]
  @timeout = opts[:timeout] || 5
  @opts = opts
end

Instance Method Details

#recv(tag, sock, io) ⇒ Boolean

Receive data over an established connection.

Doesn’t close neither sock nor io. Returns true if whole file has been received, false otherwise.

Parameters:

  • tag (String)

    Tag used for logging

  • sock (UDPSocket)

    Connected socket

  • io (IO)

    Object to write data to

Returns:

  • (Boolean)


244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/netconfgen/netconfgen.rb', line 244

def recv(tag, sock, io)
  sock.send(Packet::ACK.new(0).encode, 0)
  seq = 1
  begin
    loop do
      unless IO.select([sock], nil, nil, @timeout)
        log :warn, "#{tag} Timeout at block ##{seq}"
        return false
      end
      msg, _ = sock.recvfrom(516, 0)
      pkt = Packet.parse(msg)
      if pkt.class != Packet::DATA
        log :warn, "#{tag} Expected DATA but got: #{pkt.class}"
        return false
      end
      if pkt.seq != seq
        log :warn, "#{tag} Seq mismatch: #{seq} != #{pkt.seq}"
        return false
      end
      io.write(pkt.data)
      sock.send(Packet::ACK.new(seq).encode, 0)
      break if pkt.last?
      seq = (seq + 1) & 0xFFFF
    end
  rescue ParseError => e
    log :warn, "#{tag} Packet parse error: #{e.to_s}"
    return false
  end
  log :info, "#{tag} Received file"
  true
end

#send(tag, sock, io) ⇒ Object

Send data over an established connection.

Doesn’t close neither sock nor io.

Parameters:

  • tag (String)

    Tag used for logging

  • sock (UDPSocket)

    Connected socket

  • io (IO)

    Object to send data from



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/netconfgen/netconfgen.rb', line 203

def send(tag, sock, io)
  seq = 1
  begin
    while not io.eof?
      block = io.read(512)
      sock.send(Packet::DATA.new(seq, block).encode, 0)
      unless IO.select([sock], nil, nil, @timeout)
        log :warn, "#{tag} Timeout at block ##{seq}"
        return
      end
      msg, _ = sock.recvfrom(4, 0)
      pkt = Packet.parse(msg)
      if pkt.class != Packet::ACK
        log :warn, "#{tag} Expected ACK but got: #{pkt.class}"
        return
      end
      if pkt.seq != seq
        log :warn, "#{tag} Seq mismatch: #{seq} != #{pkt.seq}"
        return
      end
      # Increment with wrap around at 16 bit boundary,
      # because of tftp block number field size limit.
      seq = (seq + 1) & 0xFFFF
    end
    sock.send(Packet::DATA.new(seq, '').encode, 0) if io.size % 512 == 0
  rescue ParseError => e
    log :warn, "#{tag} Packet parse error: #{e.to_s}"
    return
  end
  log :info, "#{tag} Sent file"
end