# InteractionBroadcastList.cc

Go to the documentation of this file.

    00001 // ----------------------------------------------------------------------------
    00002 // CERTI - HLA RunTime Infrastructure
    00003 // Copyright (C) 2002, 2003  ONERA
    00004 //
    00005 // This file is part of CERTI-libCERTI
    00006 //
    00007 // CERTI-libCERTI is free software ; you can redistribute it and/or
    00008 // modify it under the terms of the GNU Lesser General Public License
    00009 // as published by the Free Software Foundation ; either version 2 of
    00010 // the License, or (at your option) any later version.
    00011 //
    00012 // CERTI-libCERTI is distributed in the hope that it will be useful, but
    00013 // WITHOUT ANY WARRANTY ; without even the implied warranty of
    00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    00015 // Lesser General Public License for more details.
    00016 //
    00017 // You should have received a copy of the GNU Lesser General Public
    00018 // License along with this program ; if not, write to the Free Software
    00019 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
    00020 // USA
    00021 //
    00022 // $Id: InteractionBroadcastList.cc,v 3.13 2008/05/30 14:01:05 erk Exp $
    00023 // ----------------------------------------------------------------------------
    00024
    00025
    00026
    00027 #include "InteractionBroadcastList.hh"
    00028 #include "PrettyDebug.hh"
    00029
    00030 using std::list ;
    00031
    00032 namespace certi {
    00033
    00034 static pdCDebug D("INTBROADCASTLIST", "(broadcas) - ");
    00035 static PrettyDebug G("GENDOC",__FILE__) ;
    00036
    00037 // ----------------------------------------------------------------------------
    00043 void
    00044 InteractionBroadcastList::addFederate(FederateHandle federate)
    00045 {
    00046     // 1. Is there already a line in the list for this Federate?
    00047     InteractionBroadcastLine * line = getLineWithFederate(federate);
    00048
    00049     // If NO, add a new one, in the bsWaiting State.
    00050     if (line == 0) {
    00051         line = new InteractionBroadcastLine(federate,
    00052                                             InteractionBroadcastLine::waiting);
    00053         lines.push_front(line);
    00054         D.Out(pdRegister, "Adding new line in list for Federate %d.", federate);
    00055     }
    00056     else
    00057         D.Out(pdTrace, "Message already sent to federate %d.", federate);
    00058 }
    00059
    00060 // ----------------------------------------------------------------------------
    00064 InteractionBroadcastList::InteractionBroadcastList(NetworkMessage *theMsg)
    00065 {
    00066
    00067     G.Out(pdGendoc,"enter InteractionBroadcastList::InteractionBroadcastList");
    00068
    00069     if (theMsg == 0)
    00070         throw RTIinternalError("Null Broadcast Message.");
    00071
    00072     message = theMsg ;
    00073
    00074     // Add reference of the sender(so it does not receive its own message).
    00075     if (message->federate != 0) {
    00076         InteractionBroadcastLine *firstLine ;
    00077         firstLine =
    00078             new InteractionBroadcastLine(message->federate,
    00079                                          InteractionBroadcastLine::sent);
    00080         lines.push_front(firstLine);
    00081     }
    00082
    00083     G.Out(pdGendoc,"exit InteractionBroadcastList::InteractionBroadcastList");
    00084
    00085 }
    00086
    00087 // ----------------------------------------------------------------------------
    00089 InteractionBroadcastList::~InteractionBroadcastList()
    00090 {
    00091     clear();
    00092 }
    00093
    00094 // ----------------------------------------------------------------------------
    00096 void
    00097 InteractionBroadcastList::clear()
    00098 {
    00099     delete message ;
    00100     message = NULL ;
    00101
    00102     while (!lines.empty()) {
    00103         delete lines.front();
    00104         lines.pop_front();
    00105     }
    00106
    00107     D.Out(pdTerm, "List is now empty.");
    00108 }
    00109
    00110 // ----------------------------------------------------------------------------
    00112 InteractionBroadcastLine*
    00113 InteractionBroadcastList::getLineWithFederate(FederateHandle federate)
    00114 {
    00115     list<InteractionBroadcastLine *>::iterator i ;
    00116     for (i = lines.begin(); i != lines.end(); i++) {
    00117         if ((*i)->federate == federate)
    00118             return (*i);
    00119     }
    00120
    00121     return 0 ;
    00122 }
    00123
    00124 // ----------------------------------------------------------------------------
    00131 void
    00132 InteractionBroadcastList::sendPendingMessage(SecurityServer *server)
    00133 {
    00134
    00135     G.Out(pdGendoc,"enter InteractionBroadcastList::sendPendingMessage");
    00136
    00137     list<InteractionBroadcastLine *>::iterator i ;
    00138     for (i = lines.begin(); i != lines.end(); i++) {
    00139         // If federate is waiting for a message.
    00140         if ((*i)->state == InteractionBroadcastLine::waiting) {
    00141
    00142             // 1. Send message to federate.
    00143             D.Out(pdProtocol, "Broadcasting message to Federate %d.",
    00144                   (*i)->federate);
    00145
    00146             Socket *socket = 0 ;
    00147             try {
    00148 #ifdef HLA_USES_UDP
    00149                 socket = server->getSocketLink((*i)->federate, BEST_EFFORT);
    00150 #else
    00151                 socket = server->getSocketLink((*i)->federate);
    00152 #endif
    00153
    00154                 G.Out(pdGendoc,"sendPendingMessage===>write");
    00155
    00156                 message->send(socket,NM_msgBufSend);
    00157             }
    00158             catch (RTIinternalError &e) {
    00159                 D.Out(pdExcept,
    00160                       "Reference to a killed Federate while broadcasting.");
    00161             }
    00162             catch (NetworkError &e) {
    00163                 D.Out(pdExcept, "Network error while broadcasting, ignoring.");
    00164             }
    00165
    00166             // 2. Mark federate as having received the message.
    00167             (*i)->state = InteractionBroadcastLine::sent ;
    00168         }
    00169         else
    00170             D.Out(pdProtocol, "No message sent to Federate %d.",
    00171                   (*i)->federate);
    00172     }
    00173
    00174     G.Out(pdGendoc,"exit InteractionBroadcastList::sendPendingMessage");
    00175
    00176 }
    00177
    00178 } // namespace certi
    00179
    00180 // $Id: InteractionBroadcastList.cc,v 3.13 2008/05/30 14:01:05 erk Exp $