def handleFileDataAvailable(self, fd, Args): print("lalalalalaalalalalalalalallaalalalalalala") fileToSend = Args[0] # fileToSend = open(") lftpSocket = Args[1] print(lftpSocket.__str__()) data = fileToSend.read(800) print("datais:" + data) if data: vsFtpPacket = VsPacket() vsFtpPacket.type = VsPacket.TYPE_DATA vsFtpPacket.data = data if lftpSocket.sendToAll(vsFtpPacket.pack()) == False: print("Transmission error, quiting") sys.exit() else: ''' Send END packet''' vsFtpPacket = VsPacket() vsFtpPacket.type = VsPacket.TYPE_END vsFtpPacket.data = data if lftpSocket.sendToAll(vsFtpPacket.pack()) == False: print("Transmission error, quiting") Event.eventFdDelete(self.handleFileDataAvailable, (fileToSend, lftpSocket)) Lftp.closeSocket(lftpSocket)
def testPacketTypeEnd(self): vsFtpPacket = VsPacket() vsFtpPacket.type = VsPacket.TYPE_END packed = vsFtpPacket.pack() packet2 = VsPacket().unpack(packed) self.assertEqual( vsFtpPacket.type, packet2.type, "Type were not the same," + self._printPackets(vsFtpPacket, packet2)) self.assertEqual( vsFtpPacket.data, packet2.data, "Data was not the same " + self._printPackets(vsFtpPacket, packet2))
def testPacketTypeEnd(self): vsFtpPacket = VsPacket() vsFtpPacket.type = VsPacket.TYPE_END packed = vsFtpPacket.pack() packet2 = VsPacket().unpack(packed) self.assertEqual( vsFtpPacket.type, packet2.type, "Type were not the same," + self._printPackets(vsFtpPacket, packet2) ) self.assertEqual( vsFtpPacket.data, packet2.data, "Data was not the same " + self._printPackets(vsFtpPacket, packet2) )
def testPacketTypeData(self): vsFtpPacket = VsPacket() vsFtpPacket.type = VsPacket.TYPE_DATA vsFtpPacket.data = "this must be a file name" packed = vsFtpPacket.pack() packet2 = VsPacket().unpack(packed) self.assertEqual( vsFtpPacket.type, packet2.type, "Type were not the same," + self._printPackets(vsFtpPacket, packet2) ) self.assertEqual( vsFtpPacket.data, packet2.data, "Data was not the same " + self._printPackets(vsFtpPacket, packet2) )
def send(self): #把send拆分为preSend self.lftpSocket = Lftp.createSocket(UDP_IP, UDP_PORT) for desination in self.addresses: self.lftpSocket.addPeer(desination) ''' 发送开始包''' vsFtpPacket = VsPacket() vsFtpPacket.type = VsPacket.TYPE_BEGIN if (self.RType == "get"): vsFtpPacket.Rt = VsPacket.RT_GET elif (self.RType == "put"): vsFtpPacket.Rt = VsPacket.RT_PUT vsFtpPacket.data = FileSender.processFileName(self.fileName) if self.lftpSocket.sendToAll(vsFtpPacket.pack()) == False: print("Transmission error, quiting") sys.exit() if (self.RType == "get"): self.filehH = open(self.fileName, 'w') Lftp.registerReceiveHandler(self.lftpSocket, self.receiveHandler2) print("Started Receiver on " + str(self.lftpSocket.socket.getsockname())) # self.filehH = open(self.fileName, 'w') elif (self.RType == "put"): fileToSend = open(self.fileName) print("Openned fileToSend DF:" + str(fileToSend.fileno()) + " name: " + str(self.fileName)) Lftp.registerEventHandler(self.lftpSocket, fileSender.handleEvent) Event.eventFd(fileToSend.fileno(), self.handleFileDataAvailable, fileToSend, "FileDataAvailable")
def send(self): self.rudpSocket = Rudp.createSocket(UDP_IP, UDP_PORT) Rudp.registerEventHandler(self.rudpSocket, fileSender.handleEvent) for desination in self.addresses: self.rudpSocket.addPeer(desination) fileToSend = open(self.fileName) Event.eventFd(fileToSend.fileno(), self.handleFileDataAvailable, fileToSend, "FileDataAvailable") print "Openned fileToSend DF:" + str(fileToSend.fileno()) + " name: " + str(self.fileName) ''' Send BEGIN packet''' vsFtpPacket = VsPacket() vsFtpPacket.type = VsPacket.TYPE_BEGIN vsFtpPacket.data = FileSender.processFileName(self.fileName) if self.rudpSocket.sendToAll(vsFtpPacket.pack()) == False: print "Transmission error, quiting" sys.exit()
def send(self): self.rudpSocket = Rudp.createSocket(UDP_IP, UDP_PORT) Rudp.registerEventHandler(self.rudpSocket, fileSender.handleEvent) for desination in self.addresses: self.rudpSocket.addPeer(desination) fileToSend = open(self.fileName) Event.eventFd(fileToSend.fileno(), self.handleFileDataAvailable, fileToSend, "FileDataAvailable") print "Openned fileToSend DF:" + str( fileToSend.fileno()) + " name: " + str(self.fileName) ''' Send BEGIN packet''' vsFtpPacket = VsPacket() vsFtpPacket.type = VsPacket.TYPE_BEGIN vsFtpPacket.data = FileSender.processFileName(self.fileName) if self.rudpSocket.sendToAll(vsFtpPacket.pack()) == False: print "Transmission error, quiting" sys.exit()
def handleFileDataAvailable(self, fd, fileToSend): data = fileToSend.read(800) if data: vsFtpPacket = VsPacket() vsFtpPacket.type = VsPacket.TYPE_DATA vsFtpPacket.data = data if self.lftpSocket.sendToAll(vsFtpPacket.pack()) == False: print("Transmission error, quiting") sys.exit() self.packetcount = self.packetcount + 1 print("### " + str(self.packetcount)) else: ''' Send END packet''' vsFtpPacket = VsPacket() vsFtpPacket.type = VsPacket.TYPE_END vsFtpPacket.data = data if self.lftpSocket.sendToAll(vsFtpPacket.pack()) == False: print("Transmission error, quiting") Event.eventFdDelete(self.handleFileDataAvailable, fileToSend) print("File " + self.fileName + " sent") Lftp.closeSocket(self.lftpSocket)
def receiveHandler2(self, lftpSocket, senderAddress, data): packet = VsPacket().unpack(data) print(">> " + str(packet)) ''' Get or create file info object''' if packet.type == VsPacket.TYPE_DATA: self.filehH.write(str(packet.data)) pass elif packet.type == VsPacket.TYPE_END: print("GOT PACKET END, closing file") self.filehH.close() Lftp.closeSocket(self.lftpSocket) pass pass
def receiveHandler(self, lftpSocket, senderAddress, data): packet = VsPacket().unpack(data) print(">> " + str(packet)) ''' Get or create file info object''' fileInfo = None for fInfoTmp in self.files: if fInfoTmp.sender == senderAddress: fileInfo = fInfoTmp if fileInfo is None: fileInfo = FileInfo() fileInfo.sender = senderAddress self.files.append(fileInfo) ''' Handle different VSFTP pacekt types''' if packet.type == VsPacket.TYPE_BEGIN: if fileInfo.filename is not None: print("File already open !!!!") sys.exit(1) filename = packet.data print("GOT PACKET BEGIN, openning fileToWrite for writing:" + filename) fileInfo.filename = filename fileInfo.filehandle = open(filename, 'w') fileInfo.sendStarted = Event.getCurrentMills() pass elif packet.type == VsPacket.TYPE_DATA: fileInfo.filehandle.write(packet.data) pass elif packet.type == VsPacket.TYPE_END: print("GOT PACKET END, closing file") fileInfo.filehandle.close() self.files.remove(fileInfo) print("Socket closed event received on " + str(lftpSocket)) print("Lost Packets:" + str(lftpSocket.packetloss)) print("Sent Data packets:" + str(lftpSocket.packetsSentData)) print("Sent Control packets:" + str(lftpSocket.packetsSentControl)) print("Received packets(total):" + str(lftpSocket.packetsReceived)) print("Received data packets:" + str(lftpSocket.packetsReceivedData)) print("Received and skipped packets:" + str(lftpSocket.packetsReceivedIgnored)) print("Fake loss:" + str(lftpSocket.packetFakeLoss)) print("Time taken: " + str((Event.getCurrentMills() - fileInfo.sendStarted))) pass pass
def testPacketTypeData(self): vsFtpPacket = VsPacket() vsFtpPacket.type = VsPacket.TYPE_DATA vsFtpPacket.data = "this must be a file name" packed = vsFtpPacket.pack() packet2 = VsPacket().unpack(packed) self.assertEqual( vsFtpPacket.type, packet2.type, "Type were not the same," + self._printPackets(vsFtpPacket, packet2)) self.assertEqual( vsFtpPacket.data, packet2.data, "Data was not the same " + self._printPackets(vsFtpPacket, packet2))
def handleFileDataAvailable(self, fd, fileToSend): data = fileToSend.read(800) if data: vsFtpPacket = VsPacket() vsFtpPacket.type = VsPacket.TYPE_DATA vsFtpPacket.data = data if self.rudpSocket.sendToAll(vsFtpPacket.pack()) == False: print "Transmission error, quiting" sys.exit() self.packetcount = self.packetcount + 1 print "### " + str(self.packetcount) #time.sleep(0) else: ''' Send END packet''' vsFtpPacket = VsPacket() vsFtpPacket.type = VsPacket.TYPE_END vsFtpPacket.data = data if self.rudpSocket.sendToAll(vsFtpPacket.pack()) == False: print "Transmission error, quiting" Event.eventFdDelete(self.handleFileDataAvailable, fileToSend) print "File " + self.fileName + " sent" Rudp.closeSocket(self.rudpSocket)
def prereceiveHandler(self, lftpSocket, senderAddress, data): packet = VsPacket().unpack(data) print(">> " + str(packet)) ''' Get or create file info object''' ''' Handle different VSFTP pacekt types''' if packet.type == VsPacket.TYPE_BEGIN: #func = packet.data.split('/')[0] filename = packet.data if packet.Rt == VsPacket.RT_GET: newR = Lftp.createSocket(self.host, self.validPort) self.validPort = self.validPort + 1 newR.addPeer(senderAddress) fileToSend = open(filename) Event.eventFd(fileToSend.fileno(), self.handleFileDataAvailable, (fileToSend, newR), "FileDataAvailable") elif packet.Rt == VsPacket.RT_PUT: fileInfo = None for fInfoTmp in self.files: if fInfoTmp.sender == senderAddress: fileInfo = fInfoTmp if fileInfo is None: fileInfo = FileInfo() fileInfo.sender = senderAddress self.files.append(fileInfo) Lftp.registerReceiveHandler(self.lftpSocket, self.receiveHandler) if fileInfo.filename is not None: print("File already open !!!!") sys.exit(1) print("GOT PACKET BEGIN, openning fileToWrite for writing:" + str(filename)) fileInfo.filename = filename fileInfo.filehandle = open(filename, 'w') fileInfo.sendStarted = Event.getCurrentMills() pass