root/juggler/branches/2.2/modules/gadgeteer/cluster/Packets/Header.cpp

Revision 20582, 7.3 kB (checked in by patrick, 1 year ago)

Added a missing newline.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /*************** <auto-copyright.pl BEGIN do not edit this line> **************
2  *
3  * VR Juggler is (C) Copyright 1998-2007 by Iowa State University
4  *
5  * Original Authors:
6  *   Allen Bierbaum, Christopher Just,
7  *   Patrick Hartling, Kevin Meinert,
8  *   Carolina Cruz-Neira, Albert Baker
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Library General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Library General Public License for more details.
19  *
20  * You should have received a copy of the GNU Library General Public
21  * License along with this library; if not, write to the
22  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23  * Boston, MA 02111-1307, USA.
24  *
25  *************** <auto-copyright.pl END do not edit this line> ***************/
26
27 //#include <gadget/gadgetConfig.h>
28 #include <cluster/Packets/Header.h>
29 #include <gadget/Util/Debug.h>
30
31 #include <vpr/IO/Socket/SocketStream.h>
32
33 namespace cluster
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!\n"
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 )
94       {
95          vprDEBUG( gadgetDBG_RIM, vprDBG_CONFIG_LVL )
96             << clrOutBOLD( clrRED, "ERROR:" )
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;
166          
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;
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 cluster namespace
223
Note: See TracBrowser for help on using the browser.