相比起來,Viewer聯邦與transport聯邦很相似,都是TAR方式推進時間的,步長都是1.0,lookahead不同,Viewer的lookahead是0.1
程序結構也很相似。 同樣,Viewer沒有實質性地參與聯邦運行。這點與transport聯邦不同。
主要的流程都在mainThread函數中:
(1)讀取配置數據,產生聯邦,加入聯邦;
(2)使能時間調節(爲了使其他聯邦運行得足夠慢)和時間受限;
(3)獲取各種handles,訂閱。Viewer聯邦訂閱瞭如下屬性和交互:
_rti.subscribeObjectClassAttributesPassively(_BoatClass, _BoatAttributes);
//changing these subscriptions to active causes the Serving.type and state
//attributes to be updated
_rti.subscribeObjectClassAttributes(_ServingClass, _ServingAttributes);
_rti.subscribeObjectClassAttributes(_ChefClass, _ChefAttributes);
_rti.subscribeObjectClassAttributes(_DinerClass, _DinerAttributes);
_rti.subscribeInteractionClass(_SimulationEndsClass);
(4)在第一個同步點ReadyToPopulate完成同步;
(5)在第二個同步點ReadyToRun完成同步;
(6)進入主循環(與transport聯邦的主循環完全一樣):
//advance time in steps until SimulationEnds received
_targetTime = new LogicalTimeDouble(0.0);
_targetTime.setTo(_logicalTime); //設置要TAR的時間點初始時間爲0.0
timeLoop:
while (!_simulationEndsReceived) {
//advance by a step
_targetTime.increaseBy(_advanceInterval); //時間請求按步長進行,每次1.0
_userInterface.setTimeStateAdvancing();
_userInterface.post("This is before TAR: "+_targetTime);
_rti.timeAdvanceRequest(_targetTime); //時間推進請求TAR
//chew through all the events we receive from the RTI
boolean wasTimeAdvanceGrant;
do {
Callback callback = _callbackQueue.dequeue(); //獲取外部時間隊列中的第一個事件(注意callbackQueue是排序的)
wasTimeAdvanceGrant = callback.dispatch(); //通過調用該事件的dispatch函數來處理事件,並返回是否是time grant的布爾量
if (_simulationEndsReceived) break timeLoop; //如果是SimulationEnds交互則推出外層循環
} while (!wasTimeAdvanceGrant); //直到收到time grant事件
updateInternalStateAtNewTime(); //在新的time grant時間點,更新所有對象實例(transport聯邦發佈的Boat和Serving)的屬性
}
(8)在第三個同步點ReadyToResign完成同步。
--------------------------------------------------------------------------------------------------------------------------------
下面是viewer運行的log:
RTIambassador created
Federation execution restaurant_1 already exists.
Joined as federate 4
Enabling time constraint...
...constraint enabled at time<0.0>
Enabling time regulation...
...regulation enabled at time<0.0>
Waiting for ReadyToPopulate...
...federation synchronized.
Waiting for ReadyToRun...
TAR request attime<1.0> //TAR 1.0
This is DiscoverObjectInstanceCallback's dispatch C_5_0
This is DiscoverObjectInstanceCallback's dispatch D_6_0
This is DiscoverObjectInstanceCallback's dispatch B_2_0
This is DiscoverObjectInstanceCallback's dispatch D_6_1
This is DiscoverObjectInstanceCallback's dispatch D_6_2
This is DiscoverObjectInstanceCallback's dispatch D_6_3
This is DiscoverObjectInstanceCallback's dispatch D_6_4
This is DiscoverObjectInstanceCallback's dispatch D_6_5
This is DiscoverObjectInstanceCallback's dispatch C_5_1
This is DiscoverObjectInstanceCallback's dispatch C_5_2
This is DiscoverObjectInstanceCallback's dispatch B_2_1
This is DiscoverObjectInstanceCallback's dispatch C_5_3
This is DiscoverObjectInstanceCallback's dispatch B_2_2
This is DiscoverObjectInstanceCallback's dispatch B_2_3
This is DiscoverObjectInstanceCallback's dispatch B_2_4
This is DiscoverObjectInstanceCallback's dispatch B_2_5
This is DiscoverObjectInstanceCallback's dispatch B_2_6
This is DiscoverObjectInstanceCallback's dispatch B_2_7
This is DiscoverObjectInstanceCallback's dispatch C_5_4
This is DiscoverObjectInstanceCallback's dispatch C_5_5
//以上爲註冊對象類實例被viewer聯邦發現
This is ReflectAttributeValuesEvent's dispatch 106 se.pitch.prti.prti302@bf5555
This is ReflectAttributeValuesEvent's dispatch 109 se.pitch.prti.prti302@1ec58a
This is ReflectAttributeValuesEvent's dispatch 105 se.pitch.prti.prti302@c4d04d
This is ReflectAttributeValuesEvent's dispatch 119 se.pitch.prti.prti302@19fe451
This is ReflectAttributeValuesEvent's dispatch 124 se.pitch.prti.prti302@3f96ee
This is ReflectAttributeValuesEvent's dispatch 120 se.pitch.prti.prti302@32bd65
This is ReflectAttributeValuesEvent's dispatch 123 se.pitch.prti.prti302@bcb23e
This is ReflectAttributeValuesEvent's dispatch 122 se.pitch.prti.prti302@b1a4e2
This is ReflectAttributeValuesEvent's dispatch 117 se.pitch.prti.prti302@9903f4
This is ReflectAttributeValuesEvent's dispatch 121 se.pitch.prti.prti302@19ecd80
This is ReflectAttributeValuesEvent's dispatch 116 se.pitch.prti.prti302@11c0d60
This is ReflectAttributeValuesEvent's dispatch 118 se.pitch.prti.prti302@1c783c5
This is ReflectAttributeValuesEvent's dispatch 115 se.pitch.prti.prti302@1cd280b
This is ReflectAttributeValuesEvent's dispatch 107 se.pitch.prti.prti302@b0095d
This is ReflectAttributeValuesEvent's dispatch 114 se.pitch.prti.prti302@1be2893
This is ReflectAttributeValuesEvent's dispatch 112 se.pitch.prti.prti302@1d87b85
This is ReflectAttributeValuesEvent's dispatch 113 se.pitch.prti.prti302@102b2b6
This is ReflectAttributeValuesEvent's dispatch 111 se.pitch.prti.prti302@1e1962d
This is ReflectAttributeValuesEvent's dispatch 108 se.pitch.prti.prti302@17779e3
This is ReflectAttributeValuesEvent's dispatch 110 se.pitch.prti.prti302@98ce7e
This is ReflectAttributeValuesEvent's dispatch 106 se.pitch.prti.prti302@1cb7a1
This is ReflectAttributeValuesEvent's dispatch 109 se.pitch.prti.prti302@b20352
This is ReflectAttributeValuesEvent's dispatch 112 se.pitch.prti.prti302@c6eff5
This is ReflectAttributeValuesEvent's dispatch 111 se.pitch.prti.prti302@1cfb802
This is ReflectAttributeValuesEvent's dispatch 108 se.pitch.prti.prti302@111ded2
This is ReflectAttributeValuesEvent's dispatch 110 se.pitch.prti.prti302@1415056
This is ReflectAttributeValuesEvent's dispatch 105 se.pitch.prti.prti302@b09697
This is ReflectAttributeValuesEvent's dispatch 114 se.pitch.prti.prti302@1db484d
This is ReflectAttributeValuesEvent's dispatch 124 se.pitch.prti.prti302@1a1c42f
This is ReflectAttributeValuesEvent's dispatch 120 se.pitch.prti.prti302@120dbf3
This is ReflectAttributeValuesEvent's dispatch 113 se.pitch.prti.prti302@13e75a5
This is ReflectAttributeValuesEvent's dispatch 118 se.pitch.prti.prti302@943dc4
//以上爲訂閱對象類實例相關屬性的反射
This is GrantEvent's dispatch time<1.0> //time grant to 1.0
TAR request attime<2.0> //TAR to 2.0
This is ReflectAttributeValuesEvent's dispatch 119 se.pitch.prti.prti302@510e39
This is ReflectAttributeValuesEvent's dispatch 123 se.pitch.prti.prti302@1295fe8
This is ReflectAttributeValuesEvent's dispatch 121 se.pitch.prti.prti302@115d06c
This is ReflectAttributeValuesEvent's dispatch 122 se.pitch.prti.prti302@17dc1cb
This is ReflectAttributeValuesEvent's dispatch 117 se.pitch.prti.prti302@11410e5
This is ReflectAttributeValuesEvent's dispatch 115 se.pitch.prti.prti302@7244ca
This is ReflectAttributeValuesEvent's dispatch 116 se.pitch.prti.prti302@17f11fb
This is ReflectAttributeValuesEvent's dispatch 107 se.pitch.prti.prti302@ebf068
//繼續反射所訂閱的對象類屬性
This is DiscoverObjectInstanceCallback's dispatch S_5_0
//發現新的對象類實例
This is ReflectAttributeValuesEvent's dispatch 124 se.pitch.prti.prti302@1db9852
This is ReflectAttributeValuesEvent's dispatch 125 se.pitch.prti.prti302@3cb075
//繼續反射所訂閱的對象類屬性
This is GrantEvent's dispatch time<2.0> //time grant to 2.0
TAR request attime<3.0> //TAR to 3.0
//以下跟前面的類似
This is ReflectAttributeValuesEvent's dispatch 107 se.pitch.prti.prti302@c16b18
This is ReflectAttributeValuesEvent's dispatch 115 se.pitch.prti.prti302@5e222e
This is ReflectAttributeValuesEvent's dispatch 116 se.pitch.prti.prti302@15fb38
This is ReflectAttributeValuesEvent's dispatch 117 se.pitch.prti.prti302@a7c45e
This is ReflectAttributeValuesEvent's dispatch 119 se.pitch.prti.prti302@4d2af2
This is ReflectAttributeValuesEvent's dispatch 121 se.pitch.prti.prti302@3e97df
This is ReflectAttributeValuesEvent's dispatch 122 se.pitch.prti.prti302@164debb
This is ReflectAttributeValuesEvent's dispatch 123 se.pitch.prti.prti302@15b0333
This is DiscoverObjectInstanceCallback's dispatch S_5_1
This is ReflectAttributeValuesEvent's dispatch 114 se.pitch.prti.prti302@1c1f5b2
This is ReflectAttributeValuesEvent's dispatch 126 se.pitch.prti.prti302@e99ce5
This is GrantEvent's dispatch time<3.0>
TAR request attime<4.0>
This is ReflectAttributeValuesEvent's dispatch 119 se.pitch.prti.prti302@2f356f
This is ReflectAttributeValuesEvent's dispatch 123 se.pitch.prti.prti302@15d252d
This is ReflectAttributeValuesEvent's dispatch 121 se.pitch.prti.prti302@33b121
This is ReflectAttributeValuesEvent's dispatch 122 se.pitch.prti.prti302@1b22920
This is ReflectAttributeValuesEvent's dispatch 117 se.pitch.prti.prti302@1aa2c23
This is ReflectAttributeValuesEvent's dispatch 115 se.pitch.prti.prti302@137d4a4
This is ReflectAttributeValuesEvent's dispatch 116 se.pitch.prti.prti302@6963d0
This is ReflectAttributeValuesEvent's dispatch 107 se.pitch.prti.prti302@118223d
This is DiscoverObjectInstanceCallback's dispatch S_5_2
This is DiscoverObjectInstanceCallback's dispatch S_5_3
This is ReflectAttributeValuesEvent's dispatch 105 se.pitch.prti.prti302@fa39d7
This is ReflectAttributeValuesEvent's dispatch 128 se.pitch.prti.prti302@1eb904d
This is ReflectAttributeValuesEvent's dispatch 120 se.pitch.prti.prti302@4eeaaf
This is ReflectAttributeValuesEvent's dispatch 127 se.pitch.prti.prti302@111bfbc
This is GrantEvent's dispatch time<4.0>
-------------------------------------------------------------------------------------------------------------------
從上面Viewer聯邦運行的log可以看到,Viewer的主要功能就是發現所訂閱的對象類實例,並反射這些對象類實例的相關屬性。並根據這些屬性,在ViewerFrame界面上動態顯示聯邦的運行情況。