/* InfoTest.c
**
** $VER: InfoTest.c 1.0 (17.5.99)
** Shows some information in a DragonDaemon requester.
** Written by Kai Radewald <kai.radewald@stud.uni-hannover.de>
**     for SAS/C v6.58. May work with other compilers as well.
** This demo program is in the public domain.
** Please have a look at my homepage [http://browse.to/radewald]
** DragonDaemon 1.0 is © 1999 Juergen Reinert <ac-techno@t-online.de>
*/

#include <proto/dos.h>
#include <proto/exec.h>
#include <exec/memory.h>
#include "dragondaemon.h"

static const STRPTR VersionTag = "$VER: InfoTest 0.1 " __AMIGADATE__;

static struct DD_Message *ddmsg;
static struct MsgPort    *ddreply;

/****************************************
** SEND A COMMAND TO THE DRAGON DAEMON **
****************************************/

LONG Send2Daemon (LONG cmd, LONG arg0, LONG arg1, LONG opt)
{
    /* Local variables */

    LONG error = 1;

    struct MsgPort *dcrx;
    struct Message *dummy;

    /* Fill in the DD_Message structure */

    ddmsg->dd_StdMsg.mn_Node.ln_Type = NT_MESSAGE;
    ddmsg->dd_StdMsg.mn_ReplyPort    = ddreply;
    ddmsg->dd_StdMsg.mn_Length       = sizeof (struct DD_Message);
    ddmsg->dd_Command                = cmd;
    ddmsg->dd_Arg0                   = arg0;
    ddmsg->dd_Arg1                   = arg1;
    ddmsg->dd_Options                = opt;

    /* Obtain DragonDaemon's public message port */

    if (dcrx = FindPort (DD_PORTNAME))
    {
        PutMsg (dcrx, (struct Message*) ddmsg);
        WaitPort (ddreply);

        while (dummy = GetMsg (ddreply))
        {
            error = ddmsg->dd_Return;
        }
    }

    /* End of function */

    return error;
}

/******************
** MAIN FUNCTION **
******************/

int __stdargs main (void)
{
    /* Local variables */

    LONG dderr;

    /* Allocate memory for DD_Message structure */

    if (ddmsg = (struct DD_Message*) AllocVec (sizeof (struct DD_Message), MEMF_CLEAR|MEMF_PUBLIC))
    {
        /* Create reply message port */

        if (ddreply = CreateMsgPort ())
        {
            /* Make reply port public */

            AddPort (ddreply);

            /* Send a command to DragonDaemon */

            if (dderr = Send2Daemon (DDCMD_INFO, (LONG) "Dies ist eine Info!", 0, 0))
            {
                Printf ("DragonDaemon returned error %ld.\n", dderr);
            }

            /* Remove port from public list and delete it */

            RemPort (ddreply);
            DeleteMsgPort (ddreply);
        }
        else
        {
            PutStr ("Couldn't create message port.\n");
        }

        /* Free memory of DD_Message structure */

        FreeVec ((APTR) ddmsg);
    }
    else
    {
        PutStr ("No memory for DD_Message structure.\n");
    }

    /* That's all folks! ;-) */

    return RETURN_OK;
}
