/* ====================================================================
 * The Vovida Software License, Version 1.0 
 * 
 * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 
 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
 *    and "Vovida Open Communication Application Library (VOCAL)" must
 *    not be used to endorse or promote products derived from this
 *    software without prior written permission. For written
 *    permission, please contact vocal@vovida.org.
 *
 * 4. Products derived from this software may not be called "VOCAL", nor
 *    may "VOCAL" appear in their name, without prior written
 *    permission of Vovida Networks, Inc.
 * 
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
 * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 * 
 * ====================================================================
 * 
 * This software consists of voluntary contributions made by Vovida
 * Networks, Inc. and many individuals on behalf of Vovida Networks,
 * Inc.  For more information on Vovida Networks, Inc., please see
 * <http://www.vovida.org/>.
 *
 */

static const char* const HeartbeatTxThread_cxx_Version = "HeartbeatTxThread.cxx,v 1.32 2002/04/25 19:56:15 bko Exp";

#include <sys/param.h>
#include <netinet/in.h>
#include "cpLog.h"
#include "UdpStack.hxx"
#include "HeartbeatParms.hxx"
#include "HeartbeatTxThread.hxx"
#include "HeartbeatCommon.h"
#include "VMutex.h"
#include "VCondition.h"

/**
 * Constructor - Define HeartbeatTxThread
 * @param  port  The local port
 */
HeartbeatTxThread::HeartbeatTxThread( const int port )
  : ThreadIf(), itsPort(port)
{

    HeartbeatParms& parms = HeartbeatParms::instance();
    cpLog( LOG_DEBUG, "Assign HeartbeatTx MulticastAddr" );
    Data multicastAddr;
    parms.getMulticastAddr( multicastAddr );
    LocalScopeAllocator lo;
    multicastGroupAddr = NetworkAddress( multicastAddr.getData(lo) );
    delayInMillisec = parms.getHeartbeatInterval();

    itsTxUdpStack = new UdpStack(&multicastGroupAddr);
    assert(itsTxUdpStack != 0 );
}

/**
 * Constructor 
 * @param  port  The local port
 * @param  delayIntervalInMs  Delay in milli-seconds between sending heartbeats
 * @param  multicastIP  Address to send heartbeats to
 * @param  multicastPort  Port on which to transmit heartbeats
 */
HeartbeatTxThread::HeartbeatTxThread(
    const int port,
    int delayIntervalInMs,
    const char * multicastIP,
    u_int16_t multicastPort
    )
  : ThreadIf(), 
    multicastGroupAddr(multicastIP, multicastPort),
    delayInMillisec(delayIntervalInMs),
    itsPort(port)
{
    itsTxUdpStack = new UdpStack(&multicastGroupAddr);
    assert(itsTxUdpStack != 0 );
}

/**
 * Destructor
 */
HeartbeatTxThread::~HeartbeatTxThread()
{}

/**
 * Obtain the transmit UDP stack
 * @return  The local UDP stack for transmitting
 */
Sptr < UdpStack >
HeartbeatTxThread::getTxUdpStack( ) const
{
    return itsTxUdpStack;
}

/**
 * Send heartbeats continuously (with the given delay in between)
 * for as long as this thread is active.
 */
void
HeartbeatTxThread::thread()
{
    cpLog( LOG_DEBUG, "Start heartbeat thread" );
    
    int delay = delayInMillisec * 1000;
    heartbeatPortData hbPortData;
    hbPortData.dataType = htons((u_int16_t)HeartbeatData);
    hbPortData.padding = 0; // this is only to keep purify from complaining
    hbPortData.port = htons(itsPort);
    hbPortData.load = htons(5);
    cpLog (LOG_DEBUG_HB, "Transmit heartbeat to Multicast Address: %s",
	       multicastGroupAddr.getIpName().c_str());

    while ( isShutdown() != true )
    {
        vusleep(delay);

        /// Send heartbeat
        itsTxUdpStack->transmitTo( (char*)&hbPortData, sizeof(heartbeatPortData), &multicastGroupAddr);
    }
}

/// End of File


syntax highlighted by Code2HTML, v. 0.9.1