def parseDataGet(obj, data, info, opt='json'): ''' this fxn analysis the data and reponse to it @param obj not used @param info ''' if opt != 'nojson' and 'type' not in data.keys(): logging.error(str(data) + '\nhas no key "type"') return ERR #nojson: FPfilenamehere for 28bytes 20byte seq 10byte filesize, #data[60:60+filesize] if opt == 'nojson': jar = RecvSeq.getJar(data[2:30].strip()) jar.pushData(data, 'nojson') if jar.isfull: fname = data[2:30].strip() fBinData = jar.dataStr f = open(os.sep.join([config['downloadPath'], fname]), 'wb') f.write(fBinData) f.close() ''' P2pMainWin.appInstance.modallessMessageBox( data['filename']\ +u' 下载完成' ) ''' print 'download end' logging.info('download file succeeded! ' + str(len(fBinData)) + ' bytes') RecvSeq.releaseJar(data[2:30].strip()) return ENDOFCONNECTION return 0 if data['type'] == 'GF': fileFullPath = os.sep.join([config['downloadPath'], data['filename']]) if not os.path.isfile(fileFullPath): dataSend = json.dumps(dict(type='NOF')) info[0].send(dataSend) else: f = open(fileFullPath, 'rb') iTtl = os.stat(fileFullPath).st_size / (RECVBUFFSIZE - 60) iCur = 0 dataRead = f.read(RECVBUFFSIZE - 60) while P2pMainWin.running and dataRead: dataSend = 'FP' dataSend = dataSend + data['filename'].encode('utf-8') if len(dataSend) < 30: dataSend = dataSend + ' ' * (30 - len(dataSend)) seq = str(iCur) + ' ' + str(iTtl) dataSend = dataSend + seq if len(dataSend) < 50: dataSend = dataSend + ' ' * (50 - len(dataSend)) dataSend = dataSend + str(len(dataRead)) if len(dataSend) < 60: dataSend = dataSend + ' ' * (60 - len(dataSend)) dataSend = dataSend + dataRead #print 'download data send '+str(iCur) if len(dataSend) < RECVBUFFSIZE: dataSend = dataSend + (RECVBUFFSIZE - len(dataSend)) * ' ' info[0].send(dataSend) #print dataSend[0:60] dataRead = f.read(RECVBUFFSIZE - 60) iCur = iCur + 1 f.close() return ENDOFCONNECTION #todo better way of if else if data['type'] == 'GFL': #todo get file list #todo semaphore for file read here hashFilePath = os.sep.join( [config['appDataPath'],'data',\ 'hashfile'] ) f = open(hashFilePath) iTtl = os.stat(hashFilePath).st_size / (RECVBUFFSIZE / 2) iCur = 0 #minus 2048 for some other field such as type, and there will be many / added by json dataRead = f.read(RECVBUFFSIZE / 2) while P2pMainWin.running and dataRead: dataSend = json.dumps(\ dict( type='FL', seq=str(iCur)+' '+str(iTtl),data=dataRead ) ) if len(dataSend) <= RECVBUFFSIZE: print str(len(dataSend)) + ' hashfile dataSend' dataSend = dataSend + (RECVBUFFSIZE - len(dataSend)) * ' ' else: print 'error dataSend too large: ' + str(len(dataSend)) logging.error( 'dataSend '+str(len(dataSend))+' larger than'+\ ' buffersize '+str(RECVBUFFSIZE) ) info[0].send(dataSend) dataRead = f.read(RECVBUFFSIZE / 2) iCur = iCur + 1 f.close() return ENDOFCONNECTION if data['type'] == 'NOF': logging.info('parseDataGet get NOF from ' + str(info[1])) return ENDOFCONNECTION if data['type'] == 'F': #todo Oops! Some dup code with type=='FL' jar = RecvSeq.getJar(data['filename']) jar.pushData(data) if jar.isfull: fBinData = jar.dataStr.decode(codec) #fBinData = jar.dataStr #print fBinData f = open(os.sep.join([config['downloadPath'], data['filename']]), 'w') f.write(fBinData) f.close() ''' P2pMainWin.appInstance.modallessMessageBox( data['filename']\ +u' 下载完成' ) ''' print 'download end' logging.info('download file succeeded! ' + str(len(fBinData)) + ' bytes') RecvSeq.releaseJar(data['filename']) return ENDOFCONNECTION return 0 if data['type'] == 'FL': jar = RecvSeq.getJar(info[1][1]) jar.pushData(data) if jar.isfull: p = AllPeerInfo.getPeer(info[1][0]) p.fileDict = json.loads(jar.dataStr) netHashFilePath = os.sep.join( [config['appDataPath'],'data',\ 'nethashfile'] ) f = open(netHashFilePath, 'w') f.write(jar.dataStr) f.close() logging.info('nethashfile created!') RecvSeq.releaseJar(info[1][1]) if P2pMainWin.appInstance: l = [] for k, v in p.fileDict.iteritems(): l.append(k) P2pMainWin.appInstance.setNetFileList(l) return ENDOFCONNECTION else: return 0
def parseDataGet( obj, data, info, opt='json' ): ''' this fxn analysis the data and reponse to it @param obj not used @param info ''' if opt != 'nojson' and 'type' not in data.keys(): logging.error( str(data)+'\nhas no key "type"' ) return ERR #nojson: FPfilenamehere for 28bytes 20byte seq 10byte filesize, #data[60:60+filesize] if opt == 'nojson': jar = RecvSeq.getJar( data[2:30].strip() ) jar.pushData( data, 'nojson' ) if jar.isfull: fname = data[2:30].strip() fBinData = jar.dataStr f = open( os.sep.join([config['downloadPath'],fname]),'wb') f.write( fBinData ) f.close() ''' P2pMainWin.appInstance.modallessMessageBox( data['filename']\ +u' 下载完成' ) ''' print 'download end' logging.info( 'download file succeeded! '+str(len(fBinData))+' bytes' ) RecvSeq.releaseJar( data[2:30].strip() ) return ENDOFCONNECTION return 0 if data['type'] == 'GF': fileFullPath = os.sep.join([config['downloadPath'],data['filename']]) if not os.path.isfile( fileFullPath ): dataSend = json.dumps( dict(type='NOF') ) info[0].send( dataSend ) else: f = open( fileFullPath,'rb' ) iTtl = os.stat( fileFullPath ).st_size / (RECVBUFFSIZE-60) iCur = 0 dataRead = f.read( RECVBUFFSIZE-60) while P2pMainWin.running and dataRead: dataSend = 'FP' dataSend = dataSend + data['filename'].encode('utf-8') if len(dataSend) < 30: dataSend = dataSend + ' '*(30-len(dataSend)) seq =str(iCur)+' '+str(iTtl) dataSend = dataSend + seq if len(dataSend) < 50: dataSend = dataSend + ' '*(50-len(dataSend)) dataSend = dataSend + str(len(dataRead)) if len(dataSend) < 60: dataSend = dataSend + ' '*(60-len(dataSend)) dataSend = dataSend + dataRead #print 'download data send '+str(iCur) if len(dataSend) < RECVBUFFSIZE: dataSend = dataSend + (RECVBUFFSIZE-len(dataSend))*' ' info[0].send( dataSend ) #print dataSend[0:60] dataRead = f.read( RECVBUFFSIZE-60) iCur = iCur + 1 f.close() return ENDOFCONNECTION #todo better way of if else if data['type'] == 'GFL': #todo get file list #todo semaphore for file read here hashFilePath = os.sep.join( [config['appDataPath'],'data',\ 'hashfile'] ) f = open( hashFilePath ) iTtl = os.stat( hashFilePath ).st_size / (RECVBUFFSIZE/2) iCur = 0 #minus 2048 for some other field such as type, and there will be many / added by json dataRead = f.read( RECVBUFFSIZE/2) while P2pMainWin.running and dataRead: dataSend = json.dumps(\ dict( type='FL', seq=str(iCur)+' '+str(iTtl),data=dataRead ) ) if len( dataSend ) <= RECVBUFFSIZE: print str(len(dataSend))+' hashfile dataSend' dataSend = dataSend + (RECVBUFFSIZE-len(dataSend))*' ' else: print 'error dataSend too large: '+str(len(dataSend)) logging.error( 'dataSend '+str(len(dataSend))+' larger than'+\ ' buffersize '+str(RECVBUFFSIZE) ) info[0].send( dataSend ) dataRead = f.read( RECVBUFFSIZE/2) iCur = iCur + 1 f.close() return ENDOFCONNECTION if data['type'] == 'NOF': logging.info( 'parseDataGet get NOF from '+str(info[1]) ) return ENDOFCONNECTION if data['type'] == 'F': #todo Oops! Some dup code with type=='FL' jar = RecvSeq.getJar( data['filename'] ) jar.pushData( data ) if jar.isfull: fBinData = jar.dataStr.decode(codec) #fBinData = jar.dataStr #print fBinData f = open( os.sep.join([config['downloadPath'],data['filename']]),'w') f.write( fBinData ) f.close() ''' P2pMainWin.appInstance.modallessMessageBox( data['filename']\ +u' 下载完成' ) ''' print 'download end' logging.info( 'download file succeeded! '+str(len(fBinData))+' bytes' ) RecvSeq.releaseJar( data['filename'] ) return ENDOFCONNECTION return 0 if data['type'] == 'FL': jar = RecvSeq.getJar( info[1][1] ) jar.pushData( data ) if jar.isfull: p = AllPeerInfo.getPeer(info[1][0]) p.fileDict = json.loads( jar.dataStr ) netHashFilePath = os.sep.join( [config['appDataPath'],'data',\ 'nethashfile'] ) f = open( netHashFilePath, 'w' ) f.write( jar.dataStr ) f.close() logging.info( 'nethashfile created!' ) RecvSeq.releaseJar( info[1][1] ) if P2pMainWin.appInstance: l = [] for k,v in p.fileDict.iteritems(): l.append( k ) P2pMainWin.appInstance.setNetFileList( l ) return ENDOFCONNECTION else: return 0
os.mkdir(os.sep.join([config['appDataPath'], 'data', 'log'])) createConfigFile(config['appDataPath']) if not os.path.isdir(config['downloadPath']): os.mkdir(config['downloadPath']) pass #now start the application, build a qt app app = QApplication(sys.argv) createEssentialDir() initLog() Server.parseDataGet = parseDataGet Server.serverd((config['hostname'], config['port'])) myMainWin = P2pMainWin() P2pMainWin.appInstance = myMainWin P2pMainWin.parseDataGet = parseDataGet myMainWin.show() FileMgr.downloadDirPath = config['downloadPath'] FileMgr.localHashFilePath = os.sep.join([config['appDataPath'],'data',\ 'hashfile']) FileMgr.init() AllPeerInfo.port = config['port'] AllPeerInfo.parseDataGet = parseDataGet AllPeerInfo.getAllPeerFileList() #todo close all thread sys.exit(app.exec_())
os.mkdir( config['appDataPath']+os.sep+'data' ) os.mkdir( os.sep.join( [config['appDataPath'],'data','log'] ) ) createConfigFile( config['appDataPath'] ) if not os.path.isdir( config['downloadPath'] ): os.mkdir( config['downloadPath'] ) pass #now start the application, build a qt app app = QApplication( sys.argv ) createEssentialDir() initLog() Server.parseDataGet = parseDataGet Server.serverd( (config['hostname'],config['port']) ) myMainWin = P2pMainWin() P2pMainWin.appInstance = myMainWin P2pMainWin.parseDataGet = parseDataGet myMainWin.show() FileMgr.downloadDirPath = config['downloadPath'] FileMgr.localHashFilePath = os.sep.join([config['appDataPath'],'data',\ 'hashfile']) FileMgr.init() AllPeerInfo.port = config['port'] AllPeerInfo.parseDataGet = parseDataGet AllPeerInfo.getAllPeerFileList() #todo close all thread sys.exit( app.exec_() )