tcpip::Storage Class Reference

#include <storage.h>


Detailed Description

Definition at line 35 of file storage.h.


Public Types

typedef std::vector< unsigned
char > 
StorageType

Public Member Functions

StorageType::const_iterator begin () const
StorageType::const_iterator end () const
virtual unsigned int position () const
virtual int readByte () throw (std::invalid_argument)
virtual unsigned char readChar () throw (std::invalid_argument)
virtual double readDouble () throw (std::invalid_argument)
virtual float readFloat () throw (std::invalid_argument)
virtual int readInt () throw (std::invalid_argument)
virtual int readShort () throw (std::invalid_argument)
virtual std::string readString () throw (std::invalid_argument)
virtual std::vector< std::string > readStringList () throw (std::invalid_argument)
virtual int readUnsignedByte () throw (std::invalid_argument)
void reset ()
size_t size () const
 Storage (unsigned char[], int length=-1)
 Constructor, that fills the storage with an char array. If length is -1, the whole array is handed over.
 Storage ()
 Standard Constructor.
virtual bool valid_pos ()
virtual void writeByte (int) throw (std::invalid_argument)
virtual void writeChar (unsigned char) throw ()
virtual void writeDouble (double) throw ()
virtual void writeFloat (float) throw ()
virtual void writeInt (int) throw ()
virtual void writePacket (unsigned char *packet, int length)
virtual void writeShort (int) throw (std::invalid_argument)
virtual void writeStorage (tcpip::Storage &store)
virtual void writeString (std::string s) throw ()
virtual void writeStringList (const std::vector< std::string > &s) throw ()
virtual void writeUnsignedByte (int) throw (std::invalid_argument)
virtual ~Storage ()

Private Member Functions

void init ()
 Used in constructors to initialize local variables.

Private Attributes

bool bigEndian_
StorageType::const_iterator iter_
StorageType::const_iterator iterEnd_
bool iterEndValid_
bool iterValid_
unsigned int pos_
StorageType store

Member Typedef Documentation

typedef std::vector<unsigned char> tcpip::Storage::StorageType

Definition at line 39 of file storage.h.


Constructor & Destructor Documentation

tcpip::Storage::Storage (  ) 

Standard Constructor.

Definition at line 28 of file storage.cpp.

References init().

00029     {
00030         init();
00031     }

tcpip::Storage::Storage ( unsigned char  packet[],
int  length = -1 
)

Constructor, that fills the storage with an char array. If length is -1, the whole array is handed over.

Definition at line 34 of file storage.cpp.

References init(), and store.

00035     {
00036         // Length is calculated, if -1, or given
00037         if (length == -1) length = sizeof(packet) / sizeof(unsigned char);
00038         
00039         store.reserve(length);
00040         // Get the content
00041         for(int i = 0; i < length; ++i) store.push_back(packet[i]);
00042 
00043         init();
00044     }

tcpip::Storage::~Storage (  )  [virtual]

Definition at line 61 of file storage.cpp.

00062     {}


Member Function Documentation

StorageType::const_iterator tcpip::Storage::begin (  )  const [inline]

Definition at line 110 of file storage.h.

References store.

Referenced by tcpip::Socket::sendExact().

00110 { return store.begin(); }

StorageType::const_iterator tcpip::Storage::end (  )  const [inline]

Definition at line 111 of file storage.h.

References store.

Referenced by tcpip::Socket::sendExact().

00111 { return store.end(); }

void tcpip::Storage::init (  )  [private]

Used in constructors to initialize local variables.

Definition at line 48 of file storage.cpp.

References bigEndian_, iterEndValid_, iterValid_, pos_, and valid_pos().

Referenced by Storage().

00049     {
00050         // Initialize local variables
00051         pos_=0;
00052         iterValid_ = iterEndValid_ = false;
00053         valid_pos();
00054 
00055         short a = 0x0102;
00056         unsigned char *p_a = reinterpret_cast<unsigned char*>(&a);
00057         bigEndian_ = (p_a[0] == 0x01); // big endian?
00058     }

unsigned int tcpip::Storage::position (  )  const [virtual]

int tcpip::Storage::readByte (  )  throw (std::invalid_argument) [virtual]

Reads a byte form the array

Returns:
The read byte (between -128 and 127)

Definition at line 141 of file storage.cpp.

References readChar().

Referenced by testclient::TraCITestClient::readAndReportTypeDependent().

