anjana_sreekumar@infosys.com | 991c206 | 2020-01-08 11:42:57 +0530 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2019, Infosys Ltd. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <iostream> |
| 18 | #include <time.h> |
| 19 | #include <msgBuffer.h> |
| 20 | #include "stateMachineEngine.h" |
| 21 | #include "controlBlock.h" |
| 22 | #include "event.h" |
| 23 | #include "state.h" |
| 24 | #include "smTypes.h" |
| 25 | extern "C" |
| 26 | { |
| 27 | #include "log.h" |
| 28 | } |
| 29 | using namespace std; |
| 30 | |
| 31 | namespace SM |
| 32 | { |
| 33 | StateMachineEngine::StateMachineEngine(): |
| 34 | procQ_m() |
| 35 | { |
| 36 | } |
| 37 | |
| 38 | |
| 39 | StateMachineEngine::~StateMachineEngine() |
| 40 | { |
| 41 | } |
| 42 | |
| 43 | |
| 44 | StateMachineEngine* StateMachineEngine::Instance() |
| 45 | { |
| 46 | static StateMachineEngine SM; |
| 47 | return &SM; |
| 48 | } |
| 49 | |
| 50 | |
| 51 | bool StateMachineEngine::addCBToProcQ(ControlBlock* cb) |
| 52 | { |
| 53 | return procQ_m.push(cb); |
| 54 | } |
| 55 | |
| 56 | void StateMachineEngine::run() |
| 57 | { |
| 58 | ControlBlock* cb = procQ_m.pop(); |
| 59 | |
| 60 | if(cb == NULL) |
| 61 | return; |
| 62 | |
| 63 | if (cb->getControlBlockState() == FREE) |
| 64 | { |
| 65 | log_msg(LOG_INFO, "Control block is freed\n"); |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | while (true) |
| 70 | { |
| 71 | Event currentEvent; |
| 72 | if (cb->getCurrentEvent(currentEvent) == false) |
| 73 | { |
| 74 | break; |
| 75 | } |
| 76 | |
| 77 | State *currentState_p = cb->getCurrentState(); |
| 78 | if (currentState_p == NULL) |
| 79 | { |
| 80 | log_msg(LOG_INFO, "Current state is NULL" |
| 81 | " for control block idx %d\n", cb->getCBIndex()); |
| 82 | |
| 83 | // TODO: free event msg data or cleaner handling |
| 84 | |
| 85 | break;; |
| 86 | } |
| 87 | |
| 88 | log_msg(LOG_DEBUG, |
| 89 | "################ Executing actions for event: %s and State: %s #################\n", |
| 90 | Events[currentEvent.getEventId()], States[currentState_p->getStateId()]); |
| 91 | |
| 92 | time_t mytime = time(NULL); |
| 93 | debugEventInfo dEventInfo(currentEvent.getEventId(), currentState_p->getStateId(), mytime); |
| 94 | cb->addDebugInfo(dEventInfo); |
| 95 | |
| 96 | ActStatus ret = currentState_p->executeActions(currentEvent.getEventId(),*cb); |
| 97 | if(PROCEED != ret) |
| 98 | { |
| 99 | // TODO: Error Handling, do not process further events for now |
| 100 | break;; |
| 101 | } |
| 102 | |
| 103 | void * event_data = currentEvent.getEventData(); |
| 104 | if (event_data != NULL) |
| 105 | delete static_cast <cmn::utils::MsgBuffer *>(event_data); |
| 106 | } |
| 107 | |
| 108 | if (cb->getControlBlockState() == ALLOCATED) |
| 109 | cb->setProcQueueFlag(false); |
| 110 | } |
| 111 | } |
| 112 | |