Skip to content
davidrmiller edited this page Nov 19, 2014 · 3 revisions

Overview

The neural2d console program contains an embedded web server that provides an interface through a web browser.

The embedded web server uses only C++-11 and standard POSIX sockets provided by the headers <sys/socket.h> and <netinet/in.h>.

The integrated web server has been tested on Linux systems and on Windows using Cygwin.

By default, the web server is compiled into neural2d. To prevent the web server from being compiled, add the following to the g++ command line in the Makefile:

 -DDISABLE_WEBSERVER

Theory of operation

When the web server is started with a call to WebServer::start(), it launches a thread that opens a socket and listens to the specified port. It also initializes an HTTP response page in memory by reading and parsing the file http-response-template.txt. That file is a template that the web server uses to dynamically create an HTTP response when needed.

The HTML page defined by http-response-template.txt contains an HTML page in which each <input> element is wrapped in a <form> element. When the user changes a field in the web form, it causes a submit() of the form, which includes just the single element inside the enclosing form element. The neural2d program receives this as a GET or POST, extracts the relevant information and pushes it onto a thread-safe message queue. At the end of each forward propagation, the main neural2d thread acts on any command(s) in the message queue, updates internal state, and sends back an HTTP reply with an updated HTML page.

When the web server constructs an HTTP response, it does so by creating a string containing a series of JavaScript variable assignments. The webserver inserts that string into the middle of the response template. The template contains sufficient JavaScript logic to display the GUI fields properly.

Clone this wiki locally