treewide: remove FSF address
[osmocom-bb.git] / src / target / trx_toolkit / udp_link.py
blobc3f14765c28a5c0a900b41c5e0b79a1391dd8e09
1 # -*- coding: utf-8 -*-
3 # TRX Toolkit
4 # UDP link implementation
6 # (C) 2017 by Vadim Yanitskiy <axilirator@gmail.com>
8 # All Rights Reserved
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 import socket
22 class UDPLink:
23 def __init__(self, remote_addr, remote_port, bind_addr = '0.0.0.0', bind_port = 0):
24 self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
25 self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
26 self.sock.bind((bind_addr, bind_port))
27 self.sock.setblocking(False)
29 # Save remote info
30 self.remote_addr = remote_addr
31 self.remote_port = remote_port
33 def __del__(self):
34 self.sock.close()
36 def desc_link(self):
37 (bind_addr, bind_port) = self.sock.getsockname()
39 return "L:%s:%u <-> R:%s:%u" \
40 % (bind_addr, bind_port, self.remote_addr, self.remote_port)
42 def send(self, data):
43 if type(data) not in [bytearray, bytes]:
44 data = data.encode()
46 self.sock.sendto(data, (self.remote_addr, self.remote_port))
48 def sendto(self, data, remote):
49 if type(data) not in [bytearray, bytes]:
50 data = data.encode()
52 self.sock.sendto(data, remote)