Sample Program Listing

The following is a listing of the sample program for the AMIP API:

#include <inc_path/amip.h>

#include <conio.h>

class CAMIPSample:public CAMIPBase

{

public:

    CAMIPSample(){m_bAbort=FALSE;m_fp=NULL;};

    ~CAMIPSample(){CloseOutPort();};

//overide

    virtual void OnAlarmInfo(CAlarmUpdateInfo *pAlarmInfo);

    virtual void OnShutdown();

    virtual void OnStatus(COR_STATUS *pStatus);

    virtual void NotifyStopOnError();

    BOOL m_bAbort;

    BOOL OpenOutPort()

    {

        if (m_fp)

            return TRUE;

        m_fp = fopen("\\sample.log","w");

        if (m_fp==NULL)

            return FALSE;

        return TRUE;

    }

    void CloseOutPort()

    {

        if (m_fp)

            fclose(m_fp);

        m_fp=NULL;

    }

    void DumpToOutPort(LPCTSTR lpszBuf)

    {

        fprintf(m_fp,lpszBuf);

        fflush(m_fp);

    }

protected:

    FILE *m_fp;

};

void CAMIPSample::OnAlarmInfo(CAlarmUpdateInfo *pAlarmInfo)

{

    TCHAR szBuf[256];

    OpenOutPort();

    _stprintf(szBuf,"Alarm: %s ",pAlarmInfo->alarm_id );

    DumpToOutPort(szBuf);

    _stprintf(szBuf," Resource: %s ",pAlarmInfo->fr_id );

    DumpToOutPort(szBuf);

    _stprintf(szBuf,"\nReference:%s ",pAlarmInfo->ref_id );

    DumpToOutPort(szBuf);

    _stprintf(szBuf," Class: %s ",pAlarmInfo->class_id );

    DumpToOutPort(szBuf);

    _stprintf(szBuf," File: %d ",pAlarmInfo->log_file );

    DumpToOutPort(szBuf);

    _stprintf(szBuf,"\n%s\n ",pAlarmInfo->alarm_msg );

    DumpToOutPort(szBuf);

    _stprintf(szBuf," States -- ");

    DumpToOutPort(szBuf);

    _stprintf(szBuf," Prev: %d ",pAlarmInfo->prev_state );

    DumpToOutPort(szBuf);

    _stprintf(szBuf," Req: %d ",pAlarmInfo->requested_action );

    DumpToOutPort(szBuf);

    _stprintf(szBuf," Final: %d\n ",pAlarmInfo->final_state );

    DumpToOutPort(szBuf);

}

void  CAMIPSample::OnShutdown()

{

    DumpToOutPort(_T("\nShutdown\n"));

}

void  CAMIPSample::OnStatus(COR_STATUS *pStatus)

{

    DumpToOutPort(_T("\nStatus Block Recived\n"));

}

void  CAMIPSample::NotifyStopOnError()

{

    if (GetLastError()==errorRouterDied)

        DumpToOutPort(_T("\nError : Router Died.Stopping ...\n"));

    else

        DumpToOutPort(_T("\nError : Stopping ...\n"));

    m_bAbort=TRUE;

}

void main(int argc,char **argv)

{

    BOOL    bAsync=FALSE;

    if (argc >1)

       if (strcmpi(argv[1],"ASYNC")==0)

  bAsync=TRUE;

    CAMIPSample sample;

    if (sample.GetLastError()!=CAMIPBase::errorNo)

    {

        printf("Error in initialization(%d)\nExiting ...\n",sample.GetLastError());

        exit(-1);

    }

    if (!sample.Start(bAsync))

    {

        if (sample.GetLastError()==CAMIPBase::errorInit)

            printf("Error in Starting\nExiting ...");

        exit(-1);

    }

    if (bAsync)

    {

        while(!_kbhit())

        {

          if (sample.m_bAbort)

             exit(-1);

        }

        _getch();

        printf("Stopping ...\n");

        sample.Stop();

    }

    printf("Exiting ...\n");

}

More information

About the Sample Program.

Step 1. Create the Sample Program Executable.