summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael P. Soulier <msoulier@digitaltorque.ca>2010-05-10 16:11:22 -0400
committerMichael P. Soulier <msoulier@digitaltorque.ca>2010-05-10 16:11:22 -0400
commitfaebd4497843d8cb31ee50c33842d79ff8093654 (patch)
tree86fe498240c1a493f2c326c1536bd6ab9dbda068
parenta07154990f351b6afde17dbf5f41056861c0b73d (diff)
downloadtftpy-faebd4497843d8cb31ee50c33842d79ff8093654.tar.gz
Fixing buffering issue in upload. Uploads work now.
-rw-r--r--tftpy/TftpClient.py2
-rw-r--r--tftpy/TftpServer.py4
-rw-r--r--tftpy/TftpStates.py44
3 files changed, 27 insertions, 23 deletions
diff --git a/tftpy/TftpClient.py b/tftpy/TftpClient.py
index f2c7e78..5bf4658 100644
--- a/tftpy/TftpClient.py
+++ b/tftpy/TftpClient.py
@@ -85,7 +85,7 @@ class TftpClient(TftpSession):
if metrics.duration == 0:
log.info("Duration too short, rate undetermined")
else:
- log.info("Uploaded %.2f bytes in %.2f seconds" % (metrics.bytes, metrics.duration))
+ log.info("Uploaded %d bytes in %.2f seconds" % (metrics.bytes, metrics.duration))
log.info("Average rate: %.2f kbps" % metrics.kbps)
log.info("%.2f bytes in resent data" % metrics.resent_bytes)
log.info("Resent %d packets" % metrics.dupcount)
diff --git a/tftpy/TftpServer.py b/tftpy/TftpServer.py
index 862f1a1..589a1f3 100644
--- a/tftpy/TftpServer.py
+++ b/tftpy/TftpServer.py
@@ -126,6 +126,8 @@ class TftpServer(TftpSession):
log.error("Fatal exception thrown from "
"session %s: %s"
% (key, str(err)))
+ # Break out of for loop since we found the correct
+ # session.
break
else:
@@ -152,7 +154,7 @@ class TftpServer(TftpSession):
if metrics.duration == 0:
log.info("Duration too short, rate undetermined")
else:
- log.info("Transferred %.2f bytes in %.2f seconds"
+ log.info("Transferred %d bytes in %.2f seconds"
% (metrics.bytes, metrics.duration))
log.info("Average rate: %.2f kbps" % metrics.kbps)
log.info("%.2f bytes in resent data" % metrics.resent_bytes)
diff --git a/tftpy/TftpStates.py b/tftpy/TftpStates.py
index e26b268..89d512f 100644
--- a/tftpy/TftpStates.py
+++ b/tftpy/TftpStates.py
@@ -82,6 +82,12 @@ class TftpContext(object):
self.last_dat_pkt = None
self.dyn_file_func = dyn_file_func
+ def __del__(self):
+ """Simple destructor to try to call housekeeping in the end method if
+ not called explicitely. Leaking file descriptors is not a good
+ thing."""
+ self.end()
+
def checkTimeout(self, now):
"""Compare current time with last_update time, and raise an exception
if we're over SOCK_TIMEOUT time."""
@@ -92,7 +98,13 @@ class TftpContext(object):
raise NotImplementedError, "Abstract method"
def end(self):
- raise NotImplementedError, "Abstract method"
+ """Perform session cleanup, since the end method should always be
+ called explicitely by the calling code, this works better than the
+ destructor."""
+ log.debug("in TftpContext.end")
+ if not self.fileobj.closed:
+ log.debug("self.fileobj is open - closing")
+ self.fileobj.close()
def gethost(self):
"Simple getter method for use in a property."
@@ -177,9 +189,6 @@ class TftpContextServer(TftpContext):
self.state = TftpStateServerStart(self)
self.root = root
self.dyn_file_func = dyn_file_func
- # In a server, the tidport is the same as the port. This is also true
- # with symmetric UDP, which we haven't implemented yet.
- #self.tidport = port
def __str__(self):
return "%s:%s %s" % (self.host, self.port, self.state)
@@ -204,11 +213,9 @@ class TftpContextServer(TftpContext):
self.host,
self.port)
- # FIXME
- # How do we ensure that the server closes files, even on error?
-
def end(self):
"""Finish up the context."""
+ TftpContext.end(self)
self.metrics.end_time = time.time()
log.debug("Set metrics.end_time to %s" % self.metrics.end_time)
self.metrics.compute()
@@ -257,15 +264,13 @@ class TftpContextClientUpload(TftpContext):
self.state = TftpStateSentWRQ(self)
- try:
- while self.state:
- log.debug("State is %s" % self.state)
- self.cycle()
- finally:
- self.fileobj.close()
+ while self.state:
+ log.debug("State is %s" % self.state)
+ self.cycle()
def end(self):
"""Finish up the context."""
+ TftpContext.end(self)
self.metrics.end_time = time.time()
log.debug("Set metrics.end_time to %s" % self.metrics.end_time)
self.metrics.compute()
@@ -318,20 +323,17 @@ class TftpContextClientDownload(TftpContext):
self.state = TftpStateSentRRQ(self)
- try:
- while self.state:
- log.debug("State is %s" % self.state)
- self.cycle()
- finally:
- self.fileobj.close()
+ while self.state:
+ log.debug("State is %s" % self.state)
+ self.cycle()
def end(self):
"""Finish up the context."""
+ TftpContext.end(self)
self.metrics.end_time = time.time()
log.debug("Set metrics.end_time to %s" % self.metrics.end_time)
self.metrics.compute()
-
###############################################################################
# State classes
###############################################################################
@@ -480,7 +482,7 @@ class TftpState(object):
"""This method sends an ack packet to the block number specified. If
none is specified, it defaults to the next_block property in the
parent context."""
- log.debug("In sendACK, blocknumber is %s" % blocknumber)
+ log.debug("In sendACK, passed blocknumber is %s" % blocknumber)
if blocknumber is None:
blocknumber = self.context.next_block
log.info("Sending ack to block %d" % blocknumber)