Hot File

A C++ Embedded Web Server

View: 3063    Dowload: 1   Comment: 0   Post by: naruto  
Author: none   Category: Embedded Systems   Fields: Other

9 point/6 review File has been tested

Download   a-c-embedded-web-server.zip (751.10 KB)

You need to Sign In to download the file a-c-embedded-web-server.zip
If you do not have an account then Sign up for free here

Download error   Report copyright violation

Do you have a web page or two? Nothing fancy, perhaps, but a neat demonstration of what can be achieved with a handful of HTML tags? Do you have a sophisticated C++ Windows desktop application which now needs to be controlled and monitored remotely? So, without learning a whole new technology, let's give your application its own web page! Webem is a web server that you can embed in your C++ application. It makes it easy to implement a browser GUI accessible from anywhere. Webem is based on a minimally modified version of the boost::asio web server, and permits HTML code to execute C++ methods. Although you do not need to look at the server code to use Webem, you will need to download and use the BOOST libraries in your projects. I suggest that if you have never used BOOST before, then Webem is probably not for you.

Introduction

Background

The available embedded C++ web servers are a challenge to use, and tend not to be Windows friendly. They are not the kind of thing you want to get into just to add the ability to monitor your lab application from your cell phone.

I have tried Wt (http://www.webtoolkit.eu/wt) but was defeated by the installation and learning curve.

I recently began using Webio by John Bartas. I liked the concept and it worked well.

However, I still found it overly complicated to use and the server code hard to understand. I wanted something easier to use, based on a well known web server that had only been slightly modified.

A lot of the complexity of Webio is caused by using an HML compiler to hide the HTML pages that control the appearance of the GUI inside a file system embedded inside the application code. I prefer to have the HTML pages outside in plain view where I can adjust the GUI without recompiling the application.

I learned a lot trying to adjust Webio to my taste, and eventually was ready to build my own exactly to my requirements.

Using the Code

You build your application's GUI exactly like you build a web site - create pages using HTML all starting from index.html.

Now you need to make the HTML invoke your C++ methods. There are two things you can do:

  • Create include methods which generate HTML to be included in the web pages
  • Create action methods called by webem when the user clicks on buttons in the web page
  • Create "web controls", a combination of the previous two where an include method generates a form which invokes an action method when the user clicks on a button - the "Calendar" example application shows how to create a control to display and update a database table.

"Hello, World"

Step 1: Create the web page. This can be as elaborate as you like, but let's keep it simple for our first "hello, world" application:

The Webem Embedded Web server says:  <!--#webem hello -->

The text in angle brackets tells webem where you want to include text from the application, and "hello" is the code for the particular application method that must be invoked to provide the included text.

Step 2: Create the class which says hello:

/// An application class which says hello
class cHello
{
public:
	char * DisplayHTML()
	{
		return "Hello World";
	}
};

Step 3: Initialize Webem, telling it the address and port to listen for browser requests and where to find the index.html that begins the web page.

// Initialize web server.
http::server::cWebem theServer(
		"0.0.0.0",				// address
               "1570",					// port
	         ".\\");					// document root 

Step 4: Register the application method with webem:

cHello hello;

// register application method
// Whenever server sees <!--#webem hello -->
// call cHello::DisplayHTML() and include the HTML returned

theServer.RegisterIncludeCode( "hello",
	boost::bind(
	&cHello::DisplayHTML,	// member function
	&hello ) );		// instance of class

Step 4: Finally, you are ready to start the server running.

// run the server
theServer.Run();

A Formal Hello

Let's create a more polite program which addresses the world by name (CodeProject is, after all, Canadian.)

Step 1: Create the website:

What is your name, please?
<form action=name.webem>
<input name=yourname /><input value="Enter" type=submit />
</form>

The Webem Embedded Web server says: <!--#webem hello -->

The form provides a field to enter the user's name, and a button to submit the entered name to the server. The form attribute "action=name.webem" ensures that the webem server will call the application method registered with "name" to process the input.

Step 2: Create the application class:

/// An application class which says hello to the identified user

class cHelloForm
{
string UserName;
http::server::cWebem& myWebem;

public:
	cHelloForm( http::server::cWebem& webem ) :
	  myWebem( webem )
	{
		myWebem.RegisterIncludeCode( "hello",
		boost::bind(
		&cHelloForm::DisplayHTML,	// member function
		this ) );			// instance of class
		myWebem.RegisterActionCode( "name",
		boost::bind(
		&cHelloForm::Action,	// member function
		this ) );			// instance of class
	}
	char * DisplayHTML()
	{
		static char buf[1000];
		if( UserName.length() )
		sprintf_s( buf, 999,
			"Hello, <em>%s</em>", UserName.c_str() );
		else
			buf[0] = '\0';
		return buf;
	}
	char * Action()
	{
		UserName = myWebem.FindValue("yourname");
		return "/index.html";
	}
};

The application class stores a reference to the webem server. This allows it to look after registering its own methods with the server when it is constructed, and to call the cWebem class FindValue() to extract the value of the field entered into the form.

The application class must register two methods, one to save the entered user name when the submit button is clicked, one to display the stored user name when the web page is assembled and sent to the browser.

The action methods must return the webpage which should be displayed in response to the click on the submit button.

Note that all action methods are invoked by Webem before the include methods, so that the web page always displays updated data.

Step 3: Construct webem, construct the application class and run the server:

// Initialize web server
http::server::cWebem theServer(
	"0.0.0.0",				// address
	"1570",					// port
	".\\");					// document root

// Initialize application code
cHelloForm hello( theServer );

// run the server
theServer.Run();

You may need to run the server in another thread, perhaps so your application can continue logging data from a lab device. To do this, modify the call to server::run:

boost::thread* pThread = new boost::thread(
boost::bind(
&http::server::server::run,		// member function
&theServer ) );			// instance of class

Webem Controls

Webem controls are classes which look after the details of displaying and manipulating application data in standard ways, so that the application programmer does not need to be concerned with all the details of generating HTML text.

The demo application uses a webem control to list the contents of a SQLITE database table with the ability to add or delete records. There is a screenshot at the top of this article.

Unicode

Webem supports Unicode application include functions, which means that your code can generate and have displayed Chinese, Cyrillic, even Klingon characters. Write an include funcion that returns a wide character string UTF-16 encoded, register it using the RegisterIncludeCodeW() function ( instead of RegisterIncludeCode() ) and Webem will convert it to UTF-8 encoding before sending it back to the browser. Like this:

class cHello
{
public:
	/**
	Hello to the wide world, returning a wide character UTF-32 encoded string 
         with chinese characters

	*/
	wchar_t * DisplayWWHello()
	{
		return L"Hello Wide World.  
                  Here are some chinese characters: \x751f\x4ea7\x8bbe\x7f6e";
	}
};

...

	theServer.RegisterIncludeCodeW( "wwwhello",
		boost::bind(
		&cHello::DisplayWWHello,	// member function
		&hello ) );			// instance of class

If you wonder why UTF-8 and UTF-16 are necessary, check out my blog article, World Wide Characters.

Points of Interest

This section describes how webem is integrated with the server. It is not necessary to read this in order to use webem.

The boost::asio HTTP server invokes the method:

request_handler::handle_request( const request& req, reply& rep)

This is where the browser requests are parsed and the new page assembled to be sent back to the browser. We need to override this method in a specialization of the request handler so that, in turn, the registered application methods can be invoked.

void cWebemRequestHandler::handle_request( const request& req, reply& rep)
{
	// check for webem action request
	request req_modified = req;
	myWebem.CheckForAction( req_modified.uri );

	// call base method to do normal handling
	request_handler::handle_request( req_modified, rep);

	// Find and include any special cWebem strings
	myWebem.Include( rep.content );
}

Unfortunately, the boost::asio server, although otherwise very elegantly designed and implemented, has not been designed with inheritance in mind. In order to allow the server to invoke the webem request handler, I have made minimal changes to the boost code:

  • Made request_handler::handle_request method virtual so specialized override will be called
  • Changed server constructor to accept a reference to the request_handler it will use.
  • Changed order of server attributes so that the connection handler will not be initialized before the request handler.

Handling Post Requests

The boost:asio

A C++ Embedded Web Server

A C++ Embedded Web Server Posted on 17-04-2014  Do you have a web page or two? Nothing fancy, perhaps, but a neat demonstration of what can be achieved with a handful of HTML tags? Do you have a sophisticated C++ Windows desktop application which now needs to be controlled and monitored remotely? So, without learning a whole new technology, let's give your application its own web page! Webem is a web server that you can embed in your C++ application. It makes it easy to implement a browser GUI accessible from anywhere. Webem is based on a minimally modified version of the boost::asio web server, and permits HTML code to execute C++ methods. Although you do not need to look at the server code to use Webem, you will need to download and use the BOOST libraries in your projects. I suggest that if you have never used BOOST before, then Webem is probably not for you. 1.5/10 3063

Comment:

To comment you must be logged in members.

Files with category

  • OpenTheme : An open source graphic user interface (GUI) toolkit

    View: 4197    Download: 2   Comment: 0   Author: none  

    OpenTheme : An open source graphic user interface (GUI) toolkit

    Category: Embedded Systems
    Fields: Other

    0.29591836734694/49 review
    OpenTheme is an XML based GUI toolkit with a set of foundation class and WYSIWYG editor. Although it's originally designed for full screen GUI, it is also good for Windowed user interface, with full localization support and arbitary...

  • Design State Machine Engine for embedded system development

    View: 3364    Download: 2   Comment: 0   Author: none  

    Design State Machine Engine for embedded system development

    Category: Embedded Systems
    Fields: none

    0/5 review
    Embedded systems are some special purpose computers that are used inside of devices. Embedded systems generally use micro controllers that contain many functions of a computer on a single device. Embedded systems have to tightly work together with...

  • The StateWizard VC++ Add-in and Engine with Source Code

    View: 3859    Download: 0   Comment: 0   Author: none  

    The StateWizard VC++ Add-in and Engine with Source Code

    Category: Embedded Systems
    Fields: none

    0/7 review
    In an environment where software systems are more and more complex, concurrent, and real-time oriented, the choice of modeling languages and tools becomes the success key for many projects. This article introduces a cross-platform state-oriented...

  • Template merging with NVelocity and ASP.NET

    View: 2907    Download: 0   Comment: 0   Author: none  

    Template merging with NVelocity and ASP.NET

    Category: Embedded Systems
    Fields: Other

    0/5 review
    If you have ever tried to implement a newsletter system or a service for communicating with website users, you have probably faced the requisite to send multiple email messages with a common template and some differences, like a personalized greeting...

  • Use PowerShell to Manage your Intel AMT Data

    View: 1894    Download: 0   Comment: 0   Author: none  

    Use PowerShell to Manage your Intel AMT Data

    Category: Embedded Systems
    Fields: Other

    0.45/10 review
    This article is in the Product Showcase section for our sponsors at CodeProject. These reviews are intended to provide you with information on products and services that we consider useful and of value to developers.

  • A C++ Embedded Web Server

    View: 3063    Download: 1   Comment: 0   Author: none  

    A C++ Embedded Web Server

    Category: Embedded Systems
    Fields: Other

    0.75/6 review
    Do you have a web page or two? Nothing fancy, perhaps, but a neat demonstration of what can be achieved with a handful of HTML tags? Do you have a sophisticated C++ Windows desktop application which now needs to be controlled and monitored remotely?...

  • Java/.NET Integration as Simple as Possible

    View: 1487    Download: 2   Comment: 0   Author: none  

    Java/.NET Integration as Simple as Possible

    Category: Embedded Systems
    Fields: Other

    0/3 review
    For a number of years, I have been developing software for integrating applications written in different platforms. And in my business, I face with interoperability issues: Java and C++, Java and COM, Java and .NET. To solve them, I have developed a...

  • Writing a 16-bit dummy kernel in C/C++

    View: 2666    Download: 3   Comment: 0   Author: none  

    Writing a 16-bit dummy kernel in C/C++

    Category: Embedded Systems
    Fields: Other

    0/11 review
    In my previous articles I was only briefing about on how to write a boot loader. That was fun and challenging. I enjoyed it a lot. But after learning how to write a boot-loader I wanted to write much better stuff like embedding more functionality...

 
Newsletter Email

File suggestion for you

File top downloads

logo codetitle
Codetitle.com - library source code to share, download the file to the community
Copyright © 2015. All rights reserved. codetitle.com Develope by Vinagon .Ltd