Task.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #ifndef __TASK_H_
00030 #define __TASK_H_
00031
00032 #ifdef MIP_HOST_APPLE
00033 #include <applePatch.h>
00034 #endif
00035
00036 #include <stdio.h>
00037 #include <vector>
00038
00039 #include <baselib.h>
00040 #include <Resource.h>
00041
00042 #include <TaskDict.h>
00043
00044 using namespace MipResources;
00045
00048 namespace MipTasks{
00049
00054
00055
00056
00057 enum TaskOutputs {
00058 TASK_OUT_GO_ON,
00059 TASK_OUT_EXIT_NORMAL,
00060 TASK_OUT_EXIT_GENERIC_PROBLEM,
00061 TASK_OUT_NUM
00062 };
00063
00064 static const char* TaskOutputsNames[TASK_OUT_NUM] = {
00065 "TASK_OUT_GO_ON",
00066 "TASK_OUT_EXIT_NORMAL",
00067 "TASK_OUT_EXIT_GENERIC_PROBLEM"
00068 };
00069
00070
00083
00084 class Task : public MIPObject{
00085
00086 public:
00087
00088 Timer lastRunTimer;
00089
00090 Task(){
00091
00092 }
00093
00095 virtual TaskPlate getPlate() const=0;
00096
00098 virtual string getName() const {
00099 assert(getPlate()<TASK_NUM);
00100 return TaskNames[getPlate()];
00101 }
00102
00103 virtual string getObjectName() const {
00104 return "Task";
00105 }
00106
00108 string print(){
00109 stringstream s;
00110 s << "Task Card: [plate=" << getPlate() << ", name=" << getName() << "]";
00111 return s.str();
00112 }
00114 virtual Time getMaxDuration()=0;
00116 virtual Time getMaxSamplPeriod()=0;
00118 virtual Time getMinSamplPeriod()=0;
00120 virtual TaskOutputs run(void)=0;
00121
00122
00125 void fatalResourceNotPresent(uint resourcePlate){
00126 if(resourcePlate < RESOURCE_NUM){
00127 stringstream ss;
00128 ss << "A Resource of " << ResourceNames[resourcePlate] << " Type is Not Present.";
00129 fatal(ss.str());
00130 }else{
00131 fatal("Wrong resource plate");
00132 }
00133 }
00134
00135
00138 void warningResourceNotPresent(uint resourcePlate){
00139 if(resourcePlate < RESOURCE_NUM){
00140 stringstream ss;
00141 ss << "A Resource of " << ResourceNames[resourcePlate] << " Type is Not Present.";
00142 warning(ss.str());
00143 }else{
00144 warning("Wrong resource plate");
00145 }
00146 }
00147
00148 };
00149
00150
00153 typedef vector<Task*> TasksPointers;
00154
00155
00158 typedef vector<Task**> TasksPPointers;
00159
00160
00163 class TaskUtilities {
00164 private:
00165
00166 public:
00167
00171 static void getPlatesFromFile(string fileName,TaskPlates& plates){
00172 File file(fileName);
00173 vector<string> words;
00174 file.allWords(words);
00175 getPlatesFromWords(words,plates);
00176 }
00177
00181 static void getPlatesFromWords(vector<string>& words,TaskPlates& plates){
00182 plates.clear();
00183 unsigned int wordsNum = words.size();
00184 for(unsigned int i=0; i<wordsNum;i++){
00185 for(unsigned int plateNum=0;plateNum<TASK_NUM;plateNum++){
00186 string taskName(TaskNames[plateNum]);
00187 if(words[i] == taskName){
00188 TaskPlate plate = (TaskPlate) plateNum;
00189 plates.push_back(plate);
00190 continue;
00191 }
00192 }
00193 }
00194 }
00195
00199 static void getPluginsFromFile(string fileName, vector<string>& names, vector<string>& libraries){
00200 names.clear();
00201 libraries.clear();
00202 File file(fileName);
00203 vector<string> words;
00204 file.allWords(words);
00205 getPluginsFromWords(words,names,libraries);
00206 }
00207
00211 static void getPluginsFromWords(vector<string>& words, vector<string>& names, vector<string>& libraries){
00212
00213 string fileName = getenv("MIP_ROOT");
00214 if(fileName.empty())
00215 fileName = "../../PluginExample/PluginList.txt";
00216 else
00217 fileName+= "/PluginExample/PluginList.txt";
00218
00219 File pluginListFile(fileName);
00220 vector<string> pluginList;
00221 pluginListFile.allWords(pluginList);
00222
00223 string plTskName("PluginTask:");
00224 unsigned int wordsNum = words.size();
00225 for(unsigned int i=0; i<wordsNum;i++){
00226 if (words[i].length()>11){
00227 string wordBeginning(words[i].substr(0, 11));
00228 if (wordBeginning == plTskName){
00229 bool libraryFound = false;
00230 int ddotsPos = words[i].find_first_of(':', 11);
00231 string libraryName(words[i].substr(11,ddotsPos-11));
00232 string library;
00233 unsigned int pluginListSize = pluginList.size();
00234 for(unsigned int j=0; j<pluginListSize; ++j){
00235 if (pluginList[j].npos > pluginList[j].rfind(libraryName)){
00236 libraryFound = true;
00237 stringstream ssLib;
00238 library = pluginList[j].substr(0, pluginList[j].size()-21);
00239 ssLib << library << "lib/lib" << libraryName << ".so";
00240 library = ssLib.str();
00241 break;
00242 }
00243 }
00244 if (!libraryFound){
00245 stringstream ssFail;
00246 ssFail << "Impossible to find MIP-Plugin " << libraryName << " library in the PluginExample/PluginList.txt. Please execute MIPProfilePlugins. ";
00247 ssFail << "If the error persists, you may have changed the name of your plugin folder, withouth changing the project name, or viceversa. ";
00248 ssFail << "Also check that the build directory of your plugin contains a MIPPluginManifest.MIP empty file.";
00249 LogTrace::fatal(ssFail.str());
00250 }
00251 string name(words[i].substr(ddotsPos+1));
00252 names.push_back(name);
00253 libraries.push_back(library);
00254 }
00255 }
00256 }
00257 }
00258 };
00259
00260
00261
00262 typedef Task* create_task(ResourcePointers resources,int argc, const char* argv[]);
00263
00264 typedef void destroy_task(Task*);
00265
00266 };
00267
00268 #endif