Preliminary main mbed library for nexpaq development
tools/host_tests/tcpecho_server_auto.py@1:d96dbedaebdb, 2016-11-04 (annotated)
- Committer:
- nexpaq
- Date:
- Fri Nov 04 20:54:50 2016 +0000
- Revision:
- 1:d96dbedaebdb
- Parent:
- 0:6c56fb4bc5f0
Removed extra directories for other platforms
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nexpaq | 0:6c56fb4bc5f0 | 1 | """ |
nexpaq | 0:6c56fb4bc5f0 | 2 | mbed SDK |
nexpaq | 0:6c56fb4bc5f0 | 3 | Copyright (c) 2011-2013 ARM Limited |
nexpaq | 0:6c56fb4bc5f0 | 4 | |
nexpaq | 0:6c56fb4bc5f0 | 5 | Licensed under the Apache License, Version 2.0 (the "License"); |
nexpaq | 0:6c56fb4bc5f0 | 6 | you may not use this file except in compliance with the License. |
nexpaq | 0:6c56fb4bc5f0 | 7 | You may obtain a copy of the License at |
nexpaq | 0:6c56fb4bc5f0 | 8 | |
nexpaq | 0:6c56fb4bc5f0 | 9 | http://www.apache.org/licenses/LICENSE-2.0 |
nexpaq | 0:6c56fb4bc5f0 | 10 | |
nexpaq | 0:6c56fb4bc5f0 | 11 | Unless required by applicable law or agreed to in writing, software |
nexpaq | 0:6c56fb4bc5f0 | 12 | distributed under the License is distributed on an "AS IS" BASIS, |
nexpaq | 0:6c56fb4bc5f0 | 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
nexpaq | 0:6c56fb4bc5f0 | 14 | See the License for the specific language governing permissions and |
nexpaq | 0:6c56fb4bc5f0 | 15 | limitations under the License. |
nexpaq | 0:6c56fb4bc5f0 | 16 | """ |
nexpaq | 0:6c56fb4bc5f0 | 17 | |
nexpaq | 0:6c56fb4bc5f0 | 18 | import re |
nexpaq | 0:6c56fb4bc5f0 | 19 | import sys |
nexpaq | 0:6c56fb4bc5f0 | 20 | import uuid |
nexpaq | 0:6c56fb4bc5f0 | 21 | import socket |
nexpaq | 0:6c56fb4bc5f0 | 22 | from sys import stdout |
nexpaq | 0:6c56fb4bc5f0 | 23 | |
nexpaq | 0:6c56fb4bc5f0 | 24 | class TCPEchoServerTest(): |
nexpaq | 0:6c56fb4bc5f0 | 25 | ECHO_SERVER_ADDRESS = "" |
nexpaq | 0:6c56fb4bc5f0 | 26 | ECHO_PORT = 0 |
nexpaq | 0:6c56fb4bc5f0 | 27 | ECHO_LOOPs = 100 |
nexpaq | 0:6c56fb4bc5f0 | 28 | s = None # Socket |
nexpaq | 0:6c56fb4bc5f0 | 29 | |
nexpaq | 0:6c56fb4bc5f0 | 30 | PATTERN_SERVER_IP = "Server IP Address is (\d+).(\d+).(\d+).(\d+):(\d+)" |
nexpaq | 0:6c56fb4bc5f0 | 31 | re_detect_server_ip = re.compile(PATTERN_SERVER_IP) |
nexpaq | 0:6c56fb4bc5f0 | 32 | |
nexpaq | 0:6c56fb4bc5f0 | 33 | def test(self, selftest): |
nexpaq | 0:6c56fb4bc5f0 | 34 | result = False |
nexpaq | 0:6c56fb4bc5f0 | 35 | c = selftest.mbed.serial_readline() |
nexpaq | 0:6c56fb4bc5f0 | 36 | if c is None: |
nexpaq | 0:6c56fb4bc5f0 | 37 | return selftest.RESULT_IO_SERIAL |
nexpaq | 0:6c56fb4bc5f0 | 38 | selftest.notify(c) |
nexpaq | 0:6c56fb4bc5f0 | 39 | |
nexpaq | 0:6c56fb4bc5f0 | 40 | m = self.re_detect_server_ip.search(c) |
nexpaq | 0:6c56fb4bc5f0 | 41 | if m and len(m.groups()): |
nexpaq | 0:6c56fb4bc5f0 | 42 | self.ECHO_SERVER_ADDRESS = ".".join(m.groups()[:4]) |
nexpaq | 0:6c56fb4bc5f0 | 43 | self.ECHO_PORT = int(m.groups()[4]) # must be integer for socket.connect method |
nexpaq | 0:6c56fb4bc5f0 | 44 | selftest.notify("HOST: TCP Server found at: " + self.ECHO_SERVER_ADDRESS + ":" + str(self.ECHO_PORT)) |
nexpaq | 0:6c56fb4bc5f0 | 45 | |
nexpaq | 0:6c56fb4bc5f0 | 46 | # We assume this test fails so can't send 'error' message to server |
nexpaq | 0:6c56fb4bc5f0 | 47 | try: |
nexpaq | 0:6c56fb4bc5f0 | 48 | self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
nexpaq | 0:6c56fb4bc5f0 | 49 | self.s.connect((self.ECHO_SERVER_ADDRESS, self.ECHO_PORT)) |
nexpaq | 0:6c56fb4bc5f0 | 50 | except Exception, e: |
nexpaq | 0:6c56fb4bc5f0 | 51 | self.s = None |
nexpaq | 0:6c56fb4bc5f0 | 52 | selftest.notify("HOST: Socket error: %s"% e) |
nexpaq | 0:6c56fb4bc5f0 | 53 | return selftest.RESULT_ERROR |
nexpaq | 0:6c56fb4bc5f0 | 54 | |
nexpaq | 0:6c56fb4bc5f0 | 55 | print 'HOST: Sending %d echo strings...'% self.ECHO_LOOPs, |
nexpaq | 0:6c56fb4bc5f0 | 56 | for i in range(0, self.ECHO_LOOPs): |
nexpaq | 0:6c56fb4bc5f0 | 57 | TEST_STRING = str(uuid.uuid4()) |
nexpaq | 0:6c56fb4bc5f0 | 58 | try: |
nexpaq | 0:6c56fb4bc5f0 | 59 | self.s.sendall(TEST_STRING) |
nexpaq | 0:6c56fb4bc5f0 | 60 | data = self.s.recv(128) |
nexpaq | 0:6c56fb4bc5f0 | 61 | except Exception, e: |
nexpaq | 0:6c56fb4bc5f0 | 62 | self.s = None |
nexpaq | 0:6c56fb4bc5f0 | 63 | selftest.notify("HOST: Socket error: %s"% e) |
nexpaq | 0:6c56fb4bc5f0 | 64 | return selftest.RESULT_ERROR |
nexpaq | 0:6c56fb4bc5f0 | 65 | |
nexpaq | 0:6c56fb4bc5f0 | 66 | received_str = repr(data)[1:-1] |
nexpaq | 0:6c56fb4bc5f0 | 67 | if TEST_STRING == received_str: # We need to cut not needed single quotes from the string |
nexpaq | 0:6c56fb4bc5f0 | 68 | sys.stdout.write('.') |
nexpaq | 0:6c56fb4bc5f0 | 69 | stdout.flush() |
nexpaq | 0:6c56fb4bc5f0 | 70 | result = True |
nexpaq | 0:6c56fb4bc5f0 | 71 | else: |
nexpaq | 0:6c56fb4bc5f0 | 72 | print "Expected: " |
nexpaq | 0:6c56fb4bc5f0 | 73 | print "'%s'"% TEST_STRING |
nexpaq | 0:6c56fb4bc5f0 | 74 | print "received: " |
nexpaq | 0:6c56fb4bc5f0 | 75 | print "'%s'"% received_str |
nexpaq | 0:6c56fb4bc5f0 | 76 | result = False |
nexpaq | 0:6c56fb4bc5f0 | 77 | break |
nexpaq | 0:6c56fb4bc5f0 | 78 | |
nexpaq | 0:6c56fb4bc5f0 | 79 | if self.s is not None: |
nexpaq | 0:6c56fb4bc5f0 | 80 | self.s.close() |
nexpaq | 0:6c56fb4bc5f0 | 81 | else: |
nexpaq | 0:6c56fb4bc5f0 | 82 | selftest.notify("HOST: TCP Server not found") |
nexpaq | 0:6c56fb4bc5f0 | 83 | result = False |
nexpaq | 0:6c56fb4bc5f0 | 84 | return selftest.RESULT_SUCCESS if result else selftest.RESULT_FAILURE |