第四章 C++程序设计

       本章讲述如何在Linux操作系统上设计GNU C++程序。演示了2个程序:聊天程序chat和时间管理程序time。chat使用HLA的交互类进行通信,没有采用tick服务;time使用HLA的对象类进行通信,采用tick服务;并说明了如何简单修改就可以变成一个不采用tick服务的程序。

4.1 多线程设计模式

       RTI由Central RTI Component(CRC)和Local RTI Component(LRC)组成。在KY-RTI中,可以把CRC简单地理解为RTI服务器,LRC理解为libRTI-NG库。

       基于KY-RTI的C++仿真程序可以仅包含2个.cpp文件,如下图所示。一个负责请求RTI;另一个负责从RTI接收回调。Main.cpp通过调用HLA服务把请求传给LRC,再由LRC传给CRC;CRC将回调传给LRC,再由LRC传给HwFederateAmbassador.cpp。HwFederateAmbassador.cpp是DMSO RTI习惯采用的文件名,专门用来接收RTI的回调数据。在KY-RTI的各类程序设计语言中,接收回调的文件名都采用了HwFederateAmbassador这个专有名称。

 

麒麟KY-RTI分布仿真技术:第四章 C++程序设计

                                                              图4.1 仿真成员采用多线程技术实现

       在KY-RTI编程模式中,Main.cpp和HwFederateAmbassador.cpp分别位于两个独立的线程中;当然,如果在同一个线程中,既要向RTI发送请求,又要从RTI接收请求,容易造成死锁。如果两个线程采用全局变量来共享数据,则同时读写一个共享变量时会引起冲突,因此应加锁或采用临界区来进行互斥访问。

4.2 多语言间的数据传输

       以C++和Java语言为例,两种程序相互之间传输数据时涉及到主机字节序与网络字节序两个概念。

网络字节序:不同计算机体系结构的存储机制可能不同,通信双方交流的信息单元(比特、字节、字、双字等)应该按照统一的规则进行发送和接收才行。如果不达成一致的理解, 通信双方将无法进行正确的通信。网络字节序规定采用big-endian规则,通信双方需要把数据按照big-endian编码之后再通过网络传输。

       主机字节序就是CPU的字节序。x86主机的字节序为little-endian。因此,在x86主机上传输数据的时候要把数据从little-endian转换为big-endian编码后,再通过网络发送出去。

       下面举一个例子说明big-endian与little-endian的区别:

       int size  = 0x01020304;

       size的类型为int,int有4个字节,其中01为最高位的字节,04为最低位的字节。那么在内存(或文件)中,该值的存储顺序为:

 

      内存(或文件)地址:0x00000001(高位)  0x00000002  0x00000003  0x00000004(低位)  

      big-endian         :           01                              02                 03                   04

      little-endian       :           04                              03                 02                   01

      

       如图4.2所示,如果两个程序都是C++程序,则它们在通信时不进行编码转换也能正常通信。

 

麒麟KY-RTI分布仿真技术:第四章 C++程序设计

                                                              图4.2 C++程序之间不进行网络编码能正常通信

       Java虚拟机采用big-endian,而运行在Java虚拟机中的Java程序的主机字节序也为big-endian。如图4.3所示,如果C++程序与Java程序进行通信,不进行编码转换就不能正确通信。

 

麒麟KY-RTI分布仿真技术:第四章 C++程序设计

                                                              图4.3 C++程序与Java程序之间不进行网络编码则通信异常

       为简化用户编程逻辑,基于KY-RTI设计的两个仿真程序可以不进行网络编码就能正常通信,但有1个简单要求。如图4.4所示,如果C++把123.45这个浮点数变成字符串'1'、'2'、'3'、'.'、'4'、'5',则C++不需要编码,Java程序也能够正确地接收。

 

麒麟KY-RTI分布仿真技术:第四章 C++程序设计

                                                              图4.4 C++程序与Java程序在KY-RTI中的通信方式

       下面几行代码是从4.4节时间管理程序中摘来的几条语句,略有改动,用户可能会经常用到。

       (1)如果两个仿真成员都是C++程序,则使用下面语句对整型属性xPos进行打包和解包。

             打包:

                       int xPos;

                       pAttrs->add(g_hxPos, (char*)&xPos, sizeof(int));

             解包:

                       int xPos;

                       theAttributes.getValue(i, (char*)&xPos, valueLength);

        (2)如果两个仿真成员采用不同语言编程,则应将整型属性xPos转为字符串。

                 也就是说,发送方应使用字符串发送,接收方应将接收到的字符串转为整数。

              打包:

                       char s[20];

                       sprintf(s, "%d\0", xPos);

                       pAttrs->add(g_hxPos, (char*)s, strlen(s));

              解包:

                       int xPos;

                       char str[20];

                       theAttributes.getValue(i, (char*)str, valueLength);

                       xPos = atoi(str);

4.3 聊天程序

4.3.1需求分析

       本项目需要实现一个类似微信群或者QQ群聊天功能的GNU C++程序,每个人发送的消息都能够被群里的其他人看到。

4.3.2项目设计

       每条聊天信息应包含2个内容:聊天者昵称、聊天的一句话,这样接收者就会知道是谁在发言。“聊天者昵称”用name表示,“聊天的一句话”用sentence表示,两个都是字符串类型。因为HLA是面向对象的,发送的数据要么采用对象类,要么采用交互类。本项目可采用交互类,将name和sentence封装到一个名叫“chat”的交互类中,如下列代码所示。

class chat {           //交互类

