<s> """<STR_LIT>""" <EOL> from twisted . news import nntp <EOL> from twisted . internet import protocol , reactor <EOL> import time <EOL> class NNTPFactory ( protocol . ServerFactory ) : <EOL> """<STR_LIT>""" <EOL> protocol = nntp . NNTPServer <EOL> def __init__ ( self , backend ) : <EOL> self . backend = backend <EOL> def buildProtocol ( self , connection ) : <EOL> p = self . protocol ( ) <EOL> p . factory = self <EOL> return p <EOL> class UsenetClientFactory ( protocol . ClientFactory ) : <EOL> def __init__ ( self , groups , storage ) : <EOL> self . lastChecks = { } <EOL> self . groups = groups <EOL> self . storage = storage <EOL> def clientConnectionLost ( self , connector , reason ) : <EOL> pass <EOL> def clientConnectionFailed ( self , connector , reason ) : <EOL> print '<STR_LIT>' , reason <EOL> def updateChecks ( self , addr ) : <EOL> self . lastChecks [ addr ] = time . mktime ( time . gmtime ( ) ) <EOL> def buildProtocol ( self , addr ) : <EOL> last = self . lastChecks . setdefault ( addr , time . mktime ( time . gmtime ( ) ) - ( <NUM_LIT> * <NUM_LIT> * <NUM_LIT> * <NUM_LIT:7> ) ) <EOL> p = nntp . UsenetClientProtocol ( self . groups , last , self . storage ) <EOL> p . factory = self <EOL> return p <EOL> class UsenetServerFactory ( NNTPFactory ) : <EOL> """<STR_LIT>""" <EOL> protocol = nntp . NNTPServer <EOL> def __init__ ( self , backend , remoteHosts = None , updatePeriod = <NUM_LIT> ) : <EOL> NNTPFactory . __init__ ( self , backend ) <EOL> self . updatePeriod = updatePeriod <EOL> self . remoteHosts = remoteHosts or [ ] <EOL> self . clientFactory = UsenetClientFactory ( self . remoteHosts , self . backend ) <EOL> def startFactory ( self ) : <EOL> self . _updateCall = reactor . callLater ( <NUM_LIT:0> , self . syncWithRemotes ) <EOL> def stopFactory ( self ) : <EOL> if self . _updateCall : <EOL> self . _updateCall . cancel ( ) <EOL> self . _updateCall = None <EOL> def buildProtocol ( self , connection ) : <EOL> p = self . protocol ( ) <EOL> p . factory = self <EOL> return p <EOL> def syncWithRemotes ( self ) : <EOL> for remote in self . remoteHosts : <EOL> reactor . connectTCP ( remote , <NUM_LIT> , self . clientFactory ) <EOL> self . _updateCall = reactor . callLater ( self . updatePeriod , self . syncWithRemotes ) <EOL> Factory = UsenetServerFactory </s>
