// Jae Chung 7-13-99
// Example of a aimple and dull Agent that
// illustrates the use of OTcl linkages


#include <stdio.h>
#include <string.h>
#include "agent.h"


/* -------------------------------
   The Implementation class
   ------------------------------- */
class MyAgent : public Agent {
public:
   MyAgent();	// Constructor - binds variables

protected:
   int command(int argc, const char*const* argv);

private:
   int    my_var1;		// Variable 1
   double my_var2;		// Variable 2

   void   MyPrivFunc(void);
   void   showvars(void);
};



/* ---------------------------------------------------
   Constructor: bind variables in C++ class to strings
   --------------------------------------------------- */
MyAgent::MyAgent() : Agent(PT_UDP) 
{
   bind("my_var1_otcl", &my_var1);
   bind("my_var2_otcl", &my_var2);
}


/* ------------------------------------------------------------
   command: MAIN entry point from TCL call.

   Whenever a TCL command contains:

      [MyAgent type Object] x y z

   Then:   x y z are passed as "argv" to this command method
   ------------------------------------------------------------ */
int MyAgent::command(int argc, const char*const* argv) 
{
   /* -----------------------------------------------------------
      In this example, we tries to detect 2 possible commands
      for argv[1]:
      ----------------------------------------------------------- */
   if (strcmp(argv[1], "call-my-priv-func") == 0) 
   {
         MyPrivFunc();
         return(TCL_OK);
   }
   else if (strcmp(argv[1], "showvars") == 0) 
   {
         showvars();
         return(TCL_OK);
   }

   /* -----------------------------------------------------------
      If we arrived here, the command issued was not one of those
      implemented by MyAgent. But since MyAgent is an Agent,
      it inherits ALL agent commands. So we must try those...
      ----------------------------------------------------------- */
   return(Agent::command(argc, argv)); // Let parent class take care of it
}


void MyAgent::MyPrivFunc(void) 
{
   Tcl& tcl = Tcl::instance();
   tcl.eval("puts \"Message From MyPrivFunc\"");
   tcl.evalf("puts \"     my_var1 = %d\"", my_var1);
   tcl.evalf("puts \"     my_var2 = %f\"", my_var2);
}

void MyAgent::showvars(void) 
{
   printf("** my_var1 = %d\n", my_var1);
   printf("** my_var2 = %lf\n", my_var2);
}



/* =============================================================== */




/* -------------------------------
   The TCL Binding class
   ------------------------------- */
static class MyAgentClass : public TclClass 
{
public:

   /* -----------------------------------------------------------
      Constructor - defines the string "Agent/MyAgentOtcl" as
		    a TCL class
      ----------------------------------------------------------- */
   MyAgentClass() : TclClass("Agent/MyAgentOtcl") {}


   TclObject* create(int, const char*const*) 
   {
      return(new MyAgent());
   }
} class_my_agent;         // This defines a dummy variable of MyAgentClass type
			  // which FORCES C++ to invoke the CONSTRUCTOR

