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