# Home    # nevrax.com   
Nevrax
Nevrax.org
#News
#Mailing-list
#Documentation
#CVS
#Bugs
#License
Docs
 
Documentation  
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Search  

async_file_manager.cpp

Go to the documentation of this file.
00001 
00007 /* Copyright, 2000 Nevrax Ltd.
00008  *
00009  * This file is part of NEVRAX NEL.
00010  * NEVRAX NEL is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2, or (at your option)
00013  * any later version.
00014 
00015  * NEVRAX NEL is distributed in the hope that it will be useful, but
00016  * WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00018  * General Public License for more details.
00019 
00020  * You should have received a copy of the GNU General Public License
00021  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00022  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00023  * MA 02111-1307, USA.
00024  */
00025 #include "stdmisc.h"
00026 #include "nel/misc/file.h"
00027 #include "nel/misc/path.h"
00028 #include "nel/misc/async_file_manager.h"
00029 
00030 
00031 using namespace std;
00032 
00033 namespace NLMISC
00034 {
00035 
00036 CAsyncFileManager *CAsyncFileManager::_Singleton = NULL;
00037 
00038 // ***************************************************************************
00039 
00040 CAsyncFileManager::CAsyncFileManager()
00041 {
00042 }
00043 
00044 // ***************************************************************************
00045 
00046 CAsyncFileManager &CAsyncFileManager::getInstance()
00047 {
00048         if (_Singleton == NULL)
00049         {
00050                 _Singleton = new CAsyncFileManager();
00051         }
00052         return *_Singleton;
00053 }
00054 
00055 // ***************************************************************************
00056 
00057 void CAsyncFileManager::terminate ()
00058 {
00059         if (_Singleton != NULL)
00060         {
00061                 delete &getInstance();
00062                 _Singleton = NULL;
00063         }
00064 }
00065 
00066 
00067 void CAsyncFileManager::addLoadTask(IRunnable *ploadTask)
00068 {
00069         addTask(ploadTask);
00070 }
00071 
00072 bool CAsyncFileManager::cancelLoadTask(const CAsyncFileManager::ICancelCallback &callback)
00073 {
00074         CSynchronized<list<IRunnable *> >::CAccessor acces(&_TaskQueue);
00075         list<IRunnable*> &rTaskQueue = acces.value ();
00076         list<IRunnable*>::iterator it = rTaskQueue.begin();
00077 
00078         while (it != rTaskQueue.end())
00079         {
00080                 IRunnable *pR = *it;
00081 
00082                 // check the task with the cancel callback.
00083                 if (callback.callback(pR))
00084                 {
00085                         // Delete the load task
00086                         delete pR;
00087                         rTaskQueue.erase (it);
00088                         return true;
00089                 }
00090                 ++it;
00091         }
00092 
00093         // If not found, the current running task may be the one we want to cancel. Must wait it.
00094         waitCurrentTaskToComplete ();
00095 
00096         return false;
00097 }
00098 
00099 // ***************************************************************************
00100 /*      
00101 void CAsyncFileManager::loadMesh(const std::string& meshName, IShape **ppShp, IDriver *pDriver)
00102 {
00103         addTask (new CMeshLoad(meshName, ppShp, pDriver));
00104 }
00105 */
00106 // ***************************************************************************
00107 /*
00108 bool CAsyncFileManager::cancelLoadMesh(const std::string& sMeshName)
00109 {
00110         CSynchronized<list<IRunnable *> >::CAccessor acces(&_TaskQueue);
00111         list<IRunnable*> &rTaskQueue = acces.value ();
00112         list<IRunnable*>::iterator it = rTaskQueue.begin();
00113 
00114         while (it != rTaskQueue.end())
00115         {
00116                 IRunnable *pR = *it;
00117                 CMeshLoad *pML = dynamic_cast<CMeshLoad*>(pR);
00118                 if (pML != NULL)
00119                 {
00120                         if (pML->MeshName == sMeshName)
00121                         {
00122                                 // Delete mesh load task
00123                                 delete pML;
00124                                 rTaskQueue.erase (it);
00125                                 return true;
00126                         }
00127                 }
00128                 ++it;
00129         }
00130         return false;
00131 }
00132 */
00133 // ***************************************************************************
00134 /*      
00135 void CAsyncFileManager::loadIG (const std::string& IGName, CInstanceGroup **ppIG)
00136 {
00137         addTask (new CIGLoad(IGName, ppIG));
00138 }
00139 
00140 // ***************************************************************************
00141         
00142 void CAsyncFileManager::loadIGUser (const std::string& IGName, UInstanceGroup **ppIG)
00143 {
00144         addTask (new CIGLoadUser(IGName, ppIG));
00145 }
00146 */
00147 // ***************************************************************************
00148         
00149 void CAsyncFileManager::loadFile (const std::string& sFileName, uint8 **ppFile)
00150 {
00151         addTask (new CFileLoad (sFileName, ppFile));
00152 }
00153 
00154 // ***************************************************************************
00155 
00156 void CAsyncFileManager::loadFiles (const std::vector<std::string> &vFileNames, const std::vector<uint8**> &vPtrs)
00157 {
00158         addTask (new CMultipleFileLoad (vFileNames, vPtrs));
00159 }
00160 
00161 // ***************************************************************************
00162 
00163 void CAsyncFileManager::signal (bool *pSgn)
00164 {
00165         addTask (new CSignal (pSgn));
00166 }
00167 
00168 // ***************************************************************************
00169 
00170 void CAsyncFileManager::cancelSignal (bool *pSgn)
00171 {
00172         CSynchronized<list<IRunnable *> >::CAccessor acces(&_TaskQueue);
00173         list<IRunnable*> &rTaskQueue = acces.value ();
00174         list<IRunnable*>::iterator it = rTaskQueue.begin();
00175 
00176         while (it != rTaskQueue.end())
00177         {
00178                 IRunnable *pR = *it;
00179                 CSignal *pS = dynamic_cast<CSignal*>(pR);
00180                 if (pS != NULL)
00181                 {
00182                         if (pS->Sgn == pSgn)
00183                         {
00184                                 // Delete signal task
00185                                 delete pS;
00186                                 rTaskQueue.erase (it);
00187                                 return;
00188                         }
00189                 }
00190                 ++it;
00191         }
00192 }
00193 
00194 // ***************************************************************************
00195 // FileLoad
00196 // ***************************************************************************
00197 
00198 // ***************************************************************************
00199 CAsyncFileManager::CFileLoad::CFileLoad (const std::string& sFileName, uint8 **ppFile)
00200 {
00201         _FileName = sFileName;
00202         _ppFile = ppFile;
00203 }
00204 
00205 // ***************************************************************************
00206 void CAsyncFileManager::CFileLoad::run (void)
00207 {
00208         FILE *f = fopen (_FileName.c_str(), "rb");
00209         if (f != NULL)
00210         {
00211                 uint8 *ptr;
00212                 fseek (f, 0, SEEK_END);
00213                 long filesize = ftell (f);
00214                 nlSleep(5);
00215                 fseek (f, 0, SEEK_SET);
00216                 ptr = new uint8[filesize];
00217                 fread (ptr, filesize, 1, f);
00218                 fclose (f);
00219 
00220                 *_ppFile = ptr;
00221         }
00222         else
00223         {
00224                 nlwarning ("Couldn't load '%s'", _FileName.c_str());
00225                 *_ppFile = (uint8*)-1;
00226         }
00227 }
00228 
00229 // ***************************************************************************
00230 // MultipleFileLoad
00231 // ***************************************************************************
00232 
00233 // ***************************************************************************
00234 CAsyncFileManager::CMultipleFileLoad::CMultipleFileLoad (const std::vector<std::string> &vFileNames, 
00235                                                                                                                  const std::vector<uint8**> &vPtrs)
00236 {
00237         _FileNames = vFileNames;
00238         _Ptrs = vPtrs;
00239 }
00240 
00241 // ***************************************************************************
00242 void CAsyncFileManager::CMultipleFileLoad::run (void)
00243 {
00244         for (uint32 i = 0; i < _FileNames.size(); ++i)
00245         {
00246                 FILE *f = fopen (_FileNames[i].c_str(), "rb");
00247                 if (f != NULL)
00248                 {
00249                         uint8 *ptr;
00250                         fseek (f, 0, SEEK_END);
00251                         long filesize = ftell (f);
00252                         nlSleep(5);
00253                         fseek (f, 0, SEEK_SET);
00254                         ptr = new uint8[filesize];
00255                         fread (ptr, filesize, 1, f);
00256                         fclose (f);
00257 
00258                         *_Ptrs[i] = ptr;
00259                 }
00260                 else
00261                 {
00262                         nlwarning ("Couldn't load '%s'", _FileNames[i].c_str());
00263                         *_Ptrs[i] = (uint8*)-1;
00264                 }
00265         }
00266 
00267 }
00268 
00269 // ***************************************************************************
00270 // Signal
00271 // ***************************************************************************
00272 
00273 // ***************************************************************************
00274 CAsyncFileManager::CSignal::CSignal (bool *pSgn)
00275 {
00276         Sgn = pSgn;
00277         *Sgn = false;
00278 }
00279 
00280 // ***************************************************************************
00281 void CAsyncFileManager::CSignal::run (void)
00282 {
00283         *Sgn = true;
00284 }
00285 
00286 
00287 } // NLMISC
00288