       int  name;     //参数

       int  sentence; //参数

}

       下面采用KY-OMT创建fed文件,相应的chat.fed文件已经在3.3.3中创建完成,将该文件保存到KY-RTI的bin目录。

       本项目对时间没有特别要求,不需要采用HLA时间管理机制。当RTI收到聊天信息时就立即发送给其他人,不需要调用tick服务。

4.3.3代码设计

       该程序比较简单,一个Chat.cpp和HwFederateAmbassador.cpp就可以实现。前者通过调用创建联邦执行、加入联邦执行、公布和订购交互类、发送交互,仿真完成时退出联邦;后者用来接收RTI的回调消息。

       Chat.cpp代码说明:

8-10行:定义交互类及其参数句柄变量;

27-36行:创建联邦执行;

38-53行:加入联邦执行;

57-59行:获取交互类及其参数句柄;

62行:公布交互类,只有公布之后才能够向RTI发送交互;

64行:订购交互类,只有订购之后才能够从RTI收到其他人的聊天内容;

68-87行:循环操作,每次输入一句话,并调用sendInteraction服务发送给RTI;当用户输入“exit”时则退出执行;

89-94行:退出联邦执行,不再参加仿真;

96-107行:销毁联邦。如果是最后一个仿真成员执行该操作,则整个仿真结束。

                                                              表4.1  C++聊天示例:Chat.cpp

  1. #include "HwFederateAmbassador.hh"
  2.  
  3. #include <RTI.hh>
  4. #include <fedtime.hh>
  5. #include <iostream>
  6. using namespace std;
  7.  
  8. RTI::InteractionClassHandle     hChatClass;
  9. RTI::ParameterHandle        hChatName;
  10. RTI::ParameterHandle        hChatSentence;
  11.  
  12. int hw_main(int argc, char *argv[])
  13. {
  14.     const char *federationExecutionName = "chat";
  15.     const char *FEDfile = "chat.fed";
  16.  
  17.     char federateName[50];
  18.     cout << "Please input your name: ";
  19.     cin >> federateName;
  20.  
  21.     try {
  22.         RTI::RTIambassador       rti;
  23.         HwFederateAmbassador     fedAmb;
  24.  
  25.         RTI::FederateHandle      federateId;
  26.  
  27.         try {
  28.             rti.createFederationExecution(federationExecutionName, FEDfile);
  29.         }
  30.         catch ( RTI::FederationExecutionAlreadyExists& e ) {
  31.             //According to the HLA standard, only the first federate can call this service succesfully.
  32.             //cerr << "FED_HW: Note: Federation execution already exists." << e << endl;
  33.         } catch ( RTI::Exception& e ) {
  34.             cerr << "FED_HW: ERROR:" << e << endl;
  35.             return -1;
  36.         }
  37.  
  38.         try {
  39.             federateId = rti.joinFederationExecution(federateName, federationExecutionName, &fedAmb);
  40.         } catch (RTI::FederateAlreadyExecutionMember& e) {
  41.             cerr << "FED_HW: ERROR: " << argv[1]
  42.                  << " already exists in the Federation Execution "
  43.                  << federationExecutionName << "." << endl;
  44.             cerr << e << endl;
  45.             return -1;
  46.         } catch (RTI::FederationExecutionDoesNotExist&) {
  47.             cerr << "FED_HW: ERROR: Federation Execution "
  48.                  << "does not exists."<< endl;
  49.             return -1;
  50.         } catch ( RTI::Exception& e ) {
  51.             cerr << "FED_HW: ERROR:" << e << endl;
  52.             return -1;
  53.         }
  54.  
  55.         /////////////////////////////////////////////////////////////////////////
  56.  
  57.         hChatClass = rti.getInteractionClassHandle("chat");
  58.         hChatName = rti.getParameterHandle("name", hChatClass);
  59.         hChatSentence = rti.getParameterHandle("sentence", hChatClass);
  60.  
  61.         //如果向外发送,则需要公布
  62.         rti.publishInteractionClass(hChatClass);
  63.         //如果需要接收,则必须订购
  64.         rti.subscribeInteractionClass(hChatClass);
  65.  
  66.         string szSentence;
  67.         cin.ignore();
  68.         while (0 != strcmp(szSentence.c_str(), "exit")) {
  69.             cout << "Please input a sentence: ";
  70.             getline(cin, szSentence);
  71.  
  72.             RTI::ParameterHandleValuePairSet* pParams = NULL;
  73.             long numParams(2);
  74.             pParams = RTI::ParameterSetFactory::create (numParams);
  75.  
  76.             pParams->add(hChatName,(char*)federateName, strlen(federateName)+1);
  77.             pParams->add(hChatSentence,(char*)szSentence.c_str(), szSentence.size());
  78.  
  79.             try {
  80.                 rti.sendInteraction(hChatClass, *pParams, "");
  81.             } catch(...) {
  82.                 cerr << "Error: send interaction" << endl;
  83.             }
  84.  
  85.             pParams->empty();
  86.             delete pParams;   // Deallocate the memory
  87.         }
  88.  
  89.         try {
  90.             rti.resignFederationExecution( RTI::DELETE_OBJECTS_AND_RELEASE_ATTRIBUTES );
  91.         } catch ( RTI::Exception& e ) {
  92.             cerr << "FED_HW: ERROR:" << e << endl;
  93.             return -1;
  94.         }
  95.  
  96.         try {
  97.             rti.destroyFederationExecution( federationExecutionName );
  98.         } catch ( RTI::FederatesCurrentlyJoined& /* e */ ) {
  99.             cerr << "FED_HW: FederatesCurrentlyJoined" << endl;
  100.             return 0;
  101.         } catch ( RTI::FederationExecutionDoesNotExist& /* e */) {
  102.             cerr << "FED_HW: FederationExecutionDoesNotExist" << endl;
  103.             return 0;
  104.         } catch ( RTI::Exception& e ) {
  105.             cerr << "FED_HW: ERROR:" << e << endl;
  106.             return -1;
  107.         }
  108.     } catch (RTI::ConcurrentAccessAttempted& e) {
  109.         cerr << e << endl;
  110.         return -1;
  111.     } catch ( RTI::Exception& e ) {
  112.         cerr << "FED_HW: ERROR:" << e << endl;
  113.         return -1;
  114.     }
  115.  
  116.     return 0;
  117. }
  118.  
  119. int
  120. main(int argc, char** argv)
  121. {
  122.     return hw_main(argc, argv);
  123. }

       HwFederateAmbassador.cpp代码说明:

