2014-03-17 17:29:18 +00:00
|
|
|
"""
|
|
|
|
mbed SDK
|
|
|
|
Copyright (c) 2011-2013 ARM Limited
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
"""
|
|
|
|
|
2014-09-15 16:43:56 +00:00
|
|
|
import sys
|
2014-03-17 17:29:18 +00:00
|
|
|
import socket
|
|
|
|
from sys import stdout
|
2014-10-30 15:22:02 +00:00
|
|
|
from host_test import HostTestResults, Test
|
2014-09-15 16:43:56 +00:00
|
|
|
from SocketServer import BaseRequestHandler, TCPServer
|
|
|
|
|
2014-03-17 17:29:18 +00:00
|
|
|
|
|
|
|
SERVER_IP = str(socket.gethostbyname(socket.getfqdn()))
|
|
|
|
SERVER_PORT = 7
|
|
|
|
|
2014-09-15 16:43:56 +00:00
|
|
|
|
2014-03-17 17:29:18 +00:00
|
|
|
class TCPEchoClientTest(Test):
|
|
|
|
def __init__(self):
|
2014-10-30 15:22:02 +00:00
|
|
|
HostTestResults.__init__(self)
|
2014-03-17 17:29:18 +00:00
|
|
|
Test.__init__(self)
|
|
|
|
|
|
|
|
def send_server_ip_port(self, ip_address, port_no):
|
2014-09-15 16:43:56 +00:00
|
|
|
""" Set up network host. Reset target and and send server IP via serial to Mbed
|
|
|
|
"""
|
|
|
|
c = self.mbed.serial_readline() # 'TCPCllient waiting for server IP and port...'
|
2014-09-04 09:10:55 +00:00
|
|
|
if c is None:
|
2014-10-17 14:14:53 +00:00
|
|
|
self.print_result(self.RESULT_IO_SERIAL)
|
2014-09-04 09:10:55 +00:00
|
|
|
return
|
|
|
|
|
2014-10-17 14:14:53 +00:00
|
|
|
self.notify(c.strip())
|
|
|
|
self.notify("HOST: Sending server IP Address to target...")
|
|
|
|
|
2014-03-17 17:29:18 +00:00
|
|
|
connection_str = ip_address + ":" + str(port_no) + "\n"
|
2014-08-14 10:29:56 +00:00
|
|
|
self.mbed.serial_write(connection_str)
|
2014-10-17 14:14:53 +00:00
|
|
|
self.notify(connection_str)
|
2014-09-15 16:43:56 +00:00
|
|
|
|
|
|
|
# Two more strings about connection should be sent by MBED
|
|
|
|
for i in range(0, 2):
|
|
|
|
c = self.mbed.serial_readline()
|
|
|
|
if c is None:
|
2014-10-17 14:14:53 +00:00
|
|
|
self.print_result(self.RESULT_IO_SERIAL)
|
2014-09-15 16:43:56 +00:00
|
|
|
return
|
2014-10-17 14:14:53 +00:00
|
|
|
self.notify(c.strip())
|
2014-03-17 17:29:18 +00:00
|
|
|
|
2014-10-30 15:22:02 +00:00
|
|
|
def test(self):
|
|
|
|
# Returning none will suppress host test from printing success code
|
|
|
|
return None
|
|
|
|
|
2014-03-17 17:29:18 +00:00
|
|
|
|
|
|
|
class TCPEchoClient_Handler(BaseRequestHandler):
|
|
|
|
def handle(self):
|
2014-10-30 15:22:02 +00:00
|
|
|
""" One handle per connection
|
|
|
|
"""
|
2014-09-15 16:43:56 +00:00
|
|
|
print "HOST: Connection received...",
|
|
|
|
count = 1;
|
2014-03-17 17:29:18 +00:00
|
|
|
while True:
|
|
|
|
data = self.request.recv(1024)
|
|
|
|
if not data: break
|
|
|
|
self.request.sendall(data)
|
2014-09-15 16:43:56 +00:00
|
|
|
if '{{end}}' in str(data):
|
|
|
|
print
|
|
|
|
print str(data)
|
|
|
|
else:
|
|
|
|
if not count % 10:
|
|
|
|
sys.stdout.write('.')
|
|
|
|
count += 1
|
2014-03-17 17:29:18 +00:00
|
|
|
stdout.flush()
|
|
|
|
|
|
|
|
|
|
|
|
server = TCPServer((SERVER_IP, SERVER_PORT), TCPEchoClient_Handler)
|
2014-10-30 15:22:02 +00:00
|
|
|
print "HOST: Listening for TCP connections: " + SERVER_IP + ":" + str(SERVER_PORT)
|
2014-03-17 17:29:18 +00:00
|
|
|
|
|
|
|
mbed_test = TCPEchoClientTest();
|
2014-10-30 15:22:02 +00:00
|
|
|
mbed_test.run()
|
2014-03-17 17:29:18 +00:00
|
|
|
mbed_test.send_server_ip_port(SERVER_IP, SERVER_PORT)
|
|
|
|
|
|
|
|
server.serve_forever()
|