Class: TFTP::Transfer

Inherits:
Object
  • Object
show all
Includes:
Protocol
Defined in:
lib/em-tftp.rb

Overview

An in-progress file transfer operation Holds all the state which a TFTP server/client needs to track for a single transfer Subclasses contain logic specific to client downloads, server downloads, client uploads, server uploads

Direct Known Subclasses

Receive, Send

Constant Summary collapse

BASE_RETRANSMIT_TIMEOUT =

seconds

1.5
MAX_RETRANSMIT_TIMEOUT =
12

Constants included from Protocol

Protocol::ERROR_MESSAGES

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, peer_addr, peer_port, listener) ⇒ Transfer

Returns a new instance of Transfer.



111
112
113
114
115
116
117
118
119
120
# File 'lib/em-tftp.rb', line 111

def initialize(connection, peer_addr, peer_port, listener)
  # 'connection' is the UDP socket used for this transfer (actually an EventMachine::Connection object)
  # each transfer uses a newly opened UDP socket, which is closed after the transfer is finished
  # this is because the UDP port number doubles as a TFTP transfer ID
  # so using the same port number for 2 successive requests to the same peer may cause problems
  @connection = connection
  @peer_addr, @peer_port, @listener = peer_addr, peer_port, listener
  @buffer = @block_no = @timer = nil
  @timeout = 1.5
end

Instance Attribute Details

#block_noObject

Returns the value of attribute block_no.



123
124
125
# File 'lib/em-tftp.rb', line 123

def block_no
  @block_no
end

#bufferObject

Returns the value of attribute buffer.



123
124
125
# File 'lib/em-tftp.rb', line 123

def buffer
  @buffer
end

#peer_addrObject (readonly)

Returns the value of attribute peer_addr.



122
123
124
# File 'lib/em-tftp.rb', line 122

def peer_addr
  @peer_addr
end

#peer_portObject (readonly)

Returns the value of attribute peer_port.



122
123
124
# File 'lib/em-tftp.rb', line 122

def peer_port
  @peer_port
end

#timeoutObject

Returns the value of attribute timeout.



123
124
125
# File 'lib/em-tftp.rb', line 123

def timeout
  @timeout
end

#timerObject

Returns the value of attribute timer.



123
124
125
# File 'lib/em-tftp.rb', line 123

def timer
  @timer
end

Instance Method Details

#abort!(code = 0, error_msg = Protocol::ERROR_MESSAGES[code]) ⇒ Object

Abort the file transfer. An optional error message and code can be included. This should be called if the transfer cannot be completed due to a full hard disk, wrong permissions, etc

TFTP error codes include:

0 Not defined, see error message (if any). 1 File not found. 2 Access violation. 3 Disk full or allocation exceeded. 4 Illegal TFTP operation. 5 Unknown transfer ID. 6 File already exists. 7 No such user.



138
139
140
141
142
143
# File 'lib/em-tftp.rb', line 138

def abort!(code=0, error_msg=Protocol::ERROR_MESSAGES[code])
  stop_timer!
  send_error(code, error_msg || "Unknown error")
  @connection.close_connection_after_writing
  @listener.failed(error_msg || "Unknown error")
end

#error!(error_msg) ⇒ Object



145
146
147
148
149
# File 'lib/em-tftp.rb', line 145

def error!(error_msg)
  stop_timer!
  @connection.close_connection
  @listener.failed(error_msg)
end