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