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.
20 from sys import stdout
21 from SocketServer import BaseRequestHandler, TCPServer
23 class TCPEchoClient_Handler(BaseRequestHandler):
25 """ One handle per connection
27 print "HOST: Connection received...",
30 data = self.request.recv(1024)
32 self.request.sendall(data)
33 if '{{end}}' in str(data):
42 class TCPEchoClientTest():
43 def send_server_ip_port(self, selftest, ip_address, port_no):
44 """ Set up network host. Reset target and and send server IP via serial to Mbed
46 c = selftest.mbed.serial_readline() # 'TCPCllient waiting for server IP and port...'
48 self.print_result(selftest.RESULT_IO_SERIAL)
51 selftest.notify(c.strip())
52 selftest.notify("HOST: Sending server IP Address to target...")
54 connection_str = ip_address + ":" + str(port_no) + "\n"
55 selftest.mbed.serial_write(connection_str)
56 selftest.notify(connection_str)
58 # Two more strings about connection should be sent by MBED
60 c = selftest.mbed.serial_readline()
62 selftest.print_result(self.RESULT_IO_SERIAL)
64 selftest.notify(c.strip())
66 def test(self, selftest):
67 # We need to discover SERVEP_IP and set up SERVER_PORT
68 # Note: Port 7 is Echo Protocol:
70 # Port number rationale:
72 # The Echo Protocol is a service in the Internet Protocol Suite defined
73 # in RFC 862. It was originally proposed for testing and measurement
74 # of round-trip times[citation needed] in IP networks.
76 # A host may connect to a server that supports the Echo Protocol using
77 # the Transmission Control Protocol (TCP) or the User Datagram Protocol
78 # (UDP) on the well-known port number 7. The server sends back an
79 # identical copy of the data it received.
80 SERVER_IP = str(socket.gethostbyname(socket.getfqdn()))
83 # Returning none will suppress host test from printing success code
84 server = TCPServer((SERVER_IP, SERVER_PORT), TCPEchoClient_Handler)
85 print "HOST: Listening for TCP connections: " + SERVER_IP + ":" + str(SERVER_PORT)
86 self.send_server_ip_port(selftest, SERVER_IP, SERVER_PORT)
87 server.serve_forever()