// Editor-info: -*- C++ -*-

#include <stdio.h>
#include <OB/CORBA.h>
#include "aa_adapter_impl.h"
#include "sinkadapterstate.h"
#include "iface/aaif/aadownprimitives.h"


//
// Method: constructor
//
// Description:
//     Calls parent class constructors.
//

aaAdapter_impl :: aaAdapter_impl()
    : aaAdapter_skel(),
      pfAdapter(),
      _connected(0),
      _count(0),
      _timeout(0)
{
    changeState(new aaSinkAdapterState);
    return;
}


//
// Method: inject
//
// Description:
//     Implements the method defined in the IDL interface. Starts the
//     process of sending given number of AA-DATA.requests to the SSCOP
//     conduit at the A side of the adapter. If a previous request isn't
//     completed yet, only an error message is printed.
//

void aaAdapter_impl :: inject(
    CORBA_Long pdus_,
    CORBA_Long interval_in_msec_)
{
    if (_count > 0)
    {
        cout << "aaAdapter: Previous request pending!" << endl;
    }
    else
    {
        cout << "aaAdapter: Sending " << pdus_ << " packets with "
             << interval_in_msec_ << " millisecond intervals" << endl;
        _timeout = OTime(interval_in_msec_ / 1000,
                         (interval_in_msec_ % 1000) * 1000);
        _count = pdus_;        
        requestTimeout(_timeout);
    }
    return;
}


//
// Method: timeoutCallback
//
// Description:
//     Sends an AA-DATA.request to the SSCOP conduit with fixed contents
//     and re-requests a timeout if there still are PDUs to be sent.
//

void aaAdapter_impl :: timeoutCallback(void)
{
    _count--;
    if (_count > 0)
    {
        requestTimeout(_timeout);
    }
    else
    {
        cout << "aaAdapter: Request done!" << endl;
    }

    for (int i=0;i<10;i++)
    {
        aaDATAreq *msg = new aaDATAreq;
        pfByte message[1024];
        sprintf((char *) message, "%s%d.%d", MY_MESSAGE, _count + 1, 10-i);
        pfFrame frame(message, 1024, 4096);
        msg->setMessageUnit(frame);
        toA(msg);
    }
    return;
}


pfByte * aaAdapter_impl :: MY_MESSAGE = (pfByte *) "MSG#";
