Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python2 |
| 2 | # -*- coding: utf-8 -*- |
| 3 | |
| 4 | # GR-GSM based transceiver |
| 5 | # UDP link implementation |
| 6 | # |
| 7 | # (C) 2017 by Vadim Yanitskiy <axilirator@gmail.com> |
| 8 | # |
| 9 | # All Rights Reserved |
| 10 | # |
| 11 | # This program is free software; you can redistribute it and/or modify |
| 12 | # it under the terms of the GNU General Public License as published by |
| 13 | # the Free Software Foundation; either version 2 of the License, or |
| 14 | # (at your option) any later version. |
| 15 | # |
| 16 | # This program is distributed in the hope that it will be useful, |
| 17 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | # GNU General Public License for more details. |
| 20 | # |
| 21 | # You should have received a copy of the GNU General Public License along |
| 22 | # with this program; if not, write to the Free Software Foundation, Inc., |
| 23 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 24 | |
| 25 | import socket |
| 26 | import select |
| 27 | |
Vadim Yanitskiy | f237f1a | 2018-12-20 09:49:56 +0700 | [diff] [blame] | 28 | class UDPLink: |
Vadim Yanitskiy | 473b35b | 2018-08-10 00:20:03 +0700 | [diff] [blame] | 29 | def __init__(self, remote_addr, remote_port, bind_addr = '0.0.0.0', bind_port = 0): |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 30 | self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
Vadim Yanitskiy | b085a2c | 2018-08-09 19:20:52 +0700 | [diff] [blame] | 31 | self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
Vadim Yanitskiy | 473b35b | 2018-08-10 00:20:03 +0700 | [diff] [blame] | 32 | self.sock.bind((bind_addr, bind_port)) |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 33 | self.sock.setblocking(0) |
| 34 | |
| 35 | # Save remote info |
| 36 | self.remote_addr = remote_addr |
| 37 | self.remote_port = remote_port |
| 38 | |
Vadim Yanitskiy | bf6f6ec | 2018-08-09 19:17:57 +0700 | [diff] [blame] | 39 | def __del__(self): |
| 40 | self.sock.close() |
| 41 | |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 42 | def loop(self): |
| 43 | r_event, w_event, x_event = select.select([self.sock], [], []) |
| 44 | |
| 45 | # Check for incoming data |
| 46 | if self.sock in r_event: |
| 47 | data, addr = self.sock.recvfrom(128) |
Vadim Yanitskiy | 0e24637 | 2018-08-09 19:30:39 +0700 | [diff] [blame] | 48 | self.handle_rx(data.decode(), addr) |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 49 | |
Vadim Yanitskiy | 2adbee4 | 2018-08-10 00:51:36 +0700 | [diff] [blame] | 50 | def desc_link(self): |
| 51 | (bind_addr, bind_port) = self.sock.getsockname() |
| 52 | |
| 53 | return "L:%s:%u <-> R:%s:%u" \ |
| 54 | % (bind_addr, bind_port, self.remote_addr, self.remote_port) |
| 55 | |
Vadim Yanitskiy | 0e24637 | 2018-08-09 19:30:39 +0700 | [diff] [blame] | 56 | def send(self, data, remote = None): |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 57 | if type(data) not in [bytearray, bytes]: |
| 58 | data = data.encode() |
| 59 | |
Vadim Yanitskiy | 0e24637 | 2018-08-09 19:30:39 +0700 | [diff] [blame] | 60 | if remote is None: |
| 61 | remote = (self.remote_addr, self.remote_port) |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 62 | |
Vadim Yanitskiy | 0e24637 | 2018-08-09 19:30:39 +0700 | [diff] [blame] | 63 | self.sock.sendto(data, remote) |
| 64 | |
| 65 | def handle_rx(self, data, remote): |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 66 | raise NotImplementedError |