10-24行:由于不处理时间参数,因此如果接收到这种类型的receiveInteraction交互,则直接调用不带时间参数的服务来统一处理;

26-63行:处理接收到的聊天信息并输出。

                                                              表4.2  C++聊天示例:HwFederateAmbassador.cpp

  1. #include "fedtime.hh"
  2. #include "HwFederateAmbassador.hh"
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. extern RTI::InteractionClassHandle  hChatClass;
  7. extern RTI::ParameterHandle     hChatName;
  8. extern RTI::ParameterHandle     hChatSentence;
  9.  
  10. void HwFederateAmbassador::receiveInteraction (
  11.     RTI::InteractionClassHandle       theInteraction,         // supplied C1
  12.     const RTI::ParameterHandleValuePairSet& theParameters,  // supplied C4
  13.     const RTI::FedTime&                     theTime,             // supplied C4
  14.     const char                                *theTag,             // supplied C4
  15.     RTI::EventRetractionHandle             theHandle)          // supplied C1
  16. throw (
  17.     RTI::InteractionClassNotKnown,
  18.     RTI::InteractionParameterNotKnown,
  19.     RTI::InvalidFederationTime,
  20.     RTI::FederateInternalError)
  21. {
  22.     //call the next service.
  23.     this->receiveInteraction( theInteraction, theParameters, theTag );
  24. }
  25.  
  26. void HwFederateAmbassador::receiveInteraction (
  27.     RTI::InteractionClassHandle       theInteraction, // supplied C1
  28.     const RTI::ParameterHandleValuePairSet& theParameters,  // supplied C4
  29.     const char                             *theTag)         // supplied C4
  30. throw (
  31.     RTI::InteractionClassNotKnown,
  32.     RTI::InteractionParameterNotKnown,
  33.     RTI::FederateInternalError)
  34. {
  35.     RTI::ParameterHandle paraHandle;
  36.     RTI::ULong           valueLength;
  37.  
  38.     //Usage of char[] and string
  39.     char name[256];   //name of sender
  40.     string sentence;  //sentence of sender
  41.  
  42.     for ( int i = 0; i < theParameters.size(); i++ ) {
  43.         paraHandle = theParameters.getHandle( i );
  44.  
  45.         if(paraHandle == hChatName) {
  46.             theParameters.getValue(i, (char*)name, valueLength);
  47.  
  48.             /*If name is a double number, you can do this way.
  49.                 double name;
  50.                 theParameters.getValue(i, (char*)&name, valueLength);
  51.             */
  52.  
  53.         } else if(paraHandle == hChatSentence) {
  54.             sentence.resize(theParameters.getValueLength(i));
  55.             theParameters.getValue(i, (char*)sentence.c_str(), valueLength);
  56.  
  57.         } else {
  58.             cout << "Receive wrong parameter handle." << endl;
  59.         }
  60.     }
  61.  
  62.     cout << endl << name << ": " << sentence << endl;
  63. }

4.3.4编译运行

4.3.4.1编译

       编译GNU C++程序,通常要先建一个Makefile文件。在Makefile文件中指定了所使用的编译器、编译命令、编译选项、连接的库和库目录等。

       表4.3是编译聊天程序的Makefile文件,对于其他程序而言,格式都一样。在该Makefile中,只要关心第10、11、13行即可。其中,第10、11行指明了本程序有两个.cpp文件;第13行则指明了最终生成的可执行程序名。

                                                              表4.3  Makefile

  1. C++FLAGS = -DRTI_USES_STD_FSTREAM -DREENTRANT -D_RH72_GCC302 \
  2.                 -DRTI_HAS_THREADS -DPOSIX_PTHREAD_SEMANTICS -g -O3
  3.  
  4. LDFLAGS += -g -O3
  5.  
  6. LIBS = -lRTI-NG -lfedtime -lpthread
  7.  
  8. C++ = g++
  9.  
  10. OBJS =  Chat.o \
  11.         HwFederateAmbassador.o
  12.  
  13. EXECUTABLE = chat
  14.  
  15. RTI_ROOT_DIR = ${RTI_HOME}/${RTI_BUILD_TYPE}
  16. RTI_INC_DIR = ${RTI_ROOT_DIR}/include
  17. RTI_LIB_DIR = ${RTI_ROOT_DIR}/lib
  18.  
  19. INC_PATH = -I${RTI_INC_DIR} -I.
  20. LIB_PATH = -L${RTI_LIB_DIR}
  21.  
  22. # Build targets
  23. %.o : %.cpp
  24.     @echo
  25.     @echo Compiling $< ...
  26.     @echo
  27.     ${C++} -c ${C++FLAGS} ${INC_PATH} $< -o [email protected]
  28.  
  29. default: ${EXECUTABLE}
  30.  
  31. ${EXECUTABLE}: ${OBJS}
  32.     @echo
  33.     @echo Linking [email protected] ...
  34.     @echo
  35.     ${C++} ${LDFLAGS} ${OBJS} -o [email protected] ${LIB_PATH} ${LIBS}
  36.  
  37. clean:
  38.     rm -rf *.o core *~ .depend Templates.DB ${EXECUTABLE}

       执行下列命令生成可执行程序chat。

       make

