Hello App: msgHello.cpp

Index Home Sample App > Hello App: msgHello.cpp

Contents

void MAE::handleMessage(Messenger & msgr, const string & src, const string & dest, const string & request, XMLData & param)
void MAE::handleInputResponse(const string & dest, UserDevice & device, const string & cbtag, const XMLData & cbdata, string response)
void MAE::handleApiMessage(Messenger & msgr, const string & src, const string & dest, const string & request, XMLData & param)
static void registerGuiMenus()
void MAE::appRegistration()

msgHello.cpp

/**

 *      msgHello.cpp - handle messages sent to us on the "hello" channel

 *

 * Copyright (c) 2024 HanoverSoft

 * Anthony V. Edwards

 * 820 Churchill Drive, Chapel Hill, NC  27517

 * ave@hanoversoft.net

 *

 * PLEASE NOTE: DO NOT ALTER THIS FILE!! This file is auto-generated by genmae

 * This file was generated from hello.mreg.

 */

#include <MAE.h>

#include <DisplayAPI.h>

/**

 * hello.mreg - MAE message registration for "hello" channel (C++ version)

 *

 * Copyright (c) 2024 HanoverSoft

 * Written by Anthony V. Edwards

 * 820 Churchill Drive, Chapel Hill, NC  27517

 * ave@hanoversoft.net

 *

 * A MAE app is expected to handle messages/events from multiple users or multiple instances.

 * This file directs MAE as to how you want those messages/events handled.

 * Each message/event has an id that is used to pull up the appropriate instance of HelloApp.

 * An instance of HelloApp is handy for maintaining session data.

 * If you don't want to maintain session data, explicitly set appclass to nothing.

 *

 * This file contains directives for the genmae tool to generate msgHello.cpp, which contains code to

 * 1) initial setup by integrating with MAE daemons (e.g. usergw and guibroker for the UI)

 * 2) respond to messages/events from MAE, such as user menu selection

 * 3) handle API calls (the simple Hello app does not define any API calls) from other MAE apps - asynchronous events

 * 4) handle RPC calls (the simple Hello app does not define any API calls) from other MAE apps - synchronous events

 * 5) respond to user input controls such as pull-down selection, text responses, etc.

 * 6) process other files (such as .cpp or .ui files) that may contain more directives (in comments)

 * Comments in this file are included in generated file output, so commenting well here means commenting well elsewhere.

 *

 * To generate msgHello.cpp, use

 *     genmae hello.mreg -msg msgHello.cpp

 * To generate C method prototypes for callbacks from this file, which is

 * useful for creating/modifying HelloApp.h, use

 *     genmae hello.mreg -hcpp

 * To generate C method dummy code for callbacks from this file, which is

 * useful for creating/modifying HelloApp.cpp, use

 *     genmae hello.mreg -cbcpp

 * If we define #api/#msg blocks for an API for other MAE apps to use, we

 * generate the include file and C++ source code using

 *     genmae hello.mreg -apicpp -apih

 * To generate wiki documentation on message handling performed here, we use

 *     genmae hello.mreg -wiki

 * which is used in our Hello app documentation stored in /usr/mae/html/doc/hello.wiki

 * To convert a .ui file into fully proper .html (removing genmae clues), use

 *     genformcode genformcode html helloPrompt.ui ../../html/record/helloPrompt.html

 */

// Our app's include file, so we have ready access to our context

// Generated code for msgHello.cpp, HelloApp.cpp, and HelloApp.h will include this

// (Use #apiinclude for API files.)

#include "hello.h"

// We communicate within MAE using our own unique channel name 'hello

/** Handles messages from the platform

 * @param msgr - handle to Messenger service

 * @param src - the source channel that sent the message

 * @param dest - the destination channel for the message, e.g. hello

 * @param request - the action/keyword/request for this message

 * @param param - the data payload for this request

 */

void MAE::handleMessage(Messenger & msgr, const string & src, const string & dest, const string & request, XMLData & param)

{

    if (debug_on) fflush (dbgf);

    if (src == "system") {

        const string & msg= param["mesg"];

        if (msg == "-Message broker not running.")

            MAE::quit(1, string(msg, 1));

        // hmmm... must be an error?

        MAE::log (string("handleMesssage/hello: Stray system message ignored: ")+msg);

        return;

    }

    // process the basics of the request

    if (request == "err") {

        // another task is returning a message we sent to it

        MAE::log (string("handleMesssage/hello: (from ") + src + ") Error: "+param["mesg"]);

        return;

    }

    // if supervisor traffic, divert to mangaement interface

    if (request == "sysadmin") {

        if (msgr.fromSysAdmin(src)) {

            mgmt.handleMesssage (param);

        }

        else {

            MAE::log ((string) "sysadmin request from other than supervisor ignored (" + param["cmd"] + ")");

        }

        return;

    }

    // process an input response elsewhere

    if (UserDevice::isQueryCB(msgr, src, dest, request, param)) return;

    UserDevice device(param["device"]);

    if (param["msgtype"] == "api") {

        // API events are handled elsewhere

        MAE::handleApiMessage(msgr, src, dest, request, param);

        return;

    }

    // Block from #idcode

    # 56 "hello.mreg"

    // Track user-specific state (if needed)

    DbRecNum user_id= device.getUserId();

    int pos= HelloCache.findPos(user_id);

    if (pos < 0 && user_id > 0) {

        // a new connection - add to cache

        HelloApp newUser(user_id);

        // remember our path to the user

        newUser.setDevice(device);

        // add this user to our in-memory cache

        pos= HelloCache.add(user_id, newUser);

    }

    if (pos < 0) {

        // We don't have a match for this device

        string msg= (string)"Unknown device (Uid=" + user_id + ",Conn="+device.getConnectionId()+",App="+device.getAppMode()+",tag="+device.getRegion()+") for " + request;

        MAE::log (msg);

        MAE::mgmt.incrementInputErrorCount();

        return;

    }

    // any connection-specific data

    HelloApp & instance= HelloCache[pos];

// Here's a code block for any code that should be executed before the message is processed

# 128 "msgHello.cpp"

    // Block from #msgprecode

    # 78 "hello.mreg"

// Here's a code block for any code that should be executed after the message is processed

# 132 "msgHello.cpp"

    mgmt.incrementInboundRequestCount();

    //----------------------------------------------------------

    // Handle hello channel requests

    //

    /**/ if (request == "userstart") {

        instance.userstart(param);

    }

    else if (request == "fromdb") {

        instance.menu_Display_FromDB();

    }

    else if (request == "clrdisp") {

        instance.menu_Display_Clear();

    }

    else if (request == "help") {

        instance.menu_Display_Help();

    }

    else if (request == "quit") {

        instance.menu_Display_Quit();

    }

    //----------------------------------------------------------

    // ERROR

    else {

        msgr.sendError (src, string("Unknown message request ")+request + " from " + src);

        MAE::log (string("Unknown hello message request. handleMesssage/hello: src=")+src+" req="+dest+":"+request);

        mgmt.incrementInboundErrorCount();

    }

    // Block from #msgpostcode

    # 80 "hello.mreg"

/** Initiate a new user session.

 * @param param - inbound variables from UserDevice::run() (if any)

 */

# 165 "msgHello.cpp"

    if (debug_on) fflush (dbgf);

}

