From 11c9128bebbeb50005232527a582687a1dc71c1b Mon Sep 17 00:00:00 2001 From: jamesbunton Date: Sun, 16 Apr 2006 11:37:56 +0000 Subject: [PATCH] P2P file transfers should be more reliable. git-svn-id: http://delx.cjb.net/svn/pymsnt/trunk@136 55fbd22a-6204-0410-b2f0-b6c764c7e90a committer: jamesbunton --- src/tlib/msn/msn.py | 23 +++++++++++++------- src/tlib/msn/test_msn.py | 13 +++++++++-- src/tlib/msn/test_msnw.py | 45 ++++++++++++++++++++++++--------------- 3 files changed, 54 insertions(+), 27 deletions(-) diff --git a/src/tlib/msn/msn.py b/src/tlib/msn/msn.py index 23464de..66e3af8 100644 --- a/src/tlib/msn/msn.py +++ b/src/tlib/msn/msn.py @@ -154,7 +154,7 @@ STATUS_LUNCH = 'LUN' PINGSPEED = 50.0 -DEBUGALL = False +DEBUGALL = True LINEDEBUG = False MESSAGEDEBUG = False MSNP2PDEBUG = False @@ -2117,14 +2117,16 @@ class SwitchboardClient(MSNEventBase): if not message.getHeader("P2P-Dest") == self.userHandle: return packet = message.message binaryFields = BinaryFields(packet=packet) - if binaryFields[0] != 0: + if binaryFields[5] == BinaryFields.BYEGOT: + pass # Ignore the ACKs to SLP messages + elif binaryFields[0] != 0: slpLink = self.slpLinks.get(binaryFields[0]) if not slpLink: # Link has been killed. Ignore return if slpLink.remoteUser == message.userHandle: slpLink.handlePacket(packet) - if binaryFields[5] == BinaryFields.ACK or binaryFields[5] == BinaryFields.BYEGOT: + elif binaryFields[5] == BinaryFields.ACK: pass # Ignore the ACKs to SLP messages else: slpMessage = MSNSLPMessage(packet) @@ -2719,7 +2721,9 @@ class SLPLink: self.seqID = SeqID() def killLink(self): + if MSNP2PDEBUG: log.msg("killLink") def kill(): + if MSNP2PDEBUG: log.msg("killLink - kill()") if not self.switchboard: return del self.switchboard.slpLinks[self.sessionID] self.switchboard = None @@ -2862,7 +2866,7 @@ class SLPLink_FileSend(SLPLink_Send): else: if slpMessage.status == "603": self.acceptDeferred.callback((False,)) - # SLPLink is over due to decline, error or BYE + if MSNP2PDEBUG: log.msg("SLPLink is over due to decline, error or BYE") self.killLink() def wait_data_ack(self, packet): @@ -2891,7 +2895,8 @@ class SLPLink_AvatarSend(SLPLink_Send): self.handlePacket = lambda packet: None def handleSLPMessage(self, slpMessage): - self.killLink() # BYE or error + if MSNP2PDEBUG: log.msg("BYE or error") + self.killLink() def close(self): SLPLink_Send.close(self) @@ -2980,6 +2985,7 @@ class SLPLink_FileReceive(SLPLink_Receive, FileReceive): FileReceive.accept(self, consumer) if not self.switchboard: return self.sendSLPMessage("200", "application/x-msnmsgr-sessionreqbody", {"SessionID":self.sessionID}, branch=self.initialBranch) + self.handlePacket = self.wait_data # Moved here because sometimes the second INVITE seems to be skipped def handleSLPMessage(self, slpMessage): if slpMessage.method == "INVITE": # The second invite @@ -2987,9 +2993,10 @@ class SLPLink_FileReceive(SLPLink_Receive, FileReceive): "Listening" : "false",\ "Hashed-Nonce": "{00000000-0000-0000-0000-000000000000}"} self.sendSLPMessage("200", "application/x-msnmsgr-transrespbody", data, branch=slpMessage.branch) - self.handlePacket = self.wait_data +# self.handlePacket = self.wait_data # Moved up else: - self.killLink() # It's either a BYE or an error + if MSNP2PDEBUG: log.msg("It's either a BYE or an error") + self.killLink() # FIXME, do some error handling if it was an error def doFinished(self): @@ -3012,7 +3019,7 @@ class SLPLink_AvatarReceive(SLPLink_Receive): if slpMessage.status == "200": self.handlePacket = self.wait_dataprep else: - # SLPLink is over due to error or BYE + if MSNP2PDEBUG: log.msg("SLPLink is over due to error or BYE") self.killLink() def doFinished(self): diff --git a/src/tlib/msn/test_msn.py b/src/tlib/msn/test_msn.py index 428e5fa..1fc4ed9 100644 --- a/src/tlib/msn/test_msn.py +++ b/src/tlib/msn/test_msn.py @@ -1,17 +1,23 @@ # Copyright (c) 2001-2005 Twisted Matrix Laboratories. -# Copyright (c) 2005 James Bunton +# Copyright (c) 2005-2006 James Bunton # See LICENSE for details. """ Test cases for msn. """ +# Settings +LOGGING = False + + + + # Twisted imports from twisted.protocols import loopback from twisted.protocols.basic import LineReceiver from twisted.internet.defer import Deferred from twisted.internet import reactor, main -from twisted.python import failure +from twisted.python import failure, log from twisted.trial import unittest # System imports @@ -19,6 +25,9 @@ import StringIO, sys, urllib, random, struct import msn +if LOGGING: + log.startLogging(sys.stdout) + def printError(f): print f diff --git a/src/tlib/msn/test_msnw.py b/src/tlib/msn/test_msnw.py index feb7820..97579db 100644 --- a/src/tlib/msn/test_msnw.py +++ b/src/tlib/msn/test_msnw.py @@ -1,10 +1,23 @@ -# Copyright 2005 James Bunton +# Copyright 2005-2006 James Bunton # Licensed for distribution under the GPL version 2, check COPYING for details """ Test cases for msnw (MSN Wrapper) """ +# Settings +TIMEOUT = 30.0 # Connection timeout in seconds +LOGGING = True +TWOFILES = False +FTSENDTEST = False +FTRECEIVETEST = False +USER1 = "messengertest1@hotmail.com" +PASS1 = "hellohello" +USER2 = "messengertest2@hotmail.com" +PASS2 = "hellohello" + + + # Twisted imports from twisted.internet.defer import Deferred from twisted.internet import reactor, error @@ -19,16 +32,6 @@ import msnw import msn -# Settings -TIMEOUT = 30.0 # Connection timeout in seconds -LOGGING = True -USER1 = "messengertest1@hotmail.com" -PASS1 = "hellohello" -USER2 = "messengertest2@hotmail.com" -PASS2 = "hellohello" - - - if LOGGING: log.startLogging(sys.stdout) @@ -167,18 +170,18 @@ class BasicTests(unittest.TestCase, TestsUtil): def testConnect(self): self.doLogins(both=False) -# testConnect.skip = "True" + testConnect.skip = FTRECEIVETEST or FTSENDTEST def testPurgeContacts(self): self.doLogins() self.doPurgeContacts() -# testPurgeContacts.skip = "True" + testPurgeContacts.skip = FTRECEIVETEST or FTSENDTEST def testAddContacts(self): self.doLogins() self.doPurgeContacts() self.doAddContacts() -# testAddContacts.skip = "True" + testAddContacts.skip = FTRECEIVETEST or FTSENDTEST def testMessageExchange(self): self.doLogins() @@ -187,7 +190,7 @@ class BasicTests(unittest.TestCase, TestsUtil): self.user1.sendMessage(USER2, "Hi user2") self.loop("Timeout exchanging message.", cond="GOTMESSAGE") self.failUnless((self.user2.message == (USER1, "Hi user2")), "Failed to transfer message.") -# testMessageExchange.skip = "True" + testMessageExchange.skip = FTRECEIVETEST or FTSENDTEST def testFileSend(self): if raw_input("\n\nALERT!!!\n\nPlease connect to account %s and accept the file transfer from %s. When you have received the complete file, send a message back to the client to signal success.\nType ok when you are ready: " % (USER2, USER1)).lower() != "ok": @@ -212,7 +215,12 @@ class BasicTests(unittest.TestCase, TestsUtil): d = self.user1.sendFile(USER2, "myfile.txt", len(data)) d.addCallback(gotFileSend) self.loop("Sending file.", cond="GOTMESSAGE", timeout=60*60) - testFileSend.skip = "True" + global TWOFILES + if TWOFILES: + d = self.user1.sendFile(USER2, "myfile2.txt", len(data)) + d.addCallback(gotFileSend) + self.loop("Sending file.", cond="GOTMESSAGE", timeout=60*60) + testFileSend.skip = not FTSENDTEST def testFileReceive(self): if raw_input("\n\nALERT!!!\n\nPlease connect to account %s and send a file transfer to %s.\nType ok when you are ready: " % (USER2, USER1)).lower() != "ok": @@ -241,5 +249,8 @@ class BasicTests(unittest.TestCase, TestsUtil): self.doPurgeContacts(both=False) self.doAddContacts(both=False) self.loop("Receiving file.", cond="GOTFILE", timeout=60*60) - testFileReceive.skip = "True" + global TWOFILES + if TWOFILES: + self.loop("Receiving file.", cond="GOTFILE", timeout=60*60) + testFileReceive.skip = not FTRECEIVETEST -- 2.39.2