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