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

fileserver.cpp

Go to the documentation of this file.
00001 /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*-
00002 
00003    this file is part of rcssserver3D
00004    Fri May 9 2003
00005    Copyright (C) 2002,2003 Koblenz University
00006    Copyright (C) 2003 RoboCup Soccer Server 3D Maintenance Group
00007    $Id: fileserver.cpp,v 1.7 2004/04/18 16:19:47 rollmark Exp $
00008 
00009    This program is free software; you can redistribute it and/or modify
00010    it under the terms of the GNU General Public License as published by
00011    the Free Software Foundation; version 2 of the License.
00012 
00013    This program is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017 
00018    You should have received a copy of the GNU General Public License
00019    along with this program; if not, write to the Free Software
00020    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00021 */
00022 #include "fileserver.h"
00023 #include <salt/fileclasses.h>
00024 #include <zeitgeist/logserver/logserver.h>
00025 #include <zeitgeist/core.h>
00026 
00027 using namespace boost;
00028 using namespace salt;
00029 using namespace zeitgeist;
00030 using namespace std;
00031 
00032 FileServer::FileServer() : Node(), mNextHandle(1)
00033 {
00034 }
00035 
00036 FileServer::~FileServer()
00037 {
00038 }
00039 
00040 shared_ptr<salt::RFile> FileServer::Open(const string& inName)
00041 {
00042     for (TLeafList::iterator i = mChildren.begin(); i != mChildren.end(); ++i)
00043         {
00044             shared_ptr<FileSystem> fileSys = shared_static_cast<FileSystem>(*i);
00045 
00046             shared_ptr<salt::RFile> file(fileSys->Open(inName));
00047 
00048             //first successful is returned
00049             if(file.get() != 0)
00050                 {
00051                     return file;
00052                 }
00053         }
00054 
00055     // try to open it via the regular file system
00056     shared_ptr<salt::RFile> file(new StdFile());
00057     if (! file->Open(inName.c_str()))
00058         {
00059             file.reset();
00060         }
00061 
00062     return file;
00063 }
00064 
00065 FileServer::THandle FileServer::Register(const string& inName)
00066 {
00067     shared_ptr<salt::RFile> file = Open(inName);
00068 
00069     if (file.get() == 0)
00070         {
00071             return 0;
00072         }
00073 
00074     mFileMap[mNextHandle] = file;
00075     THandle h = mNextHandle;
00076     ++mNextHandle;
00077 
00078     return h;
00079 }
00080 
00081 shared_ptr<salt::RFile> FileServer::Get(THandle handle) const
00082 {
00083     TFileMap::const_iterator iter = mFileMap.find(handle);
00084 
00085     if (iter == mFileMap.end())
00086         {
00087             GetLog()->Warning()
00088                 << "(FileServer::Get) Warning: Unknown file handle "
00089                 << handle << "\n";
00090             return shared_ptr<salt::RFile>();
00091         }
00092 
00093     return (*iter).second;
00094 }
00095 
00096 void FileServer::Close(THandle handle)
00097 {
00098     TFileMap::iterator iter = mFileMap.find(handle);
00099 
00100     if (iter == mFileMap.end())
00101         {
00102             GetLog()->Warning()
00103                 << "(FileServer::Close) Warning: Unknown file handle "
00104                 << handle << "\n";
00105             return;
00106         }
00107 
00108     mFileMap.erase(iter);
00109 
00110     if (mFileMap.empty())
00111         {
00112             // restart handle counting on empty FileMap
00113             mNextHandle = 1;
00114         }
00115 }
00116 
00117 void FileServer::OnUnlink()
00118 {
00119     if (! mFileMap.empty())
00120         {
00121             GetLog()->Warning() << "(FileServer) There are "
00122                                 << mFileMap.size()
00123                                 << " files left in the registry\n";
00124         }
00125 }
00126 
00127 bool FileServer::Exist(const string& inName)
00128 {
00129     return (Open(inName).get() != 0);
00130 }
00131 
00132 // this routine registers a new file system instance with the server
00133 bool FileServer::Mount(const string& inFileSysName, const string& inPath)
00134 {
00135     shared_ptr<FileSystem> fileSys =
00136         shared_static_cast<FileSystem>(GetChild(inPath));
00137 
00138     if (fileSys)
00139         {
00140             // we already have a file system which is bound to the same name
00141             if (fileSys->GetClass()->GetName().compare(inFileSysName) == 0)
00142                 {
00143                     // as the file system has the same type, we can return true
00144                     return true;
00145                 }
00146             else
00147                 {
00148                     // already have a file system of a different type, so return false
00149                     GetLog()->Error()
00150                         << "(FileServer) ERROR: a FileSystem is already mounted a "
00151                         << inPath << "\n";
00152                     return false;
00153                 }
00154         }
00155 
00156     // try to instantiate the file system
00157     fileSys = shared_static_cast<FileSystem>(GetCore()->New(inFileSysName));
00158 
00159     if (
00160         (fileSys.get() == 0) ||
00161         (! fileSys->SetPath(inPath))
00162         )
00163         {
00164             return false;
00165         }
00166 
00167     // link it into our hierarchy
00168     AddChildReference(fileSys);
00169 
00170     GetLog()->Normal() <<
00171         "(FileServer) successfully mounted a '"
00172                        << inFileSysName << "' at '"
00173                        << inPath << "'\n";
00174     return true;
00175 }
00176 
00177 bool FileServer::Unmount(const string& inPath)
00178 {
00179     // try to remove a std file system first
00180     if (Unmount ("FileSystemSTD", inPath))
00181         {
00182             return true;
00183         }
00184 
00185     shared_ptr<Leaf> leaf = GetChild(inPath);
00186 
00187     if(leaf)
00188     {
00189         leaf->Unlink();
00190  return true;
00191     }
00192 
00193   return false;
00194 }
00195 
00196 bool FileServer::Unmount(const string& inFileSysName, const string& inPath)
00197 {
00198   shared_ptr<FileSystem> fileSystem = shared_static_cast<FileSystem>(GetChild(inPath));
00199 
00200   if(fileSystem)
00201     {
00202       if (fileSystem->GetClass()->GetName().compare(inFileSysName) == 0)
00203         {
00204           fileSystem->Unlink();
00205           return true;
00206         }
00207     }
00208 
00209   return false;
00210 }
00211 
00212 int FileServer::ForEachFile(const string& /*directory*/, const string& /*name*/,
00213                             const string& /*extension*/,
00214                             FileSystem::TCallback /*callback*/, void* /*param*/)
00215 {
00216   int count = 0;
00217   /*for (TDescriptionList::iterator iter = mFileSystems.begin (); iter != mFileSystems.end (); ++iter)
00218     {
00219     count += iter->fileSystem->ForEachFile(directory,name,extension,callback,param);
00220     }*/
00221 
00222   return count;
00223 }
00224 

Generated on Thu Apr 6 15:25:37 2006 for rcssserver3d by  doxygen 1.4.4