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