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