00142     {
00143         int i = static_cast<int>(readChar());
00144         if (i < 128) return i; 
00145         else return (i - 256);
00146     }

unsigned char tcpip::Storage::readChar (  )  throw (std::invalid_argument) [virtual]

Reads a char form the array

Returns:
The read char (between 0 and 255)

Definition at line 115 of file storage.cpp.

References iter_, pos_, and valid_pos().

Referenced by readByte(), readDouble(), readFloat(), readInt(), readShort(), readString(), readUnsignedByte(), and writeStorage().

00116     {
00117         if ( !valid_pos() )
00118         {
00119             throw std::invalid_argument("Storage::readChar(): invalid position");
00120         }
00121         char hb = *iter_;
00122         ++iter_;
00123         ++pos_;
00124         return hb;
00125     }

double tcpip::Storage::readDouble (  )  throw (std::invalid_argument) [virtual]

Definition at line 411 of file storage.cpp.

References bigEndian_, and readChar().

Referenced by testclient::TraCITestClient::readAndReportTypeDependent().

00412     {
00413                 double value = 0;
00414                 unsigned char *p_value = reinterpret_cast<unsigned char*>(&value);
00415                 if (bigEndian_)
00416                 {
00417                         // network is big endian
00418                         for (int i=0; i<8; ++i)
00419                 {
00420                                 p_value[i] = readChar();
00421                         }
00422                 } else {
00423                         // network is big endian
00424                         for (int i=7; i>=0; --i) {
00425                                 p_value[i] = readChar();
00426                         }
00427                 }
00428                 return value;
00429         }

float tcpip::Storage::readFloat (  )  throw (std::invalid_argument) [virtual]

Definition at line 348 of file storage.cpp.

References bigEndian_, and readChar().

Referenced by testclient::TraCITestClient::readAndReportTypeDependent(), testclient::TraCITestClient::validateDistanceRequest(), testclient::TraCITestClient::validateGetTLStatus(), testclient::TraCITestClient::validatePositionConversion(), testclient::TraCITestClient::validateSimulationStep(), and testclient::TraCITestClient::validateStopNode().

00349     {
00350                 float value = 0;
00351                 unsigned char *p_value = reinterpret_cast<unsigned char*>(&value);
00352                 if (bigEndian_)
00353                 {
00354                         // network is big endian
00355                         p_value[0] = readChar();
00356                         p_value[1] = readChar();
00357                         p_value[2] = readChar();
00358                         p_value[3] = readChar();
00359                 } else {
00360                         // network is big endian
00361                         p_value[3] = readChar();
00362                         p_value[2] = readChar();
00363                         p_value[1] = readChar();
00364                         p_value[0] = readChar();
00365                 }
00366 
00367         return value;
00368 
00369     }

int tcpip::Storage::readInt (  )  throw (std::invalid_argument) [virtual]

Definition at line 298 of file storage.cpp.

References bigEndian_, and readChar().

Referenced by testclient::TraCITestClient::commandGetVariable(), testclient::TraCITestClient::commandGetVariablePlus(), traci::TraCIServer::processSingleSubscription(), testclient::TraCITestClient::readAndReportTypeDependent(), readString(), readStringList(), tcpip::Socket::receiveExact(), testclient::TraCITestClient::validateGetTLStatus(), testclient::TraCITestClient::validateScenario(), testclient::TraCITestClient::validateSimulationStep(), testclient::TraCITestClient::validateSimulationStep2(), testclient::TraCITestClient::validateStopNode(), and testclient::TraCITestClient::validateSubscription().

00299     {
00300                 int value = 0;
00301                 unsigned char *p_value = reinterpret_cast<unsigned char*>(&value);
00302                 if (bigEndian_)
00303                 {
00304                         // network is big endian
00305                         p_value[0] = readChar();
00306                         p_value[1] = readChar();
00307                         p_value[2] = readChar();
00308                         p_value[3] = readChar();
00309                 } else {
00310                         // network is big endian
00311                         p_value[3] = readChar();
00312                         p_value[2] = readChar();
00313                         p_value[1] = readChar();
00314                         p_value[0] = readChar();
00315                 }
00316                 return value;
00317     }

int tcpip::Storage::readShort (  )  throw (std::invalid_argument) [virtual]

Restores an integer, which was split up in two bytes according to the specification, it must have been split by its row byte representation with MSBF-order

Returns:
the unspoiled integer value (between -32768 and 32767)

