Version of easy-connect with the u-blox cellular platforms C027 and C030 added.
esp8266-driver/TESTS/net/host_tests/tcp_echo.py@6:304d3ba87a01, 2017-11-03 (annotated)
- Committer:
- RobMeades
- Date:
- Fri Nov 03 13:01:23 2017 +0000
- Revision:
- 6:304d3ba87a01
- Parent:
- 0:19aa55d66228
Add comment concerning N2XX baud rate.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
group-ublox | 0:19aa55d66228 | 1 | # Copyright 2015 ARM Limited, All rights reserved |
group-ublox | 0:19aa55d66228 | 2 | # |
group-ublox | 0:19aa55d66228 | 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
group-ublox | 0:19aa55d66228 | 4 | # you may not use this file except in compliance with the License. |
group-ublox | 0:19aa55d66228 | 5 | # You may obtain a copy of the License at |
group-ublox | 0:19aa55d66228 | 6 | # |
group-ublox | 0:19aa55d66228 | 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
group-ublox | 0:19aa55d66228 | 8 | # |
group-ublox | 0:19aa55d66228 | 9 | # Unless required by applicable law or agreed to in writing, software |
group-ublox | 0:19aa55d66228 | 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
group-ublox | 0:19aa55d66228 | 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
group-ublox | 0:19aa55d66228 | 12 | # See the License for the specific language governing permissions and |
group-ublox | 0:19aa55d66228 | 13 | # limitations under the License. |
group-ublox | 0:19aa55d66228 | 14 | |
group-ublox | 0:19aa55d66228 | 15 | import sys |
group-ublox | 0:19aa55d66228 | 16 | import select |
group-ublox | 0:19aa55d66228 | 17 | import socket |
group-ublox | 0:19aa55d66228 | 18 | import logging |
group-ublox | 0:19aa55d66228 | 19 | from threading import Thread |
group-ublox | 0:19aa55d66228 | 20 | from sys import stdout |
group-ublox | 0:19aa55d66228 | 21 | from SocketServer import BaseRequestHandler, TCPServer |
group-ublox | 0:19aa55d66228 | 22 | from mbed_host_tests import BaseHostTest, event_callback |
group-ublox | 0:19aa55d66228 | 23 | |
group-ublox | 0:19aa55d66228 | 24 | |
group-ublox | 0:19aa55d66228 | 25 | class TCPEchoClientHandler(BaseRequestHandler): |
group-ublox | 0:19aa55d66228 | 26 | def handle(self): |
group-ublox | 0:19aa55d66228 | 27 | """ |
group-ublox | 0:19aa55d66228 | 28 | Handles a connection. Test starts by client(i.e. mbed) connecting to server. |
group-ublox | 0:19aa55d66228 | 29 | This connection handler receives data and echoes back to the client util |
group-ublox | 0:19aa55d66228 | 30 | {{end}} is received. Then it sits on recv() for client to terminate the |
group-ublox | 0:19aa55d66228 | 31 | connection. |
group-ublox | 0:19aa55d66228 | 32 | |
group-ublox | 0:19aa55d66228 | 33 | Note: reason for not echoing data back after receiving {{end}} is that send |
group-ublox | 0:19aa55d66228 | 34 | fails raising a SocketError as client closes connection. |
group-ublox | 0:19aa55d66228 | 35 | """ |
group-ublox | 0:19aa55d66228 | 36 | while self.server.isrunning(): |
group-ublox | 0:19aa55d66228 | 37 | try: |
group-ublox | 0:19aa55d66228 | 38 | data = self.recv() |
group-ublox | 0:19aa55d66228 | 39 | if not data: break |
group-ublox | 0:19aa55d66228 | 40 | except Exception as e: |
group-ublox | 0:19aa55d66228 | 41 | break |
group-ublox | 0:19aa55d66228 | 42 | |
group-ublox | 0:19aa55d66228 | 43 | try: |
group-ublox | 0:19aa55d66228 | 44 | # echo data back to the client |
group-ublox | 0:19aa55d66228 | 45 | self.send(data) |
group-ublox | 0:19aa55d66228 | 46 | except Exception as e: |
group-ublox | 0:19aa55d66228 | 47 | break |
group-ublox | 0:19aa55d66228 | 48 | |
group-ublox | 0:19aa55d66228 | 49 | def recv(self): |
group-ublox | 0:19aa55d66228 | 50 | """ |
group-ublox | 0:19aa55d66228 | 51 | Try to receive until server is shutdown |
group-ublox | 0:19aa55d66228 | 52 | """ |
group-ublox | 0:19aa55d66228 | 53 | while self.server.isrunning(): |
group-ublox | 0:19aa55d66228 | 54 | rl, wl, xl = select.select([self.request], [], [], 1) |
group-ublox | 0:19aa55d66228 | 55 | if len(rl): |
group-ublox | 0:19aa55d66228 | 56 | return self.request.recv(1024) |
group-ublox | 0:19aa55d66228 | 57 | |
group-ublox | 0:19aa55d66228 | 58 | def send(self, data): |
group-ublox | 0:19aa55d66228 | 59 | """ |
group-ublox | 0:19aa55d66228 | 60 | Try to send until server is shutdown |
group-ublox | 0:19aa55d66228 | 61 | """ |
group-ublox | 0:19aa55d66228 | 62 | while self.server.isrunning(): |
group-ublox | 0:19aa55d66228 | 63 | rl, wl, xl = select.select([], [self.request], [], 1) |
group-ublox | 0:19aa55d66228 | 64 | if len(wl): |
group-ublox | 0:19aa55d66228 | 65 | self.request.sendall(data) |
group-ublox | 0:19aa55d66228 | 66 | break |
group-ublox | 0:19aa55d66228 | 67 | |
group-ublox | 0:19aa55d66228 | 68 | |
group-ublox | 0:19aa55d66228 | 69 | class TCPServerWrapper(TCPServer): |
group-ublox | 0:19aa55d66228 | 70 | """ |
group-ublox | 0:19aa55d66228 | 71 | Wrapper over TCP server to implement server initiated shutdown. |
group-ublox | 0:19aa55d66228 | 72 | Adds a flag:= running that a request handler can check and come out of |
group-ublox | 0:19aa55d66228 | 73 | recv loop when shutdown is called. |
group-ublox | 0:19aa55d66228 | 74 | """ |
group-ublox | 0:19aa55d66228 | 75 | |
group-ublox | 0:19aa55d66228 | 76 | def __init__(self, addr, request_handler): |
group-ublox | 0:19aa55d66228 | 77 | # hmm, TCPServer is not sub-classed from object! |
group-ublox | 0:19aa55d66228 | 78 | if issubclass(TCPServer, object): |
group-ublox | 0:19aa55d66228 | 79 | super(TCPServerWrapper, self).__init__(addr, request_handler) |
group-ublox | 0:19aa55d66228 | 80 | else: |
group-ublox | 0:19aa55d66228 | 81 | TCPServer.__init__(self, addr, request_handler) |
group-ublox | 0:19aa55d66228 | 82 | self.running = False |
group-ublox | 0:19aa55d66228 | 83 | |
group-ublox | 0:19aa55d66228 | 84 | def serve_forever(self): |
group-ublox | 0:19aa55d66228 | 85 | self.running = True |
group-ublox | 0:19aa55d66228 | 86 | if issubclass(TCPServer, object): |
group-ublox | 0:19aa55d66228 | 87 | super(TCPServerWrapper, self).serve_forever() |
group-ublox | 0:19aa55d66228 | 88 | else: |
group-ublox | 0:19aa55d66228 | 89 | TCPServer.serve_forever(self) |
group-ublox | 0:19aa55d66228 | 90 | |
group-ublox | 0:19aa55d66228 | 91 | def shutdown(self): |
group-ublox | 0:19aa55d66228 | 92 | self.running = False |
group-ublox | 0:19aa55d66228 | 93 | if issubclass(TCPServer, object): |
group-ublox | 0:19aa55d66228 | 94 | super(TCPServerWrapper, self).shutdown() |
group-ublox | 0:19aa55d66228 | 95 | else: |
group-ublox | 0:19aa55d66228 | 96 | TCPServer.shutdown(self) |
group-ublox | 0:19aa55d66228 | 97 | |
group-ublox | 0:19aa55d66228 | 98 | def isrunning(self): |
group-ublox | 0:19aa55d66228 | 99 | return self.running |
group-ublox | 0:19aa55d66228 | 100 | |
group-ublox | 0:19aa55d66228 | 101 | |
group-ublox | 0:19aa55d66228 | 102 | class TCPEchoClientTest(BaseHostTest): |
group-ublox | 0:19aa55d66228 | 103 | |
group-ublox | 0:19aa55d66228 | 104 | def __init__(self): |
group-ublox | 0:19aa55d66228 | 105 | """ |
group-ublox | 0:19aa55d66228 | 106 | Initialise test parameters. |
group-ublox | 0:19aa55d66228 | 107 | |
group-ublox | 0:19aa55d66228 | 108 | :return: |
group-ublox | 0:19aa55d66228 | 109 | """ |
group-ublox | 0:19aa55d66228 | 110 | BaseHostTest.__init__(self) |
group-ublox | 0:19aa55d66228 | 111 | self.SERVER_IP = None # Will be determined after knowing the target IP |
group-ublox | 0:19aa55d66228 | 112 | self.SERVER_PORT = 0 # Let TCPServer choose an arbitrary port |
group-ublox | 0:19aa55d66228 | 113 | self.server = None |
group-ublox | 0:19aa55d66228 | 114 | self.server_thread = None |
group-ublox | 0:19aa55d66228 | 115 | self.target_ip = None |
group-ublox | 0:19aa55d66228 | 116 | |
group-ublox | 0:19aa55d66228 | 117 | @staticmethod |
group-ublox | 0:19aa55d66228 | 118 | def find_interface_to_target_addr(target_ip): |
group-ublox | 0:19aa55d66228 | 119 | """ |
group-ublox | 0:19aa55d66228 | 120 | Finds IP address of the interface through which it is connected to the target. |
group-ublox | 0:19aa55d66228 | 121 | |
group-ublox | 0:19aa55d66228 | 122 | :return: |
group-ublox | 0:19aa55d66228 | 123 | """ |
group-ublox | 0:19aa55d66228 | 124 | s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
group-ublox | 0:19aa55d66228 | 125 | try: |
group-ublox | 0:19aa55d66228 | 126 | s.connect((target_ip, 0)) # Target IP, any port |
group-ublox | 0:19aa55d66228 | 127 | except socket.error: |
group-ublox | 0:19aa55d66228 | 128 | s.connect((target_ip, 8000)) # Target IP, 'random' port |
group-ublox | 0:19aa55d66228 | 129 | ip = s.getsockname()[0] |
group-ublox | 0:19aa55d66228 | 130 | s.close() |
group-ublox | 0:19aa55d66228 | 131 | return ip |
group-ublox | 0:19aa55d66228 | 132 | |
group-ublox | 0:19aa55d66228 | 133 | def setup_tcp_server(self): |
group-ublox | 0:19aa55d66228 | 134 | """ |
group-ublox | 0:19aa55d66228 | 135 | sets up a TCP server for target to connect and send test data. |
group-ublox | 0:19aa55d66228 | 136 | |
group-ublox | 0:19aa55d66228 | 137 | :return: |
group-ublox | 0:19aa55d66228 | 138 | """ |
group-ublox | 0:19aa55d66228 | 139 | # !NOTE: There should mechanism to assert in the host test |
group-ublox | 0:19aa55d66228 | 140 | if self.SERVER_IP is None: |
group-ublox | 0:19aa55d66228 | 141 | self.log("setup_tcp_server() called before determining server IP!") |
group-ublox | 0:19aa55d66228 | 142 | self.notify_complete(False) |
group-ublox | 0:19aa55d66228 | 143 | |
group-ublox | 0:19aa55d66228 | 144 | # Returning none will suppress host test from printing success code |
group-ublox | 0:19aa55d66228 | 145 | self.server = TCPServerWrapper((self.SERVER_IP, self.SERVER_PORT), TCPEchoClientHandler) |
group-ublox | 0:19aa55d66228 | 146 | ip, port = self.server.server_address |
group-ublox | 0:19aa55d66228 | 147 | self.SERVER_PORT = port |
group-ublox | 0:19aa55d66228 | 148 | self.server.allow_reuse_address = True |
group-ublox | 0:19aa55d66228 | 149 | self.log("HOST: Listening for TCP connections: " + self.SERVER_IP + ":" + str(self.SERVER_PORT)) |
group-ublox | 0:19aa55d66228 | 150 | self.server_thread = Thread(target=TCPEchoClientTest.server_thread_func, args=(self,)) |
group-ublox | 0:19aa55d66228 | 151 | self.server_thread.start() |
group-ublox | 0:19aa55d66228 | 152 | |
group-ublox | 0:19aa55d66228 | 153 | @staticmethod |
group-ublox | 0:19aa55d66228 | 154 | def server_thread_func(this): |
group-ublox | 0:19aa55d66228 | 155 | """ |
group-ublox | 0:19aa55d66228 | 156 | Thread function to run TCP server forever. |
group-ublox | 0:19aa55d66228 | 157 | |
group-ublox | 0:19aa55d66228 | 158 | :param this: |
group-ublox | 0:19aa55d66228 | 159 | :return: |
group-ublox | 0:19aa55d66228 | 160 | """ |
group-ublox | 0:19aa55d66228 | 161 | this.server.serve_forever() |
group-ublox | 0:19aa55d66228 | 162 | |
group-ublox | 0:19aa55d66228 | 163 | @event_callback("target_ip") |
group-ublox | 0:19aa55d66228 | 164 | def _callback_target_ip(self, key, value, timestamp): |
group-ublox | 0:19aa55d66228 | 165 | """ |
group-ublox | 0:19aa55d66228 | 166 | Callback to handle reception of target's IP address. |
group-ublox | 0:19aa55d66228 | 167 | |
group-ublox | 0:19aa55d66228 | 168 | :param key: |
group-ublox | 0:19aa55d66228 | 169 | :param value: |
group-ublox | 0:19aa55d66228 | 170 | :param timestamp: |
group-ublox | 0:19aa55d66228 | 171 | :return: |
group-ublox | 0:19aa55d66228 | 172 | """ |
group-ublox | 0:19aa55d66228 | 173 | self.target_ip = value |
group-ublox | 0:19aa55d66228 | 174 | self.SERVER_IP = self.find_interface_to_target_addr(self.target_ip) |
group-ublox | 0:19aa55d66228 | 175 | self.setup_tcp_server() |
group-ublox | 0:19aa55d66228 | 176 | |
group-ublox | 0:19aa55d66228 | 177 | @event_callback("host_ip") |
group-ublox | 0:19aa55d66228 | 178 | def _callback_host_ip(self, key, value, timestamp): |
group-ublox | 0:19aa55d66228 | 179 | """ |
group-ublox | 0:19aa55d66228 | 180 | Callback for request for host IP Addr |
group-ublox | 0:19aa55d66228 | 181 | |
group-ublox | 0:19aa55d66228 | 182 | """ |
group-ublox | 0:19aa55d66228 | 183 | self.send_kv("host_ip", self.SERVER_IP) |
group-ublox | 0:19aa55d66228 | 184 | |
group-ublox | 0:19aa55d66228 | 185 | @event_callback("host_port") |
group-ublox | 0:19aa55d66228 | 186 | def _callback_host_port(self, key, value, timestamp): |
group-ublox | 0:19aa55d66228 | 187 | """ |
group-ublox | 0:19aa55d66228 | 188 | Callback for request for host port |
group-ublox | 0:19aa55d66228 | 189 | """ |
group-ublox | 0:19aa55d66228 | 190 | self.send_kv("host_port", self.SERVER_PORT) |
group-ublox | 0:19aa55d66228 | 191 | |
group-ublox | 0:19aa55d66228 | 192 | def teardown(self): |
group-ublox | 0:19aa55d66228 | 193 | if self.server: |
group-ublox | 0:19aa55d66228 | 194 | self.server.shutdown() |
group-ublox | 0:19aa55d66228 | 195 | self.server_thread.join() |