1
# Copyright (c) 2001-2004 Twisted Matrix Laboratories.
2
# See LICENSE for details.
6
Maintainer: U{Jp Calderone<mailto:exarkun@twistedmatrix.com>}
11
from twisted.news import nntp
12
from twisted.internet import protocol, reactor
16
class NNTPFactory(protocol.ServerFactory):
17
"""A factory for NNTP server protocols."""
19
protocol = nntp.NNTPServer
21
def __init__(self, backend):
22
self.backend = backend
24
def buildProtocol(self, connection):
30
class UsenetClientFactory(protocol.ClientFactory):
31
def __init__(self, groups, storage):
34
self.storage = storage
37
def clientConnectionLost(self, connector, reason):
41
def clientConnectionFailed(self, connector, reason):
42
print 'Connection failed: ', reason
45
def updateChecks(self, addr):
46
self.lastChecks[addr] = time.mktime(time.gmtime())
49
def buildProtocol(self, addr):
50
last = self.lastChecks.setdefault(addr, time.mktime(time.gmtime()) - (60 * 60 * 24 * 7))
51
p = nntp.UsenetClientProtocol(self.groups, last, self.storage)
56
# XXX - Maybe this inheritence doesn't make so much sense?
57
class UsenetServerFactory(NNTPFactory):
58
"""A factory for NNTP Usenet server protocols."""
60
protocol = nntp.NNTPServer
62
def __init__(self, backend, remoteHosts = None, updatePeriod = 60):
63
NNTPFactory.__init__(self, backend)
64
self.updatePeriod = updatePeriod
65
self.remoteHosts = remoteHosts or []
66
self.clientFactory = UsenetClientFactory(self.remoteHosts, self.backend)
69
def startFactory(self):
70
self._updateCall = reactor.callLater(0, self.syncWithRemotes)
73
def stopFactory(self):
75
self._updateCall.cancel()
76
self._updateCall = None
79
def buildProtocol(self, connection):
85
def syncWithRemotes(self):
86
for remote in self.remoteHosts:
87
reactor.connectTCP(remote, 119, self.clientFactory)
88
self._updateCall = reactor.callLater(self.updatePeriod, self.syncWithRemotes)
91
# backwards compatability
92
Factory = UsenetServerFactory