NetworkMessage_RW.cc

Go to the documentation of this file.
00001 // ----------------------------------------------------------------------------
00002 // CERTI - HLA RunTime Infrastructure
00003 // Copyright (C) 2002-2005  ONERA
00004 //
00005 // This program is free software ; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public License
00007 // as published by the Free Software Foundation ; either version 2 of
00008 // the License, or (at your option) any later version.
00009 //
00010 // This program is distributed in the hope that it will be useful, but
00011 // WITHOUT ANY WARRANTY ; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00013 // Lesser General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with this program ; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00018 //
00019 // $Id: NetworkMessage_RW.cc,v 3.53 2009/04/02 19:58:08 erk Exp $
00020 // ----------------------------------------------------------------------------
00021 
00022 #include "NetworkMessage.hh"
00023 #include "PrettyDebug.hh"
00024 
00025 using std::vector;
00026 using std::endl;
00027 using std::cout;
00028 
00029 namespace certi {
00030 
00031 static PrettyDebug D("RTIG_MSG", "NetworkMessage:");
00032 static PrettyDebug G("GENDOC",__FILE__ );
00033 
00034 // ----------------------------------------------------------------------------
00035 void NetworkMessage::serialize(MessageBuffer& msgBuffer) {
00036     G.Out(pdGendoc,"enter NetworkMessage::serialize");
00037     /* We serialize the common Network messages part
00038      * ALL Network Message will contain the following
00039      */
00040     if ((type==NOT_USED) || (type==LAST)) {
00041         throw RTIinternalError("Invalid network type (not a valid type);");
00042     }
00043     D.Out(pdDebug, "Serialize <%s>", getName().c_str());
00044     /* type of message */
00045     msgBuffer.write_int32(type);
00046     msgBuffer.write_int32(exception);
00047     msgBuffer.write_int32(federate);
00048     msgBuffer.write_int32(federation);
00049     /*
00050      * "builtin" Optional part
00051      * The subclass may chose in the constructor the variable part.
00052      * isDated may be chosen on Message instance basis
00053      * (same message may Dated or Not Dated)
00054      */
00055     msgBuffer.write_bool(_isDated);
00056     if (_isDated) {
00057         msgBuffer.write_double(date.getTime());
00058         D.Out(pdDebug, "Sent Message date is  <%f>", date.getTime());
00059     }
00060     msgBuffer.write_bool(_isLabelled);
00061     if (_isLabelled) {
00062         msgBuffer.write_string(label);
00063     }
00064     msgBuffer.write_bool(_isTagged);
00065     if (_isTagged) {
00066         msgBuffer.write_string(tag);
00067     }
00068     G.Out(pdGendoc,"exit NetworkMessage::serialize");
00069 } /* end of serialize */
00070 
00071 void NetworkMessage::deserialize(MessageBuffer& msgBuffer) {
00072     G.Out(pdGendoc,"enter NetworkMessage::deserialize");
00073     /* We serialize the common Network message part
00074      * ALL Network Messages will contain the following
00075      */
00076     D[pdDebug] << "Deserialize <" << getName().c_str()<<">"<<endl;
00077     /* deserialize common part */
00078     type        = static_cast<NetworkMessage::Type>(msgBuffer.read_int32());
00079     exception   = static_cast<TypeException>(msgBuffer.read_int32());
00080     federate    = msgBuffer.read_int32();
00081     federation  = msgBuffer.read_int32();
00082     /*
00083      * "builtin" Optional part
00084      * The subclass may chose in the constructor the variable part.
00085      * isDated may be chosen on Message instance basis
00086      * (same message may Dated or Not Dated)
00087      */
00088     _isDated = msgBuffer.read_bool();
00089     if (_isDated) {
00090         date = msgBuffer.read_double();
00091         D.Out(pdDebug, "Received Message date is  <%f>", date.getTime());
00092     }
00093     _isLabelled = msgBuffer.read_bool();
00094     if (_isLabelled) {
00095         label = msgBuffer.read_string();
00096     }
00097     _isTagged = msgBuffer.read_bool();
00098     if (_isTagged) {
00099         tag = msgBuffer.read_string();
00100     }
00101     G.Out(pdGendoc,"exit NetworkMessage::deserialize");
00102 } /* end of deserialize */
00103 
00104 void
00105 NetworkMessage::send(Socket *socket, MessageBuffer& msgBuffer) throw (NetworkError, NetworkSignal){
00106     G.Out(pdGendoc,"enter NetworkMessage::send");
00107     /* 0- reset send buffer */
00108     msgBuffer.reset();
00109     /* 1- serialize the message
00110      * This is a polymorphic call
00111      * which may specialized in a daughter class
00112      */
00113     serialize(msgBuffer);
00114     /* 2- update message buffer 'reserved bytes' header */
00115     msgBuffer.updateReservedBytes();
00116     D.Out(pdDebug,"Sending <%s> whose buffer has <%u> bytes",getName().c_str(),msgBuffer.size());
00117     //msgBuffer.show(msgBuf(0),5);
00118     /* 3- effectively send the raw message to socket */
00119 
00120     if (NULL != socket) { // send only if socket is unequal to null
00121         socket->send(static_cast<unsigned char*>(msgBuffer(0)), msgBuffer.size());
00122     } else { // socket pointer was null - not sending
00123         D.Out( pdDebug, "Not sending -- socket is deleted." );
00124     }
00125     G.Out(pdGendoc,"exit  NetworkMessage::send");
00126 } /* end of send */
00127 
00128 void
00129 NetworkMessage::receive(Socket* socket, MessageBuffer& msgBuffer) throw (NetworkError, NetworkSignal) {
00130     G.Out(pdGendoc,"enter NetworkMessage::receive");
00131     /* 0- Reset receive buffer */
00132     /* FIXME this reset may not be necessary since we do
00133      * raw-receive + assume-size
00134      */
00135     msgBuffer.reset();
00136     /* 1- Read 'reserved bytes' header from socket */
00137     //D.Out(pdDebug,"Reading %d 'reserved' bytes",msgBuffer.reservedBytes);
00138     socket->receive(msgBuffer(0), msgBuffer.reservedBytes);
00139     //msgBuffer.show(msgBuf(0),5);fflush(stdout);
00140     /* 2- update (assume) complete message size from reserved bytes */
00141     msgBuffer.assumeSizeFromReservedBytes();
00142     D.Out(pdDebug,"Got a MsgBuf of size %d bytes (including %d reserved)",msgBuffer.size(),msgBuffer.reservedBytes);
00143     /* 3- receive the rest of the message */
00144     socket->receive(msgBuffer(msgBuffer.reservedBytes),msgBuffer.size()-msgBuffer.reservedBytes);
00145     /* 4- deserialize the message
00146      * This is a polymorphic call
00147      * which may specialized in a daughter class
00148      */
00149     deserialize(msgBuffer);
00150     G.Out(pdGendoc,"exit  NetworkMessage::receive");
00151 } /* end of receive */
00152 
00153 } // namespace certi
00154 
00155 // $Id: NetworkMessage_RW.cc,v 3.53 2009/04/02 19:58:08 erk Exp $

Generated on Thu Apr 30 15:53:49 2009 for CERTIDeveloperDocumentation by doxygen 1.5.5