Nicolas Borla / Mbed OS BBR_1Ebene
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers udpecho_client_auto.py Source File

udpecho_client_auto.py

00001 """
00002 mbed SDK
00003 Copyright (c) 2011-2013 ARM Limited
00004 
00005 Licensed under the Apache License, Version 2.0 (the "License");
00006 you may not use this file except in compliance with the License.
00007 You may obtain a copy of the License at
00008 
00009     http://www.apache.org/licenses/LICENSE-2.0
00010 
00011 Unless required by applicable law or agreed to in writing, software
00012 distributed under the License is distributed on an "AS IS" BASIS,
00013 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014 See the License for the specific language governing permissions and
00015 limitations under the License.
00016 """
00017 from __future__ import print_function
00018 
00019 import sys
00020 import socket
00021 from sys import stdout
00022 try:
00023     from SocketServer import BaseRequestHandler, UDPServer
00024 except ImportError:
00025     from socketserver import BaseRequestHandler, UDPServer
00026 
00027 class UDPEchoClient_Handler(BaseRequestHandler):
00028     def handle(self):
00029         """ One handle per connection
00030         """
00031         data, socket = self.request
00032         socket.sendto(data, self.client_address)
00033         if '{{end}}' in data:
00034             print("\n%s" % data)
00035         else:
00036             sys.stdout.write('.')
00037         stdout.flush()
00038 
00039 class UDPEchoClientTest():
00040 
00041     def send_server_ip_port(self, selftest, ip_address, port_no):
00042         c = selftest.mbed.serial_readline() # 'UDPCllient waiting for server IP and port...'
00043         if c is None:
00044             selftest.print_result(selftest.RESULT_IO_SERIAL)
00045             return
00046         selftest.notify(c.strip())
00047 
00048         selftest.notify("HOST: Sending server IP Address to target...")
00049         connection_str = ip_address + ":" + str(port_no) + "\n"
00050         selftest.mbed.serial_write(connection_str)
00051 
00052         c = selftest.mbed.serial_readline() # 'UDPCllient waiting for server IP and port...'
00053         if c is None:
00054             self.print_result(selftest.RESULT_IO_SERIAL)
00055             return
00056         selftest.notify(c.strip())
00057         return selftest.RESULT_PASSIVE
00058 
00059     def test(self, selftest):
00060         # We need to discover SERVEP_IP and set up SERVER_PORT
00061         # Note: Port 7 is Echo Protocol:
00062         #
00063         # Port number rationale:
00064         #
00065         # The Echo Protocol is a service in the Internet Protocol Suite defined
00066         # in RFC 862. It was originally proposed for testing and measurement
00067         # of round-trip times[citation needed] in IP networks.
00068         #
00069         # A host may connect to a server that supports the Echo Protocol using
00070         # the Transmission Control Protocol (TCP) or the User Datagram Protocol
00071         # (UDP) on the well-known port number 7. The server sends back an
00072         # identical copy of the data it received.
00073         SERVER_IP = str(socket.gethostbyname(socket.getfqdn()))
00074         SERVER_PORT = 7
00075 
00076         # Returning none will suppress host test from printing success code
00077         server = UDPServer((SERVER_IP, SERVER_PORT), UDPEchoClient_Handler)
00078         print("HOST: Listening for UDP connections...")
00079         self.send_server_ip_port(selftest, SERVER_IP, SERVER_PORT)
00080         server.serve_forever()