-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelloWorld.cpp
More file actions
934 lines (851 loc) · 29.7 KB
/
HelloWorld.cpp
File metadata and controls
934 lines (851 loc) · 29.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
#define GalHLADebug 1
#define MYTIMEAPI
#include "Country.hh"
#ifndef _MSC_VER
#include <stdlib.h>
#include <iostream>
#include <strings.h>
#include <netinet/in.h>
using std::cout;
using std::cerr;
using std::endl;
#else
#include <winsock2.h>
#include <iostream>
using std::cout;
using std::cerr;
using std::endl;
#endif
// the following are for ntohl and cvt_ftof
#if defined(_X86_) && !defined(WIN32)
#include <arpa/inet.h>
#endif
#if defined(__alpha)
#include <arpa/inet.h>
#include <cvt.h>
#endif // __alpha
//-----------------------------------------------------------------
// Static variable definition
//-----------------------------------------------------------------
RTI::RTIambassador* Country::ms_rtiAmb = NULL;
RTI::ObjectClassHandle Country::ms_countryTypeId = 0;
RTI::AttributeHandle Country::ms_nameTypeId = 0;
RTI::AttributeHandle Country::ms_popTypeId = 0;
RTI::InteractionClassHandle Country::ms_commTypeId = 0;
RTI::ParameterHandle Country::ms_commMsgTypeId = 0;
#ifdef FedTimeInteger
IntegerTime Country::ms_lookahead((long) 1);
#else
RTIfedTime Country::ms_lookahead(1.0);
#endif
char* const Country::ms_countryTypeStr = "Country";
char* const Country::ms_nameTypeStr = "Name" ;
char* const Country::ms_popTypeStr = "Population" ;
char* const Country::ms_commTypeStr = "Communication";
char* const Country::ms_commMsgTypeStr = "Message";
const double Country::ms_growthRate(0.001);
// This is bad form - The size of the array is based on a compile
// directive but the initialization of the array is hard coded for
// a specific number of elements. This will do - it's not worth
// going through the hassle of creating a dynamic array of pointers.
CountryPtr Country::ms_countryExtent[ MAX_HELLOWORLD_FEDERATES] =
{ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
unsigned int Country::ms_extentCardinality = 0;
// Non-Class Constant
const double countryDefaultPopulation = 100;
//-----------------------------------------------------------------
//
// METHOD:
// Country::Country( const char* name, const char* populationStr )
//
// PURPOSE:
// Constructor. The constructor initializes the member data
// with the values passed in and adds this Country instance to
// the Country extenet (collection of all elements of a type).
//
// RETURN VALUES:
// None.
//
// HISTORY:
// 1) Created 11/6/96
// 2) Updated to RTI 1.3 3/26/98
//
//-----------------------------------------------------------------
Country::Country( const char* name, const char* populationStr )
: m_name(NULL),
m_lastTime(*(RTI::FedTimeFactory::makeZero())),
m_TimePlusLookahead(*(RTI::FedTimeFactory::makeZero()))
{
//-----------------------------------------------------------------
// Add this new object to the extent (collection) of all
// Country instances.
//-----------------------------------------------------------------
Country::ms_countryExtent[ Country::ms_extentCardinality++ ] = this;
this->SetName( name );
if ( populationStr && strlen( populationStr ) > 0 )
{
this->SetPopulation( atof( populationStr ) );
}
else
{
this->SetPopulation( countryDefaultPopulation ); // default population
}
}
//-----------------------------------------------------------------
//
// METHOD:
// Country::Country( const char* name, const double& population )
//
// PURPOSE:
// Constructor. The constructor initializes the member data
// with the values passed in and adds this Country instance to
// the Country extenet (collection of all elements of a type).
//
// RETURN VALUES:
// None.
//
// HISTORY:
// 1) Created 11/6/96
// 2) Updated to RTI 1.3 3/26/98
//
//-----------------------------------------------------------------
Country::Country( const char* name, const double& population )
: m_name(NULL),
m_lastTime(*(RTI::FedTimeFactory::makeZero())),
m_TimePlusLookahead(*(RTI::FedTimeFactory::makeZero()))
{
//-----------------------------------------------------------------
// Add this new object to the extent (collection) of all
// Country instances.
//-----------------------------------------------------------------
Country::ms_countryExtent[ Country::ms_extentCardinality++ ] = this;
this->SetName( name );
this->SetPopulation( population );
}
//-----------------------------------------------------------------
//
// METHOD:
// Country::Country()
//
// PURPOSE:
// Constructor. The constructor initializes the member data
// with the default values and adds this Country instance to
// the Country extent (collection of all elements of a type).
//
// This constructor is used when a remote object is discovered.
//
// RETURN VALUES:
// None.
//
// HISTORY:
// 1) Created 11/6/96
// 2) Updated to RTI 1.3 3/26/98
//
//-----------------------------------------------------------------
Country::Country( RTI::ObjectHandle id)
: m_name(NULL),
m_instanceId(id),
m_lastTime(*(RTI::FedTimeFactory::makeZero())),
m_TimePlusLookahead(*(RTI::FedTimeFactory::makeZero()))
{
//-----------------------------------------------------------------
// Add this new object to the extent (collection) of all
// Country instances.
//-----------------------------------------------------------------
Country::ms_countryExtent[ Country::ms_extentCardinality++ ] = this;
this->SetName( NULL );
this->SetPopulation( countryDefaultPopulation ); // default population
}
//-----------------------------------------------------------------
//
// METHOD:
// Country::Country()
//
// PURPOSE:
// Constructor. The constructor initializes the member data
// with the default values and adds this Country instance to
// the Country extenet (collection of all elements of a type).
//
// RETURN VALUES:
// None.
//
// HISTORY:
// 1) Created 11/6/96
// 2) Updated to RTI 1.3 3/26/98
//
//-----------------------------------------------------------------
Country::Country()
: m_name(NULL),
m_lastTime(*(RTI::FedTimeFactory::makeZero())),
m_TimePlusLookahead(*(RTI::FedTimeFactory::makeZero()))
{
//-----------------------------------------------------------------
// Add this new object to the extent (collection) of all
// Country instances.
//-----------------------------------------------------------------
Country::ms_countryExtent[ Country::ms_extentCardinality++ ] = this;
this->SetName( NULL );
this->SetPopulation( countryDefaultPopulation ); // default population
}
//-----------------------------------------------------------------
//
// METHOD:
// Country::~Country()
//
// PURPOSE:
// Virtual destructor. Frees memory allocated for m_name and
// removes this instance from the extent.
//
// Note: Removing an element from the extent causes the array
// to be collapsed so as to not have any gaps. Some
// elements will have new indices within the extent
// after this occurs.
//
// RETURN VALUES:
// None.
//
// HISTORY:
// 1) Created 11/6/96
// 2) Updated to RTI 1.3 3/26/98
//
//-----------------------------------------------------------------
Country::~Country()
{
Country *pCountry = NULL;
unsigned int ndx = 0;
//-----------------------------------------------------------------
// Find the position in the extent for this instance. The
// zero'th position always hold the local instance.
//-----------------------------------------------------------------
for ( ndx = 0; ndx < Country::ms_extentCardinality; ndx++ )
{
pCountry = Country::ms_countryExtent[ ndx ];
if ( pCountry && pCountry->GetInstanceId() == this->GetInstanceId() )
{
break;
}
}
if ( pCountry )
{
//-----------------------------------------------------------------
// First thing to do is move the rest of the Country objects
// one position up in the collection and then reduce the
// cardinality by one.
//-----------------------------------------------------------------
for ( unsigned int i=ndx; (i < Country::ms_extentCardinality)
&& (Country::ms_countryExtent[ i ] != NULL); i++ )
{
Country::ms_countryExtent[ i ] = Country::ms_countryExtent[ i+1 ];
}
Country::ms_extentCardinality = Country::ms_extentCardinality - 1;
//-----------------------------------------------------------------
// If the instance was found in the 0th position then this is the
// local Country instance so we should delete it from the federation
// execution.
//-----------------------------------------------------------------
if ( ms_rtiAmb && ndx==0 )
{
//-----------------------------------------------------------------
// this call returns an event retraction handle but we don't
// support event retraction so no need to store it.
//-----------------------------------------------------------------
(void) ms_rtiAmb->deleteObjectInstance( this->GetInstanceId(),
this->GetLastTimePlusLookahead(),
NULL );
}
else
{
//-----------------------------------------------------------------
// Otherwise, this is a remote object that removeObject was called
// on.
//-----------------------------------------------------------------
//We don't need to do anything here
}
}
delete [] m_name;
}
//-----------------------------------------------------------------
//
// METHOD:
// Country* Country::Find( RTI::ObjectHandle objectId )
//
// PURPOSE:
// Looks through the extent to find the Country instance
// with the passed in object Id.
//
// RETURN VALUES:
// Pointer to country object that has the specified
// ObjectHandle.
//
// HISTORY:
// 1) Created 11/6/96
// 2) Updated to RTI 1.3 3/26/98
//
//-----------------------------------------------------------------
Country* Country::Find( RTI::ObjectHandle objectId )
{
Country *pCountry = NULL;
for ( unsigned int i = 0; i < Country::ms_extentCardinality; i++ )
{
pCountry = Country::ms_countryExtent[ i ];
if ( pCountry && pCountry->GetInstanceId() == objectId ) {
break;
} else {
pCountry = NULL;
}
}
return pCountry;
}
//-----------------------------------------------------------------
//
// METHOD:
// Country::Init( const RTI::RTIambassador* rtiAmb )
//
// PURPOSE:
// Sets the member data containing the RTI run-time type
// ids for Country's class and attributes. Stores the
// rtiAmb pointer for updating state etc.
//
// RETURN VALUES:
// None.
//
// HISTORY:
// 1) Created 11/6/96
// 2) Updated to RTI 1.3 3/26/98
//
//-----------------------------------------------------------------
void Country::Init( RTI::RTIambassador* rtiAmb )
{
ms_rtiAmb = rtiAmb;
if ( ms_rtiAmb )
{
//------------------------------------------------------
// Get the RTTI (Meta-Object Protocol MOP) handles
//
// Since the 1.0 RTI does not know anything about your data
// and thus uses Run-Time Type Identification we must ask the
// RTI what to call each of our data elements.
//
//------------------------------------------------------
ms_countryTypeId = ms_rtiAmb->getObjectClassHandle(ms_countryTypeStr);
ms_nameTypeId = ms_rtiAmb->getAttributeHandle( ms_nameTypeStr,
ms_countryTypeId);
ms_popTypeId = ms_rtiAmb->getAttributeHandle( ms_popTypeStr,
ms_countryTypeId);
}
}
//-----------------------------------------------------------------
//
// METHOD:
// void Country::PublishAndSubscribe()
//
// PURPOSE:
// Publishes and Subscribes to Object & Interaction classes
// and their member data.
//
// Note: In most reasonable applications this would be broken
// up into 2 different methods.
//
// RETURN VALUES:
// None.
//
// HISTORY:
// 1) Created 11/6/96
// 2) Updated to RTI 1.3 3/26/98
//
//-----------------------------------------------------------------
void Country::PublishAndSubscribe()
{
if ( ms_rtiAmb )
{
//------------------------------------------------------
// To actually subscribe and publish we need to build
// an AttributeHandleSet that contains a list of
// attribute type ids (AttributeHandle).
//------------------------------------------------------
RTI::AttributeHandleSet *countryAttributes;
countryAttributes = RTI::AttributeHandleSetFactory::create(2);
countryAttributes->add( ms_nameTypeId );
countryAttributes->add( ms_popTypeId );
ms_rtiAmb->subscribeObjectClassAttributes( ms_countryTypeId,
*countryAttributes );
ms_rtiAmb->publishObjectClass( ms_countryTypeId,
*countryAttributes);
countryAttributes->empty();
delete countryAttributes; // Deallocate the memory
//------------------------------------------------------
// Same as above for interactions
//------------------------------------------------------
// Get RTTI info
ms_commTypeId = ms_rtiAmb->getInteractionClassHandle( ms_commTypeStr );
ms_commMsgTypeId = ms_rtiAmb->getParameterHandle( ms_commMsgTypeStr,
ms_commTypeId);
// Declare my Interaction interests
ms_rtiAmb->subscribeInteractionClass( ms_commTypeId );
ms_rtiAmb->publishInteractionClass( ms_commTypeId );
}
}
//-----------------------------------------------------------------
//
// METHOD:
// void Country::Register()
//
// PURPOSE:
// Creates an HLA object instance and registers it with the RTI.
//
// RETURN VALUES:
// None.
//
// HISTORY:
// 1) Created 11/6/96
// 2) Updated to RTI 1.3 3/26/98
//
//-----------------------------------------------------------------
void Country::Register()
{
if ( ms_rtiAmb )
{
//---------------------------------------------------------
// Register my country object with the RTI. Registering
// an object with the RTI allows the object to be
// discovered by other federates in the federation
// execution.
//
// Note: Discovery happens after an object is registered
// not after the initial update like in the 1.0 RTI.
//---------------------------------------------------------
m_instanceId =
ms_rtiAmb->registerObjectInstance( this->GetCountryRtiId() );
}
}
//-----------------------------------------------------------------
//
// METHOD:
// void Country::Update( RTIfedTime& newTime )
//
// PURPOSE:
// Update the state of the Country's population based on
// the new time value. The deltaTime is calculated based
// on the last time the Country object was updated and
// the newTime passed in. The deltaTime is multiplied by
// the growth rate and current population to determine the
// number of births in the deltaTime. The population is
// increased by the number of births.
//
// RETURN VALUES:
// None.
//
// HISTORY:
// 1) Created 11/6/96
// 2) Updated to RTI 1.3 3/26/98
//
//-----------------------------------------------------------------
void Country::Update( RTI::FedTime& newTime )
{
//------------------------------------------------------
// we have advanced in time so calculate my next state.
//------------------------------------------------------
RTI::FedTime *pTime = RTI::FedTimeFactory::makeZero();
(*pTime) = newTime;
(*pTime) -= this->GetLastTime();
// Set last time to new time
this->SetLastTime( newTime );
if ( !(pTime->isZero()))
{
SetPopulation( GetPopulation() +
(GetPopulation()*ms_growthRate) );
}
if ( ms_rtiAmb )
{
//------------------------------------------------------
// Update state of country
//------------------------------------------------------
try
{
//------------------------------------------------------
// In order to send the values of our attributes, we must
// construct an AttributeHandleValuePairSet (AHVPS) which
// is a set comprised of attribute handles, values, and
// the size of the values. CreateNVPSet() is a method
// defined on the Country class - it is not part of the RTI.
// Look inside the method to see how to construct an AHVPS
//------------------------------------------------------
RTI::AttributeHandleValuePairSet* pNvpSet = this->CreateNVPSet();
//------------------------------------------------------
// Send the AHVPS to the federation.
//
// this call returns an event retraction handle but we
// don't support event retraction so no need to store it.
//------------------------------------------------------
(void) ms_rtiAmb->updateAttributeValues( this->GetInstanceId(),
*pNvpSet,
this->GetLastTimePlusLookahead(),
NULL );
// Must free the memory
pNvpSet->empty();
delete pNvpSet;
}
catch ( RTI::Exception& e )
{
cerr << "FED_HW: Error:" << e << endl;
}
// Periodically send an interaction to tell everyone Hello
static int periodicMessage = 0;
if ( (periodicMessage++%100) == 0 )
{
RTI::ParameterHandleValuePairSet* pParams = NULL;
//------------------------------------------------------
// Periodically stimulate an update of the "Name"
// attribute for the benefit of late-arriving federates.
// It would be more correct to use
// "requestClassAttributeValueUpdate" and
// "provideAttributeValueUpdate", but let's keep things
// simple.
//------------------------------------------------------
hasNameChanged = RTI::RTI_TRUE;
//------------------------------------------------------
// Set up the data structure required to push this
// objects's state to the RTI. The
// ParameterHandleValuePairSet is similar to the AHVPS
// except it contains ParameterHandles instead of
// AttributeHandles.
//------------------------------------------------------
pParams = RTI::ParameterSetFactory::create( 1 );
char *pMessage = "Hello World!";
pParams->add( this->GetMessageRtiId(),
(char*) pMessage,
((strlen(pMessage)+1)*sizeof(char)) );
try
{
//------------------------------------------------------
// this call returns an event retraction handle but we
// don't support event retraction so no need to store it.
//------------------------------------------------------
cerr << "Hello World!" << endl;
(void) ms_rtiAmb->sendInteraction( GetCommRtiId(), *pParams,
this->GetLastTimePlusLookahead(),
NULL );
}
catch ( RTI::Exception& e )
{
cerr << "FED_HW: Error:" << e << endl;
}
//------------------------------------------------------
// Must free the memory:
// ParameterSetFactory::create() allocates memory on
// the heap.
//------------------------------------------------------
delete pParams;
}
}
delete pTime;
}
//-----------------------------------------------------------------
//
// METHOD:
// void Country::Update( const AttributeHandleValuePairSet& theAttributes )
//
// PURPOSE:
// Update the new attribute values. If this is being called
// then this object is either a remote object that was
// discovered or it has some attributes that are owned by
// another federate.
//
// Note: This version does not implement any ownership
// transfer - the above was a generic statement.
//
// RETURN VALUES:
// None.
//
// HISTORY:
// 1) Created 11/6/96
// 2) Updated to RTI 1.3 3/26/98
//
//-----------------------------------------------------------------
void Country::Update( const RTI::AttributeHandleValuePairSet& theAttributes )
{
RTI::AttributeHandle attrHandle;
RTI::ULong valueLength;
// We need to iterate through the AttributeHandleValuePairSet
// to extract each AttributeHandleValuePair. Based on the type
// specified ( the value returned by getHandle() ) we need to
// extract the data from the buffer that is returned by
// getValue().
for ( unsigned int i = 0; i < theAttributes.size(); i++ )
{
attrHandle = theAttributes.getHandle( i );
if ( attrHandle == Country::GetPopulationRtiId() )
{
// When we run this over multiple platforms we will have
// a problem with different endian-ness of platforms. Either
// we need to encode the data using something like XDR or
// provide another mechanism.
double population;
theAttributes.getValue( i, (char*)&population, valueLength );
#if defined(_X86_) || defined(i386)
long x = ntohl(*(long*)&population);
*(long*)&population = ntohl(* (((long*)&population) + 1));
*(((long*)&population)+1) = x;
#elif defined(__alpha)
double x;
cvt_ftof(&population, CVT_IEEE_T, &x, CVT_BIG_ENDIAN_IEEE_T, 0);
population = x;
#endif // __alpha
SetPopulation( (double)population );
}
else if ( attrHandle == Country::GetNameRtiId() )
{
// Same as above goes here...
char name[ 1024 ];
theAttributes.getValue( i, (char*)name, valueLength );
SetName( (const char*)name );
}
}
}
//-----------------------------------------------------------------
//
// METHOD:
// void Country::Update( RTI::InteractionClassHandle theInteraction,
// const RTI::ParameterHandleValuePairSet& theParameters )
//
// PURPOSE:
// Process an interaction.
//
// RETURN VALUES:
// None.
//
// HISTORY:
// 1) Created 11/6/96
// 2) Updated to RTI 1.3 3/26/98
//
//-----------------------------------------------------------------
void Country::Update( RTI::InteractionClassHandle theInteraction,
const RTI::ParameterHandleValuePairSet& theParameters )
{
if ( theInteraction == Country::GetCommRtiId() )
{
RTI::ParameterHandle paramHandle;
RTI::ULong valueLength;
// We need to iterate through the AttributeHandleValuePairSet
// to extract each AttributeHandleValuePair. Based on the type
// specified ( the value returned by getHandle() ) we need to
// extract the data frlom the buffer that is returned by
// getValue().
for ( unsigned int i = 0; i < theParameters.size(); i++ )
{
paramHandle = theParameters.getHandle( i );
if ( paramHandle == Country::GetMessageRtiId() )
{
// When we run this over multiple platforms we will have
// a problem with different endian-ness of platforms. Either
// we need to encode the data using something like XDR or
// provide another mechanism.
char msg[ 1024 ];
theParameters.getValue( i, (char*)msg, valueLength );
#ifndef GalHLADebug
cout << "FED_HW: Interaction Received: " << msg << endl;
#endif
}
else
{
// There must be an error since there should only be
// one parameter to Communication.
cerr << "FED_HW: Error: I seem to have received a parameter for "
<< "interaction class Communication that I don't "
<< "know about." << endl;
}
}
}
else
{
cerr << "FED_HW: Recieved an interaction class I don't know about." << endl;
}
}
//-----------------------------------------------------------------
//
// METHOD:
// void Country::SetName( const char* )
//
// PURPOSE:
// Sets the new value of the name attribute and sets the
// changed flag to true.
//
// RETURN VALUES:
// None.
//
// HISTORY:
// 1) Created 11/6/96
// 2) Updated to RTI 1.3 3/26/98
//
//-----------------------------------------------------------------
void Country::SetName( const char* name)
{
if (m_name != name)
{
// Delete the existing memory
delete [] m_name;
// Allocate appropriate size string and copy data
if ( name && strlen( name ) > 0 )
{
m_name = new char[ strlen(name) + 1 ];
strcpy( m_name, name );
}
else
{
m_name = NULL;
}
}
// Set flag so that when Update( FederateTime ) is called
// we send this new value to the RTI.
hasNameChanged = RTI::RTI_TRUE;
}
//-----------------------------------------------------------------
//
// METHOD:
// void Country::SetPopulation( const double& population)
//
// PURPOSE:
// Sets the new value of the population attribute and sets the
// changed flag to true.
//
// RETURN VALUES:
// None.
//
// HISTORY:
// 1) Created 11/6/96
// 2) Updated to RTI 1.3 3/26/98
//
//-----------------------------------------------------------------
void Country::SetPopulation( const double& population )
{
m_population = population;
// Set flag so that when Update( FederateTime ) is called
// we send this new value to the RTI.
hasPopulationChanged = RTI::RTI_TRUE;
}
//-----------------------------------------------------------------
//
// METHOD:
// RTI::AttributeHandleValuePairSet* Country::CreateNVPSet()
//
// PURPOSE:
// Create a name value pair set (aka handle value pair) for
// the changed attributes of this country object.
//
// RETURN VALUES:
// RTI::AttributeHandleValuePairSet* containing the
// attributes that have changed in the instance of Country.
//
// HISTORY:
// 1) Created 11/6/96
// 2) Updated to RTI 1.3 3/26/98
//
//-----------------------------------------------------------------
RTI::AttributeHandleValuePairSet* Country::CreateNVPSet()
{
RTI::AttributeHandleValuePairSet* pCountryAttributes = NULL;
// Make sure the RTI Ambassador is set.
if ( ms_rtiAmb )
{
//------------------------------------------------------
// Set up the data structure required to push this
// object's state to the RTI.
//------------------------------------------------------
pCountryAttributes = RTI::AttributeSetFactory::create( 2 );
if ( hasNameChanged == RTI::RTI_TRUE )
{
// We don't do any encoding here since the name type
// is a string.
pCountryAttributes->add( this->GetNameRtiId(),
(char*) this->GetName(),
((strlen(this->GetName())+1)*sizeof(char)) );
}
if ( hasPopulationChanged == RTI::RTI_TRUE )
{
// Here we are encoding the double so that it is in a
// common format so that federates on other platforms
// know how to read it.
#if defined(_X86_) || defined(i386)
double tmp;
*((long*)&tmp) = htonl(*(((long*)&this->GetPopulation()) + 1));
*(((long*)&tmp) + 1) = htonl(*((long*)&this->GetPopulation()));
pCountryAttributes->add( this->GetPopulationRtiId(),
(char*)&tmp,
sizeof(double) );
#elif defined(__alpha)
double x;
double pop = this->GetPopulation();
cvt_ftof(&pop, CVT_BIG_ENDIAN_IEEE_T, &x, CVT_IEEE_T, 0);
pCountryAttributes->add( this->GetPopulationRtiId(),
(char*)&x, sizeof(double));
#else
pCountryAttributes->add( this->GetPopulationRtiId(),
(char*) &this->GetPopulation(),
sizeof(double) );
#endif
}
}
// pCountryAttributes is allocated on the heap and must be
// deallocated by the federate.
return pCountryAttributes;
}
//-----------------------------------------------------------------
//
// METHOD:
// ostream &operator << ( ostream &s, Country &v )
//
// PURPOSE:
// Overloaded stream operator for outputing objects contents in
// a readable format.
//
// RETURN VALUES:
// Returns the stream.
//
// HISTORY:
// 1) Created 11/6/96
// 2) Updated to RTI 1.3 3/26/98
//
//-----------------------------------------------------------------
std::ostream &operator << ( std::ostream &s, Country &v )
{
const char* name = v.GetName();
#ifdef FedTimeInteger
IntegerTime t(v.GetLastTime());
#else
RTIfedTime t(v.GetLastTime());
#endif
if (name == 0)
name = "(unknown)";
s << "Name: " << name
<< " Population: " << v.GetPopulation()
<< " Time: " << t;
return s;
}
//-----------------------------------------------------------------
//
// METHOD:
// ostream &operator << ( ostream &s, Country *v )
//
// PURPOSE:
// Overloaded stream operator for outputing objects contents in
// a readable format.
//
// RETURN VALUES:
// Returns the stream.
//
// HISTORY:
// 1) Created 11/6/96
// 2) Updated to RTI 1.3 3/26/98
//
//-----------------------------------------------------------------
ostream &operator << ( ostream &s, Country *v )
{
if ( !v )
return s;
else
{
s << *v;
}
return s;
}