mbed-os for GR-LYCHEE
Dependents: mbed-os-example-blinky-gr-lychee GR-Boads_Camera_sample GR-Boards_Audio_Recoder GR-Boads_Camera_DisplayApp ... more
tools/host_tests/udpecho_server_auto.py@0:f782d9c66c49, 2018-02-02 (annotated)
- Committer:
- dkato
- Date:
- Fri Feb 02 05:42:23 2018 +0000
- Revision:
- 0:f782d9c66c49
mbed-os for GR-LYCHEE
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dkato | 0:f782d9c66c49 | 1 | """ |
dkato | 0:f782d9c66c49 | 2 | mbed SDK |
dkato | 0:f782d9c66c49 | 3 | Copyright (c) 2011-2013 ARM Limited |
dkato | 0:f782d9c66c49 | 4 | |
dkato | 0:f782d9c66c49 | 5 | Licensed under the Apache License, Version 2.0 (the "License"); |
dkato | 0:f782d9c66c49 | 6 | you may not use this file except in compliance with the License. |
dkato | 0:f782d9c66c49 | 7 | You may obtain a copy of the License at |
dkato | 0:f782d9c66c49 | 8 | |
dkato | 0:f782d9c66c49 | 9 | http://www.apache.org/licenses/LICENSE-2.0 |
dkato | 0:f782d9c66c49 | 10 | |
dkato | 0:f782d9c66c49 | 11 | Unless required by applicable law or agreed to in writing, software |
dkato | 0:f782d9c66c49 | 12 | distributed under the License is distributed on an "AS IS" BASIS, |
dkato | 0:f782d9c66c49 | 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
dkato | 0:f782d9c66c49 | 14 | See the License for the specific language governing permissions and |
dkato | 0:f782d9c66c49 | 15 | limitations under the License. |
dkato | 0:f782d9c66c49 | 16 | """ |
dkato | 0:f782d9c66c49 | 17 | |
dkato | 0:f782d9c66c49 | 18 | import re |
dkato | 0:f782d9c66c49 | 19 | import sys |
dkato | 0:f782d9c66c49 | 20 | import uuid |
dkato | 0:f782d9c66c49 | 21 | from sys import stdout |
dkato | 0:f782d9c66c49 | 22 | from socket import socket, AF_INET, SOCK_DGRAM |
dkato | 0:f782d9c66c49 | 23 | |
dkato | 0:f782d9c66c49 | 24 | class UDPEchoServerTest(): |
dkato | 0:f782d9c66c49 | 25 | ECHO_SERVER_ADDRESS = "" |
dkato | 0:f782d9c66c49 | 26 | ECHO_PORT = 0 |
dkato | 0:f782d9c66c49 | 27 | s = None # Socket |
dkato | 0:f782d9c66c49 | 28 | |
dkato | 0:f782d9c66c49 | 29 | PATTERN_SERVER_IP = "Server IP Address is (\d+).(\d+).(\d+).(\d+):(\d+)" |
dkato | 0:f782d9c66c49 | 30 | re_detect_server_ip = re.compile(PATTERN_SERVER_IP) |
dkato | 0:f782d9c66c49 | 31 | |
dkato | 0:f782d9c66c49 | 32 | def test(self, selftest): |
dkato | 0:f782d9c66c49 | 33 | result = True |
dkato | 0:f782d9c66c49 | 34 | serial_ip_msg = selftest.mbed.serial_readline() |
dkato | 0:f782d9c66c49 | 35 | if serial_ip_msg is None: |
dkato | 0:f782d9c66c49 | 36 | return selftest.RESULT_IO_SERIAL |
dkato | 0:f782d9c66c49 | 37 | selftest.notify(serial_ip_msg) |
dkato | 0:f782d9c66c49 | 38 | # Searching for IP address and port prompted by server |
dkato | 0:f782d9c66c49 | 39 | m = self.re_detect_server_ip.search(serial_ip_msg) |
dkato | 0:f782d9c66c49 | 40 | if m and len(m.groups()): |
dkato | 0:f782d9c66c49 | 41 | self.ECHO_SERVER_ADDRESS = ".".join(m.groups()[:4]) |
dkato | 0:f782d9c66c49 | 42 | self.ECHO_PORT = int(m.groups()[4]) # must be integer for socket.connect method |
dkato | 0:f782d9c66c49 | 43 | selftest.notify("HOST: UDP Server found at: " + self.ECHO_SERVER_ADDRESS + ":" + str(self.ECHO_PORT)) |
dkato | 0:f782d9c66c49 | 44 | |
dkato | 0:f782d9c66c49 | 45 | # We assume this test fails so can't send 'error' message to server |
dkato | 0:f782d9c66c49 | 46 | try: |
dkato | 0:f782d9c66c49 | 47 | self.s = socket(AF_INET, SOCK_DGRAM) |
dkato | 0:f782d9c66c49 | 48 | except Exception, e: |
dkato | 0:f782d9c66c49 | 49 | self.s = None |
dkato | 0:f782d9c66c49 | 50 | selftest.notify("HOST: Socket error: %s"% e) |
dkato | 0:f782d9c66c49 | 51 | return selftest.RESULT_ERROR |
dkato | 0:f782d9c66c49 | 52 | |
dkato | 0:f782d9c66c49 | 53 | for i in range(0, 100): |
dkato | 0:f782d9c66c49 | 54 | TEST_STRING = str(uuid.uuid4()) |
dkato | 0:f782d9c66c49 | 55 | self.s.sendto(TEST_STRING, (self.ECHO_SERVER_ADDRESS, self.ECHO_PORT)) |
dkato | 0:f782d9c66c49 | 56 | data = self.s.recv(len(TEST_STRING)) |
dkato | 0:f782d9c66c49 | 57 | received_str = repr(data)[1:-1] |
dkato | 0:f782d9c66c49 | 58 | if TEST_STRING != received_str: |
dkato | 0:f782d9c66c49 | 59 | result = False |
dkato | 0:f782d9c66c49 | 60 | break |
dkato | 0:f782d9c66c49 | 61 | sys.stdout.write('.') |
dkato | 0:f782d9c66c49 | 62 | stdout.flush() |
dkato | 0:f782d9c66c49 | 63 | else: |
dkato | 0:f782d9c66c49 | 64 | result = False |
dkato | 0:f782d9c66c49 | 65 | |
dkato | 0:f782d9c66c49 | 66 | if self.s is not None: |
dkato | 0:f782d9c66c49 | 67 | self.s.close() |
dkato | 0:f782d9c66c49 | 68 | return selftest.RESULT_SUCCESS if result else selftest.RESULT_FAILURE |