/** Handles user responses from MAE

 * @param dest - the destination channel to receive response

 * @param device - handle to User's device/screen

 * @param cbtag - the action to take with the response

 * @param cbdata - the data associated with the action

 * @param response - the User's response

 */

void MAE::handleInputResponse(const string & dest, UserDevice & device, const string & cbtag, const XMLData & cbdata, string response)

{

    if (cbtag == "err") {

        MAE::log((string) "Error received: " + response);

        return;

    }

    // Block from #idcode

    # 56 "hello.mreg"

    // Track user-specific state (if needed)

    DbRecNum user_id= device.getUserId();

    int pos= HelloCache.findPos(user_id);

    if (pos < 0 && user_id > 0) {

        // a new connection - add to cache

        HelloApp newUser(user_id);

        // remember our path to the user

        newUser.setDevice(device);

        // add this user to our in-memory cache

        pos= HelloCache.add(user_id, newUser);

    }

    if (pos < 0) {

        // We don't have a match for this device

        string msg= (string)"Unknown device (Uid=" + user_id + ",Conn="+device.getConnectionId()+",App="+device.getAppMode()+",tag="+device.getRegion()+") for " + cbtag;

        MAE::log (msg);

        MAE::mgmt.incrementInputErrorCount();

        return;

    }

    // any connection-specific data

    HelloApp & instance= HelloCache[pos];

// Here's a code block for any code that should be executed before the message is processed

# 205 "msgHello.cpp"

    mgmt.incrementInputCount();

    //----------------------------------------------------------

    // Input Responses

    /**/ if (cbtag == "name") {

        string name(response);

        instance.inputName(name);

    }

    else {

    MAE::log ((string)"User " + device.getUserId() + " unknown hello InputResponse cbtag=" + cbtag);

    mgmt.incrementInputErrorCount();

    }

    if (debug_on) fflush (dbgf);

}

/** Handles API messages from the platform

 * @param msgr - handle to Messenger service

 * @param src - the source channel that sent the message

 * @param dest - the destination channel for the message, e.g. hello

 * @param request - the action/keyword/request for this message

 * @param param - the data payload for this request

 */

void MAE::handleApiMessage(Messenger & msgr, const string & src, const string & dest, const string & request, XMLData & param)

{

    UserDevice device(param["device"]);

    // Block from #idcode

    # 56 "hello.mreg"

    // Track user-specific state (if needed)

    DbRecNum user_id= device.getUserId();

    int pos= HelloCache.findPos(user_id);

    if (pos < 0 && user_id > 0) {

        // a new connection - add to cache

        HelloApp newUser(user_id);

        // remember our path to the user

        newUser.setDevice(device);

        // add this user to our in-memory cache

        pos= HelloCache.add(user_id, newUser);

    }

    if (pos < 0) {

        // We don't have a match for this device

        string msg= (string)"Unknown device (Uid=" + user_id + ",Conn="+device.getConnectionId()+",App="+device.getAppMode()+",tag="+device.getRegion()+") for " + request;

        MAE::log (msg);

        MAE::mgmt.incrementInputErrorCount();

        return;

    }

    // any connection-specific data

    HelloApp & instance= HelloCache[pos];

// Here's a code block for any code that should be executed before the message is processed

# 253 "msgHello.cpp"

    if (false)

        { /* no APIs */ }

    else {

        MAE::log ((string)"User " + device.getUserId() + " unknown hello API cbtag=" + request);

    }

    if (debug_on) fflush (dbgf);

}

/** Set up menus in GuiBroker

 */

static void registerGuiMenus()

{

    UserDevice::registerMenu("Display", "From DB", "fromdb", "hello");

    UserDevice::registerMenu("Display", "Clear", "clrdisp", "hello");

    UserDevice::registerMenu("Display", "Help", "help", "hello");

    UserDevice::registerMenu("Display", "Quit", "quit", "hello");

}

/** Register our various UI and message callbacks.

 */

void MAE::appRegistration()

{

    registerGuiMenus();

    DisplayAPI::registerApp("hello");

}