mumu::Entry< Type > Class Template Reference

The template represents a single file on mumufs. More...

#include <mumulib.h>

List of all members.

Public Member Functions

 Entry (const std::string &path, RWMode openMode=ReadWrite, bool isBlocking=true)
 Constructs a single entry. Creates the corresponding file if it does not exist.
 ~Entry ()
 Closes the file.
 Entry (const Entry &rhs)
 Constructs a copy of entry.
bool Select (struct timeval *inTimeout)
 select(...) for the file on mumufs
void Write (const unsigned char *Buffer, size_t BufferSize)
 Write a raw buffer to the entry.
void Write (const Type &T)
 Write the given object to the entry. Be carefull, this is the generic implementation which supposes that the data occupies a consequent memory address space.
int Read (unsigned char *Buffer, size_t BufferSize)
 Reads raw data into the given buffer.
Type Read (void)
 reading of entry. It suits for blocking mode.
int Read (Type &T)
 reading of entry. It suits for non-blocking mode.
template<>
void Write (const std::string &T)
template<>
int Read (std::string &S)
template<>
std::string Read (void)

Protected Member Functions

struct timeval AddTimeval (const struct timeval &Left, const struct timeval &Right) const
 Utility function to add two timeval values.
struct timeval SubTimeval (const struct timeval &Left, const struct timeval &Right) const
 Utility function to subtract two timeval values.
bool LessTimeval (const struct timeval &Left, const struct timeval &Right) const
 Utility function to compare two timeval values.
off_t GetFileSize (void) const
 Retrieves file size in bytes.

Detailed Description

template<typename Type>
class mumu::Entry< Type >

The template represents a single file on mumufs.

Definition at line 49 of file mumulib.h.


Constructor & Destructor Documentation

template<typename Type >
mumu::Entry< Type >::Entry ( const std::string &  path,
RWMode  openMode = ReadWrite,
bool  isBlocking = true 
) [inline, explicit]

Constructs a single entry. Creates the corresponding file if it does not exist.

Parameters:
path Path to a file on mumufs.
openMode How to open a file. Default is ReadWrite.
isBlocking file opening mode. Default is blocking.
Exceptions:
std::runtime_error in case of problems

The blocking mode matters for read(...) calls only

Definition at line 66 of file mumulib.h.

00068                                                                      : fd( -1 ), mode( openMode )
00069             {
00070                 int     flags( openMode | O_CREAT );
00071                 if ( !isBlocking )  flags |= O_NONBLOCK;
00072 
00073                 fd = open( path.c_str(), flags, S_IRUSR | S_IWUSR |
00074                                                 S_IRGRP | S_IWGRP |
00075                                                 S_IROTH | S_IWOTH );
00076                 if ( fd < 0 )       throw std::runtime_error( "Cannot open the given entry." );
00077             }

template<typename Type >
mumu::Entry< Type >::Entry ( const Entry< Type > &  rhs  )  [inline]

Constructs a copy of entry.

Parameters:
rhs Pattern
Exceptions:
std::runtime_error in case of errors

Definition at line 92 of file mumulib.h.

00092                                         : fd( dup( rhs.fd ) )
00093             {
00094                 if ( fd < 0 )       throw std::runtime_error( "Cannot duplicate file descriptor." );
00095             }


Member Function Documentation

template<typename Type >
struct timeval mumu::Entry< Type >::AddTimeval ( const struct timeval &  Left,
const struct timeval &  Right 
) const [inline, read, protected]

Utility function to add two timeval values.

Parameters:
Left First timeval
Right Second timeval
Returns:
Addition result

Definition at line 109 of file mumulib.h.

Referenced by mumu::Entry< Type >::Select().

00111             {
00112                 struct timeval      Result;
00113 
00114                 Result.tv_sec = Left.tv_sec + Right.tv_sec;
00115                 Result.tv_usec = Left.tv_usec + Right.tv_usec;
00116                 while ( Result.tv_usec >= 1000000 )
00117                 {
00118                     ++Result.tv_sec;
00119                     Result.tv_usec -= 1000000;
00120                 }
00121                 return Result;
00122             }

template<typename Type >
off_t mumu::Entry< Type >::GetFileSize ( void   )  const [inline, protected]

Retrieves file size in bytes.

Returns:
File size in bytes
Exceptions:
Throws std::runtime_error in case of fstat() call errors

Definition at line 165 of file mumulib.h.

Referenced by mumu::Entry< Type >::Read().

00166             {
00167                 struct stat     fileInfo;
00168 
00169                 if ( fstat( fd, &fileInfo ) != 0 )
00170                 {
00171                     throw std::runtime_error( "Cannot get fstat" );
00172                 }
00173                 return fileInfo.st_size;
00174             }

template<typename Type >
bool mumu::Entry< Type >::LessTimeval ( const struct timeval &  Left,
const struct timeval &  Right 
) const [inline, protected]

Utility function to compare two timeval values.

Parameters:
Left First timeval
Right Second timeval
Returns:
First < Second

Definition at line 151 of file mumulib.h.

Referenced by mumu::Entry< Type >::Select().

