1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
6 Maintainer: Jp Calderone
9 from twisted.news import nntp
10 from twisted.internet import protocol, reactor
14 class NNTPFactory(protocol.ServerFactory):
15 """A factory for NNTP server protocols."""
17 protocol = nntp.NNTPServer
19 def __init__(self, backend):
20 self.backend = backend
22 def buildProtocol(self, connection):
28 class UsenetClientFactory(protocol.ClientFactory):
29 def __init__(self, groups, storage):
32 self.storage = storage
35 def clientConnectionLost(self, connector, reason):
39 def clientConnectionFailed(self, connector, reason):
40 print 'Connection failed: ', reason
43 def updateChecks(self, addr):
44 self.lastChecks[addr] = time.mktime(time.gmtime())
47 def buildProtocol(self, addr):
48 last = self.lastChecks.setdefault(addr, time.mktime(time.gmtime()) - (60 * 60 * 24 * 7))
49 p = nntp.UsenetClientProtocol(self.groups, last, self.storage)
54 # XXX - Maybe this inheritence doesn't make so much sense?
55 class UsenetServerFactory(NNTPFactory):
56 """A factory for NNTP Usenet server protocols."""
58 protocol = nntp.NNTPServer
60 def __init__(self, backend, remoteHosts = None, updatePeriod = 60):
61 NNTPFactory.__init__(self, backend)
62 self.updatePeriod = updatePeriod
63 self.remoteHosts = remoteHosts or []
64 self.clientFactory = UsenetClientFactory(self.remoteHosts, self.backend)
67 def startFactory(self):
68 self._updateCall = reactor.callLater(0, self.syncWithRemotes)
71 def stopFactory(self):
73 self._updateCall.cancel()
74 self._updateCall = None
77 def buildProtocol(self, connection):
83 def syncWithRemotes(self):
84 for remote in self.remoteHosts:
85 reactor.connectTCP(remote, 119, self.clientFactory)
86 self._updateCall = reactor.callLater(self.updatePeriod, self.syncWithRemotes)
89 # backwards compatability
90 Factory = UsenetServerFactory