| 40 | | { |
|---|
| 41 | | Header::Header( vpr::Uint16 RIM_code, vpr::Uint16 packet_type, |
|---|
| 42 | | vpr::Uint32 packet_length, vpr::Uint32 frame ) : mPacketReader(NULL), mPacketWriter(NULL) |
|---|
| 43 | | { |
|---|
| 44 | | mRIMCode = RIM_code; |
|---|
| 45 | | mPacketType = packet_type; |
|---|
| 46 | | mPacketLength = packet_length; |
|---|
| 47 | | mFrame = frame; |
|---|
| 48 | | } |
|---|
| 49 | | |
|---|
| 50 | | void Header::readData( vpr::SocketStream* stream ) throw ( cluster::ClusterException ) |
|---|
| 51 | | { |
|---|
| 52 | | vprASSERT( NULL != stream && "Can not create a Header using a NULL SocketStream" ); |
|---|
| 53 | | |
|---|
| 54 | | // - Is stream is a valid SocketStream? |
|---|
| 55 | | // - Read in the packet from the socket |
|---|
| 56 | | // - Set the BufferObjectReader and BufferObjectWriter to use mData <====We only need BufferObjectReader |
|---|
| 57 | | if (NULL == stream) |
|---|
| | 42 | { |
|---|
| | 43 | |
|---|
| | 44 | Header::Header() |
|---|
| | 45 | {;} |
|---|
| | 46 | |
|---|
| | 47 | Header::Header( const vpr::Uint16 code, const vpr::Uint16 type, |
|---|
| | 48 | const vpr::Uint32 length, const vpr::Uint32 frame ) |
|---|
| | 49 | : mRIMCode(code) |
|---|
| | 50 | , mPacketType(type) |
|---|
| | 51 | , mPacketLength(length) |
|---|
| | 52 | , mFrame(frame) |
|---|
| | 53 | {;} |
|---|
| | 54 | |
|---|
| | 55 | void Header::readData( vpr::SocketStream* stream ) throw ( cluster::ClusterException ) |
|---|
| | 56 | { |
|---|
| | 57 | vprASSERT( NULL != stream && "Can not create a Header using a NULL SocketStream" ); |
|---|
| | 58 | |
|---|
| | 59 | // - Is stream is a valid SocketStream? |
|---|
| | 60 | // - Read in the packet from the socket |
|---|
| | 61 | // - Set the BufferObjectReader and BufferObjectWriter to use mData <====We only need BufferObjectReader |
|---|
| | 62 | if (NULL == stream) |
|---|
| | 63 | { |
|---|
| | 64 | vprDEBUG( gadgetDBG_RIM, vprDBG_CONFIG_LVL ) |
|---|
| | 65 | << clrOutBOLD( clrRED, "ERROR:" ) |
|---|
| | 66 | << " SocketSteam is NULL" |
|---|
| | 67 | << std::endl << vprDEBUG_FLUSH; |
|---|
| | 68 | |
|---|
| | 69 | throw cluster::ClusterException("Header::Header() - SocketStream is NULL"); |
|---|
| | 70 | } |
|---|
| | 71 | else |
|---|
| | 72 | { |
|---|
| | 73 | vpr::Uint32 bytes_read; |
|---|
| | 74 | |
|---|
| | 75 | vpr::ReturnStatus status = |
|---|
| | 76 | stream->readn( mData, Header::RIM_PACKET_HEAD_SIZE, bytes_read ); |
|---|
| | 77 | |
|---|
| | 78 | if ( status != vpr::ReturnStatus::Succeed || |
|---|
| | 79 | bytes_read != RIM_PACKET_HEAD_SIZE ) |
|---|
| 66 | | else |
|---|
| 67 | | { |
|---|
| 68 | | vpr::Uint32 bytes_read; |
|---|
| 69 | | |
|---|
| 70 | | vpr::ReturnStatus status = |
|---|
| 71 | | stream->readn( mData, Header::RIM_PACKET_HEAD_SIZE, bytes_read ); |
|---|
| 72 | | |
|---|
| 73 | | if ( status != vpr::ReturnStatus::Succeed || |
|---|
| 74 | | bytes_read != RIM_PACKET_HEAD_SIZE ) |
|---|
| 75 | | { |
|---|
| 76 | | vprDEBUG( gadgetDBG_RIM, vprDBG_CONFIG_LVL ) |
|---|
| 77 | | << clrOutBOLD( clrRED, "ERROR:" ) |
|---|
| 78 | | << "Header::readData() - Could not read the header!" |
|---|
| 79 | | << std::endl << vprDEBUG_FLUSH; |
|---|
| 80 | | |
|---|
| 81 | | stream->close(); |
|---|
| 82 | | delete stream; |
|---|
| 83 | | stream = NULL; |
|---|
| 84 | | |
|---|
| 85 | | if ( status != vpr::ReturnStatus::Succeed ) |
|---|
| 86 | | { |
|---|
| 87 | | throw cluster::ClusterException( "Header::Header() - Could not read the header!" ); |
|---|
| 88 | | } |
|---|
| 89 | | else |
|---|
| 90 | | { |
|---|
| 91 | | throw cluster::ClusterException( "Header::Header() - Bytes Read was lower than RIM_PACKET_HEAD_SIZE" ); |
|---|
| 92 | | } |
|---|
| 93 | | } |
|---|
| 94 | | |
|---|
| 95 | | if ( bytes_read != RIM_PACKET_HEAD_SIZE ) |
|---|
| 96 | | { |
|---|
| 97 | | vprDEBUG( gadgetDBG_RIM, vprDBG_CONFIG_LVL ) |
|---|
| 98 | | << clrOutBOLD( clrRED, "ERROR:" ) |
|---|
| 99 | | << "Header::Header Something is seriously wrong here!\n" |
|---|
| 100 | | << "We only read: " << bytes_read << " bytes for the header\n" |
|---|
| 101 | | << "When we need " << RIM_PACKET_HEAD_SIZE << " bytes!" << std::endl |
|---|
| 102 | | << vprDEBUG_FLUSH; |
|---|
| 103 | | exit(0); |
|---|
| 104 | | } |
|---|
| 105 | | |
|---|
| 106 | | mPacketReader = new vpr::BufferObjectReader( &mData ); |
|---|
| 107 | | parseHeader(); |
|---|
| 108 | | } |
|---|
| 109 | | |
|---|
| 110 | | } |
|---|
| 111 | | |
|---|
| 112 | | void Header::serializeHeader() |
|---|
| 113 | | { |
|---|
| 114 | | mPacketWriter = new vpr::BufferObjectWriter(&mData); |
|---|
| 115 | | mPacketWriter->getData()->clear(); |
|---|
| 116 | | mPacketWriter->setCurPos( 0 ); |
|---|
| 117 | | |
|---|
| 118 | | // -Write all packet header information to the base Header class |
|---|
| 119 | | |
|---|
| 120 | | |
|---|
| 121 | | // std::cout << "Head Starting at Position: " << mPacketWriter->getCurPos() << std::endl; |
|---|
| 122 | | mPacketWriter->writeUint16( mRIMCode ); |
|---|
| 123 | | // std::cout << "Write RIMCode: " << mRIMCode << std::endl; |
|---|
| 124 | | // std::cout << "Current Position: " << mPacketWriter->getCurPos() << std::endl; |
|---|
| 125 | | |
|---|
| 126 | | mPacketWriter->writeUint16( mPacketType ); |
|---|
| 127 | | // std::cout << "Write PacketType: " << mPacketType << std::endl; |
|---|
| 128 | | // std::cout << "Current Position: " << mPacketWriter->getCurPos() << std::endl; |
|---|
| 129 | | |
|---|
| 130 | | mPacketWriter->writeUint32( mFrame ); |
|---|
| 131 | | // std::cout << "Write Frame: " << mFrame << std::endl; |
|---|
| 132 | | // std::cout << "Current Position: " << mPacketWriter->getCurPos() << std::endl; |
|---|
| 133 | | |
|---|
| 134 | | mPacketWriter->writeUint32( mPacketLength ); |
|---|
| 135 | | // std::cout << "Write Packet Length: " << mPacketLength << std::endl; |
|---|
| 136 | | // std::cout << "Current Position: " << mPacketWriter->getCurPos() << std::endl; |
|---|
| 137 | | } |
|---|
| 138 | | |
|---|
| 139 | | void Header::parseHeader() |
|---|
| 140 | | { |
|---|
| 141 | | // *Now that we have recieved the correct number |
|---|
| 142 | | // of bytes from the socket(RIM_PACKET_HEAD_SIZE) |
|---|
| 143 | | |
|---|
| 144 | | // -Parse the new data using a BufferObjectReader |
|---|
| 145 | | // -Is this a valid RIM packet? |
|---|
| 146 | | // -If not exit immediately |
|---|
| 147 | | |
|---|
| 148 | | //std::cout << "Head Starting at Position: " << mPacketReader->getCurPos() << std::endl; |
|---|
| 149 | | |
|---|
| 150 | | mRIMCode = mPacketReader->readUint16(); |
|---|
| 151 | | // std::cout << "Read RIMCode: " << mRIMCode << std::endl; |
|---|
| 152 | | // std::cout << "Current Position: " << mPacketReader->getCurPos() << std::endl; |
|---|
| 153 | | mPacketType = mPacketReader->readUint16(); |
|---|
| 154 | | // std::cout << "Read PacketType: " << mPacketType << std::endl; |
|---|
| 155 | | // std::cout << "Current Position: " << mPacketReader->getCurPos() << std::endl; |
|---|
| 156 | | mFrame = mPacketReader->readUint32(); |
|---|
| 157 | | // std::cout << "Read Frame#: " << mFrame << std::endl; |
|---|
| 158 | | // std::cout << "Current Position: " << mPacketReader->getCurPos() << std::endl; |
|---|
| 159 | | mPacketLength = mPacketReader->readUint32(); |
|---|
| 160 | | // std::cout << "Read Packet Length: " << mPacketLength << std::endl; |
|---|
| 161 | | // std::cout << "Current Position: " << mPacketReader->getCurPos() << std::endl; |
|---|
| 162 | | |
|---|
| 163 | | if ( RIM_PACKET != mRIMCode ) |
|---|
| | 99 | |
|---|
| | 100 | if ( bytes_read != RIM_PACKET_HEAD_SIZE ) |
|---|
| 173 | | } |
|---|
| 174 | | |
|---|
| 175 | | vpr::ReturnStatus Header::send( vpr::SocketStream* socket ) |
|---|
| 176 | | { |
|---|
| 177 | | vprASSERT( NULL != socket && "Socket is NULL" ); |
|---|
| 178 | | |
|---|
| 179 | | // -Send the data in this packet |
|---|
| 180 | | vpr::Uint32 bytes_written; |
|---|
| 181 | | |
|---|
| 182 | | vpr::ReturnStatus status = socket->send( mData, RIM_PACKET_HEAD_SIZE, bytes_written ); |
|---|
| 183 | | return status; |
|---|
| 184 | | |
|---|
| 185 | | } |
|---|
| 186 | | |
|---|
| 187 | | void Header::dump() |
|---|
| 188 | | { |
|---|
| 189 | | std::cout << "Dumping Header(" << mData.size() << " bytes): "; |
|---|
| 190 | | for ( std::vector<vpr::Uint8>::iterator i = mData.begin(); |
|---|
| 191 | | i!= mData.end(); i++ ) |
|---|
| 192 | | { |
|---|
| 193 | | std::cout << (int)*i << " "; |
|---|
| 194 | | } |
|---|
| 195 | | std::cout << std::endl; |
|---|
| 196 | | } |
|---|
| 197 | | |
|---|
| 198 | | void Header::printData( const int debug_level ) |
|---|
| 199 | | { |
|---|
| 200 | | vprDEBUG_BEGIN( gadgetDBG_RIM, debug_level ) |
|---|
| 201 | | << clrOutBOLD( clrYELLOW, "====== Packet Header ======" ) |
|---|
| | 110 | |
|---|
| | 111 | parseHeader(); |
|---|
| | 112 | } |
|---|
| | 113 | |
|---|
| | 114 | } |
|---|
| | 115 | |
|---|
| | 116 | void Header::serializeHeader() |
|---|
| | 117 | { |
|---|
| | 118 | vpr::BufferObjectWriter writer(&mData); |
|---|
| | 119 | writer.getData()->clear(); |
|---|
| | 120 | writer.setCurPos( 0 ); |
|---|
| | 121 | |
|---|
| | 122 | // -Write all packet header information to the base Header class |
|---|
| | 123 | |
|---|
| | 124 | |
|---|
| | 125 | // std::cout << "Head Starting at Position: " << writer.getCurPos() << std::endl; |
|---|
| | 126 | writer.writeUint16( mRIMCode ); |
|---|
| | 127 | // std::cout << "Write RIMCode: " << mRIMCode << std::endl; |
|---|
| | 128 | // std::cout << "Current Position: " << writer.getCurPos() << std::endl; |
|---|
| | 129 | |
|---|
| | 130 | writer.writeUint16( mPacketType ); |
|---|
| | 131 | // std::cout << "Write PacketType: " << mPacketType << std::endl; |
|---|
| | 132 | // std::cout << "Current Position: " << writer.getCurPos() << std::endl; |
|---|
| | 133 | |
|---|
| | 134 | writer.writeUint32( mFrame ); |
|---|
| | 135 | // std::cout << "Write Frame: " << mFrame << std::endl; |
|---|
| | 136 | // std::cout << "Current Position: " << writer.getCurPos() << std::endl; |
|---|
| | 137 | |
|---|
| | 138 | writer.writeUint32( mPacketLength ); |
|---|
| | 139 | // std::cout << "Write Packet Length: " << mPacketLength << std::endl; |
|---|
| | 140 | // std::cout << "Current Position: " << writer.getCurPos() << std::endl; |
|---|
| | 141 | } |
|---|
| | 142 | |
|---|
| | 143 | void Header::parseHeader() |
|---|
| | 144 | { |
|---|
| | 145 | // *Now that we have recieved the correct number |
|---|
| | 146 | // of bytes from the socket(RIM_PACKET_HEAD_SIZE) |
|---|
| | 147 | |
|---|
| | 148 | // -Parse the new data using a BufferObjectReader |
|---|
| | 149 | // -Is this a valid RIM packet? |
|---|
| | 150 | // -If not exit immediately |
|---|
| | 151 | vpr::BufferObjectReader reader( &mData ); |
|---|
| | 152 | |
|---|
| | 153 | //std::cout << "Head Starting at Position: " << reader.getCurPos() << std::endl; |
|---|
| | 154 | |
|---|
| | 155 | mRIMCode = reader.readUint16(); |
|---|
| | 156 | // std::cout << "Read RIMCode: " << mRIMCode << std::endl; |
|---|
| | 157 | // std::cout << "Current Position: " << reader.getCurPos() << std::endl; |
|---|
| | 158 | mPacketType = reader.readUint16(); |
|---|
| | 159 | // std::cout << "Read PacketType: " << mPacketType << std::endl; |
|---|
| | 160 | // std::cout << "Current Position: " << reader.getCurPos() << std::endl; |
|---|
| | 161 | mFrame = reader.readUint32(); |
|---|
| | 162 | // std::cout << "Read Frame#: " << mFrame << std::endl; |
|---|
| | 163 | // std::cout << "Current Position: " << reader.getCurPos() << std::endl; |
|---|
| | 164 | mPacketLength = reader.readUint32(); |
|---|
| | 165 | // std::cout << "Read Packet Length: " << mPacketLength << std::endl; |
|---|
| | 166 | // std::cout << "Current Position: " << reader.getCurPos() << std::endl; |
|---|
| | 167 | |
|---|
| | 168 | if ( RIM_PACKET != mRIMCode ) |
|---|
| | 169 | { |
|---|
| | 170 | vprDEBUG( gadgetDBG_RIM, vprDBG_CONFIG_LVL ) |
|---|
| | 171 | << clrOutBOLD( clrRED, "ERROR:" ) |
|---|
| | 172 | << " This Packet is not a valid RIM Packet!!" |
|---|
| | 173 | << " RIMCode: " << mRIMCode << " is not valid!" |
|---|
| 203 | | |
|---|
| 204 | | vprDEBUG( gadgetDBG_RIM, debug_level ) |
|---|
| 205 | | << clrOutBOLD( clrYELLOW, "RIMCode: " ) << mRIMCode |
|---|
| 206 | | << std::endl << vprDEBUG_FLUSH; |
|---|
| 207 | | vprDEBUG( gadgetDBG_RIM, debug_level ) |
|---|
| 208 | | << clrOutBOLD( clrYELLOW, "PacketType: " ) << mPacketType |
|---|
| 209 | | << std::endl << vprDEBUG_FLUSH; |
|---|
| 210 | | vprDEBUG( gadgetDBG_RIM, debug_level ) |
|---|
| 211 | | << clrOutBOLD( clrYELLOW, "Frame #: " ) << mFrame |
|---|
| 212 | | << std::endl << vprDEBUG_FLUSH; |
|---|
| 213 | | vprDEBUG( gadgetDBG_RIM, debug_level ) |
|---|
| 214 | | << clrOutBOLD( clrYELLOW, "Length: " ) << mPacketLength |
|---|
| 215 | | << std::endl << vprDEBUG_FLUSH; |
|---|
| 216 | | |
|---|
| 217 | | vprDEBUG_END( gadgetDBG_RIM, debug_level ) |
|---|
| 218 | | << clrOutBOLD( clrYELLOW, "===========================" ) |
|---|
| 219 | | << std::endl << vprDEBUG_FLUSH; |
|---|
| 220 | | } |
|---|
| 221 | | |
|---|
| 222 | | }// end namespace cluster |
|---|
| | 175 | |
|---|
| | 176 | throw cluster::ClusterException( "Header::parseHeader() - Invalid packet header!" ); |
|---|
| | 177 | } |
|---|
| | 178 | } |
|---|
| | 179 | |
|---|
| | 180 | vpr::ReturnStatus Header::send( vpr::SocketStream* socket ) |
|---|
| | 181 | { |
|---|
| | 182 | vprASSERT( NULL != socket && "Socket is NULL" ); |
|---|
| | 183 | |
|---|
| | 184 | // -Send the data in this packet |
|---|
| | 185 | vpr::Uint32 bytes_written; |
|---|
| | 186 | |
|---|
| | 187 | vpr::ReturnStatus status = socket->send( mData, RIM_PACKET_HEAD_SIZE, bytes_written ); |
|---|
| | 188 | return status; |
|---|
| | 189 | |
|---|
| | 190 | } |
|---|
| | 191 | |
|---|
| | 192 | void Header::dump() |
|---|
| | 193 | { |
|---|
| | 194 | std::cout << "Dumping Header(" << mData.size() << " bytes): "; |
|---|
| | 195 | for ( std::vector<vpr::Uint8>::iterator i = mData.begin(); |
|---|
| | 196 | i!= mData.end(); i++ ) |
|---|
| | 197 | { |
|---|
| | 198 | std::cout << (int)*i << " "; |
|---|
| | 199 | } |
|---|
| | 200 | std::cout << std::endl; |
|---|
| | 201 | } |
|---|
| | 202 | |
|---|
| | 203 | void Header::printData( const int debug_level ) |
|---|
| | 204 | { |
|---|
| | 205 | vprDEBUG_BEGIN( gadgetDBG_RIM, debug_level ) |
|---|
| | 206 | << clrOutBOLD( clrYELLOW, "====== Packet Header ======" ) |
|---|
| | 207 | << std::endl << vprDEBUG_FLUSH; |
|---|
| | 208 | |
|---|
| | 209 | vprDEBUG( gadgetDBG_RIM, debug_level ) |
|---|
| | 210 | << clrOutBOLD( clrYELLOW, "RIMCode: " ) << mRIMCode |
|---|
| | 211 | << std::endl << vprDEBUG_FLUSH; |
|---|
| | 212 | vprDEBUG( gadgetDBG_RIM, debug_level ) |
|---|
| | 213 | << clrOutBOLD( clrYELLOW, "PacketType: " ) << mPacketType |
|---|
| | 214 | << std::endl << vprDEBUG_FLUSH; |
|---|
| | 215 | vprDEBUG( gadgetDBG_RIM, debug_level ) |
|---|
| | 216 | << clrOutBOLD( clrYELLOW, "Frame #: " ) << mFrame |
|---|
| | 217 | << std::endl << vprDEBUG_FLUSH; |
|---|
| | 218 | vprDEBUG( gadgetDBG_RIM, debug_level ) |
|---|
| | 219 | << clrOutBOLD( clrYELLOW, "Length: " ) << mPacketLength |
|---|
| | 220 | << std::endl << vprDEBUG_FLUSH; |
|---|
| | 221 | |
|---|
| | 222 | vprDEBUG_END( gadgetDBG_RIM, debug_level ) |
|---|
| | 223 | << clrOutBOLD( clrYELLOW, "===========================" ) |
|---|
| | 224 | << std::endl << vprDEBUG_FLUSH; |
|---|
| | 225 | } |
|---|
| | 226 | |
|---|
| | 227 | }// end cluster namespace |
|---|