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()

Files at this revision

API Documentation at this revision

Comitter:
jonathonfletcher
Date:
Thu Sep 27 12:06:18 2012 +0000
Child:
1:92d3f1118200
Commit message:
Example of rtos / ethernet / tcp / udp hanging

Changed in this revision

EthernetInterface.lib Show annotated file Show diff for this revision Revisions of this file
NTPClient.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/EthernetInterface.lib	Thu Sep 27 12:06:18 2012 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/EthernetInterface/#a0ee3ae75cfa
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/NTPClient.lib	Thu Sep 27 12:06:18 2012 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/donatien/code/NTPClient/#881559865a93
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Sep 27 12:06:18 2012 +0000
@@ -0,0 +1,141 @@
+#include "mbed.h"
+#include "rtos.h"
+
+#include "EthernetInterface.h"
+#include "NTPClient.h"
+
+
+const char *newline = "\r\n";
+const unsigned int connection_timeout = 6000;
+const unsigned int network_ticker_period = 60000;
+const unsigned int producer_ticker_period = 15000;
+
+
+BusOut leds(LED1, LED2, LED3, LED4);
+
+typedef struct {
+    uint16_t count;
+} tMessage;
+
+Mail<tMessage, 64> mailbox;
+
+EthernetInterface ethernet;
+TCPSocketConnection socket;
+
+static bool timeset = false;
+NTPClient ntp;
+
+
+
+void connection_callback()
+{
+    printf("%s:%d%s", __FILE__, __LINE__, newline);
+
+    leds = (leds ^ 1);
+    char data[512];
+    char *p = data;
+
+    uint16_t nmessages = 0;
+    osEvent evt;
+    do {
+        evt = mailbox.get(0);
+        if (osEventMail == evt.status) {
+            tMessage *message = (tMessage *)evt.value.p;
+            if (message) {
+                nmessages += 1;
+                p += snprintf(p, sizeof(data) - (p-data), "%d%s", message->count, newline);
+                mailbox.free(message);
+            }
+        }
+    } while (osEventMail == evt.status && (p-data) < 384);
+
+    printf("%s:%d nmessages:%d%s", __FILE__, __LINE__, nmessages, newline);
+    if (!socket.is_connected()) {
+        socket.connect("10.0.1.253", 8080);
+    }
+
+    printf("%s:%d%s", __FILE__, __LINE__, newline);
+    if (socket.is_connected()) {
+        if (p > data) {
+            printf(data);
+            printf(newline);
+            printf("%s:%d%s", __FILE__, __LINE__, newline);
+            int nsent = socket.send_all(data, (p-data)-1);
+            printf("%s:%d: nmessages:%d, length:%d, nsent:%d, %s", __FILE__, __LINE__, nmessages, (p-data)-1, nsent, newline);
+        }
+        socket.close();
+    } else {
+        printf("%s:%d: nmessages:%d, discarded_length:%d%s", __FILE__, __LINE__, nmessages, (p-data)-1, newline);
+    }
+
+    printf("%s:%d%s", __FILE__, __LINE__, newline);
+}
+
+
+void network_callback (const void *context)
+{
+    printf("%s:%d%s", __FILE__, __LINE__, newline);
+
+    if (!ethernet.getIPAddress()) {
+        printf("%s:%d%s", __FILE__, __LINE__, newline);
+        ethernet.connect(connection_timeout);
+        printf("%s:%d%s", __FILE__, __LINE__, newline);
+    }
+
+    if (ethernet.getIPAddress()) {
+        printf("%s:%d%s", __FILE__, __LINE__, newline);
+        leds = (leds ^ 2);
+        if (false == timeset) {
+            printf("%s:%d%s", __FILE__, __LINE__, newline);
+            if (NTP_OK == ntp.setTime("0.pool.ntp.org")) {
+                printf("%s:%d timeset%s", __FILE__, __LINE__, newline);
+                timeset = true;
+            }
+            printf("%s:%d%s", __FILE__, __LINE__, newline);
+        }
+
+        connection_callback();
+
+        ethernet.disconnect();
+        printf("%s:%d%s", __FILE__, __LINE__, newline);
+    }
+
+    printf("%s:%d%s", __FILE__, __LINE__, newline);
+}
+
+
+void producer_ticker(const void *context)
+{
+    printf("%s:%d%s", __FILE__, __LINE__, newline);
+    static uint16_t counter = 0;
+
+    tMessage *message = mailbox.alloc();
+    if (message) {
+        leds = (leds ^ 4);
+        memset(message, 0, sizeof(*message));
+        message->count = ++counter;
+        mailbox.put(message);
+    }
+    printf("%s:%d%s", __FILE__, __LINE__, newline);
+}
+
+
+int main()
+{
+    printf("%s:%d%s", __FILE__, __LINE__, newline);
+    ethernet.init();
+    printf("%s:%d%s", __FILE__, __LINE__, newline);
+
+    RtosTimer producer(producer_ticker);
+    RtosTimer network(network_callback);
+    printf("%s:%d%s", __FILE__, __LINE__, newline);
+
+    producer.start(producer_ticker_period);
+    network.start(network_ticker_period);
+    printf("%s:%d%s", __FILE__, __LINE__, newline);
+
+    while (true) {
+        Thread::wait(500);
+        leds = (leds ^ 8);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Thu Sep 27 12:06:18 2012 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed-rtos/#9654a71f5a90
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu Sep 27 12:06:18 2012 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/cd19af002ccc
\ No newline at end of file