Definition at line 248 of file storage.cpp.

References bigEndian_, and readChar().

00249     {
00250                 short value = 0;
00251                 unsigned char *p_value = reinterpret_cast<unsigned char*>(&value);
00252                 if (bigEndian_)
00253                 {
00254                         // network is big endian
00255                         p_value[0] = readChar();
00256                         p_value[1] = readChar();
00257                 } else {
00258                         // network is big endian
00259                         p_value[1] = readChar();
00260                         p_value[0] = readChar();
00261                 }
00262                 return value;
00263         }

std::string tcpip::Storage::readString (  )  throw (std::invalid_argument) [virtual]

std::vector< std::string > tcpip::Storage::readStringList (  )  throw (std::invalid_argument) [virtual]

Reads a string list form the array

Returns:
The read string

Definition at line 217 of file storage.cpp.

References readInt(), and readString().

Referenced by testclient::TraCITestClient::readAndReportTypeDependent().

00218     {
00219         std::vector<std::string> tmp;
00220         int len = readInt();
00221         for (int i = 0; i < len; i++) {
00222             tmp.push_back(readString());
00223         }
00224         return tmp;
00225     }

int tcpip::Storage::readUnsignedByte (  )  throw (std::invalid_argument) [virtual]

void tcpip::Storage::reset (  ) 

Definition at line 102 of file storage.cpp.

References iterEndValid_, iterValid_, pos_, and store.

Referenced by traci::TraCIServer::commandGetTLStatus().

00103     {
00104         store.clear();
00105         pos_=0;
00106         iterValid_ = false;
00107         iterEndValid_ = false;
00108     }

size_t tcpip::Storage::size (  )  const [inline]

bool tcpip::Storage::valid_pos (  )  [virtual]

Definition at line 65 of file storage.cpp.

References iter_, iterEnd_, iterEndValid_, iterValid_, pos_, size(), and store.

Referenced by init(), readChar(), testclient::TraCITestClient::validateGetTLStatus(), testclient::TraCITestClient::validateSimulationStep(), and writeStorage().

00066     {
00067         if (size() == 0) return false;
00068 
00069         // Check iterator iterEnd_ for validity
00070         if ( !iterEndValid_ )
00071         {
00072             iterEnd_ = store.end();
00073             iterEndValid_ = true;
00074         }
00075 
00076         // Check Iterator iter_ for validity
00077         if ( !iterValid_ ) 
00078         {
00079             iter_ = store.begin();
00080             unsigned int i = 0;
00081             while ( i < pos_ 
00082                 && iter_ != iterEnd_)
00083             {
00084                 ++i;
00085                 ++iter_;
00086             }
00087             iterValid_ = true;
00088         }
00089 
00090         return (iter_ != iterEnd_);
00091     }

void tcpip::Storage::writeByte ( int  value  )  throw (std::invalid_argument) [virtual]

Definition at line 152 of file storage.cpp.

References writeChar().

Referenced by testclient::TraCITestClient::commandScenario(), and testclient::TraCITestClient::setValueTypeDependant().

00153     {
00154         if (value < -128 || value > 127)
00155         {
00156             throw std::invalid_argument("Storage::writeByte(): Invalid value, not in [-128, 127]");
00157         }
00158         writeChar( static_cast<unsigned char>( (value+256) % 256 ) );
00159     }

void tcpip::Storage::writeChar ( unsigned char  value  )  throw () [virtual]

Definition at line 131 of file storage.cpp.

References store.

Referenced by writeByte(), writeDouble(), writeFloat(), writeInt(), writeShort(), writeStorage(), writeString(), and writeUnsignedByte().

00132     {
00133         store.push_back(value);
00134     }

void tcpip::Storage::writeDouble ( double  value  )  throw () [virtual]

Definition at line 391 of file storage.cpp.

References bigEndian_, and writeChar().

Referenced by testclient::TraCITestClient::commandChangeRoute(), and testclient::TraCITestClient::commandScenario().

00392     {
00393                 unsigned char *p_value = reinterpret_cast<unsigned char*>(&value);
00394                 if (bigEndian_)
00395                 {
00396                         // network is big endian
00397                         for (int i=0; i<8; ++i)
00398             {
00399                                 writeChar(p_value[i]);
00400                         }
00401                 } else {
00402                         // network is big endian
00403                         for (int i=7; i>=0; --i)
00404                     {
00405                                 writeChar(p_value[i]);
00406                         }
00407                 }
00408         }

