Chapter 4. Examples

This chapter describes examples available in examples subdirectory:

libmlp-cpp-1.1.0
          |
          +----examples/
                 |
                 *----curl-client/
                 *----example1/
                 *----example2/
                 *----example3/
                 *----example-guide/
                 *----example_other/
curl-client

Curl-client example need cURL library. It is simple HTTP client that asks user for Mobile Station International Subscriber Directory Number (MSISDN) and send HTTP POST request containing MLP SLIR message to Location Server. Location server should answer with SLIA message. This message is processed with MlpParser object and acquired location information is displayed to a standard output.

example1-3

This three examples are completely coincidental except each contains different MLP message. They read the MLP message from message.xml file into the std::string and pass it to the MlpParser. Example1 contains SLIR message, example2 contains SLIA message and example3 contains SLIREP message.

example-guide

In example-guide you can see simple usage of MLP Library. Following code listing is from example-guide/main.cpp.

example_other

Directory example_other contains examples that are not part of library build system. You can copy them to arbitrary location, unpack them an try to compile them according to instructions in their READMEs. You can find there example with simple Makefile and example formated as GNU autotools package. They have the same source code as the example1 but they have separated build system. This means you need to properly set paths to libraries, headers and pkg-config so your compiler finds MLP headers and your linker finds library to link.

Examples from examples_other need to know the path to MLP DTDs. To successfully run them after compilation. You need to edit this path in example source codes before compilation!!!

All examples that are part of build system (curl-client, example1-3, example-guide) are set to reach DTD files located in libmlp-cpp-1.1.0/libmlp-cpp/dtd. They should run after the make command. Path to DTDs can be specified directly in source code for simplicity of examples.

Foloving code listing is source for example-guide.

#include <iostream>
#include <sstream>
#include <string>
#include <fstream>

#include <libmlp-cpp/MlpLib.hpp>
#include <libmlp-cpp/MlpMessage.hpp>
#include <libmlp-cpp/MlpException.hpp>
#include <libmlp-cpp/MlpSubscriber.hpp>
#include <libmlp-cpp/MlpShapePoint.hpp>