4.3.4.2测试运行

       测试项目:在银河麒麟操作系统上运行2个GNU C++仿真成员,测试KY-RTI通信功能。

       测试步骤:

       第1步:修改RTI.rid,关闭tick开关。

       因为本程序没有使用tick服务,所以需要关闭tick开关。

       查看当前目录下是否存在RTI.rid,若没有则运行chat,则会自动生成该文件。将RTI.rid文件中的“;; UsingTickSwitch On”改为“;; UsingTickSwitch Off”。

       第2步:启动KY-RTI。注意,KY-RTI的IP地址和端口号要与RTI.rid一致。

       第3步:如图4.5和图4.6所示,开启两个终端,运行2个仿真成员,开始仿真。运行命令:

       ./chat

       测试结果表明:聊天功能正常,KY-RTI支持中英文传输。

麒麟KY-RTI分布仿真技术:第四章 C++程序设计

                                                              图4.5 GNU C++聊天者1

麒麟KY-RTI分布仿真技术:第四章 C++程序设计

                                                              图4.6 GNU C++聊天者2

 

4.4 时间管理程序

4.4.1需求分析

       本仿真项目的名称为“TimeManagementExample”,当然也可以叫做“空中雄鹰”、“飞行表演”之类的响亮名字。名称规定了不是“TimeManagementExample”的程序不属于本项目。对HLA/RTI程序来说,联邦名称为“TimeManagementExample”,不是该名字的仿真成员不属于本仿真。

       每个仿真成员拥有3架飞机,但其中只有1架飞机会起飞,飞机的x、y坐标为随机数(不考虑合理性),飞机会每隔1秒发布自己的位置信息。

4.4.2项目设计

       飞机每隔1秒发布自己的位置信息,意味着该仿真应采用时间管理服务,仿真步长为1。

       飞机发送的是自己的x、y二维态势信息,用一个对象类plane来封装,两个属性为xPos和yPos,类型为整型;但不管什么类型,在RTI中都是作为字符串来传送。如下列代码所示。

class plane {         //对象类

       int  xPos;      //属性

       int  yPos;      //属性

}

       下面采用KY-OMT创建fed文件,相应的tracer.fed文件已经在3.3.2中创建完成,将该文件保存到KY-RTI的bin目录。

4.4.3代码设计

       该程序包括TimeManagement.cpp和HwFederateAmbassador.cpp两个实现文件。前者通过调用创建联邦执行、加入联邦执行、公布和订购对象类、注册对象实例、周期性地发送二维态势信息和推进仿真,仿真完成时退出联邦;后者用来接收RTI的二维态势信息,并处理相关的时间管理服务等。

       TimeManagement.cpp代码说明:

10-17行:定义全局变量,由两个线程共享;

21行:联邦名称定义为“TimeManagementExample”;

22行:fed文件定义为“tracer.fed”;

35-44行:创建联邦执行;

46-61行:加入联邦执行;

65-67行:获取对象类及其属性句柄;

75行:公布对象类属性,只有公布之后才能够向RTI发送二维态势信息,即xPos和yPos;

76行:订购交互类属性,只有订购之后才能够从RTI收到二维态势信息;

81-86行:注册3架飞机;

93行:将仿真成员设置为时间管理受限的;

94-102行:等待RTI同意将该仿真成员设置为时间管理受限的(注意,由于Word自动对齐,100行之后会比之前多一个缩进,所以while循环的‘{}’没有对齐);

104行:将仿真成员设置为时间管控成员;

105-113行:等待RTI同意将该仿真成员设置为时间管控成员;

115行:打开异步消息开关;

122行:仿真周期设置为1秒;这里的逻辑时间1对应物理时间的1秒(假设设置为2,则1个逻辑时间单位对应物理时间的0.5秒,2个逻辑时间单位对应仿真周期1秒);

127-180行:每隔1秒循环推进仿真,直到中断退出仿真;

150行:发送飞机在下一时刻的二维态势信息(如果采用RO消息也可以发送当前时刻的态势,依仿

真模型而定);

163行:将仿真请求推进到下一步;

164-172行:等待RTI同意该仿真成员推进到下一步;

183-188行:退出联邦执行,不再参加仿真;