void tcpip::Storage::writeFloat ( float  value  )  throw () [virtual]

void tcpip::Storage::writeInt ( int  value  )  throw () [virtual]

Definition at line 320 of file storage.cpp.

References bigEndian_, and writeChar().

Referenced by testclient::TraCITestClient::commandChangeLane(), testclient::TraCITestClient::commandChangeRoute(), testclient::TraCITestClient::commandChangeTarget(), testclient::TraCITestClient::commandGetTLStatus(), traci::TraCIServer::commandGetTLStatus(), testclient::TraCITestClient::commandScenario(), testclient::TraCITestClient::commandSetMaximumSpeed(), testclient::TraCITestClient::commandSimulationStep(), testclient::TraCITestClient::commandSimulationStep2(), testclient::TraCITestClient::commandSlowDown(), testclient::TraCITestClient::commandStopNode(), testclient::TraCITestClient::commandSubscribeVariable(), traci::TraCIServer::postProcessSimulationStep(), TraCIServerAPI_Vehicle::processGet(), TraCIServerAPI_TLS::processGet(), TraCIServerAPI_Simulation::processGet(), TraCIServerAPI_Route::processGet(), TraCIServerAPI_MeMeDetector::processGet(), TraCIServerAPI_Lane::processGet(), TraCIServerAPI_InductionLoop::processGet(), TraCIServerAPI_Edge::processGet(), tcpip::Socket::sendExact(), testclient::TraCITestClient::setValueTypeDependant(), writeString(), and writeStringList().

00321     {
00322                 unsigned char *p_value = reinterpret_cast<unsigned char*>(&value);
00323                 if (bigEndian_)
00324                 {
00325                         // network is big endian
00326                         writeChar(p_value[0]);
00327                         writeChar(p_value[1]);
00328                         writeChar(p_value[2]);
00329                         writeChar(p_value[3]);
00330                 } else {
00331                         // network is big endian
00332                         writeChar(p_value[3]);
00333                         writeChar(p_value[2]);
00334                         writeChar(p_value[1]);
00335                         writeChar(p_value[0]);
00336                 }
00337     }

void tcpip::Storage::writePacket ( unsigned char *  packet,
int  length 
) [virtual]

Definition at line 433 of file storage.cpp.

References store.

00434         {
00435             store.reserve(length);
00436             for(int i = 0; i < length; ++i) store.push_back(packet[i]);
00437             //init();
00438         }

void tcpip::Storage::writeShort ( int  value  )  throw (std::invalid_argument) [virtual]

Definition at line 266 of file storage.cpp.

References bigEndian_, and writeChar().

00267     {
00268         if (value < -32768 || value > 32767)
00269         {
00270             throw std::invalid_argument("Storage::writeShort(): Invalid value, not in [-32768, 32767]");
00271         }
00272 
00273         short svalue = static_cast<short>(value);
00274         //assert(svalue == value);
00275 
00276                 unsigned char *p_svalue = reinterpret_cast<unsigned char*>(&svalue);
00277                 if (bigEndian_)
00278                 {
00279                         // network is big endian
00280                         writeChar(p_svalue[0]);
00281                         writeChar(p_svalue[1]);
00282                 } else {
00283                        // network is big endian
00284                        writeChar(p_svalue[1]);
00285                        writeChar(p_svalue[0]);
00286                 }
00287     }

void tcpip::Storage::writeStorage ( tcpip::Storage store  )  [virtual]

void tcpip::Storage::writeString ( std::string  s  )  throw () [virtual]

Writes a string into the array;

Parameters:
s The string to be written

Definition at line 204 of file storage.cpp.

References writeChar(), and writeInt().

