// $Source: /usr/local/SimSoft/Sfx3D/src/gsl/Sfxhla.h,v $
// $Revision: 1.2 $
// $Author: baum $
// $Date: 2001/11/29 15:18:10 $
//
// Login name of the user who locked the revision
// $Locker:  $
// Tag name used to checkout the file
// $Name:  $
// State assigned to revision
// $State: Exp $
//
//
// $Log: Sfxhla.h,v $
// Revision 1.2  2001/11/29 15:18:10  baum
// -Realzeitsteuerung eingebaut
// -Handsteuerung eingebaut
//
#ifndef _SFXHLA_H
#define _SFXHLA_H     1

#include <tcl.h>
#include <Map.h>
#include <DynArray.h>


#ifdef _SFXHLA_CPP
#  undef _impexp
#  define _impexp __declspec(dllexport)
#elif defined BUILD_MAIN_DLL
#  undef _impexp
#  define _impexp 
#else
#  undef _impexp
#  define _impexp __declspec(dllimport)
#endif

#if defined __GNUC__ || defined __hpux
#  undef _impexp
#  define _impexp
#endif

#ifdef WIN32
#include <windows.h>
#define libhandle HMODULE
#endif

#ifdef __GNUC__
#include <dlfcn.h>
#define libhandle void*
#endif

class hla4sfx;
class HlaAttribute;
/**< 20-01-03: Damit global über diesen Zeiger auf die Methoden zugeriffen werden kann.*/
_impexp extern class SFXHla *MySFXHla;




/**
 * Fuer jedes Object, das abboniert oder veroeffentlicht wird, muss
 * diese Struktur angelegt werden.
 *
 * Hinweis: RTI::ObjectHandle, RTI::ObjectClassHandle sind hier unsigned long
 *				RTIfedTime und RTI::FedTime sind double
 *
 */
class ObjectStructure {
public:
	ObjectStructure (const char* className);
	~ObjectStructure ();

	void SetName (const char* className);
	const char* GetName ();

	void SetOwner (const char* owner);
	const char* GetOwner ();

	void SetClassHandle (unsigned long classHandle);
	unsigned long& GetClassHandle ();

	void SetInstanceId (unsigned long instance);
	unsigned long& GetInstanceId();

	void SetObjectTime (double objTime);
	double const & GetLastTime();

	void AddAttribute (HlaAttribute& attr);
	void AddAttributes (DynArray<HlaAttribute>& attr);
	DynArray<HlaAttribute>& GetAttributeList ();
		
	void SetTime (double const & time);
	void SetLastTime(double const & time);
	double const & GetTime ();


   unsigned long& AddInstance (const unsigned long& objID, DynArray<HlaAttribute> attrList);
	void RemoveInstance (unsigned long &objID);

	Map<unsigned long, DynArray<HlaAttribute> >& GetInstanceList ();


private:

	DynArray<HlaAttribute> AttributeList;
	Map<unsigned long, DynArray<HlaAttribute> > objectInstances;

	double objectTime;
	double objectLastTime;

	// ID fuer den Object-Erzeuger - jedem Object wird eine ID zugewiesen.
		
   const char* objectClassName;
	const char* objectClassOwner;
	
	unsigned long objectInstanceId; //RTI::ObjectHandle
	unsigned long objectClassHandle; // RTI::ObjectClassHandle

};



class HlaAttribute {
public:

	HlaAttribute (const char* attributeName);
	HlaAttribute (const char* attributeName, int intVal);
	HlaAttribute (const char* attributeName, double doubleVal);
	HlaAttribute (const char* attributeName, const char* textVal);
	// copy constructor
	HlaAttribute (const HlaAttribute&);

	~HlaAttribute ();

	const char* getName ();

	void setIntValue (int val);
	void setDoubleValue (double val);
	void setTextValue (const char* str);

	int& getIntValue ();
	double&	getDoubleValue ();
	const char* getTextValue ();

	bool intValueSet ();
	bool doubleValueSet ();
	bool textValueSet ();


	void setAttributeRtiId (unsigned long handle);
	unsigned long getAttributeRtiId ();

   const HlaAttribute& addOwner (const unsigned long& objID, const HlaAttribute& attr);
	void removeOwner (unsigned long &objID);

	const Map<unsigned long, HlaAttribute>& getOwnerList () const;

	void setInstanceId (unsigned long instance);
	unsigned long& getInstanceId ();


private:

	int objInt;
	double objDouble;
	const char* objText;

	bool objIntSet;
	bool objDoubleSet;
	bool objTextSet;