int main( int argc, char* argv[] )
{
    // Initialize
    MLP_LIB_CPP_INIT;

    // Message for parsing
    std::string myMsg =  "<?xml version = \"1.0\" ?> \
<!DOCTYPE svc_init PUBLIC \"-//OMA//DTD MLP 3.2.0//EN\" \
\"http://www.openmobilealliance.org/Tech/dtd/MLP_SVC_INIT_320.dtd\"> \
<svc_init ver=\"3.2.0\"> \
    <hdr ver=\"3.2.0\"> \
        <client> \
            <id>theasp</id> \
            <pwd>thepwd</pwd> \
            <serviceid>0005</serviceid> \
            <requestmode type=\"ACTIVE\"/> \
        </client> \
        <subclient last_client=\"YES\"> \
            <id>theSubAsp</id> \
            <pwd>thepwd2</pwd> \
            <serviceid>0005</serviceid> \
        </subclient> \
        <requestor type=\"MSISDN\"> \
            <id>461018765710</id> \
        </requestor> \
    </hdr> \
    <slir ver=\"3.2.0\" res_type=\"ASYNC\"> \
        <msids> \
            <msid enc=\"CRP\" type=\"IPV4\">93.10.0.250</msid> \
            <codeword>rtep1</codeword> \
            <session type=\"APN\">mySession1</session> \
            <msid_range> \
                <start_msid> \
                    <msid enc=\"CRP\" type=\"IPV4\">461018765710</msid> \
                </start_msid> \
                <stop_msid> \
                    <msid enc=\"CRP\" type=\"IPV4\">461018765712</msid> \
                </stop_msid> \
            </msid_range> \
            <msid type=\"ASID\">441728922342</msid> \
            <codeword>rtep2</codeword> \
            <msid_range> \
                <start_msid> \
                    <msid>461018765720</msid> \
                </start_msid> \
                <stop_msid> \
                    <msid>461018765728</msid> \
                </stop_msid> \
            </msid_range> \
            <codeword>ahoj1</codeword> \
            <codeword>ahoj2</codeword> \
            <codeword>ahoj3</codeword> \
            <codeword>ahoj4</codeword> \
        </msids> \
        <eqop> \
            <resp_req type=\"LOW_DELAY\" /> \
            <hor_acc qos_class=\"ASSURED\">1000</hor_acc> \
        </eqop> \
        <geo_info> \
            <CoordinateReferenceSystem> \
                <Identifier> \
                    <code>4004</code> \
                    <codeSpace>EPSG</codeSpace> \
                    <edition>6.1</edition> \
                </Identifier> \
            </CoordinateReferenceSystem> \
        </geo_info> \
        <loc_type type=\"CURRENT_OR_LAST\" /> \
        <prio type=\"HIGH\" /> \
    </slir> \
</svc_init>" ;
    
    MlpConfig config;
    config.setDTDpath ("../../dtd/" );

    // New MlpParser
    MlpParser * myMlpParser = new MlpParser();
    try
    {
        myMlpParser->configure( &config );
        myMlpParser->init();
    }
    catch (MlpException &e)
    {
        // Error in initialization
        std::cout << e.getMessage() << std::endl;
        delete myMlpParser;
        myMlpParser = NULL;
    }

    if( myMlpParser != NULL )
    {
        std::cout << "Press Enter to start parsing." << std::endl;
        std::cin.get();

            try{
                MlpMessage inMsg;
                MlpHdr* inMlpHdr;

                const MlpHdrClientList* inMlpHdrClientList;
                const MlpSubscriberList* inMlpSubscriberList;
                const MlpPositionList* inMlpPositionList;

                // Parsing
                myMlpParser->parse( &inMsg, myMsg );


                std::cout << "   Msg Type:         " 
                          << inMsg.getMsgType() << std::endl;
                std::cout << "   Msg Service Type: " 
                          << inMsg.getServiceType() << std::endl;

                inMlpHdr = inMsg.getMlpHdr();
                // If message contains header, print some information
                if (inMlpHdr != NULL)
                {
                    std::cout << "      Hdr Request Mode: " 
                              << inMlpHdr->getRequestMode() << std::endl;

                    inMlpHdrClientList = (inMsg.getMlpHdr())->getHdrClientList();
                    for ( t_MlpHdrClientListIterator it = inMlpHdr->getBegin() ;
                            it != inMlpHdr->getEnd() ;
                            it++
                            )
                    {
                        std::cout << "        Klient type:  "
                                  << (*it)->getClientType() << std::endl;
                        std::cout << "          Client ID:    "
                                  << (*it)->getId() << std::endl;
                    }
                }

                
                // Present only if the message contains subscribers that should be located.
                // Present only in Request messages.
                inMlpSubscriberList = inMsg.getSubscriberList();
                if ( inMlpSubscriberList != NULL)
                {

                    for ( t_MlpSubscriberListIterator 
                            it  = (inMsg.getSubscriberList())->begin();
                            it != (inMsg.getSubscriberList())->end() ; 
                            it++ 
                        )
                    {
                        if( (*it)->getSubscriberType() == MlpConstants::SUBSCRIBER_MSID )
                        {
                            std::cout << "      Subscriber type: MSID: "
                                      << (*it)->getMsid() << std::endl;
                        }
                        else if( (*it)->getSubscriberType() 
                                == MlpConstants::SUBSCRIBER_MSID_RANGE )
                        {
                            std::cout << "      Subscriber type: MSID_RANGE" << std::endl;
                            std::cout << "          Start MSID:      "
                                      << (*it)->getStartMsid() << std::endl;
                            std::cout << "          Stop MSID:       "
                                      << (*it)->getStopMsid() << std::endl;
                        }
                    }
                }

                // Present only in response messages
                // Prints information oubt localized subscribers
                inMlpPositionList = inMsg.getPositionList();
                if ( inMlpPositionList != NULL)
                {

                    for ( t_MlpPositionListIterator 
                            it  = (inMsg.getPositionList())->begin();
                            it != (inMsg.getPositionList())->end() ; 
                            it++ 
                        )
                    {
                        std::cout << "      Position: " << std::endl;
                        std::cout << "          MSID:       " 
                                      << (*it)->getMsid() << std::endl;
                        // error during localization
                        if( (*it)->wasError() )
                        {
                            std::cout << "          Error:       " 
                                      << (*it)->getResult() << std::endl;
                            std::cout << "          Error Id:    " 
                                      << (*it)->getResultId() << std::endl;
                        }
                        // localization was successful
                        else
                        {
                            std::cout << "          Shape type:  " 
                                      << ( (*it)->getShape() )->getShapeType()
                                      << std::endl;
                            switch( ( (*it)->getShape() )->getShapeType() ){
                                case( MlpConstants::SHAPE_POINT ):
                                    std::cout << "          X:          " 
                                              << ( (MlpShapePoint* )(*it)->getShape() )->getX()
                                              << std::endl;
                                    std::cout << "          Y:          " 
                                              << ( (MlpShapePoint* )(*it)->getShape() )->getY()
                                              << std::endl;
                                    std::cout << "          Z:          " 
                                              << ( (MlpShapePoint* )(*it)->getShape() )->getZ()
                                              << std::endl;
                                    break;
                                    // Other shapes not supported yet
                                case( MlpConstants::SHAPE_LINESTRING ):
                                    break;
                                case( MlpConstants::SHAPE_POLYGON ):
                                    break;
                                case( MlpConstants::SHAPE_BOX ):
                                    break;
                                case( MlpConstants::SHAPE_CIRCULAR_AREA ):
                                    break;
                                case( MlpConstants::SHAPE_CIRCULAR_ARC_AREA ):
                                    break;
                                case( MlpConstants::SHAPE_ELLIPTICAL_AREA ):
                                    break;
                                case( MlpConstants::SHAPE_MULTILINE_STRING ):
                                    break;
                                case( MlpConstants::SHAPE_MULTIPOINT ):
                                    break;
                                case( MlpConstants::SHAPE_MULTIPOLYGON ):
                                    break;
                                case( MlpConstants::SHAPE_LINEAR_RING ):
                                    break;
                            }
                        }
                    }
                }
            }
            catch ( MlpException &e ){
                std::cout << e.getMessage() << std::endl;
            }
    }

    std::cout << "Press Enter to stop." << std::endl;
    std::cin.get();

    // Delete the parser
    delete myMlpParser;

    // Terminate the environment
    MLP_LIB_CPP_TERMINATE;

    return 0;
}