Clone of official tools
host_tests/udpecho_client_auto.py@0:66f3b5499f7f, 2016-05-19 (annotated)
- Committer:
- screamer
- Date:
- Thu May 19 19:44:41 2016 +0100
- Revision:
- 0:66f3b5499f7f
- Child:
- 43:2a7da56ebd24
Initial revision
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
screamer | 0:66f3b5499f7f | 1 | """ |
screamer | 0:66f3b5499f7f | 2 | mbed SDK |
screamer | 0:66f3b5499f7f | 3 | Copyright (c) 2011-2013 ARM Limited |
screamer | 0:66f3b5499f7f | 4 | |
screamer | 0:66f3b5499f7f | 5 | Licensed under the Apache License, Version 2.0 (the "License"); |
screamer | 0:66f3b5499f7f | 6 | you may not use this file except in compliance with the License. |
screamer | 0:66f3b5499f7f | 7 | You may obtain a copy of the License at |
screamer | 0:66f3b5499f7f | 8 | |
screamer | 0:66f3b5499f7f | 9 | http://www.apache.org/licenses/LICENSE-2.0 |
screamer | 0:66f3b5499f7f | 10 | |
screamer | 0:66f3b5499f7f | 11 | Unless required by applicable law or agreed to in writing, software |
screamer | 0:66f3b5499f7f | 12 | distributed under the License is distributed on an "AS IS" BASIS, |
screamer | 0:66f3b5499f7f | 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
screamer | 0:66f3b5499f7f | 14 | See the License for the specific language governing permissions and |
screamer | 0:66f3b5499f7f | 15 | limitations under the License. |
screamer | 0:66f3b5499f7f | 16 | """ |
screamer | 0:66f3b5499f7f | 17 | |
screamer | 0:66f3b5499f7f | 18 | import sys |
screamer | 0:66f3b5499f7f | 19 | import socket |
screamer | 0:66f3b5499f7f | 20 | from sys import stdout |
screamer | 0:66f3b5499f7f | 21 | from SocketServer import BaseRequestHandler, UDPServer |
screamer | 0:66f3b5499f7f | 22 | |
screamer | 0:66f3b5499f7f | 23 | class UDPEchoClient_Handler(BaseRequestHandler): |
screamer | 0:66f3b5499f7f | 24 | def handle(self): |
screamer | 0:66f3b5499f7f | 25 | """ One handle per connection |
screamer | 0:66f3b5499f7f | 26 | """ |
screamer | 0:66f3b5499f7f | 27 | data, socket = self.request |
screamer | 0:66f3b5499f7f | 28 | socket.sendto(data, self.client_address) |
screamer | 0:66f3b5499f7f | 29 | if '{{end}}' in data: |
screamer | 0:66f3b5499f7f | 30 | |
screamer | 0:66f3b5499f7f | 31 | print data |
screamer | 0:66f3b5499f7f | 32 | else: |
screamer | 0:66f3b5499f7f | 33 | sys.stdout.write('.') |
screamer | 0:66f3b5499f7f | 34 | stdout.flush() |
screamer | 0:66f3b5499f7f | 35 | |
screamer | 0:66f3b5499f7f | 36 | class UDPEchoClientTest(): |
screamer | 0:66f3b5499f7f | 37 | |
screamer | 0:66f3b5499f7f | 38 | def send_server_ip_port(self, selftest, ip_address, port_no): |
screamer | 0:66f3b5499f7f | 39 | c = selftest.mbed.serial_readline() # 'UDPCllient waiting for server IP and port...' |
screamer | 0:66f3b5499f7f | 40 | if c is None: |
screamer | 0:66f3b5499f7f | 41 | selftest.print_result(selftest.RESULT_IO_SERIAL) |
screamer | 0:66f3b5499f7f | 42 | return |
screamer | 0:66f3b5499f7f | 43 | selftest.notify(c.strip()) |
screamer | 0:66f3b5499f7f | 44 | |
screamer | 0:66f3b5499f7f | 45 | selftest.notify("HOST: Sending server IP Address to target...") |
screamer | 0:66f3b5499f7f | 46 | connection_str = ip_address + ":" + str(port_no) + "\n" |
screamer | 0:66f3b5499f7f | 47 | selftest.mbed.serial_write(connection_str) |
screamer | 0:66f3b5499f7f | 48 | |
screamer | 0:66f3b5499f7f | 49 | c = selftest.mbed.serial_readline() # 'UDPCllient waiting for server IP and port...' |
screamer | 0:66f3b5499f7f | 50 | if c is None: |
screamer | 0:66f3b5499f7f | 51 | self.print_result(selftest.RESULT_IO_SERIAL) |
screamer | 0:66f3b5499f7f | 52 | return |
screamer | 0:66f3b5499f7f | 53 | selftest.notify(c.strip()) |
screamer | 0:66f3b5499f7f | 54 | return selftest.RESULT_PASSIVE |
screamer | 0:66f3b5499f7f | 55 | |
screamer | 0:66f3b5499f7f | 56 | def test(self, selftest): |
screamer | 0:66f3b5499f7f | 57 | # We need to discover SERVEP_IP and set up SERVER_PORT |
screamer | 0:66f3b5499f7f | 58 | # Note: Port 7 is Echo Protocol: |
screamer | 0:66f3b5499f7f | 59 | # |
screamer | 0:66f3b5499f7f | 60 | # Port number rationale: |
screamer | 0:66f3b5499f7f | 61 | # |
screamer | 0:66f3b5499f7f | 62 | # The Echo Protocol is a service in the Internet Protocol Suite defined |
screamer | 0:66f3b5499f7f | 63 | # in RFC 862. It was originally proposed for testing and measurement |
screamer | 0:66f3b5499f7f | 64 | # of round-trip times[citation needed] in IP networks. |
screamer | 0:66f3b5499f7f | 65 | # |
screamer | 0:66f3b5499f7f | 66 | # A host may connect to a server that supports the Echo Protocol using |
screamer | 0:66f3b5499f7f | 67 | # the Transmission Control Protocol (TCP) or the User Datagram Protocol |
screamer | 0:66f3b5499f7f | 68 | # (UDP) on the well-known port number 7. The server sends back an |
screamer | 0:66f3b5499f7f | 69 | # identical copy of the data it received. |
screamer | 0:66f3b5499f7f | 70 | SERVER_IP = str(socket.gethostbyname(socket.getfqdn())) |
screamer | 0:66f3b5499f7f | 71 | SERVER_PORT = 7 |
screamer | 0:66f3b5499f7f | 72 | |
screamer | 0:66f3b5499f7f | 73 | # Returning none will suppress host test from printing success code |
screamer | 0:66f3b5499f7f | 74 | server = UDPServer((SERVER_IP, SERVER_PORT), UDPEchoClient_Handler) |
screamer | 0:66f3b5499f7f | 75 | print "HOST: Listening for UDP connections..." |
screamer | 0:66f3b5499f7f | 76 | self.send_server_ip_port(selftest, SERVER_IP, SERVER_PORT) |
screamer | 0:66f3b5499f7f | 77 | server.serve_forever() |