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



273
274
275
276
277
# File 'lib/netconfgen/netconfgen.rb', line 273

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)


327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/netconfgen/netconfgen.rb', line 327

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



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/netconfgen/netconfgen.rb', line 286

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