190-201行:销毁联邦。如果是最后一个仿真成员执行该操作,则整个仿真结束。

                                                              表4.4  C++时间管理示例:TimeManagement.cpp

  1. #include "HwFederateAmbassador.hh"
  2.  
  3. #include <RTI.hh>
  4. #include <fedtime.hh>
  5. #include <unistd.h> //for usleep
  6. #include <stdlib.h> //for rand
  7. #include <iostream>
  8. using namespace std;
  9.  
  10. RTI::ObjectHandle            g_hInstance1, g_hInstance2, g_hInstance3;
  11. RTI::AttributeHandle        g_hxPos;
  12. RTI::AttributeHandle        g_hyPos;
  13.  
  14. RTIfedTime                    g_currentTime = 0.0;
  15. bool                           g_bConstrained = false;
  16. bool                           g_bRegulation = false;
  17. bool                           g_granted = false;
  18.  
  19. int hw_main(int argc, char *argv[])
  20. {
  21.     const char *federationExecutionName = "TimeManagementExample";
  22.     const char *FEDfile = "tracer.fed";
  23.  
  24.     char federateName[50];
  25.     cout << "Please input the federate name: ";
  26.     cin >> federateName;
  27.  
  28.     RTI::RTIambassador       rti;
  29.     HwFederateAmbassador     fedAmb;
  30.     RTIfedTime               lookahead = 1.0;
  31.  
  32.     try {
  33.         RTI::FederateHandle      federateId;
  34.  
  35.         try {
  36.             rti.createFederationExecution(federationExecutionName, FEDfile);
  37.         }
  38.         catch ( RTI::FederationExecutionAlreadyExists& e ) {
  39.             //According to the HLA standard, only the first federate can call this service succesfully.
  40.             //cerr << "FED_HW: Note: Federation execution already exists." << e << endl;
  41.         } catch ( RTI::Exception& e ) {
  42.             cerr << "FED_HW: ERROR:" << e << endl;
  43.             return -1;
  44.         }
  45.  
  46.         try {
  47.             federateId = rti.joinFederationExecution(federateName, federationExecutionName, &fedAmb);
  48.         } catch (RTI::FederateAlreadyExecutionMember& e) {
  49.             cerr << "FED_HW: ERROR: " << argv[1]
  50.                  << " already exists in the Federation Execution "
  51.                  << federationExecutionName << "." << endl;
  52.             cerr << e << endl;
  53.             return -1;
  54.         } catch (RTI::FederationExecutionDoesNotExist&) {
  55.             cerr << "FED_HW: ERROR: Federation Execution "
  56.                  << "does not exists."<< endl;
  57.             return -1;
  58.         } catch ( RTI::Exception& e ) {
  59.             cerr << "FED_HW: ERROR:" << e << endl;
  60.             return -1;
  61.         }
  62.  
  63.         ///////////////////////////////////////////////////////////
  64.  
  65.         RTI::ObjectClassHandle hPlaneClass = rti.getObjectClassHandle("plane");
  66.         g_hxPos = rti.getAttributeHandle("xPos", hPlaneClass);
  67.         g_hyPos = rti.getAttributeHandle("yPos", hPlaneClass);
  68.  
  69.         RTI::AttributeHandleSet *theAttributes;
  70.         theAttributes = RTI::AttributeHandleSetFactory::create(2);
  71.  
  72.         theAttributes->add(g_hxPos);
  73.         theAttributes->add(g_hyPos);
  74.  
  75.         rti.publishObjectClass(hPlaneClass, *theAttributes);
  76.         rti.subscribeObjectClassAttributes(hPlaneClass, *theAttributes);
  77.  
  78.         theAttributes->empty();
  79.         delete theAttributes;
  80.  
  81.         //register one plane
  82.         g_hInstance1 = rti.registerObjectInstance(hPlaneClass);
  83.         //register 2nd plane
  84.         g_hInstance2 = rti.registerObjectInstance(hPlaneClass);
  85.         //register 3rd plane
  86.         g_hInstance3 = rti.registerObjectInstance(hPlaneClass);
  87.     } catch ( RTI::Exception& e ) {
  88.         cerr << "FED_HW: ERROR:" << e << endl;
  89.         return -1;
  90.     }
  91.  
  92.     try {
  93.         rti.enableTimeConstrained();
  94.         while(!g_bConstrained) {
  95.             //use tick
  96.             //    RTI.rid: ';; UsingTickSwitch On'
  97.             rti.tick(0.001, 1.0);
  98.  
  99.             //don't use tick
  100.             //    RTI.rid: ';; UsingTickSwitch Off'
  101.             //usleep(1000); //1 millisecond
  102.         }
  103.  
  104.         rti.enableTimeRegulation((RTIfedTime)0.0, lookahead);
  105.         while(!g_bRegulation) {
  106.             //use tick
  107.             //    RTI.rid: ';; UsingTickSwitch On'
  108.             rti.tick(0.001, 1.0);
  109.  
  110.             //don't use tick
  111.             //    RTI.rid: ';; UsingTickSwitch Off'
  112.             //usleep(1000); //1 millisecond
  113.         }
  114.  
  115.         rti.enableAsynchronousDelivery();
  116.     } catch ( RTI::Exception& e ) {
  117.         cerr << "FED_HW: ERROR:" << e << endl;
  118.         return -1;
  119.     }
  120.  
  121.     try {
  122.         RTIfedTime intervalTime = 1.0;
  123.         int xPos, yPos;
  124.         const char *tag = "C++";
  125.         int step = 0;
  126.  
  127.         while(1) {
  128.             step++;
  129.             cout << "Step: " << step << endl;
  130.  
  131.             xPos=rand();
  132.             yPos=rand();
  133.  
  134.             RTI::AttributeHandleValuePairSet* pAttrs = NULL;
  135.             pAttrs = RTI::AttributeSetFactory::create (2);
  136.  
  137.             /* 如果两个仿真成员都是C++程序,则使用下面两条语句即可 */
  138.             //pAttrs->add(g_hxPos, (char*)&xPos, sizeof(int));
  139.             //pAttrs->add(g_hyPos, (char*)&yPos, sizeof(int));
  140.  
  141.             /* 如果两个仿真成员采用不同语言编程,则应转为字符串再发送 */
  142.             char s[20];
  143.             sprintf(s, "%d\0", xPos);
  144.             pAttrs->add(g_hxPos, (char*)s, strlen(s));
  145.  
  146.             sprintf(s, "%d\0", yPos);
  147.             pAttrs->add(g_hyPos, (char*)s, strlen(s));
  148.  
  149.             try {
  150.                 rti.updateAttributeValues(g_hInstance1, *pAttrs, g_currentTime + lookahead, tag);
  151.             } catch(...) {
  152.                 cerr << "Error: send interaction" << endl;
  153.             }
  154.  
  155.             pAttrs->empty();
  156.             delete pAttrs;
  157.  
  158.             //-----------------------------------------------------------
  159.             RTIfedTime targetTime = g_currentTime + intervalTime;
  160.             cout << "This federate will advance to " << targetTime << endl;
  161.  
  162.             try {
  163.                 rti.timeAdvanceRequest(targetTime);
  164.                 while(!g_granted) {
  165.                     //use tick
  166.                     //    RTI.rid: ';; UsingTickSwitch On'
  167.                     rti.tick(0.001, 1.0);
  168.  
  169.                     //don't use tick
  170.                     //    RTI.rid: ';; UsingTickSwitch Off'
  171.                     //usleep(1000); //1 millisecond
  172.                 }
  173.  
  174.                 g_granted = false;
  175.                 cout << "The federate has advanced to " << g_currentTime << endl << endl;
  176.             } catch ( RTI::Exception& e ) {
  177.                 cerr << "FED_HW: ERROR:" << e << endl;
  178.                 return -1;
  179.             }
  180.         }
  181.  
  182.         //After the program exits, the RTI will automatically calls 'resignFederationExecution' and 'destroyFederationExecution'. Of course, you can write them for yourself.
  183.         try {
  184.             rti.resignFederationExecution( RTI::DELETE_OBJECTS_AND_RELEASE_ATTRIBUTES );
  185.         } catch ( RTI::Exception& e ) {
  186.             cerr << "FED_HW: ERROR:" << e << endl;
  187.             return -1;
  188.         }
  189.  
  190.         try {
  191.             rti.destroyFederationExecution( federationExecutionName );
  192.         } catch ( RTI::FederatesCurrentlyJoined& /* e */ ) {
  193.             cerr << "FED_HW: FederatesCurrentlyJoined" << endl;
  194.             return 0;
  195.         } catch ( RTI::FederationExecutionDoesNotExist& /* e */) {
  196.             cerr << "FED_HW: FederationExecutionDoesNotExist" << endl;
  197.             return 0;
  198.         } catch ( RTI::Exception& e ) {
  199.             cerr << "FED_HW: ERROR:" << e << endl;
  200.             return -1;
  201.         }
  202.     } catch (RTI::ConcurrentAccessAttempted& e) {
  203.         cerr << e << endl;
  204.         return -1;
  205.     } catch ( RTI::Exception& e ) {
  206.         cerr << "FED_HW: ERROR:" << e << endl;
  207.         return -1;
  208.     }
  209.  
  210.     return 0;
  211. }
  212.  
  213. int
  214. main(int argc, char** argv)
  215. {
  216.     return hw_main(argc, argv);
  217. }

       HwFederateAmbassador.cpp代码说明:

