src/ActionScheduler.cpp

Go to the documentation of this file.
00001 #include <list>
00002 #include <iterator>
00003 #include "ActionScheduler.h"
00004 
00005 using namespace std;
00006 
00010 ActionScheduler::ActionScheduler(WorldModel* wm, ServerSettings *ss, PlayerSettings *ps)
00011 {
00012         WM         = wm;
00013         SS         = ss;
00014         PS         = ps;
00015 
00016 #ifdef DEBUG
00017         LOG_PRINT(LOG_INFO, "Initializing ActionScheduler");
00018 #endif //DEBUG
00019 }
00020 
00025 ActionScheduler::~ActionScheduler()
00026 {
00027 #ifdef DEBUG
00028         LOG_PRINT(LOG_INFO, "Calling ActionScheduler destructor");
00029 #endif //DEBUG
00030 
00031         while( ! scheduled_actions.empty() ) {
00032                 struct scheduled_action *action = &(scheduled_actions.front());
00033                 
00034                 if(action->cleanup_callback != NULL)
00035                         (action->cleanup_callback)(&(action->params), action->params_size);
00036                 
00037                 scheduled_actions.pop();
00038         }
00039 }
00040 
00045 void ActionScheduler::dispatchAction(int cycle) {
00046 #ifdef DEBUG
00047         LOG_PRINTP(LOG_DEBUG, "Executing actions - cycle %d", cycle);
00048 #endif //DEBUG
00049 
00050         while( ! scheduled_actions.empty() ) {
00051                 struct scheduled_action *action = &(scheduled_actions.front());
00052                 
00053                 if(action->cycle > cycle) 
00054                         break;
00055 
00056 #ifdef DEBUG            
00057                 LOG_PRINTP(LOG_DEBUG, "Dispatching action - method address: %p, cleanup_callback: %p",
00058                         action->action_callback, action->cleanup_callback );
00059 #endif //DEBUG
00060                         
00061                 doDispatchAction(action);
00062                 
00063                 scheduled_actions.pop();
00064         }
00065 }
00066 
00070 void ActionScheduler::doDispatchAction(struct scheduled_action *action) {
00071         (action->action_callback) (WM, SS, PS, action->params, action->params_size);
00072         if( action->cleanup_callback != NULL )
00073                 (action->cleanup_callback) (action->params, action->params_size);
00074 }
00075 
00085 void ActionScheduler::scheduleAction(
00086                 int cycle, 
00087                 void (*action_callback)(WorldModel*,ServerSettings*,PlayerSettings*, void* params, int params_size), 
00088                 void (*cleanup_callback)(void*,int), 
00089                 void* params,
00090                 int params_size) {
00091         
00092 #ifdef DEBUG
00093         LOG_PRINTP(LOG_DEBUG, "Scheduling action_callback: %p, cleanup_callback: %p, for cycle %d",
00094                 action_callback, cleanup_callback, cycle );
00095 #endif //DEBUG
00096         
00097         struct scheduled_action action;
00098         action.cycle = cycle;
00099         action.action_callback = action_callback;
00100         action.cleanup_callback = cleanup_callback;
00101         action.params = params;
00102         action.params_size = params_size;
00103         
00104         scheduled_actions.push(action);
00105 }

Generated on Thu Apr 26 22:45:26 2007 for GangOfSix(GOS)-RoboCupTeamProject by  doxygen 1.5.1-p1