blob: cedcb1b0fc39e70699bf59f7ece1b40926efbb1f [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
Vadim Yanitskiy873e44e2017-10-17 06:52:01 +070028class udp_link:
Piotr Krysik902f4eb2017-09-19 08:04:33 +020029 def __init__(self, remote_addr, remote_port, bind_port):
30 self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
Piotr Krysik3dd981c2017-12-01 11:59:32 +010031 self.sock.bind((remote_addr, bind_port))
Piotr Krysik902f4eb2017-09-19 08:04:33 +020032 self.sock.setblocking(0)
33
34 # Save remote info
35 self.remote_addr = remote_addr
36 self.remote_port = remote_port
37
Vadim Yanitskiybf6f6ec2018-08-09 19:17:57 +070038 def __del__(self):
39 self.sock.close()
40
Piotr Krysik902f4eb2017-09-19 08:04:33 +020041 def loop(self):
42 r_event, w_event, x_event = select.select([self.sock], [], [])
43
44 # Check for incoming data
45 if self.sock in r_event:
46 data, addr = self.sock.recvfrom(128)
47 self.handle_rx(data.decode())
48
Piotr Krysik902f4eb2017-09-19 08:04:33 +020049 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