Easily add all supported connectivity methods to your mbed OS project
esp8266-driver/TESTS/net/host_tests/udp_shotgun.py@0:615f90842ce8, 2017-07-12 (annotated)
- Committer:
- MACRUM
- Date:
- Wed Jul 12 10:52:58 2017 +0000
- Revision:
- 0:615f90842ce8
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MACRUM | 0:615f90842ce8 | 1 | """ |
MACRUM | 0:615f90842ce8 | 2 | mbed SDK |
MACRUM | 0:615f90842ce8 | 3 | Copyright (c) 2011-2013 ARM Limited |
MACRUM | 0:615f90842ce8 | 4 | |
MACRUM | 0:615f90842ce8 | 5 | Licensed under the Apache License, Version 2.0 (the "License"); |
MACRUM | 0:615f90842ce8 | 6 | you may not use this file except in compliance with the License. |
MACRUM | 0:615f90842ce8 | 7 | You may obtain a copy of the License at |
MACRUM | 0:615f90842ce8 | 8 | |
MACRUM | 0:615f90842ce8 | 9 | http://www.apache.org/licenses/LICENSE-2.0 |
MACRUM | 0:615f90842ce8 | 10 | |
MACRUM | 0:615f90842ce8 | 11 | Unless required by applicable law or agreed to in writing, software |
MACRUM | 0:615f90842ce8 | 12 | distributed under the License is distributed on an "AS IS" BASIS, |
MACRUM | 0:615f90842ce8 | 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
MACRUM | 0:615f90842ce8 | 14 | See the License for the specific language governing permissions and |
MACRUM | 0:615f90842ce8 | 15 | limitations under the License. |
MACRUM | 0:615f90842ce8 | 16 | """ |
MACRUM | 0:615f90842ce8 | 17 | |
MACRUM | 0:615f90842ce8 | 18 | import sys |
MACRUM | 0:615f90842ce8 | 19 | import socket |
MACRUM | 0:615f90842ce8 | 20 | import json |
MACRUM | 0:615f90842ce8 | 21 | import random |
MACRUM | 0:615f90842ce8 | 22 | import itertools |
MACRUM | 0:615f90842ce8 | 23 | import time |
MACRUM | 0:615f90842ce8 | 24 | from sys import stdout |
MACRUM | 0:615f90842ce8 | 25 | from threading import Thread |
MACRUM | 0:615f90842ce8 | 26 | from SocketServer import BaseRequestHandler, UDPServer |
MACRUM | 0:615f90842ce8 | 27 | from mbed_host_tests import BaseHostTest, event_callback |
MACRUM | 0:615f90842ce8 | 28 | |
MACRUM | 0:615f90842ce8 | 29 | |
MACRUM | 0:615f90842ce8 | 30 | class UDPEchoClientHandler(BaseRequestHandler): |
MACRUM | 0:615f90842ce8 | 31 | def handle(self): |
MACRUM | 0:615f90842ce8 | 32 | """ UDP packet handler. Responds with multiple simultaneous packets |
MACRUM | 0:615f90842ce8 | 33 | """ |
MACRUM | 0:615f90842ce8 | 34 | data, sock = self.request |
MACRUM | 0:615f90842ce8 | 35 | pattern = [ord(d) << 4 for d in data] |
MACRUM | 0:615f90842ce8 | 36 | |
MACRUM | 0:615f90842ce8 | 37 | # Each byte in request indicates size of packet to recieve |
MACRUM | 0:615f90842ce8 | 38 | # Each packet size is shifted over by 4 to fit in a byte, which |
MACRUM | 0:615f90842ce8 | 39 | # avoids any issues with endianess or decoding |
MACRUM | 0:615f90842ce8 | 40 | for packet in pattern: |
MACRUM | 0:615f90842ce8 | 41 | data = [random.randint(0, 255) for _ in range(packet-1)] |
MACRUM | 0:615f90842ce8 | 42 | data.append(reduce(lambda a,b: a^b, data)) |
MACRUM | 0:615f90842ce8 | 43 | data = ''.join(map(chr, data)) |
MACRUM | 0:615f90842ce8 | 44 | sock.sendto(data, self.client_address) |
MACRUM | 0:615f90842ce8 | 45 | |
MACRUM | 0:615f90842ce8 | 46 | # Sleep a tiny bit to compensate for local network |
MACRUM | 0:615f90842ce8 | 47 | time.sleep(0.01) |
MACRUM | 0:615f90842ce8 | 48 | |
MACRUM | 0:615f90842ce8 | 49 | |
MACRUM | 0:615f90842ce8 | 50 | class UDPEchoClientTest(BaseHostTest): |
MACRUM | 0:615f90842ce8 | 51 | def __init__(self): |
MACRUM | 0:615f90842ce8 | 52 | """ |
MACRUM | 0:615f90842ce8 | 53 | Initialise test parameters. |
MACRUM | 0:615f90842ce8 | 54 | |
MACRUM | 0:615f90842ce8 | 55 | :return: |
MACRUM | 0:615f90842ce8 | 56 | """ |
MACRUM | 0:615f90842ce8 | 57 | BaseHostTest.__init__(self) |
MACRUM | 0:615f90842ce8 | 58 | self.SERVER_IP = None # Will be determined after knowing the target IP |
MACRUM | 0:615f90842ce8 | 59 | self.SERVER_PORT = 0 # Let TCPServer choose an arbitrary port |
MACRUM | 0:615f90842ce8 | 60 | self.server = None |
MACRUM | 0:615f90842ce8 | 61 | self.server_thread = None |
MACRUM | 0:615f90842ce8 | 62 | self.target_ip = None |
MACRUM | 0:615f90842ce8 | 63 | |
MACRUM | 0:615f90842ce8 | 64 | @staticmethod |
MACRUM | 0:615f90842ce8 | 65 | def find_interface_to_target_addr(target_ip): |
MACRUM | 0:615f90842ce8 | 66 | """ |
MACRUM | 0:615f90842ce8 | 67 | Finds IP address of the interface through which it is connected to the target. |
MACRUM | 0:615f90842ce8 | 68 | |
MACRUM | 0:615f90842ce8 | 69 | :return: |
MACRUM | 0:615f90842ce8 | 70 | """ |
MACRUM | 0:615f90842ce8 | 71 | s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
MACRUM | 0:615f90842ce8 | 72 | try: |
MACRUM | 0:615f90842ce8 | 73 | s.connect((target_ip, 0)) # Target IP, any port |
MACRUM | 0:615f90842ce8 | 74 | except socket.error: |
MACRUM | 0:615f90842ce8 | 75 | s.connect((target_ip, 8000)) # Target IP, 'random' port |
MACRUM | 0:615f90842ce8 | 76 | ip = s.getsockname()[0] |
MACRUM | 0:615f90842ce8 | 77 | s.close() |
MACRUM | 0:615f90842ce8 | 78 | return ip |
MACRUM | 0:615f90842ce8 | 79 | |
MACRUM | 0:615f90842ce8 | 80 | def setup_udp_server(self): |
MACRUM | 0:615f90842ce8 | 81 | """ |
MACRUM | 0:615f90842ce8 | 82 | sets up a UDP server for target to connect and send test data. |
MACRUM | 0:615f90842ce8 | 83 | |
MACRUM | 0:615f90842ce8 | 84 | :return: |
MACRUM | 0:615f90842ce8 | 85 | """ |
MACRUM | 0:615f90842ce8 | 86 | # !NOTE: There should mechanism to assert in the host test |
MACRUM | 0:615f90842ce8 | 87 | if self.SERVER_IP is None: |
MACRUM | 0:615f90842ce8 | 88 | self.log("setup_udp_server() called before determining server IP!") |
MACRUM | 0:615f90842ce8 | 89 | self.notify_complete(False) |
MACRUM | 0:615f90842ce8 | 90 | |
MACRUM | 0:615f90842ce8 | 91 | # Returning none will suppress host test from printing success code |
MACRUM | 0:615f90842ce8 | 92 | self.server = UDPServer((self.SERVER_IP, self.SERVER_PORT), UDPEchoClientHandler) |
MACRUM | 0:615f90842ce8 | 93 | ip, port = self.server.server_address |
MACRUM | 0:615f90842ce8 | 94 | self.SERVER_PORT = port |
MACRUM | 0:615f90842ce8 | 95 | self.server.allow_reuse_address = True |
MACRUM | 0:615f90842ce8 | 96 | self.log("HOST: Listening for UDP packets: " + self.SERVER_IP + ":" + str(self.SERVER_PORT)) |
MACRUM | 0:615f90842ce8 | 97 | self.server_thread = Thread(target=UDPEchoClientTest.server_thread_func, args=(self,)) |
MACRUM | 0:615f90842ce8 | 98 | self.server_thread.start() |
MACRUM | 0:615f90842ce8 | 99 | |
MACRUM | 0:615f90842ce8 | 100 | @staticmethod |
MACRUM | 0:615f90842ce8 | 101 | def server_thread_func(this): |
MACRUM | 0:615f90842ce8 | 102 | """ |
MACRUM | 0:615f90842ce8 | 103 | Thread function to run TCP server forever. |
MACRUM | 0:615f90842ce8 | 104 | |
MACRUM | 0:615f90842ce8 | 105 | :param this: |
MACRUM | 0:615f90842ce8 | 106 | :return: |
MACRUM | 0:615f90842ce8 | 107 | """ |
MACRUM | 0:615f90842ce8 | 108 | this.server.serve_forever() |
MACRUM | 0:615f90842ce8 | 109 | |
MACRUM | 0:615f90842ce8 | 110 | @event_callback("target_ip") |
MACRUM | 0:615f90842ce8 | 111 | def _callback_target_ip(self, key, value, timestamp): |
MACRUM | 0:615f90842ce8 | 112 | """ |
MACRUM | 0:615f90842ce8 | 113 | Callback to handle reception of target's IP address. |
MACRUM | 0:615f90842ce8 | 114 | |
MACRUM | 0:615f90842ce8 | 115 | :param key: |
MACRUM | 0:615f90842ce8 | 116 | :param value: |
MACRUM | 0:615f90842ce8 | 117 | :param timestamp: |
MACRUM | 0:615f90842ce8 | 118 | :return: |
MACRUM | 0:615f90842ce8 | 119 | """ |
MACRUM | 0:615f90842ce8 | 120 | self.target_ip = value |
MACRUM | 0:615f90842ce8 | 121 | self.SERVER_IP = self.find_interface_to_target_addr(self.target_ip) |
MACRUM | 0:615f90842ce8 | 122 | self.setup_udp_server() |
MACRUM | 0:615f90842ce8 | 123 | |
MACRUM | 0:615f90842ce8 | 124 | @event_callback("host_ip") |
MACRUM | 0:615f90842ce8 | 125 | def _callback_host_ip(self, key, value, timestamp): |
MACRUM | 0:615f90842ce8 | 126 | """ |
MACRUM | 0:615f90842ce8 | 127 | Callback for request for host IP Addr |
MACRUM | 0:615f90842ce8 | 128 | |
MACRUM | 0:615f90842ce8 | 129 | """ |
MACRUM | 0:615f90842ce8 | 130 | self.send_kv("host_ip", self.SERVER_IP) |
MACRUM | 0:615f90842ce8 | 131 | |
MACRUM | 0:615f90842ce8 | 132 | @event_callback("host_port") |
MACRUM | 0:615f90842ce8 | 133 | def _callback_host_port(self, key, value, timestamp): |
MACRUM | 0:615f90842ce8 | 134 | """ |
MACRUM | 0:615f90842ce8 | 135 | Callback for request for host port |
MACRUM | 0:615f90842ce8 | 136 | """ |
MACRUM | 0:615f90842ce8 | 137 | self.send_kv("host_port", self.SERVER_PORT) |
MACRUM | 0:615f90842ce8 | 138 | |
MACRUM | 0:615f90842ce8 | 139 | def teardown(self): |
MACRUM | 0:615f90842ce8 | 140 | if self.server: |
MACRUM | 0:615f90842ce8 | 141 | self.server.shutdown() |
MACRUM | 0:615f90842ce8 | 142 | self.server_thread.join() |