本文整理汇总了Python中status.Status.getLocalFilesDict方法的典型用法代码示例。如果您正苦于以下问题:Python Status.getLocalFilesDict方法的具体用法?Python Status.getLocalFilesDict怎么用?Python Status.getLocalFilesDict使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类status.Status
的用法示例。
在下文中一共展示了Status.getLocalFilesDict方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Peer
# 需要导入模块: from status import Status [as 别名]
# 或者: from status.Status import getLocalFilesDict [as 别名]
#.........这里部分代码省略.........
else:
logging.error( 'Invalid command: ' + command )
else:
logging.error( 'Invalid packageType; package ignored' )
def shareChunks( self ):
"""Send out a chunk to each registered peer"""
for p in self.activePeers:
peer = self.getPeerTuple( p )
context, data = self.status.getChunkToSend( p )
if data is None:
continue # no chunks to share with that peer
context['type'] = 'chunk'
if self.sender.sendData( peer[0], peer[1], context, data ):
self.status.markChunkSent( p,context['filename'], context['chunkNum'] )
self.activePeers[ p ] = 0
else:
logging.error( 'Failed to send packet to: ' + p )
# increment number of failures
self.activePeers[ p ] += 1
# Remove any peers that failed too many times
self.checkPeerFailures()
def massQuery( self ):
"""Send out query message to all peers to get updated status"""
for p in self.activePeers:
if p in self.waitingQueries:
# Still waiting for reply from last query; skip
continue
peer = self.getPeerTuple( p )
if self.sender.sendMessage( peer[0], peer[1], 'query' ):
self.activePeers[ p ] = 0
self.waitingQueries.append( p )
else :
logging.error( 'Failed to send query meessage to: ' + p )
self.activePeers[ p ] += 1
# Remove any peers that failed too many times
self.checkPeerFailures()
def sendStatus( self, peer, requestReply ):
"""Send out status message to given peer"""
context = { 'type' : 'status', 'reply' : requestReply }
data = self.status.getLocalFilesDict()
return self.sender.sendData( peer[0], peer[1], context, data )
def checkPeerFailures( self ):
"""
Check the number of consecutive failures per peer and remove
peers with > MAXFAILURES
"""
for p in self.activePeers.keys():
if self.activePeers[ p ] >= MAXFAILURES:
logging.info( 'MAXFAILURES reached, removing peer : ' + p )
del self.activePeers[ p ]
self.status.removePeer( p )
@staticmethod
def getPeerKey( host, port ):
"""
Returns a string for use as a dictionary key
They key is simply the host and port concatenated
"""
if not host or not port:
logging.error('Invalid host or port provided to getPeerKey')
raise TypeError('Invalid host or port')
return host + ':' + str( port )
@staticmethod
def getPeerTuple( peerKey ):
"""
Returns a host, port tuple extracted from a peerKey
"""
if not peerKey or not len(peerKey) > 0:
logging.error('Invalid peerKey provided to getPeerTuple')
raise TypeError('Invalid peerKey')
vals = peerKey.split(':')
return ( vals[0], int( vals[1] ) )
def getAllPeerKeys( self ):
"""
Returns a list of all peer keys in the form of
a string of the format: 'peer:host'
"""
peerKeys = []
for p in self.peers:
peerKeys.append( getPeerKey( p[0], p[1] ) )
return peerKeys