/* ----   file information  -------------------------------------------------

   Black Sun Interactive

   Project:    Community Services
   Subsystem:  Hubbot API

   Purpose:    Call functions from Hubbot Script. Implementation of hbAPI derived
               class.

   Description:
               cltcontext.h contains the TextClientContext class which is derived from 
               the hbAPI class.

               We only redefine the Constructor/Destructor, the className() method.
               The Constructor of Controller registers the function we want to call
               from the Hubbot scripts. 

   Module:     cltcontext.cpp

   Date:       1998/01/22
   $Log: cltcontext.cpp,v $
   Revision 1.1  2001/03/12 15:04:38  robert
   Initial revision



   ----   file information  --------------------------------------------------*/



//\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
//             Include files and macros                                                  
//\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "hbapi.h"          // Agent API include file
#include "cltcontext.h"     // derivation of hbAPI class


#ifdef WIN32 
  #define EXT_HBAPI_EXPORT __declspec( dllexport )
#else
  #define EXT_HBAPI_EXPORT 
#endif


//\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
//          Logging utilities and macros
//\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/


#define ROBOT_LOG(X)             writeLog X 
#define ROBOT_ERR(X)             writeLog X
#define ROBOT_TRA(X)             writeLog X 



class myClientContext : public hbClientContext
{
       
   public:
           int saidHi;     
           int saidHello;     

           myClientContext() { saidHi = 0; saidHello = 0; };
           ~myClientContext() {};

};

//\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
//          Fixed interface to Hubbot, please do not touch!!!
//\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/


//
//  Constructor
//
TestClientContext::TestClientContext( CSHandle_t sessionHdl ) : hbAPI( sessionHdl )
{ 
    int rc;
      
   // here we register the function we want to call from withing the
   // Hubbot script.

#ifdef WIN32
   rc = registerFunc( "SaidHi", (hbFuncType) SaidHi);
#else
   rc = registerFunc( "SaidHi", (hbFuncType) &SaidHi);
#endif
   if ( rc != 0 )
   {
       ROBOT_TRA(("%-14s: API Could not register function: init_array\n", "" ));
   }
};


//
//  Destructor
//
TestClientContext::~TestClientContext() 
{ 
//	hbAPIgetVersion();
};


//
//  this method just returns the name of the current class
//
const char * TestClientContext::className() const
{
   return "TestClientContext";
}



//
//  This is the entry point function for hubbot.
//  This function creates an instance of the current Hubbot API class.
//  Once it succeded, this function will never be called again.
//

EXT_HBAPI_EXPORT hbAPI * createInstance( CSHandle_t sessionHdl, const char *name )
{
   hbInitAPISession();
   if ( name != (char *) 0 )
   {
     return new TestClientContext( sessionHdl );
   }

   return 0;
};



//\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
//          LOGIC                                                    
//\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/


// This is the function which is called by the Agent script
// in a  *Call* action.
//
// The input parameter "input" must not be modified by this function.
// The result returned is a string which also can be a null pointer.
// If the result string is not null, it is immediately copied into 
// Agent's memory.
//
// This function has to make sure to free memory allocated for the
// result strings or pass pointers to static memory as result 
//


#define RESULT_BUFFER_SIZE 1024
char * TestClientContext::SaidHi( const char * input )
{
  static char resultBuf [RESULT_BUFFER_SIZE+1];
  if ( ! input ) 
  { 
     return 0; 
  }
  resultBuf[0] = 0;

  
  if ( hasClientContext() )
  {
      writeLog("Setting client context\n");
      myClientContext *contxt = (myClientContext *) getClientContext();

      if (! contxt)
      {
         contxt = new myClientContext();
      }

      if ( contxt && !contxt->saidHi )
      {
         strcpy ( resultBuf, "Hi" );
         contxt->saidHi = 1;
      }
      else if ( contxt && !contxt->saidHello )
      {
         strcpy ( resultBuf, "Hello" );
         contxt->saidHello = 1;
      }
    
      setClientContext( contxt );
  }
  else
  {
      writeLog("No client context available\n");
  }

  return resultBuf;
}

//\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
//          END LOGIC                                                
//\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
