Class: EventMachine::FtpClient::ControlConnection

Inherits:
Connection
  • Object
show all
Includes:
Protocols::LineText2
Defined in:
lib/em-ftp-client/control_connection.rb

Defined Under Namespace

Classes: InvalidResponseFormat, Response

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeControlConnection

Returns a new instance of ControlConnection.



70
71
# File 'lib/em-ftp-client/control_connection.rb', line 70

def initialize
end

Instance Attribute Details

#passwordObject

Returns the value of attribute password.



6
7
8
# File 'lib/em-ftp-client/control_connection.rb', line 6

def password
  @password
end

#responderObject (readonly)

Returns the value of attribute responder.



8
9
10
# File 'lib/em-ftp-client/control_connection.rb', line 8

def responder
  @responder
end

#usernameObject

Returns the value of attribute username.



6
7
8
# File 'lib/em-ftp-client/control_connection.rb', line 6

def username
  @username
end

Instance Method Details

#call_callback(*args) ⇒ Object



122
123
124
125
126
# File 'lib/em-ftp-client/control_connection.rb', line 122

def call_callback(*args)
  old_callback = @callback
  @callback = nil
  old_callback.call(*args) if old_callback
end

#call_errback(*args) ⇒ Object



128
129
130
131
# File 'lib/em-ftp-client/control_connection.rb', line 128

def call_errback(*args)
  @errback.call(*args) if @errback
  @errback = nil
end

#callback(&blk) ⇒ Object



114
115
116
# File 'lib/em-ftp-client/control_connection.rb', line 114

def callback(&blk)
  @callback = blk
end

#closeObject



174
175
176
177
178
179
180
# File 'lib/em-ftp-client/control_connection.rb', line 174

def close
  if @data_connection
    raise "Can not close connection while data connection is still open"
  end
  send_data("QUIT\r\n")
  @responder = :close_response
end

#close_response(response = nil) ⇒ Object



300
301
302
303
# File 'lib/em-ftp-client/control_connection.rb', line 300

def close_response(response=nil)
  close_connection
  call_callback
end

#connection_completedObject



79
80
81
# File 'lib/em-ftp-client/control_connection.rb', line 79

def connection_completed
  @responder = :receive_greetings
end

#cwd(dir) ⇒ Object



149
150
151
152
# File 'lib/em-ftp-client/control_connection.rb', line 149

def cwd(dir)
  send_data("CWD #{dir}\r\n")
  @responder = :cwd_response
end

#cwd_response(response) ⇒ Object

Called when a response for the CWD or CDUP is received



213
214
215
216
217
218
# File 'lib/em-ftp-client/control_connection.rb', line 213

def cwd_response(response)
  if response && response.code != "226"
    @responder = nil
    call_callback
  end
end

#data_connection_closed(data) ⇒ Object



108
109
110
111
112
# File 'lib/em-ftp-client/control_connection.rb', line 108

def data_connection_closed(data)
  @data_buffer = data
  @data_connection = nil
  send(@responder) if @responder and @response.complete?
end

#errback(&blk) ⇒ Object



118
119
120
# File 'lib/em-ftp-client/control_connection.rb', line 118

def errback(&blk)
  @errback = blk
end

#error(e) ⇒ Object



83
84
85
# File 'lib/em-ftp-client/control_connection.rb', line 83

def error(e)
  @errback.call(e) if @errback
end

#listObject



182
183
184
185
# File 'lib/em-ftp-client/control_connection.rb', line 182

def list
  send_data("LIST\r\n")
  @responder = :list_response
end

#list_response(response = nil) ⇒ Object



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/em-ftp-client/control_connection.rb', line 277

def list_response(response=nil)
  if response && response.code != "226"
    @data_connection.close_connection
    @responder = nil
    error(response)
  end

  if response && @data_connection
    #well we still gots to wait for the file
  elsif @data_connection
    #well we need to wait for a response
  else
    @responder = nil
    old_data_buffer = @data_buffer
    @data_buffer = nil
    # parse it into a real form
    file_list = old_data_buffer.split("\n").map do |line|
      ::Net::FTP::List.parse(line.strip)
    end
    call_callback(file_list)
  end
end

#pass(word) ⇒ Object



139
140
141
142
# File 'lib/em-ftp-client/control_connection.rb', line 139

def pass(word)
  send_data("PASS #{word}\r\n")
  @responder = :password_response
end

#password_response(response) ⇒ Object

Called when a response for the PASS verb is received



202
203
204
# File 'lib/em-ftp-client/control_connection.rb', line 202

