Hello App: hello.cpp
Contents
hello.cpp
/**
* hello.cpp - MAE "Hello, World" program - a sample MAE app
*
* Copyright (c) 2024 HanoverSoft
* Written by Anthony V. Edwards
* 820 Churchill Drive, Chapel Hill, NC 27517
* ave@hanoversoft.net
*
* The app starting point, integrated with MAE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <MAEApp.h>
#include "hello.h"
/** Tell MAE how we behave and what channel we listen on.
* MAEDaemon means we're always running; we don't perform a task and exit.
* If we optionally listened on more channels than just hello, we'd specify
* that here (as second parameter).
* The global variable 'MAETask task' contains this information and more.
* Calling MAE::getName() will return "hello".
* When debug mode is enabled, the FILE* dbgf will append /usr/mae/log/hellodbg.log.
*/
MAEApp maeapp("hello");
/** Initialize the app from command line arguments.
* At this point, messages can be sent and the database is available, but
* the app is not considered ready; CommHub shows the app status as Init;
* other apps are not told that this app is running yet.
* @param argc - # of command line arguments
* @param argv - the command line arguments
*/
{
/* process command line args */
while (argc > 1 && argv[1][0] == '-') {
if (strcmp (argv[1], "-g") == 0) {
toggleDebug(0); // see /usr/mae/log/hellodbg.log
argc--, argv++;
}
else {
fprintf (stderr, "hello: unknown command line option: %s\n", argv[1]);
argc--, argv++;
// If we want to exit in err, then call MAE::failed() with a message.
}
}
// allocate a cache of HelloApp instances
cacheAlloc(MAE::getName());
// OK, now we're ready
}
/** CommHub is now showing our app status as Ready. Other apps have been notified that we're now ready.
* The main body of our program, if we do anything more than respond to events.
* To allow MAE to get and process events, call MAE::process().
*/
{
}
/** Shutdown sequence.
* @param rc - program return code to send to OS/CommHub; 0 on clean exit, 1+ on error exit
* @param msg - an explanation of why we're exiting, visible when using 'tstatus hello'
*/
{
// Save out the cache
HelloCache.save();
// Now shutting down
if (debug_on) fprintf (dbgf, "%s shutting down at %ld\n", MAE::getName().c_str(), (long)time(0));
signal (SIGALRM, SIG_IGN); /* turn off alarms, if any */
if (debug_on) fprintf (dbgf, "%s exit at %d\n", MAE::getName().c_str(), (int)time(0));
if (rc == 0)
// Tell CommHub that we're existing cleanly
MAE::done(msg.empty()? (string)"Done.": msg);
else
// Tell CommHUb that we're exiting under an error condition
MAE::failed(msg);
// That's it, we're done
exit (rc);
}