7-14行:定义全局变量,由两个线程共享;

16-26行:将发现的飞机输出到终端;

28-89行:将收到的飞机态势信息输出到终端;

91-101行:RTI同意将仿真成员设置为时间管控成员;

103-113行:RTI同意将仿真成员设置为时间管理受限的成员;

115-125行:RTI同意仿真成员推进到下一步。

                                                              表4.5  C++时间管理示例:HwFederateAmbassador.cpp

  1. #include "fedtime.hh"
  2. #include "HwFederateAmbassador.hh"
  3. #include <stdlib.h> //atoi
  4. #include <iostream>
  5. using namespace std;
  6.  
  7. extern RTI::ObjectHandle               g_hInstance1, g_hInstance2, g_hInstance3;
  8. extern RTI::AttributeHandle           g_hxPos;
  9. extern RTI::AttributeHandle           g_hyPos;
  10.  
  11. extern RTIfedTime                       g_currentTime;
  12. extern bool                              g_bConstrained;
  13. extern bool                              g_bRegulation;
  14. extern bool                              g_granted;
  15.  
  16. void HwFederateAmbassador::discoverObjectInstance (
  17.     RTI::ObjectHandle          theObject,      // supplied C1
  18.     RTI::ObjectClassHandle     theObjectClass, // supplied C1
  19.     const char *          theObjectName)  // supplied C4
  20. throw (
  21.     RTI::CouldNotDiscover,
  22.     RTI::ObjectClassNotKnown,
  23.     RTI::FederateInternalError)
  24. {
  25.     cout << "discoverObjectInstance: " << theObject << "," << theObjectClass << "," << theObjectName << endl;
  26. }
  27.  
  28. void HwFederateAmbassador::reflectAttributeValues (
  29.     RTI::ObjectHandle                 theObject,     // supplied C1
  30.     const RTI::AttributeHandleValuePairSet& theAttributes, // supplied C4
  31.     const RTI::FedTime&                     theTime,       // supplied C1
  32.     const char                             *theTag,        // supplied C4
  33.     RTI::EventRetractionHandle        theHandle)     // supplied C1
  34. throw (
  35.     RTI::ObjectNotKnown,
  36.     RTI::AttributeNotKnown,
  37.     RTI::FederateOwnsAttributes,
  38.     RTI::InvalidFederationTime,
  39.     RTI::FederateInternalError)
  40. {
  41.     //call the next service.
  42.     reflectAttributeValues(theObject, theAttributes, theTag);
  43. }
  44.  
  45. void HwFederateAmbassador::reflectAttributeValues (
  46.     RTI::ObjectHandle                 theObject,     // supplied C1
  47.     const RTI::AttributeHandleValuePairSet& theAttributes, // supplied C4
  48.     const char                             *theTag)        // supplied C4
  49. throw (
  50.     RTI::ObjectNotKnown,
  51.     RTI::AttributeNotKnown,
  52.     RTI::FederateOwnsAttributes,
  53.     RTI::FederateInternalError)
  54. {
  55.     cout << "reflectAttributeValues: " << theObject << endl;
  56.     RTI::AttributeHandle attrHandle;
  57.     RTI::ULong           valueLength;
  58.  
  59.     int value;
  60.     char str[20];
  61.  
  62.     for (int i = 0; i < theAttributes.size(); i++) {
  63.         attrHandle = theAttributes.getHandle( i );
  64.  
  65.         if(attrHandle == g_hxPos) {
  66.             /* 如果两个仿真成员都是C++程序,则使用下面语句即可 */
  67.             //theAttributes.getValue(i, (char*)&value, valueLength);
  68.  
  69.             /* 如果两个仿真成员采用不同语言编程,则发送方应使用字符串发送,接收方应将接收到的字符串转为整数 */
  70.             theAttributes.getValue(i, (char*)str, valueLength);
  71.             value = atoi(str);
  72.  
  73.         } else if(attrHandle == g_hyPos) {
  74.             /* 如果两个仿真成员都是C++程序,则使用下面语句即可 */
  75.             //theAttributes.getValue(i, (char*)&value, valueLength);
  76.  
  77.             /* 如果两个仿真成员采用不同语言编程,则发送方应使用字符串发送,接收方应将接收到的字符串转为整数 */
  78.             theAttributes.getValue(i, (char*)str, valueLength);
  79.             value = atoi(str);
  80.  
  81.         } else {
  82.             cout << "Receive wrong parameter handle." << endl;
  83.         }
  84.  
  85.         cout << "    <" << attrHandle << "," << value << ">" << endl;
  86.     }
  87.  
  88.     cout << "    tag:" << theTag << endl;
  89. }
  90.  
  91. void HwFederateAmbassador::timeRegulationEnabled (
  92.     const  RTI::FedTime& theFederateTime) // supplied C4
  93. throw (
  94.     RTI::InvalidFederationTime,
  95.     RTI::EnableTimeRegulationWasNotPending,
  96.     RTI::FederateInternalError)
  97. {
  98.     g_currentTime = theFederateTime;
  99.     g_bRegulation = true;
  100.     cout << "timeRegulationEnabled: " << theFederateTime << endl;
  101. }
  102.  
  103. void HwFederateAmbassador::timeConstrainedEnabled (
  104.     const RTI::FedTime& theFederateTime) // supplied C4
  105. throw (
  106.     RTI::InvalidFederationTime,
  107.     RTI::EnableTimeConstrainedWasNotPending,
  108.     RTI::FederateInternalError)
  109. {
  110.     g_currentTime = theFederateTime;
  111.     g_bConstrained = true;
  112.     cout << "timeRegulationEnabled: " << theFederateTime << endl;
  113. }
  114.  
  115. void HwFederateAmbassador::timeAdvanceGrant (
  116.     const RTI::FedTime& theTime) // supplied C4
  117. throw (
  118.     RTI::InvalidFederationTime,
  119.     RTI::TimeAdvanceWasNotInProgress,
  120.     RTI::FederateInternalError)
  121. {
  122.     g_currentTime = theTime;
  123.     g_granted = true;
  124.     cout << "timeAdvanceGrant: " << theTime << endl;
  125. }

 