	const char* objAttributeName;

	//RTI::ObjectHandle objectInstanceId;
	unsigned long objInstanceId;

	Map<unsigned long, HlaAttribute> objOwnerListOfAttribute;

	unsigned long myRTI_ID;
};



//---------------------------------------------------------
// class InteractionStructure
//---------------------------------------------------------



class InteractionStructure {
public:
	InteractionStructure (const char* className);
	~InteractionStructure ();

	void SetName (const char* className);
	const char* GetName ();

	void SetOwner (const char* owner);
	const char* GetOwner ();


	void SetClassHandle (unsigned long classHandle);
	unsigned long GetClassHandle ();

	void SetTime (double const & time);
	//void SetLastTime(double const & time);
	double const & GetTime ();

		
private:	
	double interactionTime;

   const char* interactionClassName;
	const char* interactionClassOwner;

	unsigned long interactionClassHandle;
};

//---------------------------------------------------------
// class HlaParameter - Alle Parameter werden hiermit erzeugt
//---------------------------------------------------------

class HlaParameter {
public:

	HlaParameter (const char* parName);
	HlaParameter (const char* parName, const char* textVal);
	~HlaParameter ();

	const char* GetName ();

	void AddTextValue (const char* str);
	const char* GetFirstTextValue ();
	int CountTextValues ();

	void SetParameterRtiId (unsigned long handle);
	unsigned long GetParameterRtiId();

   void SetOwner (const char* owner);
	const char* GetOwner ();

private:
	DynArray<char> parameterTextList;
	const char* parameterName;
	const char* ownerOfParameter;

	unsigned long myRTI_ID;
};


//---------------------------------------------------------
// class SFXHla
//---------------------------------------------------------

class _impexp SFXHla {
  public:
    SFXHla  (Tcl_Interp* interp,const char* fedName, const char* fExecName, double lookAhaed, \
    	double tickMin, double tickMax, const char* fedFileName, const char* pClass, 					DynArray<char> &pParList, const char* sClass, DynArray<char> &sParList);
    ~SFXHla ();

	void Init ();
	void Exit ();
	void SendUpdate (int  BausteinNr,    int  ETyp, int  Parameter);
	void SendInteraction (int  BausteinNr,    int  ETyp, int  Parameter);
	void SendInteractionText (const char* nText);

	void SendInteractionWithClassAndParName (double EreignisZeit, int ETyp, int BausteinNr, int ArbPlNr, int Menge, const char* className, const char* parName);
	void SendInteractionMessageWithClassAndParName (const char* iaMessage, const char* className, const char* parName);
	void SendSfxInteraction (const char* msg);
	void Senden (double EreignisZeit, int ETyp, int BausteinNr, int ArbPlNr, int Menge);
	
	double meinHlaLesen  ();
	int  Lesen  (int *VonBausteinNr, int *ETyp, int *Parameter);
	void SendNextEventRequest (double requestTime);
	double GetGrantTime ();
	
	void RegisterObject (const char* className, int bstNr, DynArray<char> &attribs);
	void SubscribeObject (const char* className, DynArray<char> &attribs);

	void VerarbeiteInteractions ();
	void VerarbeiteSfxInteractions ();
	const char* GetReceivedInteraction ();

	void Planeein(const SimTime& zeit, int typ, int info1, int info2, int info3);

	int GetInteractionCounter ();
	const char* GetInteraction ();
	
	double GetTickMin () {return ObjTickMin;};
	double GetTickMax () {return ObjTickMax;};
	void Tick (double min, double max);

  private:
  	hla4sfx* myHla4SfxPtr;
	Tcl_Interp* ObjInterp;
	
	const char* ObjFederateName;
	const char* ObjFederateExecutionName;
	const char* ObjFEDFileName;
	const char* ObjPublishInteractionClassName;
	const char* ObjSubscribeInteractionClassName;

	double ObjLookAhead;
	double ObjTickMin;
	double ObjTickMax;

	Map<ObjectStructure, DynArray<HlaAttribute> > PublishedObjectsMap;
	Map<ObjectStructure, DynArray<HlaAttribute> > SubscribedObjectsMap;

	Map<InteractionStructure, DynArray<HlaParameter> > PublishedInteractionsMap;
	Map<InteractionStructure, DynArray<HlaParameter> > SubscribedInteractionsMap;

	//DynArray<HlaParameter> PublishedInteractionList;
	//DynArray<HlaParameter> SubscribedInteractionList;

	DynArray<char> HlaReceivedInteractionList;
};
#endif //_SFXHLA_H

