Machine Vision Status TCP Server

Dependencies:   C12832 EthernetInterface mbed-rtos mbed ConfigFile

Revision:
1:8efef658d90b
Child:
2:a8eebf64cd3e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TcpDaemon.cpp	Thu Mar 05 13:18:28 2015 +0000
@@ -0,0 +1,109 @@
+#include "TcpDaemon.h"
+#include "Log.h"
+
+// Incomming message end
+#define END_MSG_SEQUENCE "\r\n"
+
+namespace MachineVision{
+    /*
+     * TcpDaemon constructor
+     *
+     * @server_port the port the daemon will be listening on
+     */
+    TcpDaemon::TcpDaemon(int server_port) {
+        this->server_port = server_port;
+        this->keepListening = true;
+    }
+
+    /*
+     * Make the daemon start listening for incoming connections
+     */
+    void TcpDaemon::startListening() {
+        if (this->bindSocket()) {
+            this->doListen();
+        }
+    }
+
+    /*
+     * Bind to server socket
+     *
+     * @return true on success
+     */
+    bool TcpDaemon::bindSocket() {
+        if(server.bind(server_port) < 0) {
+            Log::e("Bind failed. Error\r\n");
+            return false;
+        } else {
+            Log::d("Bind ok\r\n");
+            return true;
+        }
+    }
+
+    /*
+     * Listen for incoming connections
+     */
+    void TcpDaemon::doListen() {
+       
+        // Listen for incoming connection
+        if (server.listen(MAX_BACKLOG) < 0) {
+            Log::w("Listen failed\r\n");
+            return;
+        }
+        
+        while (this->keepListening) {
+            Log::d("Listening for incoming connection ...\r\n");
+            
+            // Accept connection from an incoming client
+            if (server.accept(client) < 0) {
+                Log::w("Client accept failed\r\n");
+            } else {
+                Log::d("Client connected: %s\r\n", client.get_address());
+                
+                // Set non-blocking
+                client.set_blocking(false, TCP_TIMEOUT);
+                
+                while (client.is_connected()) {
+                    // Keep receiving messages from the client
+                    int read_size = 0;
+                    int total_read_size = 0;
+                    char * end = NULL;
+                    
+                    while((read_size = client.receive(buffer, BUFFER_SIZE)) > 0) {
+                        total_read_size = read_size;
+                        
+                        // Null-terminate string
+                        this->buffer[total_read_size] = '\0';
+                        Log::v("Received partial: %s\r\n", this->buffer);
+
+                        // Keep reading until full message is received
+                        end = strstr(this->buffer, END_MSG_SEQUENCE);        // Find END_MSG_SEQUENCE
+                        while (total_read_size < BUFFER_SIZE && end == NULL) {
+                            read_size = client.receive(this->buffer+total_read_size, BUFFER_SIZE-total_read_size);
+                            if (read_size != -1) {
+                                total_read_size += read_size;
+                                end = strstr(this->buffer, END_MSG_SEQUENCE);
+                            }
+                            Log::v("Waiting for rest of message\r\n");
+                        }
+                    }
+                    
+                    if (total_read_size > 0) {
+                        if (end == NULL) {
+                            Log::w("Buffer full\r\n");
+                        } else {
+                            // Null-terminate string
+                            this->buffer[total_read_size] = '\0';
+                            
+                            // Full string received, lets do our thing
+                            Log::v("Received: %s", this->buffer);
+                        }
+                    }
+                }
+                
+                Log::d("Client disconnected\r\n");
+                fflush(stdout);
+                client.close();
+            }
+        }
+    }
+}