Listing 5: Demonstration program that sets up multiple functionality for SIGUSR1 and SIGUSR2 signals using CmdControl class

#include <unistd.h>
#include <std/csignal.h>
#include <std/string.h>
#include "CmdControl.h"

void simulate_report();
void reset_globals();
void raise_diagnostics();
void lower_diagnostics();
void demonstration1 ();
void demonstration2 ();
void demonstration3 ();
void demonstration4 ();
void shutitdown (int);

CmdControl *usr1hdle;
CmdControl *usr2hdle;

int main(void){

    int k;

    // create instances of CmdControl class for
    // SIGUSR1 and SIGUSR2 signals
    usr1hdle = new CmdControl(SIGUSR1);
    usr2hdle = new CmdControl(SIGUSR2);

    // Associate commands with desired functions
    // when sending the SIGUSR1 signal
    usr1hdle->set_action( "REPORT", simulate_report);
    usr1hdle->set_action( "RESET", reset_globals);
    usr1hdle->set_action( "HIGHER DIAGNOSTICS", raise_diagnostics);
    usr1hdle->set_action( "LOWER DIAGNOSTICS", lower_diagnostics);

    // Associate commands with desired functions 
    // when sendng the SIGUSR2 signal
    usr2hdle->set_action( "DEMO1", demonstration1);
    usr2hdle->set_action( "DEMO2", demonstration2);
    usr2hdle->set_action( "DEMO3", demonstration3);
    usr2hdle->set_action( "DEMO4", demonstration4);

    // Disassociate a command with a function 
    usr2hdle->unset_action("DEMO4");

    // Test the is_used function  member
    if ( usr2hdle->is_used("DEMO4") == 0 )
        cout << "DEMO4 is not used for SIGUSR2" << endl;

    if ( usr1hdle->is_used("REPORT") == 1 )
        cout << "REPORT is used for SIGUSR1" << endl;

    // invalid use of CmdControl::cmd_handler
    // any instance of SIGTERM should generate an error
    // message.
    signal(SIGTERM, CmdControl::cmd_handler);

    // Use the standard approach to provide handling to
    // the interrupt signal
    signal(SIGINT, shutitdown);

    // just run forever until stopped by interrupt
    while (true)
        sleep(1);

    return 0;
}


void simulate_report() {

    cout << "Diagnostics report."<< endl;
}

void reset_globals() {
    
    cout << "Reseting globals" << endl;
}

void raise_diagnostics () {
    cout << "raising application's diagnostic level." << endl;
}

void lower_diagnostics () {
    cout << "lowering application's diagnostic level." << endl;
}

void demonstration1 () {
    cout << "demonstration 1 running." << endl;
}

void demonstration2(){
    cout << "demonstration 2 running." << endl;
}

void demonstration3(){
    cout << "demonstration 3 running." << endl;
}

void demonstration4(){
    cout << "demonstration 4 running." << endl;
}

void shutitdown(int in_signal){
    delete usr1hdle;
    delete usr2hdle;
    exit(1);
}

- End of Listing -