Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
esp8266-driver/TESTS/net/host_tests/udp_echo.py@0:8f8e8f3cbd1c, 2018-06-21 (annotated)
- Committer:
- mayur098
- Date:
- Thu Jun 21 17:50:21 2018 +0000
- Revision:
- 0:8f8e8f3cbd1c
first commit;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mayur098 | 0:8f8e8f3cbd1c | 1 | """ |
mayur098 | 0:8f8e8f3cbd1c | 2 | mbed SDK |
mayur098 | 0:8f8e8f3cbd1c | 3 | Copyright (c) 2011-2013 ARM Limited |
mayur098 | 0:8f8e8f3cbd1c | 4 | |
mayur098 | 0:8f8e8f3cbd1c | 5 | Licensed under the Apache License, Version 2.0 (the "License"); |
mayur098 | 0:8f8e8f3cbd1c | 6 | you may not use this file except in compliance with the License. |
mayur098 | 0:8f8e8f3cbd1c | 7 | You may obtain a copy of the License at |
mayur098 | 0:8f8e8f3cbd1c | 8 | |
mayur098 | 0:8f8e8f3cbd1c | 9 | http://www.apache.org/licenses/LICENSE-2.0 |
mayur098 | 0:8f8e8f3cbd1c | 10 | |
mayur098 | 0:8f8e8f3cbd1c | 11 | Unless required by applicable law or agreed to in writing, software |
mayur098 | 0:8f8e8f3cbd1c | 12 | distributed under the License is distributed on an "AS IS" BASIS, |
mayur098 | 0:8f8e8f3cbd1c | 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mayur098 | 0:8f8e8f3cbd1c | 14 | See the License for the specific language governing permissions and |
mayur098 | 0:8f8e8f3cbd1c | 15 | limitations under the License. |
mayur098 | 0:8f8e8f3cbd1c | 16 | """ |
mayur098 | 0:8f8e8f3cbd1c | 17 | |
mayur098 | 0:8f8e8f3cbd1c | 18 | import sys |
mayur098 | 0:8f8e8f3cbd1c | 19 | import socket |
mayur098 | 0:8f8e8f3cbd1c | 20 | from sys import stdout |
mayur098 | 0:8f8e8f3cbd1c | 21 | from threading import Thread |
mayur098 | 0:8f8e8f3cbd1c | 22 | from SocketServer import BaseRequestHandler, UDPServer |
mayur098 | 0:8f8e8f3cbd1c | 23 | from mbed_host_tests import BaseHostTest, event_callback |
mayur098 | 0:8f8e8f3cbd1c | 24 | |
mayur098 | 0:8f8e8f3cbd1c | 25 | |
mayur098 | 0:8f8e8f3cbd1c | 26 | class UDPEchoClientHandler(BaseRequestHandler): |
mayur098 | 0:8f8e8f3cbd1c | 27 | def handle(self): |
mayur098 | 0:8f8e8f3cbd1c | 28 | """ UDP packet handler. Echoes data back to sender's address. |
mayur098 | 0:8f8e8f3cbd1c | 29 | """ |
mayur098 | 0:8f8e8f3cbd1c | 30 | data, sock = self.request |
mayur098 | 0:8f8e8f3cbd1c | 31 | sock.sendto(data, self.client_address) |
mayur098 | 0:8f8e8f3cbd1c | 32 | |
mayur098 | 0:8f8e8f3cbd1c | 33 | |
mayur098 | 0:8f8e8f3cbd1c | 34 | class UDPEchoClientTest(BaseHostTest): |
mayur098 | 0:8f8e8f3cbd1c | 35 | |
mayur098 | 0:8f8e8f3cbd1c | 36 | def __init__(self): |
mayur098 | 0:8f8e8f3cbd1c | 37 | """ |
mayur098 | 0:8f8e8f3cbd1c | 38 | Initialise test parameters. |
mayur098 | 0:8f8e8f3cbd1c | 39 | |
mayur098 | 0:8f8e8f3cbd1c | 40 | :return: |
mayur098 | 0:8f8e8f3cbd1c | 41 | """ |
mayur098 | 0:8f8e8f3cbd1c | 42 | BaseHostTest.__init__(self) |
mayur098 | 0:8f8e8f3cbd1c | 43 | self.SERVER_IP = None # Will be determined after knowing the target IP |
mayur098 | 0:8f8e8f3cbd1c | 44 | self.SERVER_PORT = 0 # Let TCPServer choose an arbitrary port |
mayur098 | 0:8f8e8f3cbd1c | 45 | self.server = None |
mayur098 | 0:8f8e8f3cbd1c | 46 | self.server_thread = None |
mayur098 | 0:8f8e8f3cbd1c | 47 | self.target_ip = None |
mayur098 | 0:8f8e8f3cbd1c | 48 | |
mayur098 | 0:8f8e8f3cbd1c | 49 | @staticmethod |
mayur098 | 0:8f8e8f3cbd1c | 50 | def find_interface_to_target_addr(target_ip): |
mayur098 | 0:8f8e8f3cbd1c | 51 | """ |
mayur098 | 0:8f8e8f3cbd1c | 52 | Finds IP address of the interface through which it is connected to the target. |
mayur098 | 0:8f8e8f3cbd1c | 53 | |
mayur098 | 0:8f8e8f3cbd1c | 54 | :return: |
mayur098 | 0:8f8e8f3cbd1c | 55 | """ |
mayur098 | 0:8f8e8f3cbd1c | 56 | s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
mayur098 | 0:8f8e8f3cbd1c | 57 | try: |
mayur098 | 0:8f8e8f3cbd1c | 58 | s.connect((target_ip, 0)) # Target IP, any port |
mayur098 | 0:8f8e8f3cbd1c | 59 | except socket.error: |
mayur098 | 0:8f8e8f3cbd1c | 60 | s.connect((target_ip, 8000)) # Target IP, 'random' port |
mayur098 | 0:8f8e8f3cbd1c | 61 | ip = s.getsockname()[0] |
mayur098 | 0:8f8e8f3cbd1c | 62 | s.close() |
mayur098 | 0:8f8e8f3cbd1c | 63 | return ip |
mayur098 | 0:8f8e8f3cbd1c | 64 | |
mayur098 | 0:8f8e8f3cbd1c | 65 | def setup_udp_server(self): |
mayur098 | 0:8f8e8f3cbd1c | 66 | """ |
mayur098 | 0:8f8e8f3cbd1c | 67 | sets up a UDP server for target to connect and send test data. |
mayur098 | 0:8f8e8f3cbd1c | 68 | |
mayur098 | 0:8f8e8f3cbd1c | 69 | :return: |
mayur098 | 0:8f8e8f3cbd1c | 70 | """ |
mayur098 | 0:8f8e8f3cbd1c | 71 | # !NOTE: There should mechanism to assert in the host test |
mayur098 | 0:8f8e8f3cbd1c | 72 | if self.SERVER_IP is None: |
mayur098 | 0:8f8e8f3cbd1c | 73 | self.log("setup_udp_server() called before determining server IP!") |
mayur098 | 0:8f8e8f3cbd1c | 74 | self.notify_complete(False) |
mayur098 | 0:8f8e8f3cbd1c | 75 | |
mayur098 | 0:8f8e8f3cbd1c | 76 | # Returning none will suppress host test from printing success code |
mayur098 | 0:8f8e8f3cbd1c | 77 | self.server = UDPServer((self.SERVER_IP, self.SERVER_PORT), UDPEchoClientHandler) |
mayur098 | 0:8f8e8f3cbd1c | 78 | ip, port = self.server.server_address |
mayur098 | 0:8f8e8f3cbd1c | 79 | self.SERVER_PORT = port |
mayur098 | 0:8f8e8f3cbd1c | 80 | self.server.allow_reuse_address = True |
mayur098 | 0:8f8e8f3cbd1c | 81 | self.log("HOST: Listening for UDP packets: " + self.SERVER_IP + ":" + str(self.SERVER_PORT)) |
mayur098 | 0:8f8e8f3cbd1c | 82 | self.server_thread = Thread(target=UDPEchoClientTest.server_thread_func, args=(self,)) |
mayur098 | 0:8f8e8f3cbd1c | 83 | self.server_thread.start() |
mayur098 | 0:8f8e8f3cbd1c | 84 | |
mayur098 | 0:8f8e8f3cbd1c | 85 | @staticmethod |
mayur098 | 0:8f8e8f3cbd1c | 86 | def server_thread_func(this): |
mayur098 | 0:8f8e8f3cbd1c | 87 | """ |
mayur098 | 0:8f8e8f3cbd1c | 88 | Thread function to run TCP server forever. |
mayur098 | 0:8f8e8f3cbd1c | 89 | |
mayur098 | 0:8f8e8f3cbd1c | 90 | :param this: |
mayur098 | 0:8f8e8f3cbd1c | 91 | :return: |
mayur098 | 0:8f8e8f3cbd1c | 92 | """ |
mayur098 | 0:8f8e8f3cbd1c | 93 | this.server.serve_forever() |
mayur098 | 0:8f8e8f3cbd1c | 94 | |
mayur098 | 0:8f8e8f3cbd1c | 95 | @event_callback("target_ip") |
mayur098 | 0:8f8e8f3cbd1c | 96 | def _callback_target_ip(self, key, value, timestamp): |
mayur098 | 0:8f8e8f3cbd1c | 97 | """ |
mayur098 | 0:8f8e8f3cbd1c | 98 | Callback to handle reception of target's IP address. |
mayur098 | 0:8f8e8f3cbd1c | 99 | |
mayur098 | 0:8f8e8f3cbd1c | 100 | :param key: |
mayur098 | 0:8f8e8f3cbd1c | 101 | :param value: |
mayur098 | 0:8f8e8f3cbd1c | 102 | :param timestamp: |
mayur098 | 0:8f8e8f3cbd1c | 103 | :return: |
mayur098 | 0:8f8e8f3cbd1c | 104 | """ |
mayur098 | 0:8f8e8f3cbd1c | 105 | self.target_ip = value |
mayur098 | 0:8f8e8f3cbd1c | 106 | self.SERVER_IP = self.find_interface_to_target_addr(self.target_ip) |
mayur098 | 0:8f8e8f3cbd1c | 107 | self.setup_udp_server() |
mayur098 | 0:8f8e8f3cbd1c | 108 | |
mayur098 | 0:8f8e8f3cbd1c | 109 | @event_callback("host_ip") |
mayur098 | 0:8f8e8f3cbd1c | 110 | def _callback_host_ip(self, key, value, timestamp): |
mayur098 | 0:8f8e8f3cbd1c | 111 | """ |
mayur098 | 0:8f8e8f3cbd1c | 112 | Callback for request for host IP Addr |
mayur098 | 0:8f8e8f3cbd1c | 113 | |
mayur098 | 0:8f8e8f3cbd1c | 114 | """ |
mayur098 | 0:8f8e8f3cbd1c | 115 | self.send_kv("host_ip", self.SERVER_IP) |
mayur098 | 0:8f8e8f3cbd1c | 116 | |
mayur098 | 0:8f8e8f3cbd1c | 117 | @event_callback("host_port") |
mayur098 | 0:8f8e8f3cbd1c | 118 | def _callback_host_port(self, key, value, timestamp): |
mayur098 | 0:8f8e8f3cbd1c | 119 | """ |
mayur098 | 0:8f8e8f3cbd1c | 120 | Callback for request for host port |
mayur098 | 0:8f8e8f3cbd1c | 121 | """ |
mayur098 | 0:8f8e8f3cbd1c | 122 | self.send_kv("host_port", self.SERVER_PORT) |
mayur098 | 0:8f8e8f3cbd1c | 123 | |
mayur098 | 0:8f8e8f3cbd1c | 124 | def teardown(self): |
mayur098 | 0:8f8e8f3cbd1c | 125 | if self.server: |
mayur098 | 0:8f8e8f3cbd1c | 126 | self.server.shutdown() |
mayur098 | 0:8f8e8f3cbd1c | 127 | self.server_thread.join() |