3 Copyright (c) 2011-2013 ARM Limited
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
9 http://www.apache.org/licenses/LICENSE-2.0
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
22 from sys import stdout
24 class TCPEchoServerTest():
25 ECHO_SERVER_ADDRESS = ""
30 PATTERN_SERVER_IP = "Server IP Address is (\d+).(\d+).(\d+).(\d+):(\d+)"
31 re_detect_server_ip = re.compile(PATTERN_SERVER_IP)
33 def test(self, selftest):
35 c = selftest.mbed.serial_readline()
37 return selftest.RESULT_IO_SERIAL
40 m = self.re_detect_server_ip.search(c)
41 if m and len(m.groups()):
42 self.ECHO_SERVER_ADDRESS = ".".join(m.groups()[:4])
43 self.ECHO_PORT = int(m.groups()[4]) # must be integer for socket.connect method
44 selftest.notify("HOST: TCP Server found at: " + self.ECHO_SERVER_ADDRESS + ":" + str(self.ECHO_PORT))
46 # We assume this test fails so can't send 'error' message to server
48 self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
49 self.s.connect((self.ECHO_SERVER_ADDRESS, self.ECHO_PORT))
52 selftest.notify("HOST: Socket error: %s"% e)
53 return selftest.RESULT_ERROR
55 print 'HOST: Sending %d echo strings...'% self.ECHO_LOOPs,
56 for i in range(0, self.ECHO_LOOPs):
57 TEST_STRING = str(uuid.uuid4())
59 self.s.sendall(TEST_STRING)
60 data = self.s.recv(128)
63 selftest.notify("HOST: Socket error: %s"% e)
64 return selftest.RESULT_ERROR
66 received_str = repr(data)[1:-1]
67 if TEST_STRING == received_str: # We need to cut not needed single quotes from the string
73 print "'%s'"% TEST_STRING
75 print "'%s'"% received_str
79 if self.s is not None:
82 selftest.notify("HOST: TCP Server not found")
84 return selftest.RESULT_SUCCESS if result else selftest.RESULT_FAILURE