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