Sample to show hanging during socket initialization

Dependencies:   EthernetInterface NTPClient mbed-rtos mbed

sample server (python) to go with the RTOSTest

Import programRTOSTest

Sample to show hanging during socket initialization

#!/usr/bin/python2.7

import sys, os
import asyncore, socket
import time, datetime

class EchoClient(asyncore.dispatcher_with_send):
	def handle_read(self):
		buffer = self.recv(1024)
		if buffer:
			print str(datetime.datetime.fromtimestamp(time.time()))
			print buffer
		else:
			self.close()
	
class Server(asyncore.dispatcher):
	def __init__(self, host, port):
		asyncore.dispatcher.__init__(self)
		self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
		self.set_reuse_addr()
		self.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
		self.bind(('', port))
		self.listen(8)

	def handle_accept(self):
		socket, address = self.accept()
		print str(datetime.datetime.fromtimestamp(time.time()))
		print 'Connection by', address
		EchoClient(socket)


s = Server('', 8080)
asyncore.loop()

Committer:
jonathonfletcher
Date:
Thu Sep 27 12:06:18 2012 +0000
Revision:
0:5197a41c178f
Child:
1:92d3f1118200
Example of rtos / ethernet / tcp / udp hanging

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jonathonfletcher 0:5197a41c178f 1 #include "mbed.h"
jonathonfletcher 0:5197a41c178f 2 #include "rtos.h"
jonathonfletcher 0:5197a41c178f 3
jonathonfletcher 0:5197a41c178f 4 #include "EthernetInterface.h"
jonathonfletcher 0:5197a41c178f 5 #include "NTPClient.h"
jonathonfletcher 0:5197a41c178f 6
jonathonfletcher 0:5197a41c178f 7
jonathonfletcher 0:5197a41c178f 8 const char *newline = "\r\n";
jonathonfletcher 0:5197a41c178f 9 const unsigned int connection_timeout = 6000;
jonathonfletcher 0:5197a41c178f 10 const unsigned int network_ticker_period = 60000;
jonathonfletcher 0:5197a41c178f 11 const unsigned int producer_ticker_period = 15000;
jonathonfletcher 0:5197a41c178f 12
jonathonfletcher 0:5197a41c178f 13
jonathonfletcher 0:5197a41c178f 14 BusOut leds(LED1, LED2, LED3, LED4);
jonathonfletcher 0:5197a41c178f 15
jonathonfletcher 0:5197a41c178f 16 typedef struct {
jonathonfletcher 0:5197a41c178f 17 uint16_t count;
jonathonfletcher 0:5197a41c178f 18 } tMessage;
jonathonfletcher 0:5197a41c178f 19
jonathonfletcher 0:5197a41c178f 20 Mail<tMessage, 64> mailbox;
jonathonfletcher 0:5197a41c178f 21
jonathonfletcher 0:5197a41c178f 22 EthernetInterface ethernet;
jonathonfletcher 0:5197a41c178f 23 TCPSocketConnection socket;
jonathonfletcher 0:5197a41c178f 24
jonathonfletcher 0:5197a41c178f 25 static bool timeset = false;
jonathonfletcher 0:5197a41c178f 26 NTPClient ntp;
jonathonfletcher 0:5197a41c178f 27
jonathonfletcher 0:5197a41c178f 28
jonathonfletcher 0:5197a41c178f 29
jonathonfletcher 0:5197a41c178f 30 void connection_callback()
jonathonfletcher 0:5197a41c178f 31 {
jonathonfletcher 0:5197a41c178f 32 printf("%s:%d%s", __FILE__, __LINE__, newline);
jonathonfletcher 0:5197a41c178f 33
jonathonfletcher 0:5197a41c178f 34 leds = (leds ^ 1);
jonathonfletcher 0:5197a41c178f 35 char data[512];
jonathonfletcher 0:5197a41c178f 36 char *p = data;
jonathonfletcher 0:5197a41c178f 37
jonathonfletcher 0:5197a41c178f 38 uint16_t nmessages = 0;
jonathonfletcher 0:5197a41c178f 39 osEvent evt;
jonathonfletcher 0:5197a41c178f 40 do {
jonathonfletcher 0:5197a41c178f 41 evt = mailbox.get(0);
jonathonfletcher 0:5197a41c178f 42 if (osEventMail == evt.status) {
jonathonfletcher 0:5197a41c178f 43 tMessage *message = (tMessage *)evt.value.p;
jonathonfletcher 0:5197a41c178f 44 if (message) {
jonathonfletcher 0:5197a41c178f 45 nmessages += 1;
jonathonfletcher 0:5197a41c178f 46 p += snprintf(p, sizeof(data) - (p-data), "%d%s", message->count, newline);
jonathonfletcher 0:5197a41c178f 47 mailbox.free(message);
jonathonfletcher 0:5197a41c178f 48 }
jonathonfletcher 0:5197a41c178f 49 }
jonathonfletcher 0:5197a41c178f 50 } while (osEventMail == evt.status && (p-data) < 384);
jonathonfletcher 0:5197a41c178f 51
jonathonfletcher 0:5197a41c178f 52 printf("%s:%d nmessages:%d%s", __FILE__, __LINE__, nmessages, newline);
jonathonfletcher 0:5197a41c178f 53 if (!socket.is_connected()) {
jonathonfletcher 0:5197a41c178f 54 socket.connect("10.0.1.253", 8080);
jonathonfletcher 0:5197a41c178f 55 }
jonathonfletcher 0:5197a41c178f 56
jonathonfletcher 0:5197a41c178f 57 printf("%s:%d%s", __FILE__, __LINE__, newline);
jonathonfletcher 0:5197a41c178f 58 if (socket.is_connected()) {
jonathonfletcher 0:5197a41c178f 59 if (p > data) {
jonathonfletcher 0:5197a41c178f 60 printf(data);
jonathonfletcher 0:5197a41c178f 61 printf(newline);
jonathonfletcher 0:5197a41c178f 62 printf("%s:%d%s", __FILE__, __LINE__, newline);
jonathonfletcher 0:5197a41c178f 63 int nsent = socket.send_all(data, (p-data)-1);
jonathonfletcher 0:5197a41c178f 64 printf("%s:%d: nmessages:%d, length:%d, nsent:%d, %s", __FILE__, __LINE__, nmessages, (p-data)-1, nsent, newline);
jonathonfletcher 0:5197a41c178f 65 }
jonathonfletcher 0:5197a41c178f 66 socket.close();
jonathonfletcher 0:5197a41c178f 67 } else {
jonathonfletcher 0:5197a41c178f 68 printf("%s:%d: nmessages:%d, discarded_length:%d%s", __FILE__, __LINE__, nmessages, (p-data)-1, newline);
jonathonfletcher 0:5197a41c178f 69 }
jonathonfletcher 0:5197a41c178f 70
jonathonfletcher 0:5197a41c178f 71 printf("%s:%d%s", __FILE__, __LINE__, newline);
jonathonfletcher 0:5197a41c178f 72 }
jonathonfletcher 0:5197a41c178f 73
jonathonfletcher 0:5197a41c178f 74
jonathonfletcher 0:5197a41c178f 75 void network_callback (const void *context)
jonathonfletcher 0:5197a41c178f 76 {
jonathonfletcher 0:5197a41c178f 77 printf("%s:%d%s", __FILE__, __LINE__, newline);
jonathonfletcher 0:5197a41c178f 78
jonathonfletcher 0:5197a41c178f 79 if (!ethernet.getIPAddress()) {
jonathonfletcher 0:5197a41c178f 80 printf("%s:%d%s", __FILE__, __LINE__, newline);
jonathonfletcher 0:5197a41c178f 81 ethernet.connect(connection_timeout);
jonathonfletcher 0:5197a41c178f 82 printf("%s:%d%s", __FILE__, __LINE__, newline);
jonathonfletcher 0:5197a41c178f 83 }
jonathonfletcher 0:5197a41c178f 84
jonathonfletcher 0:5197a41c178f 85 if (ethernet.getIPAddress()) {
jonathonfletcher 0:5197a41c178f 86 printf("%s:%d%s", __FILE__, __LINE__, newline);
jonathonfletcher 0:5197a41c178f 87 leds = (leds ^ 2);
jonathonfletcher 0:5197a41c178f 88 if (false == timeset) {
jonathonfletcher 0:5197a41c178f 89 printf("%s:%d%s", __FILE__, __LINE__, newline);
jonathonfletcher 0:5197a41c178f 90 if (NTP_OK == ntp.setTime("0.pool.ntp.org")) {
jonathonfletcher 0:5197a41c178f 91 printf("%s:%d timeset%s", __FILE__, __LINE__, newline);
jonathonfletcher 0:5197a41c178f 92 timeset = true;
jonathonfletcher 0:5197a41c178f 93 }
jonathonfletcher 0:5197a41c178f 94 printf("%s:%d%s", __FILE__, __LINE__, newline);
jonathonfletcher 0:5197a41c178f 95 }
jonathonfletcher 0:5197a41c178f 96
jonathonfletcher 0:5197a41c178f 97 connection_callback();
jonathonfletcher 0:5197a41c178f 98
jonathonfletcher 0:5197a41c178f 99 ethernet.disconnect();
jonathonfletcher 0:5197a41c178f 100 printf("%s:%d%s", __FILE__, __LINE__, newline);
jonathonfletcher 0:5197a41c178f 101 }
jonathonfletcher 0:5197a41c178f 102
jonathonfletcher 0:5197a41c178f 103 printf("%s:%d%s", __FILE__, __LINE__, newline);
jonathonfletcher 0:5197a41c178f 104 }
jonathonfletcher 0:5197a41c178f 105
jonathonfletcher 0:5197a41c178f 106
jonathonfletcher 0:5197a41c178f 107 void producer_ticker(const void *context)
jonathonfletcher 0:5197a41c178f 108 {
jonathonfletcher 0:5197a41c178f 109 printf("%s:%d%s", __FILE__, __LINE__, newline);
jonathonfletcher 0:5197a41c178f 110 static uint16_t counter = 0;
jonathonfletcher 0:5197a41c178f 111
jonathonfletcher 0:5197a41c178f 112 tMessage *message = mailbox.alloc();
jonathonfletcher 0:5197a41c178f 113 if (message) {
jonathonfletcher 0:5197a41c178f 114 leds = (leds ^ 4);
jonathonfletcher 0:5197a41c178f 115 memset(message, 0, sizeof(*message));
jonathonfletcher 0:5197a41c178f 116 message->count = ++counter;
jonathonfletcher 0:5197a41c178f 117 mailbox.put(message);
jonathonfletcher 0:5197a41c178f 118 }
jonathonfletcher 0:5197a41c178f 119 printf("%s:%d%s", __FILE__, __LINE__, newline);
jonathonfletcher 0:5197a41c178f 120 }
jonathonfletcher 0:5197a41c178f 121
jonathonfletcher 0:5197a41c178f 122
jonathonfletcher 0:5197a41c178f 123 int main()
jonathonfletcher 0:5197a41c178f 124 {
jonathonfletcher 0:5197a41c178f 125 printf("%s:%d%s", __FILE__, __LINE__, newline);
jonathonfletcher 0:5197a41c178f 126 ethernet.init();
jonathonfletcher 0:5197a41c178f 127 printf("%s:%d%s", __FILE__, __LINE__, newline);
jonathonfletcher 0:5197a41c178f 128
jonathonfletcher 0:5197a41c178f 129 RtosTimer producer(producer_ticker);
jonathonfletcher 0:5197a41c178f 130 RtosTimer network(network_callback);
jonathonfletcher 0:5197a41c178f 131 printf("%s:%d%s", __FILE__, __LINE__, newline);
jonathonfletcher 0:5197a41c178f 132
jonathonfletcher 0:5197a41c178f 133 producer.start(producer_ticker_period);
jonathonfletcher 0:5197a41c178f 134 network.start(network_ticker_period);
jonathonfletcher 0:5197a41c178f 135 printf("%s:%d%s", __FILE__, __LINE__, newline);
jonathonfletcher 0:5197a41c178f 136
jonathonfletcher 0:5197a41c178f 137 while (true) {
jonathonfletcher 0:5197a41c178f 138 Thread::wait(500);
jonathonfletcher 0:5197a41c178f 139 leds = (leds ^ 8);
jonathonfletcher 0:5197a41c178f 140 }
jonathonfletcher 0:5197a41c178f 141 }