~bernanet84/bordergateway/BorderGateway

« back to all changes in this revision

Viewing changes to UdpServer.py

  • Committer: Luca Bernardini
  • Date: 2010-04-19 16:21:45 UTC
  • Revision ID: luca@luca-laptop-20100419162145-uivu5nlysugexrnv
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# 2010 Luca Bernardini
 
2
# UdpServer.py  berna
 
3
 
 
4
 
 
5
from warnings import filterwarnings
 
6
from twisted.internet import reactor
 
7
from twisted.internet.protocol import DatagramProtocol
 
8
from socket import inet_aton
 
9
from datetime import datetime
 
10
from traceback import print_exc
 
11
from sys import stdout
 
12
 
 
13
class Udp_server(DatagramProtocol):
 
14
    data_callback = None
 
15
 
 
16
    def __init__(self, address = None, data_callback = None):
 
17
        self.data_callback = data_callback
 
18
        if address == None:
 
19
            reactor.listenUDP(0, self)
 
20
        else:
 
21
            reactor.listenUDP(address[1], self, address[0])
 
22
        filterwarnings('ignore', '^Please only pass')
 
23
 
 
24
    def send_to(self, data, address):
 
25
        try:
 
26
            inet_aton(address[0])
 
27
        except:
 
28
            reactor.callInThread(self.transport.write, data, address)
 
29
            return
 
30
        #self.transport.write(data, address)
 
31
        reactor.callInThread(self.transport.write, data, address)
 
32
 
 
33
    def datagramReceived(self, data, address):
 
34
        if self.data_callback != None:
 
35
            try:
 
36
                self.data_callback(data, address, self)
 
37
                #reactor.callInThread(self.data_callback, data, address, self)
 
38
            except:
 
39
                print datetime.now(), '** Udp_server: unhandled exception in incoming data callback'
 
40
                print '-' * 70
 
41
                print_exc(file = stdout)
 
42
                print '-' * 70
 
43
                stdout.flush()
 
44
 
 
45
 
 
46
### CODICE DI TEST
 
47
 
 
48
class Container():
 
49
   
 
50
    def __init__(self):
 
51
        self.x = {}
 
52
    
 
53
    def putV(self, k, v):
 
54
        self.x[k]=v
 
55
    
 
56
    def getV(self, k):
 
57
        return self.x[k]
 
58
 
 
59
    def ping_received(self, data, address, udp_server):
 
60
        if not (data == 'ping!' and address == ('127.0.0.1', 54321)):
 
61
            return
 
62
        print 'ping_received'
 
63
        #reactor.callInThread(aSillyBlockingMethod, "2 seconds have passed")
 
64
        udp_server.send_to('pong!', address)
 
65
 
 
66
    def pong_received(self, data, address, udp_server):
 
67
        print 'pong_received'
 
68
        if not (data == 'pong!' and address == ('127.0.0.1', 12345)):
 
69
            return
 
70
        reactor.callFromThread(reactor.stop)
 
71
        #reactor.stop()
 
72
    
 
73
def aSillyBlockingMethod(x):       
 
74
        print x
 
75
        time.sleep(1)
 
76
        print x
 
77
 
 
78
if __name__ == '__main__':
 
79
    import time
 
80
    c = Container()
 
81
    udp_server_ping = Udp_server(('127.0.0.1', 12345), c.ping_received)
 
82
    udp_server_pong = Udp_server(('127.0.0.1', 54321), c.pong_received)
 
83
    udp_server_pong.send_to('ping!', ('127.0.0.1', 12345))
 
84
 
 
85
  # Thread(name='Reactor Thread', target=self._run_reactor).start()
 
86
 
 
87
    # run method in thread
 
88
    #reactor.callInThread(aSillyBlockingMethod, "2 seconds have passed")
 
89
    reactor.run()