~certify-web-dev/twisted/certify-trunk

« back to all changes in this revision

Viewing changes to twisted/news/news.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2007-01-17 14:52:35 UTC
  • mfrom: (1.1.5 upstream) (2.1.2 etch)
  • Revision ID: james.westby@ubuntu.com-20070117145235-btmig6qfmqfen0om
Tags: 2.5.0-0ubuntu1
New upstream version, compatible with python2.5.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) 2001-2004 Twisted Matrix Laboratories.
 
2
# See LICENSE for details.
 
3
 
 
4
 
 
5
"""
 
6
Maintainer: U{Jp Calderone<mailto:exarkun@twistedmatrix.com>}
 
7
 
 
8
Stability: semi-stable
 
9
"""
 
10
 
 
11
from twisted.news import nntp
 
12
from twisted.internet import protocol, reactor
 
13
 
 
14
import time
 
15
 
 
16
class NNTPFactory(protocol.ServerFactory):
 
17
    """A factory for NNTP server protocols."""
 
18
    
 
19
    protocol = nntp.NNTPServer
 
20
    
 
21
    def __init__(self, backend):
 
22
        self.backend = backend
 
23
    
 
24
    def buildProtocol(self, connection):
 
25
        p = self.protocol()
 
26
        p.factory = self
 
27
        return p
 
28
 
 
29
 
 
30
class UsenetClientFactory(protocol.ClientFactory):
 
31
    def __init__(self, groups, storage):
 
32
        self.lastChecks = {}
 
33
        self.groups = groups
 
34
        self.storage = storage
 
35
 
 
36
 
 
37
    def clientConnectionLost(self, connector, reason):
 
38
        pass
 
39
 
 
40
 
 
41
    def clientConnectionFailed(self, connector, reason):
 
42
        print 'Connection failed: ', reason
 
43
    
 
44
    
 
45
    def updateChecks(self, addr):
 
46
        self.lastChecks[addr] = time.mktime(time.gmtime())
 
47
 
 
48
 
 
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)
 
52
        p.factory = self
 
53
        return p
 
54
 
 
55
 
 
56
# XXX - Maybe this inheritence doesn't make so much sense?
 
57
class UsenetServerFactory(NNTPFactory):
 
58
    """A factory for NNTP Usenet server protocols."""
 
59
 
 
60
    protocol = nntp.NNTPServer
 
61
 
 
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)
 
67
 
 
68
 
 
69
    def startFactory(self):
 
70
        self._updateCall = reactor.callLater(0, self.syncWithRemotes)
 
71
 
 
72
 
 
73
    def stopFactory(self):
 
74
        if self._updateCall:
 
75
            self._updateCall.cancel()
 
76
            self._updateCall = None
 
77
 
 
78
 
 
79
    def buildProtocol(self, connection):
 
80
        p = self.protocol()
 
81
        p.factory = self
 
82
        return p
 
83
 
 
84
 
 
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)
 
89
 
 
90
 
 
91
# backwards compatability
 
92
Factory = UsenetServerFactory