void Core::change_state(States new_state) { //COPY PASTAAAAAAAAA if(core_curr_state) { core_curr_state->on_exit(); core_prev_state = core_curr_state->get_state(); } switch(new_state) { case States::CORE_TOPOLOGY: core_curr_state = &core_state_topology; break; case States::CORE_IDLE: core_curr_state = &core_state_idle; break; case States::CORE_SERVE: core_curr_state = &core_state_serve; break; default: core_curr_state = nullptr; std::cout << "Illegal state transition!\n" << "Our previous state was " << state_to_text(core_prev_state) << " and the new state was " << state_to_text(new_state) << std::endl; exit(-15); break; } core_curr_state->on_entry(); }
// Uses a basic switch statement to see what new state has been selected. void Simulation::change_state(States new_state) { if (m_current_state) { // First we make sure to cleanup the state we are in m_current_state->on_exit(); // Then we can safely set our previous state variable m_previous_state = m_current_state->get_state(); } switch (new_state) { case States::SIM_START: m_current_state = &simulation_start; break; case States::SIM_RUN: m_current_state = &simulation_running; break; case States::SIM_END: m_current_state = &simulation_exit; break; default: // First set our current state to nothing. m_current_state = nullptr; // Print out error message std::cout << "Illegal state transition!\n" << "Our previous state was " << state_to_text(m_previous_state) << " and the new state was " << state_to_text(new_state) << std::endl; exit(-15); break; } // If all is OK, we can start the new state. m_current_state->on_entry(); }
void Dispatcher::change_state(States new_state) { //copy-pasta from charlie's awesome error checking if (disp_curr_state) { // First we make sure to cleanup the state we are in disp_curr_state->on_exit(); // Then we can safely set our previous state variable disp_prev_state = disp_curr_state->get_state(); } switch (new_state) { case States::DISP_IDLE: disp_curr_state = &disp_state_idle; break; case States::DISP_SERVE: disp_curr_state = &disp_state_serve; break; default: // First set our current state to nothing. disp_curr_state = nullptr; // Print out error message std::cout << "Illegal state transition!\n" << "Our previous state was " << state_to_text(disp_prev_state) << " and the new state was " << state_to_text(new_state) << std::endl; exit(-15); break; } // If all is OK, we can start the new state. disp_curr_state->on_entry(); }
void Simulation_running::on_entry() { /*****************************/ //auto seed = chrono::high_resolution_clock::now().time_since_epoch().count(); //mt19937 mt_rand(seed); auto dice_rand = std::bind(std::uniform_int_distribution<int>(1,9999), mt19937(seed)); mt19937 nrg; poisson_distribution<int> poisson(4.9); /*****************************/ std::cout << "Simulation_running on_entry()\n"; std::cout<<"State: "<<state_to_text( m_state_machine_controller.m_current_state->get_state())<<std::endl; bool flag = false; auto sim_run_thread = sim_running_thread(m_state_machine_controller); while (!flag) // Infinite loop producing random numbers every "poisson" time { flag = check_me2(m_state_machine_controller); int job = dice_rand(); int sth = random_disp(); std::cout<<"DA FAQ "<<flag<<" - - - Random Disp Number: "<<sth<<" - - - Random Job: "<<job<<std::endl; std::cout<<"Job sending"<<std::endl; vDisp.at(sth)->add_job_q(job); vDisp.at(sth)->schedule_event(Events::DISP_JOB); std::this_thread::sleep_for(std::chrono::seconds(poisson(nrg))); //std::this_thread::sleep_for(std::chrono::milliseconds(2000)); } //sim_run_thread.get(); //std::cout<<"State: "<<state_to_text(m_state_machine_controller.m_previous_state)<<std::endl; }