views:

46

answers:

1

I'm building an FTP client from scratch and I've noticed that the response codes aren't immediate (which is no surprise). What would be a good approach for getting the corresponding code to a command?

Below is an example of the output of Filezilla server. The response code is the three digits near the end of each line.

(000057) 23/05/2010 19:43:10 - (not logged in) (127.0.0.1)> Connected, sending welcome message...
(000057) 23/05/2010 19:43:10 - (not logged in) (127.0.0.1)> 220-FileZilla Server version 0.9.12 beta
(000057) 23/05/2010 19:43:10 - (not logged in) (127.0.0.1)> 220-written by Tim Kosse ([email protected])
(000057) 23/05/2010 19:43:10 - (not logged in) (127.0.0.1)> 220 Please visit http://sourceforge.net/projects/filezilla/
(000057) 23/05/2010 19:43:10 - (not logged in) (127.0.0.1)> user anonymous
(000057) 23/05/2010 19:43:10 - (not logged in) (127.0.0.1)> 331 Password required for anonymous
+1  A: 

In this particular case I'd probably not look to implement this asynchronously. Unless the delay between sending the command and receiving the respond code is large (which it probably isn't for FTP), and you can safely execute another command not knowing the outcome of the last (which you probably can't), it's not really worth trying to implement this asynchronously.

I'd block execution between sending the command string and receiving the full response back, i.e. in pseudocode you might have an execute method like:

  1. Send command string over the network
  2. Wait for a character to come back (or for a timeout, if you want to do this) using a blocking network read method or using a non-blocking method + Thread.sleep(..)
  3. Check if the character is an 'end of response' token (newline?)
  4. If not, go back to 2, if it is, return full the response string

If you're really determined to go down the asynchronous route, you should have a look at the Callback pattern.

Hope this helps.

dogbert
It did. I also had a dig through Apache's Commons Net. What they've used is a combination of an EventObject and EventListener. For the response, readLine() is called on an InputStreamReader as long as there's a code and the resulting lines are added to an ArrayList. There doesn't seem to be any asynchronous handling as such and timeout is probably handled by a java.net class.
James P.