4.4.4编译运行   

4.4.4.1编译

       在编译时要创建一个Makefile文件,参考4.3.4.1节把第10行的“Chat”改成“TimeManagement”,第13行的“chat”改成“timemanagement”即可。

       执行下列命令生成可执行程序timemanagement。

       make

4.4.4.2测试运行

       测试项目:在银河麒麟操作系统上运行2个GNU C++仿真成员,两个仿真成员启动后尽可能快地向前推进;测试KY-RTI的HLA基本服务功能,特别是时间管理同步功能。

       测试步骤:

       第1步:修改RTI.rid,打开tick开关。

       本程序使用了tick服务,因此需要打开tick开关。查看当前目录下是否有RTI.rid,若没有则运行程序后会在当前目录下自动生成RTI.rid。将RTI.rid文件中的“;; UsingTickSwitch Off”改为“;; UsingTickSwitch On”。

       第2步:启动KY-RTI。注意,KY-RTI的IP地址和端口号要与RTI.rid一致。

       第3步:开启两个终端,运行2个仿真成员,开始仿真。运行程序命令:

       ./time

       测试结果表明:KY-RTI运行效率高,时间管理同步功能强。

       测试说明:

       (1)图4.7是KY-RTI的运行界面。可以看到,联邦名称为“TimeManagementExample”,两个名为“Air01”、“Air02”的仿真成员先后加入仿真;当运行一段时间之后,两个仿真成员被Ctrl+C中断执行,KY-RTI的监控器发现了二者的异常退出。当两个仿真成员都退出后,KY-RTI认为整个仿真结束,联邦被摧毁,KY-RTI清理现场,准备开始下一次仿真。

       (2)图4.8是仿真成员“Air01”被Ctrl+C中断执行时的界面。之前,它能收到仿真成员“Air02”发送的二维态势信息。

       (3)图4.9是仿真成员“Air02”被Ctrl+C中断执行时的界面。之前,只显示它自己的运行信息,没有收到其他仿真成员的二维态势信息。

       (4)当仿真成员“Air01”启动后,在显示众多信息的情况下,瞬间运行到上万步;再启动仿真成员“Air02”,两者同步推进;图4.8显示的仿真时间为40412,图4.9显示的仿真时间为51420,相差10000多,其实就是一瞬间运行了上万步。