00153             {
00154                 if ( Left.tv_sec < Right.tv_sec )   return true;
00155                 if ( Left.tv_sec > Right.tv_sec )   return false;
00156                 return Left.tv_usec < Right.tv_usec;
00157             }

template<>
std::string mumu::Entry< std::string >::Read ( void   )  [inline]
Returns:
0 - no new data available, positive value - # of bytes read

Definition at line 400 of file mumulib.h.

References mumu::Entry< Type >::Read().

00401     {
00402         std::string     S;
00403         Read( S );
00404         return S;
00405     }

template<>
int mumu::Entry< std::string >::Read ( std::string &  S  )  [inline]
Parameters:
S A reference to the object to be filled
Exceptions:
std::runtime_error in case of errors
Returns:
0 - no new data available, positive value - # of bytes read

Definition at line 359 of file mumulib.h.

References mumu::Entry< Type >::GetFileSize().

00360     {
00361         if ( mode == WriteOnly ) throw std::runtime_error( "Cannot read from WriteOnly entry" );
00362 
00363         off_t           BufferSize( GetFileSize() + 1 );
00364         char *          Buffer( new char[ BufferSize ] );
00365 
00366         for ( ; ; )
00367         {
00368             int     Bytes( read( fd, Buffer, BufferSize ) );
00369 
00370             if ( Bytes > 0 && Bytes < static_cast<int>( BufferSize ) )
00371             {
00372                 if ( Buffer[ Bytes - 1 ] == '\0' )  S = std::string( Buffer, Bytes - 1 );
00373                 else                                S = std::string( Buffer, Bytes );
00374                 delete [] Buffer;
00375                 return Bytes;
00376             }
00377 
00378             if ( Bytes == -1 && errno == EAGAIN ) { delete [] Buffer; return 0; }   // Blocking IO
00379             if ( Bytes == -1 && errno == EINTR ) continue;
00380             if ( Bytes == -1 && errno == EINVAL )
00381             {
00382                 delete [] Buffer;
00383                 BufferSize = GetFileSize() + 1;
00384                 Buffer = new char[ BufferSize ];
00385                 continue;
00386             }
00387 
00388             delete [] Buffer;
00389             throw std::runtime_error( "Error reading from file descriptor" );
00390         }
00391     }

template<typename Type >
int mumu::Entry< Type >::Read ( Type &  T  )  [inline]

reading of entry. It suits for non-blocking mode.

Parameters:
T A reference to the object to be filled
Exceptions:
std::runtime_error in case of errors
Returns:
0 - no new data available, positive value - # of bytes read

Definition at line 313 of file mumulib.h.

00314             {
00315                 if ( mode == WriteOnly ) throw std::runtime_error( "Cannot read from WriteOnly entry" );
00316                 for ( ; ; )
00317                 {
00318                     int     Bytes( read( fd, &T, sizeof( T ) ) );
00319                     if ( Bytes == sizeof( T ) ) return Bytes;
00320 
00321                     if ( Bytes == -1 && errno == EINTR ) continue;
00322                     if ( Bytes == -1 && errno == EAGAIN ) return 0;
00323 
00324                     throw std::runtime_error( "Error reading from file descriptor" );
00325                 }
00326             }

template<typename Type >
Type mumu::Entry< Type >::Read ( void   )  [inline]

reading of entry. It suits for blocking mode.

Returns:
A constructed object

Definition at line 300 of file mumulib.h.

Referenced by mumu::Entry< Type >::Read().

00301             {
00302                 Type    T;
00303                 Read( T );
00304                 return T;
00305             }

template<typename Type >
int mumu::Entry< Type >::Read ( unsigned char *  Buffer,
size_t  BufferSize 
) [inline]

Reads raw data into the given buffer.

Parameters:
Buffer Pointer to the buffer
BufferSize Size of the provided buffer
Returns:
Number of bytes that were actually read or -1 if the buffer is not big enough 0 if no data is available
Exceptions:
std::runtime_error in case of errors

Definition at line 280 of file mumulib.h.

00281             {
00282                 if ( mode == WriteOnly ) throw std::runtime_error( "Cannot read from WriteOnly entry" );
00283 
00284                 for ( ; ; )
00285                 {
00286                     int     Bytes( read( fd, Buffer, BufferSize ) );
00287                     if ( Bytes > 0 ) return Bytes;
00288                     if ( Bytes == -1 && errno == EINTR ) continue;
00289                     if ( Bytes == -1 && errno == EAGAIN ) return 0;
00290                     if ( Bytes == -1 && errno == EINVAL ) return -1;
00291 
00292                     throw std::runtime_error( "Error reading from file descriptor." );
00293                 }
00294             }

template<typename Type >
bool mumu::Entry< Type >::Select ( struct timeval *  inTimeout  )  [inline]

select(...) for the file on mumufs

Parameters:
inTimeout select timeout. It could be NULL. If NULL then waits forever.
Returns:
true if Entry is ready for reading without blocking.
Exceptions:
if the open mode is WriteOnly

Definition at line 184 of file mumulib.h.