Referenced by testclient::TraCITestClient::commandChangeRoute(), testclient::TraCITestClient::commandChangeTarget(), testclient::TraCITestClient::commandDistanceRequest(), traci::TraCIServer::commandGetTLStatus(), testclient::TraCITestClient::commandGetVariable(), testclient::TraCITestClient::commandGetVariablePlus(), testclient::TraCITestClient::commandPositionConversion(), traci::TraCIServer::commandPositionConversion(), testclient::TraCITestClient::commandScenario(), testclient::TraCITestClient::commandSetValue(), testclient::TraCITestClient::commandStopNode(), testclient::TraCITestClient::commandSubscribeVariable(), traci::TraCIServer::handleTrafficLightDomain(), traci::TraCIServer::postProcessSimulationStep(), TraCIServerAPI_VehicleType::processGet(), TraCIServerAPI_Vehicle::processGet(), TraCIServerAPI_TLS::processGet(), TraCIServerAPI_Simulation::processGet(), TraCIServerAPI_Route::processGet(), TraCIServerAPI_Polygon::processGet(), TraCIServerAPI_POI::processGet(), TraCIServerAPI_MeMeDetector::processGet(), TraCIServerAPI_Lane::processGet(), TraCIServerAPI_Junction::processGet(), TraCIServerAPI_InductionLoop::processGet(), TraCIServerAPI_Edge::processGet(), traci::TraCIServer::processSingleSubscription(), testclient::TraCITestClient::setValueTypeDependant(), TraCIServerAPIHelper::writeStatusCmd(), and writeStringList().

00205     {
00206         writeInt(static_cast<int>(s.length()));
00207         for (string::iterator it = s.begin(); it!=s.end() ; it++) {
00208             writeChar(*it);
00209         }
00210     }

void tcpip::Storage::writeStringList ( const std::vector< std::string > &  s  )  throw () [virtual]

void tcpip::Storage::writeUnsignedByte ( int  value  )  throw (std::invalid_argument) [virtual]

Definition at line 175 of file storage.cpp.

References writeChar().

Referenced by testclient::TraCITestClient::commandChangeLane(), testclient::TraCITestClient::commandChangeRoute(), testclient::TraCITestClient::commandChangeTarget(), testclient::TraCITestClient::commandClose(), testclient::TraCITestClient::commandDistanceRequest(), testclient::TraCITestClient::commandGetTLStatus(), traci::TraCIServer::commandGetTLStatus(), testclient::TraCITestClient::commandGetVariable(), testclient::TraCITestClient::commandGetVariablePlus(), testclient::TraCITestClient::commandPositionConversion(), traci::TraCIServer::commandPositionConversion(), testclient::TraCITestClient::commandScenario(), testclient::TraCITestClient::commandSetMaximumSpeed(), testclient::TraCITestClient::commandSetValue(), testclient::TraCITestClient::commandSimulationStep(), testclient::TraCITestClient::commandSimulationStep2(), testclient::TraCITestClient::commandSlowDown(), testclient::TraCITestClient::commandStopNode(), testclient::TraCITestClient::commandSubscribeVariable(), traci::TraCIServer::handleTrafficLightDomain(), traci::TraCIServer::postProcessSimulationStep(), TraCIServerAPI_VehicleType::processGet(), TraCIServerAPI_Vehicle::processGet(), TraCIServerAPI_TLS::processGet(), TraCIServerAPI_Simulation::processGet(), TraCIServerAPI_Route::processGet(), TraCIServerAPI_Polygon::processGet(), TraCIServerAPI_POI::processGet(), TraCIServerAPI_MeMeDetector::processGet(), TraCIServerAPI_Lane::processGet(), TraCIServerAPI_Junction::processGet(), TraCIServerAPI_InductionLoop::processGet(), TraCIServerAPI_Edge::processGet(), traci::TraCIServer::processSingleSubscription(), testclient::TraCITestClient::setValueTypeDependant(), and TraCIServerAPIHelper::writeStatusCmd().

00176     {
00177         if (value < 0 || value > 255)
00178         {
00179             throw std::invalid_argument("Storage::writeUnsignedByte(): Invalid value, not in [0, 255]");
00180         }
00181         writeChar( static_cast<unsigned char>( value ));
00182     }


Field Documentation

StorageType::const_iterator tcpip::Storage::iter_ [private]

Definition at line 50 of file storage.h.

Referenced by readChar(), and valid_pos().

StorageType::const_iterator tcpip::Storage::iterEnd_ [private]

Definition at line 51 of file storage.h.

Referenced by valid_pos().

Definition at line 47 of file storage.h.

Referenced by init(), reset(), and valid_pos().

Definition at line 45 of file storage.h.

Referenced by init(), reset(), and valid_pos().

unsigned int tcpip::Storage::pos_ [private]

Definition at line 44 of file storage.h.

Referenced by init(), position(), readChar(), reset(), and valid_pos().

Definition at line 42 of file storage.h.

Referenced by begin(), end(), reset(), Storage(), valid_pos(), writeChar(), and writePacket().


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

Generated on Wed May 5 00:07:02 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6