Changed AppMgr to AppState

The previous name implied some kind of daemon. AppState is
aggregated state of an application's running state and interfaces
to request app termination, and check app state.

Signed-off-by: Mark Charlebois <charlebm@gmail.com>
This commit is contained in:
Mark Charlebois 2015-03-13 16:39:41 -07:00
parent 5259f1c861
commit 938751993d
8 changed files with 45 additions and 34 deletions

View File

@ -43,14 +43,14 @@
#include <unistd.h>
#include <stdio.h>
px4::AppMgr HelloExample::mgr;
px4::AppState HelloExample::appState;
int HelloExample::main()
{
mgr.setRunning(true);
appState.setRunning(true);
int i=0;
while (!mgr.exitRequested() && i<5) {
while (!appState.exitRequested() && i<5) {
sleep(2);
printf(" Doing work...\n");

View File

@ -49,5 +49,5 @@ public:
int main();
static px4::AppMgr mgr; /* Manage requests to terminate app */
static px4::AppState appState; /* track requests to terminate app */
};

View File

@ -63,7 +63,7 @@ int hello_main(int argc, char *argv[])
if (!strcmp(argv[1], "start")) {
if (HelloExample::mgr.isRunning()) {
if (HelloExample::appState.isRunning()) {
printf("already running\n");
/* this is not an error */
return 0;
@ -80,12 +80,12 @@ int hello_main(int argc, char *argv[])
}
if (!strcmp(argv[1], "stop")) {
HelloExample::mgr.requestExit();
HelloExample::appState.requestExit();
return 0;
}
if (!strcmp(argv[1], "status")) {
if (HelloExample::mgr.isRunning()) {
if (HelloExample::appState.isRunning()) {
printf("is running\n");
} else {

View File

@ -45,7 +45,7 @@
#include <unistd.h>
#include <stdio.h>
px4::AppMgr VCDevExample::mgr;
px4::AppState VCDevExample::appState;
using namespace device;
@ -117,7 +117,7 @@ int test_pub_block(int fd, unsigned long blocked)
int VCDevExample::main()
{
mgr.setRunning(true);
appState.setRunning(true);
_node = new VCDevNode();
@ -164,7 +164,7 @@ int VCDevExample::main()
writer_main,
(char* const*)NULL);
while (!mgr.exitRequested() && i<5) {
while (!appState.exitRequested() && i<3) {
sleep(2);
printf(" polling...\n");
@ -179,12 +179,15 @@ int VCDevExample::main()
printf("poll failed %d %d\n", ret, px4_errno);
px4_close(fd);
}
else if (ret == 0)
printf(" Nothing to read\n");
else {
printf(" %d to read\n", ret);
else if (i > 0 && ret == 0)
printf(" Nothing to read - PASS\n");
else if (i == 0) {
if (ret == 1)
printf(" %d to read - %s\n", ret, fds[0].revents & POLLIN ? "PASS" : "FAIL");
else
printf(" %d to read - FAIL\n", ret);
}
printf(" Doing work...\n");
++i;
}
px4_close(fd);

View File

@ -51,7 +51,7 @@ public:
int main();
static px4::AppMgr mgr; /* Manage requests to terminate app */
static px4::AppState appState; /* track requests to terminate app */
private:
VCDevNode *_node;

View File

@ -58,7 +58,7 @@ int vcdevtest_main(int argc, char *argv[])
if (!strcmp(argv[1], "start")) {
if (VCDevExample::mgr.isRunning()) {
if (VCDevExample::appState.isRunning()) {
printf("already running\n");
/* this is not an error */
return 0;
@ -75,12 +75,12 @@ int vcdevtest_main(int argc, char *argv[])
}
if (!strcmp(argv[1], "stop")) {
VCDevExample::mgr.requestExit();
VCDevExample::appState.requestExit();
return 0;
}
if (!strcmp(argv[1], "status")) {
if (VCDevExample::mgr.isRunning()) {
if (VCDevExample::appState.isRunning()) {
printf("is running\n");
} else {

View File

@ -1,6 +1,6 @@
/****************************************************************************
*
* Copyright (c) 2015 Mark Charlebois. All rights reserved.
* Copyright (c) 2015 Mark Charlebois. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@ -31,21 +31,28 @@
*
****************************************************************************/
/**
* @file px4_app.h
*
* PX4 app template classes, functions and defines. Apps need to call their
* main function PX4_MAIN.
*/
#pragma once
namespace px4 {
class AppMgr {
class AppState {
public:
~AppMgr() {}
~AppState() {}
#if defined(__PX4_ROS)
AppMgr() {}
AppState() {}
bool exitRequested() { return !ros::ok(); }
void requestExit() { ros::shutdown(); }
#else
AppMgr() : _exitRequested(false), _isRunning(false) {}
AppState() : _exitRequested(false), _isRunning(false) {}
bool exitRequested() { return _exitRequested; }
void requestExit() { _exitRequested = true; }
@ -58,22 +65,23 @@ protected:
bool _isRunning;
#endif
private:
AppMgr(const AppMgr&);
const AppMgr& operator=(const AppMgr&);
AppState(const AppState&);
const AppState& operator=(const AppState&);
};
}
// PX4_MAIN is defined if module.mk sets MODULE_COMMAND
// For ROS and NuttX it is "main" and for Linux it is
// $(MODULE_COMMAND)_app_main since some apps already
// define $(MODULE_COMMAND)_main
// Task/process based build
#if defined(__PX4_ROS) || defined(__PX4_NUTTX)
// Thread based build
#else
// The name passed must be globally unique
// set PX4_APPMAIN in module.mk
// EXTRADEFINES += -DPX4_MAIN=foo_app
#ifdef PX4_MAIN
extern int PX4_MAIN(int argc, char *argv[]);
#endif

View File

@ -142,11 +142,11 @@ protected:
class __EXPORT NodeHandle
{
public:
NodeHandle(AppMgr &a) :
NodeHandle(AppState &a) :
_subs(),
_pubs(),
_sub_min_interval(nullptr),
_mgr(a)
_appState(a)
{}
~NodeHandle()
@ -264,7 +264,7 @@ public:
*/
void spin()
{
while (!_mgr.exitRequested()) {
while (!_appState.exitRequested()) {
const int timeout_ms = 100;
/* Only continue in the loop if the nodehandle has subscriptions */
@ -289,7 +289,7 @@ protected:
SubscriberNode *_sub_min_interval; /**< Points to the sub wtih the smallest interval
of all Subscriptions in _subs*/
AppMgr &_mgr;
AppState &_appState;
/**
* Check if this is the smallest interval so far and update _sub_min_interval