References mumu::Entry< Type >::AddTimeval(), mumu::Entry< Type >::LessTimeval(), and mumu::Entry< Type >::SubTimeval().

00185             {
00186                 if ( mode == WriteOnly ) throw std::runtime_error( "Select for WriteOnly mode is not supported" );
00187 
00188                 fd_set             Set;
00189                 struct timeval     AbsoluteEnd;
00190                 struct timeval     Timeout;
00191                 struct timeval *   Argument( NULL );
00192 
00193                 if ( inTimeout != NULL )
00194                 {
00195                     Timeout = *inTimeout;
00196                     Argument = &Timeout;
00197                     gettimeofday( &AbsoluteEnd, NULL );
00198                     AbsoluteEnd = AddTimeval( AbsoluteEnd, Timeout );
00199                 }
00200 
00201 
00202                 FD_ZERO( &Set );
00203                 FD_SET( fd, &Set );
00204 
00205                 int         RetVal;
00206 
00207                 for ( ; ; )
00208                 {
00209                     RetVal = select( fd + 1, &Set, NULL, NULL, Argument );
00210 
00211                         // We have exactly 1 fd so this check is enough for this specific case
00212                     if ( RetVal >= 1 ) return true;
00213                     if ( RetVal == 0 ) return false;
00214 
00215                         // The last case is RetVal < 0 => error
00216                     if ( errno == EINTR )
00217                     {
00218                         // It is the timeout exceeded, we need to call select again
00219                         // We can reach this point only if we had !NULL timeout
00220                         FD_ZERO( &Set );
00221                         FD_SET( fd, &Set );
00222 
00223                         struct timeval  Current;
00224                         gettimeofday( &Current, NULL );
00225 
00226                         if ( LessTimeval( AbsoluteEnd, Current ) )    return false;
00227 
00228                         Timeout = SubTimeval( AbsoluteEnd, Current );
00229                         continue;
00230                     }
00231 
00232                     throw std::runtime_error( "Select call error." );
00233                 }
00234             }

template<typename Type >
struct timeval mumu::Entry< Type >::SubTimeval ( const struct timeval &  Left,
const struct timeval &  Right 
) const [inline, read, protected]

Utility function to subtract two timeval values.

Parameters:
Left First timeval
Right Second timeval
Returns:
Subtraction result

Definition at line 130 of file mumulib.h.

Referenced by mumu::Entry< Type >::Select().

00132             {
00133                 struct timeval      Result;
00134 
00135                 Result.tv_sec = Left.tv_sec - Right.tv_sec;
00136                 Result.tv_usec = Left.tv_usec - Right.tv_usec;
00137                 while ( Result.tv_usec < 0 )
00138                 {
00139                     --Result.tv_sec;
00140                     Result.tv_usec += 1000000;
00141                 }
00142                 return Result;
00143             }

template<>
void mumu::Entry< std::string >::Write ( const std::string &  T  )  [inline]
Parameters:
T Object to be written
Exceptions:
std::runtime_error in case of problems

Definition at line 336 of file mumulib.h.

00337     {
00338         if ( mode == ReadOnly ) throw std::runtime_error( "Cannot write into ReadOnly entry" );
00339 
00340         size_t      size( T.size() + 1 );   // +1 is for trailing \0
00341         for ( ; ; )
00342         {
00343             int  Bytes( write( fd, T.c_str(), size ) );
00344             if ( Bytes == static_cast< int >( size ) ) return;
00345 
00346             if ( Bytes == -1 && errno == EINTR ) continue;
00347 
00348             throw std::runtime_error( "Error writing to file descriptor" );
00349         }
00350     }

template<typename Type >
void mumu::Entry< Type >::Write ( const Type &  T  )  [inline]

Write the given object to the entry. Be carefull, this is the generic implementation which supposes that the data occupies a consequent memory address space.

Parameters:
T Object to be written
Exceptions:
std::runtime_error in case of problems

Definition at line 265 of file mumulib.h.

References mumu::Entry< Type >::Write().

00266             {
00267                 this->Write( reinterpret_cast<const unsigned char *>(&T), sizeof( T ) );
00268             }

template<typename Type >
void mumu::Entry< Type >::Write ( const unsigned char *  Buffer,
size_t  BufferSize 
) [inline]

Write a raw buffer to the entry.

Parameters:
Buffer Pointer to the buffer
BufferSize Size of the buffer
Exceptions:
std::runtime_error in case of problems

Definition at line 242 of file mumulib.h.

Referenced by mumu::Entry< Type >::Write().

00243             {
00244                 if ( mode == ReadOnly ) throw std::runtime_error( "Cannot write into ReadOnly entry" );
00245 
00246                 for ( ; ; )
00247                 {
00248                     int     Bytes( write( fd, Buffer, BufferSize ) );
00249                     if ( Bytes == static_cast<int>(BufferSize) ) return;
00250 
00251                     if ( Bytes == -1 && errno == EINTR ) continue;
00252 
00253                     throw std::runtime_error( "Error writing to file descriptor." );
00254                 }
00255             }


The documentation for this class was generated from the following file: