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
Makefile
pollEventLoop.cpp

Configuration File

pollEventLoop.conf

DocumentRoot "."
Listen 8888
ThreadLimit 0
AddHandler copyHandler


This file is an AppWeb configuration file. It is configured to run single-threaded and assumes that the sample is being run from the current directory.

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

Makefile

The Makefile will build on Windows or Linux. A Windows VS 6.0 project file is also supplied.

Typical output from the Makefile build is listed below. This is the output on a Windows system:

cl -o pollEventLoop.exe pollEventLoop.cpp -Zi -Od -D_NDEBUG -W3 -nologo -MDd -FD -DWIN -D_DLL -D_MT \
  -D_WINDOWS -DWIN32 -D_WIN32_WINNT=0x500 -D_X86_=1 -D_CRT_SECURE_NO_DEPRECATE -D_USRDLL \
  -I../../../include  ../../../bin/libappWebStatic.lib ws2_32.lib advapi32.lib user32.lib

Source Code

pollEventLoop.cpp

///
/// @file   pollEventLoop.cpp
/// @brief  Embed the AppWeb server in a single-threaded program using
///         a polled event loop.
//  Copyright (c) Mbedthis Software LLC, 2003-2005. All Rights Reserved.
//
/////////////////////////////// Includes ///////////////////////////////

#include    "appWeb/appWeb.h"

//////////////////////////// Forward Declarations //////////////////////

static void eventLoop();

/////////////////////////////////// Code ///////////////////////////////

int main(int argc, char** argv)
{
    MaHttp      *http;                  // Http service inside our app
    MaServer    *server;                // For the HTTP server
    Mpr         mpr("pollEventLoop");   // Initialize the run time

#if BLD_FEATURE_LOG
    //
    //  Do the following two statements only if you want debug trace
    //
    mpr.addListener(new MprLogToFile());
    mpr.setLogSpec("stdout:4");
#endif

    //
    //  Start the Mbedthis Portable Runtime and request single threading
    //
    mpr.start();

    //
    //  Create Http and Server objects for this application. We set the
    //  ServerName to be "default" and the initial serverRoot to be ".".
    //  This will be overridden in pollEventLoop.conf.
    //
    http = new MaHttp();
    server = new MaServer(http, "default", ".");
    
    //
    //  Activate the copy module and handler
    //
    new MaCopyModule(0);

    //
    //  Configure the server with the configuration directive file
    //
    if (server->configure("pollEventLoop.conf") < 0) {
        mprFprintf(MPR_STDERR, 
            "Can't configure the server. Error on line %d\n", 
            server->getLine());
        exit(2);
    }
    
    //
    //  Start the server
    //
    if (http->start() < 0) {
        mprFprintf(MPR_STDERR, "Can't start the server\n");
        exit(2);
    }

    //
    //  Service incoming requests until time to exit.
    //
    eventLoop();

    //
    //  Orderly shutdown
    //
    http->stop();
    delete server;
    delete http;

    //
    //  MPR run-time will automatically stop and be cleaned up
    //
    return 0;
}

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

static void eventLoop()
{
    Mpr             *mpr;
    int         till, timeout;

    mpr = mprGetMpr();

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

    while (!mpr->isExiting()) {

        mpr->runTimers();
        till = mpr->getIdleTime();

        //
        //  Run any queued tasks
        //
        if (mpr->runTasks() > 0) {          // Returns > 0 if more work to do
            till = 0;                       // So don't block in serviceEvents
        }

        //
        //  Do some work here
        //
        

        //
        //  Now service any pending I/O
        //
        mpr->serviceEvents(1, min(timeout, till));
    }
}



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