File: Twisted.py

package info (click to toggle)
tilecache 2.11%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 532 kB
  • ctags: 423
  • sloc: python: 3,086; makefile: 90
file content (35 lines) | stat: -rw-r--r-- 1,091 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# BSD Licensed, Copyright (c) 2006-2010 TileCache Contributors

from Swarm import Message, Client
from twisted.internet.protocol import DatagramProtocol
from twisted.web import getPage
from twisted.internet import reactor

class TwistedClient (DatagramProtocol, Client):
    directoryURL = "..."

    def __init__ (self, service, key, weight = 10.0, *args, **kwargs):
        DatagramProtocol.__init__(self, *args, **kwargs)
        Client.__init__(self, service, key, None, weight)
    
    def datagramReceived(self, data, (host, port)):
        self.handle(data, (host, port))

    def send (self, peer, msg):
        self.transport.write( msg.freeze(), peer.address )
    
    def schedule (self, when, callback, *args):
        reactor.callLater(time.time() + when, callback, *args)

    def load_peers (self):
        d = getPage(self.directoryURL)
        d.addCallback(self.load_peers_from_string)
        self.schedule(15.0, self.load_peers)

    def startProtocol (self):
        self.load_peers()

if __name__ == '__main__':
    reactor.listenUDP(5150, Echo())
    reactor.run()