![]() |
VR-Link API Documentation for HLA 1.3
|
00001 //******************************************************************** 00002 // Copyright (c) 2006 MaK Technologies, Inc. 00003 // All rights reserved. 00004 //******************************************************************** 00005 #if DtHLA 00006 00007 #include <stdio.h> 00008 #include <vl/entitySR.h> 00009 #include <vlutil/vlString.h> 00010 #include <vl/hPlatformEnc.h> 00011 #include <vl/hPlatformDec.h> 00012 #include <vl/fomMapper.h> 00013 #include <vl/encFactory.h> 00014 #include <vl/decFactory.h> 00015 #include <vl/fom.h> 00016 #include <vlpi/EntityTypes.h> 00017 00022 class DtF18EntityStateRep : public DtEntityStateRepository 00023 { 00024 public: 00026 DtF18EntityStateRep() : DtEntityStateRepository(), 00027 myAttrib("") 00028 00029 { printf("\n===================\n" 00030 "Constructing DtF18EntityStateRep\n" 00031 "=====================\n"); } 00032 00034 virtual ~DtF18EntityStateRep() {} 00035 00036 private: 00037 00039 DtF18EntityStateRep(const DtF18EntityStateRep& orig); 00040 00042 DtF18EntityStateRep& operator=(const DtF18EntityStateRep& orig); 00043 00044 public: 00045 00047 virtual void setF18Attribute(const DtString & val) { myAttrib = val;} 00049 virtual DtString f18Attribute() const { return myAttrib;} 00050 00053 static DtF18EntityStateRep* create() { return new DtF18EntityStateRep; }; 00054 00055 00056 protected: 00057 00058 DtString myAttrib; 00059 00060 }; 00061 00065 class DtF18EntityDecoder : public DtPlatformDecoder 00066 { 00067 public: 00069 DtF18EntityDecoder( DtExerciseConn* exConn, 00070 DtObjClassDesc* classDesc): 00072 DtPlatformDecoder(exConn, classDesc) 00073 { 00075 addDecoder("SpecialF18Attribute", 00076 (DtAttributeDecoder) decodeSpecialF18Attribute); 00077 } 00078 00080 virtual ~DtF18EntityDecoder(){} 00081 00082 protected: 00083 00087 static void decodeSpecialF18Attribute(DtF18EntityStateRep* stateRep, 00088 const RTI::AttributeHandleValuePairSet& attrs, 00089 int pairSetIndex) 00090 { 00091 RTI::ULong length = attrs.getValueLength(pairSetIndex); 00092 // if there is nothing in there, we can exit safely 00093 if (length == 0) return; 00094 00095 char* buffer = DtDecodingBuffer(length+1); 00096 attrs.getValue(pairSetIndex, buffer, length); 00097 buffer[length] = '\0'; 00098 00099 stateRep->setF18Attribute(buffer); 00100 } 00101 00102 }; 00103 00107 class DtF18EntityEncoder : public DtPlatformEncoder 00108 { 00109 public: 00111 DtF18EntityEncoder( DtExerciseConn* exConn, 00112 DtObjClassDesc* classDesc): 00113 // By calling the base class ctor all the needed encoders and checkers are registered 00114 DtPlatformEncoder(exConn, classDesc) 00115 { 00116 // Register our encoder functions for all the attributes we need. 00117 addEncoder("SpecialF18Attribute", 00118 (DtAttributeEncoder) encodeSpecialF18Attribute); 00119 addChecker("SpecialF18Attribute", 00120 (DtAttributeChecker) needSpecialF18Attribute); 00121 } 00122 virtual ~DtF18EntityEncoder() {} 00123 00124 protected: 00125 00126 static bool needSpecialF18Attribute( const DtF18EntityStateRep& stateRep, 00127 const DtF18EntityStateRep& asSeenByRemote) 00128 { 00129 return(stateRep.f18Attribute() != asSeenByRemote.f18Attribute()); 00130 } 00131 00132 static void encodeSpecialF18Attribute( const DtF18EntityStateRep& stateRep, 00133 RTI::AttributeHandleValuePairSet* avList, 00134 RTI::AttributeHandle attrHandle) 00135 { 00136 // This is easy, they are not all this easy. The point of this example 00137 // is not to show how to encode/decode complex data types. 00138 avList->add(attrHandle, 00139 stateRep.f18Attribute().c_str(), 00140 stateRep.f18Attribute().size()); 00141 } 00142 00143 }; 00144 00145 // Some useful macros for adding the encoders/decoders 00147 #define DtADD_ENCODER(className, derivedEnc) \ 00148 { \ 00149 DtObjClassDesc* classDesc = exConn.fom()->objClassByName(#className); \ 00150 if (classDesc) \ 00151 { \ 00152 exConn.fomMapper()->stateEncoderFactory()->addEncoder( \ 00153 classDesc->handle(), new derivedEnc(&exConn, classDesc)); \ 00154 } \ 00155 else \ 00156 { \ 00157 DtWarn("Can't add encoder for object class %s: Not in FOM.\n", #className); \ 00158 } \ 00159 } 00160 00162 #define DtADD_DECODER(className, derivedDec) \ 00163 { \ 00164 DtObjClassDesc* classDesc = exConn.fom()->objClassByName(#className); \ 00165 if (classDesc) \ 00166 { \ 00167 exConn.fomMapper()->stateDecoderFactory()->addDecoder( \ 00168 classDesc->handle(), new derivedDec(&exConn, classDesc)); \ 00169 } \ 00170 else \ 00171 { \ 00172 DtWarn("Can't add decoder for object class %s: Not in FOM.\n", #className); \ 00173 } \ 00174 } 00175 00177 char* DtF18EntityChooser( const char* vrlinkClassName, 00178 DtExerciseConn* exConn, 00179 void *usr) 00180 { 00181 printf("=========\n"); 00182 printf(" In Class Chooser, deciding which entity Type to publish\n"); 00183 printf("=========\n"); 00184 00185 // For Entities, the usr data is always of DtEntityType. This is just 00186 // the way it is. 00187 00188 DtEntityType& type = *((DtEntityType*) usr); 00189 00190 if (type.subCategory() == DtF18) 00191 { 00192 printf("Choosing F18 Type, we will use the new class!\n"); 00193 // Return the FOM OBJECT CLASS name 00194 return "BaseEntity.PhysicalEntity.Platform.Aircraft.F18"; 00195 } 00196 00197 // If this is not the right type, let the default chooser decide 00198 return(DtRprFomMapper::dfltEntityClassChooser(vrlinkClassName, 00199 exConn, usr)); 00200 } 00201 00202 00203 #endif