麒麟KY-RTI分布仿真技术:第四章 C++程序设计

 

                                                              图4.7 KY-RTI运行界面

麒麟KY-RTI分布仿真技术:第四章 C++程序设计

 

                                                              图4.8 使用时间管理服务的仿真成员Air01

麒麟KY-RTI分布仿真技术:第四章 C++程序设计

 

                                                              图4.9 使用时间管理服务的仿真成员Air02

 

4.4.5 tick服务/非tick服务的切换

       TimeManagement.cpp使用了tick服务编程方法。下列代码在TimeManagement.cpp中一共出现了3处,使用tick服务来获取RTI的回调消息,如果没有收到,则循环调用tick服务,直到收到回调消息。

       然而,如果把下面代码中的tick语句注释掉,换成后面的usleep语句,程序同样能够正常运行,于是程序就变成非tick服务方式。

                                                              表4.6  tick/usleep代码切换

  1.                     //use tick
  2.                     //    RTI.rid: ';; UsingTickSwitch On'
  3.                     rti.tick(0.001, 1.0);
  4.  
  5.                     //don't use tick
  6.                     //    RTI.rid: ';; UsingTickSwitch Off'
  7.                     //usleep(1000); //1 millisecond

       对本程序而言,这两种编程方法并没有本质区别,甚至非tick服务方式效率更高。但这两种方式在编程时还是有一些细节要注意,以表4.7中TimeManagement.cpp中的下列代码段为例,如果把第12行代码放到第1行与第2行之间,变成表4.8,那么对于tick服务来说,不会有任何问题;但改成usleep则可能while循环会一直等不到g_granted标识设置为真。

                                                              表4.7  tick/usleep代码切换不会导致问题

  1.                 rti.timeAdvanceRequest(targetTime);
  2.                 while(!g_granted) {
  3.                     //use tick
  4.                     //    RTI.rid: ';; UsingTickSwitch On'
  5.                     rti.tick(0.001, 1.0);
  6.  
  7.                     //don't use tick
  8.                     //    RTI.rid: ';; UsingTickSwitch Off'
  9.                     //usleep(1000); //1 millisecond
  10.                 }
  11.  
  12.                 g_granted = false;

       因为主线程和回调线程是2个不同的线程,在表4.8中,当执行完第1行语句之后,RTI就可以给仿真成员发回调消息,回调线程收到回调消息后将全局变量g_granted变成true;当主线程执行第2条语句之后,g_granted变成false,则主线程再执行while语句会循环等待,因为回调服务已经处理过了,再也不会有新的回调服务来将g_granted变成true。

       这里所说的问题不是KY-RTI特有的问题,而是不采用tick服务的仿真系统可能普遍存在的问题,譬如采用IEEE1516标准的RTI来开发仿真系统,一般都不使用tick服务(IEEE1516标准中称之为evokeCallback)。

                                                              表4.8  tick/usleep代码切换潜在问题

  1.                 rti.timeAdvanceRequest(targetTime);
  2.                 g_granted = false;
  3.                 while(!g_granted) {
  4.                     //use tick
  5.                     //    RTI.rid: ';; UsingTickSwitch On'
  6.                     rti.tick(0.001, 1.0);
  7.  
  8.                     //don't use tick
  9.                     //    RTI.rid: ';; UsingTickSwitch Off'
  10.                     //usleep(1000); //1 millisecond
  11.                 }

麒麟RTI软件KY-RTI的Linux、Windows版本和源码请联系作者:[email protected]

 

麒麟KY-RTI分布仿真技术:前 言

麒麟KY-RTI分布仿真技术:第一章 简介

麒麟KY-RTI分布仿真技术:第二章 系统安装

麒麟KY-RTI分布仿真技术:第三章 KY-OMT对象模型模板工具

麒麟KY-RTI分布仿真技术:第四章 C++程序设计

麒麟KY-RTI分布仿真技术:第五章 Qt程序设计

麒麟KY-RTI分布仿真技术:第六章 Java程序设计

麒麟KY-RTI分布仿真技术:第七章 Visual C++程序设计

麒麟KY-RTI分布仿真技术:第八章 Visual C#程序设计

麒麟KY-RTI分布仿真技术:第九章 综合演示

相关文章:

  • 2021-09-10
  • 2021-08-10
  • 2021-05-27
  • 2021-08-31
  • 2021-04-27
  • 2021-10-23
  • 2021-09-14
猜你喜欢
  • 2021-07-04
  • 2021-06-28
  • 2021-04-26
  • 2022-01-12
  • 2021-11-03
  • 2021-07-15
相关资源
相似解决方案