def password_response(response)
  type "I"
end

#pasvObject



159
160
161
162
# File 'lib/em-ftp-client/control_connection.rb', line 159

def pasv
  send_data("PASV\r\n")
  @responder = :pasv_response
end

#pasv_response(response) ⇒ Object

Called when a response for the PASV verb is received

Opens a new data connection and executes the callback



231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/em-ftp-client/control_connection.rb', line 231

def pasv_response(response)
  @responder = nil
  if response.code == "227"
    if m = /(\d{1,3},\d{1,3},\d{1,3},\d{1,3}),(\d+),(\d+)/.match(response.body)
      host_ip = m[1].gsub(",", ".")
      host_port = m[2].to_i*256 + m[3].to_i
      pasv_callback = @callback
      @data_connection = EM.connect(host_ip, host_port, DataConnection)
      @data_connection.on_connect &pasv_callback
      @data_connection.callback {|data| data_connection_closed(data) }
    end
  end
end

#post_initObject



73
74
75
76
77
# File 'lib/em-ftp-client/control_connection.rb', line 73

def post_init
  @data_connection = nil
  @response = Response.new
  @responder = nil
end

#pwdObject



154
155
156
157
# File 'lib/em-ftp-client/control_connection.rb', line 154

def pwd
  send_data("PWD\r\n")
  @responder = :pwd_response
end

#pwd_response(response) ⇒ Object

Called when a response for the PWD verb is received

Calls out with the result to the callback given to pwd



223
224
225
226
# File 'lib/em-ftp-client/control_connection.rb', line 223

def pwd_response(response)
  @responder = nil
  call_callback(response.body)
end

#receive_greetings(banner) ⇒ Object

Called after initial connection



190
191
192
193
194
# File 'lib/em-ftp-client/control_connection.rb', line 190

def receive_greetings(banner)
  if banner.code == "220"
    user username
  end
end

#receive_line(line) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/em-ftp-client/control_connection.rb', line 87

def receive_line(line)
  @response << line
  if @response.complete?
    # get a new fresh response ready
    old_response = @response
    @response = Response.new

    # dispatch appropriately
    if old_response.success?
      send(@responder, old_response) if @responder
    elsif old_response.mark?
      #maybe notice the mark or something
    elsif old_response.failure?
      error(old_response)
    end
  end
rescue InvalidResponseFormat => e
  @response = Response.new
  error(e)
end

#retr(file) ⇒ Object



164
165
166
167
# File 'lib/em-ftp-client/control_connection.rb', line 164

def retr(file)
  send_data("RETR #{file}\r\n")
  @responder = :retr_response
end

#retr_response(response = nil) ⇒ Object



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/em-ftp-client/control_connection.rb', line 245

def retr_response(response=nil)
  if response && response.code != "226"
    @data_connection.close_connection
    @responder = nil
    error(response)
  end

  if response && @data_connection
    @response = response
    #well we still gots to wait for the file
  elsif @data_connection
    #well we need to wait for a response
  else
    @responder = nil
    old_data_buffer = @data_buffer
    @data_buffer = nil
    call_callback(old_data_buffer)
  end
end

#stor(filename) ⇒ Object



169
170
171
172
# File 'lib/em-ftp-client/control_connection.rb', line 169

def stor(filename)
  send_data("STOR #{filename}\r\n")
  @responder = :stor_response
end

#stor_response(response = nil) ⇒ Object



265
266
267
268
269
270
271
272
273
274
275
# File 'lib/em-ftp-client/control_connection.rb', line 265

def stor_response(response=nil)
  if response && response.code != "226"
    @data_connection.close_connection
    @responder = nil
    error(response)
  end

  @responder = nil
  @data_buffer = nil
  call_callback
end

#type(t) ⇒ Object



144
145
146
147
# File 'lib/em-ftp-client/control_connection.rb', line 144

def type(t)
  send_data("TYPE #{t}\r\n")
  @responder = :type_response
end

#type_response(response) ⇒ Object

Called when a response for the TYPE verb is received



207
208
209
210
# File 'lib/em-ftp-client/control_connection.rb', line 207

def type_response(response)
  @responder = nil
  call_callback
end

#user(name) ⇒ Object

commands



134
135
136
137
# File 'lib/em-ftp-client/control_connection.rb', line 134

def user(name)
  send_data("USER #{name}\r\n")
  @responder = :user_response
end

#user_response(response) ⇒ Object

Called when a response for the USER verb is received



197
198
199
# File 'lib/em-ftp-client/control_connection.rb', line 197

def user_response(response)
  pass password
end