Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members

ManagerImpl.cc

Go to the documentation of this file.
00001 /*
00002  * ManagerImpl.cc
00003  *
00004  * Copyright (c) 2003 The University of Utah and the Flux Group.
00005  * All rights reserved.
00006  *
00007  * This file is licensed under the terms of the GNU Public License.  
00008  * See the file "license.terms" for restrictions on redistribution 
00009  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
00010  */
00011 
00012 /**
00013  * @file ManagerImpl.cc
00014  *
00015  * Implementation of the ManagerImpl class.
00016  */
00017 
00018 #include "config.h"
00019 
00020 #include <assert_pp.h>
00021 
00022 #include "ManagerImpl.hh"
00023 
00024 ManagerImpl::ManagerImpl(const char *name)
00025 {
00026 }
00027 
00028 ManagerImpl::~ManagerImpl()
00029 {
00030 }
00031 
00032 void ManagerImpl::RepairTaskList(void)
00033 {
00034     unsigned int lpc;
00035 
00036     /* Walk the list, */
00037     for( lpc = 0; lpc < this->mi_Tasks.length(); )
00038     {
00039         Broker::Task_ptr task = this->mi_Tasks[lpc];
00040         
00041         try
00042         {
00043             CORBA::String_var name;
00044 
00045             /* ... do an RPC, and */
00046             name = task->Name();
00047             /* ... move to the next index. */
00048             lpc += 1;
00049         }
00050         catch(const CORBA::TRANSIENT &e)
00051         {
00052             /* Oops, task is toast, try to tell the policy and */
00053             if( !CORBA::is_nil(this->mi_CurrentPolicy.in()) )
00054             {
00055                 try
00056                 {
00057                     this->mi_CurrentPolicy->RemoveTask(task);
00058                 }
00059                 catch(const CORBA::SystemException &e)
00060                 {
00061                     cerr << "Error while repairing: "
00062                          << e
00063                          << " file: "
00064                          << __FILE__
00065                          << " line: "
00066                          << __LINE__
00067                          << endl;
00068                 }
00069             }
00070             /* ... then remove it. */
00071             this->RemoveTaskAt(lpc);
00072             task = Broker::Task::_nil(); /* 'task' is dead after removal. */
00073         }
00074     }
00075 }
00076 
00077 int ManagerImpl::TaskIndex(Broker::Task_ptr task)
00078 {
00079     unsigned int lpc;
00080     int retval = -1;
00081 
00082     require(!CORBA::is_nil(task));
00083     
00084     for( lpc = 0; (lpc < this->mi_Tasks.length()) && (retval == -1); lpc++ )
00085     {
00086         if( this->mi_Tasks[lpc]->_is_equivalent(task) )
00087         {
00088             retval = lpc;
00089         }
00090     }
00091     return( retval );
00092 }
00093 
00094 Broker::Policy_ptr ManagerImpl::CurrentPolicy(void)
00095     throw (CORBA::SystemException)
00096 {
00097     Broker::Policy_var retval;
00098 
00099     retval = this->mi_CurrentPolicy;
00100     return( retval._retn() );
00101 }
00102 
00103 void ManagerImpl::CurrentPolicy(Broker::Policy_ptr policy)
00104     throw (CORBA::SystemException)
00105 {
00106     if( !CORBA::is_nil(this->mi_CurrentPolicy.in()) )
00107     {
00108         this->mi_CurrentPolicy->Deactivate();
00109     }
00110     this->mi_CurrentPolicy = Broker::Policy::_duplicate(policy);
00111     if( !CORBA::is_nil(this->mi_CurrentPolicy.in()) )
00112     {
00113         this->mi_CurrentPolicy->Activate(this->mi_Tasks);
00114     }
00115 }
00116 
00117 void ManagerImpl::AddTask(Broker::Task_ptr task,
00118                           const Broker::ScheduleParameters &sp)
00119     throw (CORBA::SystemException,
00120            Broker::DuplicateScheduleParameter,
00121            Broker::InvalidScheduleParameter,
00122            Broker::MissingScheduleParameter)
00123 {
00124     int slot;
00125 
00126     if( CORBA::is_nil(task) )
00127     {
00128         throw CORBA::BAD_PARAM();
00129     }
00130 
00131     this->RepairTaskList();
00132 
00133     if( this->TaskIndex(task) != -1 )
00134     {
00135         throw CORBA::BAD_PARAM();
00136     }
00137     
00138     slot = this->mi_Tasks.length();
00139     this->mi_Tasks.length(slot + 1);
00140     this->mi_Tasks[slot] = Broker::Task::_duplicate(task);
00141 
00142     try
00143     {
00144         task->BeginCPUScheduling(this->_this(), sp);
00145         if( !CORBA::is_nil(this->mi_CurrentPolicy.in()) )
00146         {
00147             this->mi_CurrentPolicy->AddTask(task, sp);
00148         }
00149     }
00150     catch(const CORBA::Exception &e)
00151     {
00152         this->mi_Tasks.length(slot);
00153         task->EndCPUScheduling();
00154         throw e;
00155     }
00156 }
00157 
00158 void ManagerImpl::RemoveTaskAt(unsigned int index)
00159 {
00160     unsigned int lpc;
00161     
00162     require(index < this->mi_Tasks.length());
00163 
00164     /* Fill the gap created by the removal and */
00165     for( lpc = (unsigned int)index;
00166          lpc < (this->mi_Tasks.length() - 1);
00167          lpc++ )
00168     {
00169         this->mi_Tasks[lpc] = this->mi_Tasks[lpc + 1];
00170     }
00171     /* ... decrement the length. */
00172     this->mi_Tasks.length(this->mi_Tasks.length() - 1);
00173 }
00174 
00175 void ManagerImpl::RemoveTask(Broker::Task_ptr task)
00176     throw (CORBA::SystemException)
00177 {
00178     int slot;
00179     
00180     if( CORBA::is_nil(task) )
00181     {
00182         throw CORBA::BAD_PARAM();
00183     }
00184 
00185     /* Find the task, */
00186     if( (slot = this->TaskIndex(task)) == -1 )
00187     {
00188         throw CORBA::BAD_PARAM();
00189     }
00190 
00191     /* ... remove it from our list, */
00192     this->RemoveTaskAt(slot);
00193 
00194     try
00195     {
00196         /* ... tell the task, and */
00197         task->EndCPUScheduling();
00198     }
00199     catch(const CORBA::SystemException &e)
00200     {
00201         cerr << e << endl; /* Atleast log the failure. */
00202     }
00203     
00204     /* ... tell the policy. */
00205     if( !CORBA::is_nil(this->mi_CurrentPolicy.in()) )
00206     {
00207         this->mi_CurrentPolicy->RemoveTask(task);
00208     }
00209 }
00210 
00211 Broker::TaskList *ManagerImpl::GetTaskList(void)
00212     throw (CORBA::SystemException)
00213 {
00214     Broker::TaskList_var retval;
00215 
00216     retval = new Broker::TaskList(this->mi_Tasks);
00217     return( retval._retn() );
00218 }
00219 
00220 void ManagerImpl::ChangeTaskCPU(Broker::RealTimeTask_ptr task,
00221                                 CORBA::ULong ct,
00222                                 CORBA::ULong status,
00223                                 CORBA::ULong advice)
00224     throw (CORBA::SystemException, Broker::InvalidState)
00225 {
00226     if( CORBA::is_nil(task) )
00227     {
00228         throw CORBA::BAD_PARAM();
00229     }
00230 
00231     if( !CORBA::is_nil(this->mi_CurrentPolicy.in()) )
00232     {
00233         /* Have a policy, just pass it on. */
00234         this->mi_CurrentPolicy->ChangeTaskCPU(task, ct, status, advice);
00235     }
00236     else
00237     {
00238         /* No policy, just set the time and return. */
00239         try
00240         {
00241             task->SetComputeTime(advice);
00242         }
00243         catch(...)
00244         {
00245             /* XXX */
00246         }
00247     }
00248 }
00249 
00250 Broker::Task_ptr ManagerImpl::ResolveTask(CORBA::ORB_ptr orb,
00251                                           Broker::TaskList &tl,
00252                                           const char *name)
00253     throw (CORBA::SystemException)
00254 {
00255     Broker::Task_var retval;
00256     unsigned int lpc;
00257 
00258     require(!CORBA::is_nil(orb));
00259     require(name != NULL);
00260 
00261     /* First try finding the task, by name, in the list. */
00262     for( lpc = 0; (lpc < tl.length()) && CORBA::is_nil(retval.in()); lpc++ )
00263     {
00264         try
00265         {
00266             CORBA::String_var task_name;
00267             
00268             task_name = tl[lpc]->Name();
00269             if( strcmp(task_name.in(), name) == 0 )
00270             {
00271                 retval = Broker::Task::_duplicate(tl[lpc]);
00272             }
00273         }
00274         catch(const CORBA::SystemException &e)
00275         {
00276             /* ... */
00277         }
00278     }
00279     if( CORBA::is_nil(retval.in()) )
00280     {
00281         CORBA::Object_var obj;
00282 
00283         /* No luck finding it in the list, check if it is an IOR. */
00284         obj = orb->string_to_object(name);
00285         retval = Broker::Task::_narrow(obj.in());
00286         if( CORBA::is_nil(retval.in()) )
00287         {
00288             throw CORBA::BAD_PARAM();
00289         }
00290     }
00291     return( retval._retn() );
00292 }

Generated on Mon Dec 1 16:29:06 2003 for CPU Broker by doxygen 1.3.4