Jack Hansdampf / mbed-mqtt-GSOE1

Dependents:   ESP8266MQTT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LTaskManager.cpp Source File

LTaskManager.cpp

00001 /**************************************************************************************
00002  * Copyright (c) 2016, Tomoaki Yamaguchi
00003  *
00004  * All rights reserved. This program and the accompanying materials
00005  * are made available under the terms of the Eclipse Public License v1.0
00006  * and Eclipse Distribution License v1.0 which accompany this distribution.
00007  *
00008  * The Eclipse Public License is available at
00009  *    http://www.eclipse.org/legal/epl-v10.html
00010  * and the Eclipse Distribution License is available at
00011  *   http://www.eclipse.org/org/documents/edl-v10.php.
00012  *
00013  * Contributors:
00014  *    Tomoaki Yamaguchi - initial API and implementation and/or initial documentation
00015  **************************************************************************************/
00016 
00017 
00018 #include <stdio.h>
00019 #include <string.h>
00020 
00021 #include "LMqttsnClientApp.h"
00022 #include "LSubscribeManager.h"
00023 #include "LTimer.h"
00024 #include "LMqttsnClient.h"
00025 #include "LTaskManager.h"
00026 #include "LScreen.h"
00027 
00028 using namespace std;
00029 using namespace linuxAsyncClient;
00030 
00031 extern LMqttsnClient* theClient;
00032 extern LScreen* theScreen;
00033 extern bool theClientMode;
00034 /*=====================================
00035            TaskManager
00036  ======================================*/
00037 LTaskManager::LTaskManager(void){
00038     _tasks = 0;
00039     _tests = 0;
00040     _index = 0;
00041 }
00042 
00043 LTaskManager::~LTaskManager(void){
00044     
00045 }
00046 
00047 void LTaskManager::add(TaskList* task){
00048     _tasks = task;
00049 }
00050 
00051 void LTaskManager::add(TestList* test){
00052     _tests = test;
00053 }
00054 
00055 void LTaskManager::run(void){
00056     int i = 0;
00057     char c = 0;
00058     bool cancelFlg = false;
00059 
00060     if ( !theClientMode )
00061     {
00062             theClient->getGwProxy()->getMessage();
00063 
00064             for (i = 0; _tests[i].testTask > 0; i++)
00065             {
00066                 PROMPT("Execute \"%s\" ? ( y/n ) : ", _tests[i].testLabel);
00067                 while (true)
00068                 {
00069                     if (CHECKKEYIN(&c))
00070                     {
00071                         if ( toupper(c) == 'N' )
00072                         {
00073 
00074                             DISPLAY("\033[0m\033[0;32m\n**** %s is canceled ****\033[0m\033[0;37m\n\n", _tests[i].testLabel);
00075                             theScreen->prompt("");
00076                             cancelFlg = true;
00077                             break;
00078                         }
00079                         else if ( toupper(c) == 'Y' )
00080                         {
00081                             DISPLAY("\033[0m\033[0;32m\n\n**** %s start ****\033[0m\033[0;37m\n", _tests[i].testLabel);
00082                             theScreen->prompt("");
00083                             (_tests[i].testTask)();
00084                             cancelFlg = false;
00085                             break;
00086                         }
00087                     }
00088                     else
00089                     {
00090                         theClient->getGwProxy()->getMessage();
00091                     }
00092                 }
00093 
00094                 while ( true )
00095                 {
00096                     do
00097                     {
00098                         theClient->getGwProxy()->getMessage();
00099                     }
00100                     while(theClient->getPublishManager()->isMaxFlight() ||
00101                            !theClient->getSubscribeManager()->isDone() ||
00102                            !theClient->getRegisterManager()->isDone());
00103 
00104                     if (theClient->getPublishManager()->isDone())
00105                     {
00106                         break;
00107                     }
00108                 }
00109                 if ( !cancelFlg )
00110                 {
00111                     DISPLAY("\033[0m\033[0;32m\n**** %s complete ****\033[0m\033[0;37m\n\n", _tests[i].testLabel);
00112                 }
00113             }
00114             DISPLAY("\033[0m\033[0;32m\n\n#########  All tests complete!  ###########\033[0m\033[0;37m\n\n");
00115     }
00116     else
00117     {
00118         while (true)
00119         {
00120             theClient->getGwProxy()->getMessage();
00121             for (_index = 0; _tasks[_index].callback > 0; _index++)
00122             {
00123                 if ((_tasks[_index].prevTime + _tasks[_index].interval <= time(NULL)) &&
00124                      _tasks[_index].count == 0)
00125                 {
00126                     _tasks[_index].prevTime = time(NULL);
00127                     (_tasks[_index].callback)();
00128                 }
00129             }
00130 
00131             do
00132             {
00133                 theClient->getGwProxy()->getMessage();
00134             }
00135             while(theClient->getPublishManager()->isMaxFlight() ||
00136                    !theClient->getSubscribeManager()->isDone() ||
00137                    !theClient->getRegisterManager()->isDone());
00138 
00139             if (theClient->getPublishManager()->isDone())
00140             {
00141                 break;
00142             }
00143         }
00144     }
00145 }
00146 
00147 uint8_t LTaskManager::getIndex(void){
00148     return _index;
00149 }
00150 
00151 void LTaskManager::done(uint8_t index){
00152     if (_tasks )
00153     {
00154         if (_tasks[index].count > 0)
00155         {
00156             _tasks[index].count--;
00157         }
00158     }
00159     if (_tests )
00160         {
00161             if (_tests[index].count > 0)
00162             {
00163                 _tests[index].count--;
00164             }
00165         }
00166 }
00167 
00168 void LTaskManager::suspend(uint8_t index){
00169     if ( _tasks )
00170     {
00171         _tasks[index].count++;
00172     }
00173     if ( _tests )
00174     {
00175         _tests[index].count++;
00176     }
00177 }