Poll Event Loop Sample in C

The Poll Event Loop sample demonstrates how to add HTTP server functionality to your C application and use a polled event loop to wait for I/O events. AppWeb offers several methods to wait for events. See the Programming Paradigms document for further information.

The sample is a single-threaded main program that listens on port 8888 for HTTP requests. See also the equivalent C++ pollEventLoop sample.

Files

pollEventLoop.conf
pollEventLoop.c

Configuration File

pollEventLoop.conf

DocumentRoot "."
Listen 8888
ThreadLimit 0

LoadModule ejs ../../../lib/libejsModule
LoadModule egi ../../../lib/libegiHandler
LoadModule esp ../../../lib/libespHandler
LoadModule static ../../../lib/libcopyHandler

AddHandler egiHandler .egi
AddHandler espHandler .esp
AddHandler copyHandlerc

This configuration file loads the embedded JavaScript module and the embedded server pages and embedded gateway interface, and static content handlers. It is configured to run single-threaded. It assumes that the sample is being run from the C/pollEventLoop directory. Modify these module paths to suit your installation.

You should modify the DocumentRoot and Listen directives to suit your application's needs.

Source Code

pollEventLoop.c

/*
* Copyright (c) Mbedthis Software LLC, 2003-2004. All Rights Reserved.
*/
/*!
* @file pollEventLoop.c
* @brief Embed the AppWeb server in a simple single-threaded C
* application that uses a polling event loop.
*/

/******************************* Includes *****************************/

#include "appWeb/appWeb.h"

#if MPR_FEATURE_C_API
/************************** Forward Declarations **********************/

static void eventLoop();

/********************************* Code *******************************/

int main(int argc, char** argv)
{
MaHttp *http; /* For the http service inside our app */
MaServer *server; /* For a HTTP server */

/*
* Initialize the run-time and give our app a name
* "pollEventLoop"
*/
mprCreateMpr("pollEventLoop");

/*
* Do the following two statements only if you want debug trace
*/
mprAddLogFileListener();
mprSetLogSpec("stdout:4");

/*
* Start run-time services
*/
mprStartMpr(0);

/*
* Create the HTTP and server objects. Give the server a name
* "default" and define "." as the default serverRoot, ie. the
* directory with the server configuration files.
*/
http = maCreateHttp();
server = maCreateServer(http, "default", ".");

/*
* Configure the server based on the directives in
* pollEventLoop.conf.
*/
if (maConfigureServer(server, "pollEventLoop.conf", 0) < 0) {
fprintf(stderr,
"Can't configure the server. Error on line %d\n",
maGetConfigErrorLine(server));
exit(2);
}

/*
* Start serving pages. After this we are live.
*/
if (maStartServers(http) < 0) {
fprintf(stderr, "Can't start the server\n");
exit(2);
}

/*
* Service events. This call will block until the server is exited
* Call mprTerminate() at any time to instruct the server to exit.
*/
eventLoop();

/*
* Stop all HTTP services
*/
maStopServers(http);

/*
* Delete the server and http objects
*/
maDeleteServer(server);
maDeleteHttp(http);

/*
* Stop and delete the run-time services
*/
mprStopMpr();
mprDeleteMpr();

return 0;
}

/**********************************************************************/
/*
* Sample main event loop using polling. This demonstrates how to
* integrate AppWeb with your applications event loop using a polled
* event loop architecture.
 */

static void eventLoop()
{
int timeout, till;

/*
* We will nap for 50 milliseconds to avoid busy waiting
*/
timeout = 50;

while (!mprIsExiting()) {

if (mprRunTimers() > 0) {
till = 0;
} else {
till = mprGetIdleTime();
}

/*
* This will run tasks if maxThreads == 0 (single threaded). If
* multithreaded, the thread pool will run tasks
*/
if (mprRunTasks() > 0) { /* Returns > 0 if more work to do */
till = 0; /* So don't block in select */
}

/*
* Do some work here
*/


/*
* Now service any pending I/O
*/
mprServiceEvents(1, min(timeout, till));
}
}

/**********************************************************************/
#else // MPR_FEATURE_C_API

int main()
{
fprintf(stderr, "MPR_FEATURE_C_API is not defined in config.h\n");
exit(2);
}
#endif // MPR_FEATURE_C_API




© Mbedthis Software LLC, 2003-2204. All rights reserved. Mbedthis is a trademark of Mbedthis Software LLC.