News Archive (1999-2012) | 2013-current at LinuxGizmos | Current Tech News Portal |    About   

Article: Implementing an embedded Linux web app framework

Aug 3, 2006 — by LinuxDevices Staff — from the LinuxDevices Archive — 9 views

Foreword — This article describes a way to build embedded Linux web applications that follow the “Model, View, Controller” (MVC) software architecture popular nowadays among enterprise web application developers, yet are capable of running on a 130MHz ARM processor. The method is based on several popular open source software components. Enjoy . . . !


How to implement a web application framework in an embedded Linux system

by Cliff Brake

As devices are increasingly more networked, an embedded web server is becoming a standard way for users to interact with and configure an embedded device using a standard web browser. As an example, most pieces of networking equipment (such as routers and wireless access points) are configured this way.

There are many ways to implement a web server. One way is to just write a monolithic program that handles the requests and outputs HTML using print statements. Without a lot of planning, you usually end up with something where logic is intermixed with presentation. The disadvantage of such an approach is that it quickly becomes very difficult to maintain your web application as it grows and changes. The current best practice with web application frameworks (such as Ruby on Rails) splits a web application into 3 distinct components (Model, View, Controller — MVC) so that changes to one component can be made with minimal impact to others.

This article details a solution used in a recent project to implement such an architecture using the following open source components: SQLite, Clearsilver, and Python. The challenge was to find a solution that performed acceptably on a resource constrained 130MHz ARM-Linux system.

Components Used

The requirements for the web application framework for this system are:

  • Footprint must be fairly small — less than 10MiB.
  • Must enable us to implement a clean MVC type architecture
  • Must support a high level language like python for rapid development
  • Includes a database
  • Reasonable performance — less than 1 second to render a typical page

After researching and testing several options, the following components were chosen to implement the framework:

  • Web Server: Cherokee (1MiB)
  • Model: SQLite (290KB)
  • View: Clearsilver (170KB)
  • Controller: Python (2-3MB)


Web application architecture

Total size for the above components is about 4MB.

The following sequence occurs during a typical web transaction:

  • An HTTP request is received by Cherokee
  • If the request matches the URI for the web application, Cherokee forwards the request to the web application framework via CGI
  • Clearsilver parses the HTTP headers (including CGI GET and POST parameters) and provides the data to python in easy-to-access HDF (hierarchical data format)
  • Python looks at the URI and dispatches the request to the appropriate function; data is extracted from the SQLite database and an HDF datastructure is created.
  • Clearsilver is called to render the appropriate template; data from the HDF datastructure is used in the template to provide the dynamic content in the page
  • The rendered HTML is passed back to Cherokee, and is then returned to the user's web browser

Each of these components is discussed in more detail in the following sections.

Performance

One of the issues with modern web frameworks is that most require a lot of processing power. Many frameworks are written in interpreted languages, which tend to be not very efficient on embedded systems such as the 130MHz ARM-Linux system used in this project. As most of the application development for this project is done in Python, I tried several other Python-based solutions with the following results:

  • Django — takes 10 seconds to render a page
  • webpy — takes 5 seconds to render a page
  • Clearsilver/Python — takes about 1 second to render a page

Anything over 1 second is very slow for a web interface. With fastcgi, webpy could probably have been sped up to be acceptably fast, but I don't think there is much hope for frameworks like django on this type of system. I have read of similar experiences trying to run Ruby-on-Rails on ARM-Linux systems. As I'm currently just using a basic CGI interface, the Clearsilver solution could also be sped up significantly as well, if the Python portion could be kept running between requests with fastcgi or a similar mechanism.

Cherokee Web Server

A web application typically consists of a web server (like Apache) and a program that provides the dynamic web content. Some of the functions of a web server are:

  • receives HTTP requests
  • handles the request by serving static files/images or routes the request to a program that outputs dynamic content
  • authentication
  • encryption (SSL, TLS)

There are many web servers that can be used in embedded devices. Some of them are:

Cherokee was chosen for this application because it provides a good balance between the size and functionality I am looking for. It is also included in the OpenEmbedded build system I am using. Other systems will have different requirements — use the one that fits your application best.

Clearsilver

Clearsilver is the real gem that was discovered during this exercise. Clearsilver is a language-neutral HTML template system written in C. It is used as the templating system for many high volume sites such as Google Groups 2. Clearsilver also provides CGI handling functions and bindings to several languages including Python.

The fact that it is written in C and is fast for high volume sites also makes it fast enough on slower embedded systems. Because Clearsilver is written in C, the templating system is already fast. Parts or all of the application can also be moved to C as needed to get the required performance. Having a performance upgrade path is nice. Clearsilver also forces a strict separation of application logic and presentation templates, which keeps things in line with the MVC architecture.

SQLite

SQLite is a small C library that implements a self-contained, embeddable, zero-configuration SQL database engine. SQLite works as expected and also has bindings to a number of languages including Python. A SQLite database is just a single file that requires no configuration, making it very easy to use. The database engine runs in the same process as the application using it, which increases performance because there is no context switch when running database operations.

Because SQLite does not have its own process, it does not handle concurrency as well as other databases such as MySQL, but in an embedded system you seldom have more than one process using the database. SQLite provides some support for concurrency with reader/writer locks for the entire database. If your application is not real busy, this is often adequate if you need two processes to share some data.

Python

Python is the glue that ties things together in this web application framework. It sits between Clearsilver and the database. The advantages of using a language like Python are rapid development and an extensive library. Python is considerably slower than C, but there is always the option to move parts of the application into C as needed.

OpenEmbedded Build System

How does one put together a system with all these components? The answer is the OpenEmbedded Build System (OE). OE includes support for all of the components discussed in this article.

Summary

The combination of Python, Clearsilver, and SQLite provides a compelling solution for a web application framework in an embedded Linux system. The solution is reasonably small, performs well, and provides a path to improve performance if needed. We also get a clean architecture to create clean, maintainable web applications.

Copyright (C) BEC Systems, 2006. All rights reserved. Reproduced by LinuxDevices.com with permission.


About the author — Cliff Brake owns BEC Systems, a consulting operation that “helps customers utilize modern computer technologies in their products.” BEC offers a range of services “to make its customers' embedded projects a success,” including technology selection, development, troubleshooting, and training. Further information and other free resources are available on the company's website.


 
This article was originally published on LinuxDevices.com and has been donated to the open source community by QuinStreet Inc. Please visit LinuxToday.com for up-to-date news and articles about Linux and open source.



Comments are closed.