Package brisa :: Package core :: Module network_senders
[hide private]
[frames] | no frames]

Source Code for Module brisa.core.network_senders

  1  # Licensed under the MIT license 
  2  # http://opensource.org/licenses/mit-license.php or see LICENSE file. 
  3  # Copyright 2007-2008 Brisa Team <brisa-develop@garage.maemo.org> 
  4   
  5  """ Facilities for sending UDP datagrams and TCP messages. 
  6  """ 
  7   
  8  import socket 
  9  import threading 
 10   
 11  from brisa.core import log 
 12  from brisa.core.threaded_call import run_async_function 
 13   
 14   
15 -class UDPTransport(object):
16 """ Provides methods for sending UDP datagrams. 17 """ 18
19 - def __init__(self, TTL=2):
20 """ Constructor for the UDPTransport. 21 22 @param TTL: time to live. Default is 2 23 @type TTL: integer 24 """ 25 self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 26 socket.IPPROTO_UDP) 27 self.set_TTL(TTL)
28
29 - def set_TTL(self, TTL):
30 """ Setter for the time to live parameter. 31 32 @param TTL: time to live 33 @type TTL: integer 34 """ 35 self.socket.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, TTL)
36
37 - def send_data(self, data, host, port):
38 """ Sends an UDP datagram to the address specified. 39 40 @param data: raw data 41 @param host: target host 42 @param port: target port 43 44 @type data: string 45 @type host: string 46 @type port: integer 47 """ 48 self.socket.sendto(data, (host, port))
49
50 - def send_delayed(self, delay, data, host, port):
51 """ Sends an UDP datagram to the address specified after the delay. 52 53 @param delay: delay to wait before sending 54 @param data: raw data 55 @param host: target host 56 @param port: target port 57 58 @type delay: float 59 @type data: string 60 @type host: string 61 @type port: integer 62 """ 63 t = threading.Timer(delay, self.send_data, args=[data, host, port]) 64 t.start()
65 66
67 -class TCPTransport(object):
68 # TODO fixme to use thread manager fd facility 69 """ Provides methods for sending data through TCP. Receiving host must be 70 listening for connections. 71 """ 72
73 - def __init__(self):
74 """ Constructor for the TCPTransport class. 75 """ 76 self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
77
78 - def send_data(self, data, (host, port)):
79 """ Sends data to the specified address. This is a non-blocking method. 80 81 @param data: raw data 82 @param host: target host 83 @param port: target port 84 85 @type data: string 86 @type host: string 87 @type port: integer 88 """ 89 run_async_function(self._send_data, (data, (host, port)))
90
91 - def _send_data(self, data, (host, port)):
92 """ Sends data to the specified address (implementation). If used 93 directly will block the thread until it is complete. 94 """ 95 try: 96 self.socket.connect((host, port)) 97 self.socket.send(data) 98 self.socket.close() 99 except socket.error, e: 100 log.debug('TCPTransport: couldn\'t connect to %s:%d to send data \ 101 - %s' % (host, port, e.message)) 102 return
103
104 - def connect_and_feed(self, feeder, (host, port)):
105 """ Connects to the specified address and feeds it with data from the 106 feeder. Note that feeder is supposed to be a generator. 107 108 @param feeder: data generator for feeding 109 @param host: target host 110 @param port: target port 111 112 @type feeder: generator 113 @type host: string 114 @type port: integer 115 """ 116 try: 117 self.socket.connect((host, port)) 118 for feed in feeder.next(): 119 self.socket.send(feed) 120 self.socket.close() 121 except socket.error, e: 122 log.debug('TCPTransport: error while feeding socket - %s' 123 % e.message)
124