blob: 0d90ec942944411f62454b8e82cbad226fce433e [file] [log] [blame]
Piotr Krysik902f4eb2017-09-19 08:04:33 +02001#!/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
25import socket
26import select
27
28class UDPLink:
29 def __init__(self, remote_addr, remote_port, bind_port):
30 self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
31 self.sock.bind(('0.0.0.0', bind_port))
32 self.sock.setblocking(0)
33
34 # Save remote info
35 self.remote_addr = remote_addr
36 self.remote_port = remote_port
37
38 def loop(self):
39 r_event, w_event, x_event = select.select([self.sock], [], [])
40
41 # Check for incoming data
42 if self.sock in r_event:
43 data, addr = self.sock.recvfrom(128)
44 self.handle_rx(data.decode())
45
46 def shutdown(self):
47 self.sock.close();
48
49 def send(self, data):
50 if type(data) not in [bytearray, bytes]:
51 data = data.encode()
52
53 self.sock.sendto(data, (self.remote_addr, self.remote_port))
54
55 def handle_rx(self, data):
56 raise NotImplementedError