程式如下:是opnet hla 里面的程式。
/* External federate used to test features of HLA-PM */
#include "controller_tutorial.hpp"
#include <fedtime.hh>
#include <stdlib.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <iostream>
#include <iomanip>
#if !defined (_MSC_VER)
# include <netinet/in.h>
# include <unistd.h> /* for sleep */
#else
# include <windows.h> /* for Sleep */
#endif
#include <strstrea.h>
using namespace std;
#include "s_hla_stream.hpp"
/* RTI ambassador shared among the functions of the program */
static RTI::RTIambassador rti_amb;
/* Federate ambassador */
static Master_FA fedAmb;
static int time_regulation_enabled = 0;
static int time_constrained_enabled = 0;
static const char * fedExecName;
static RTI::Boolean timeToQuit = RTI::RTI_FALSE;
static RTIfedTime currentTime (0.0);
static RTIfedTime next_time ()
{ RTIfedTime lookahead;
rti_amb.queryLookahead (lookahead);
return currentTime + lookahead.getTime (); }
static RTI::Boolean timeAdvanceOutstanding = RTI::RTI_FALSE;
static RTI::FederateHandle federateId;
// Interarctions
enum OrderAttributes { Bearing, Speed, NbOrderAttributes };
enum ReplyAttributes { Id, ChangeLat, ChangeLong, NbReplyAttributes };
static const char * const orderAttrTypeStr [] =
{ "bearing", "speed" };
static const char * const replyAttrTypeStr [] =
{ "id", "change_lat", "change_long" };
static RTI::InteractionClassHandle order_handle;
static RTI::ParameterHandle order_parameters [NbOrderAttributes];
static RTI::InteractionClassHandle reply_handle;
static RTI::ParameterHandle reply_parameters [NbReplyAttributes];
/******* Pending Requests ******/
class PendingRequest
{
public:
PendingRequest (void) : next(NULL) {}
virtual ~PendingRequest (void) {}
// apply appropriate behavior
virtual void apply (void) = 0;
static int empty () { return head == (PendingRequest *)NULL; }
static void enqueue (PendingRequest * pr);
static PendingRequest * dequeue ();
private:
class PendingRequest * next;
static PendingRequest * head;
static PendingRequest * tail;
friend class Master_FA;
};
PendingRequest * PendingRequest::head = (PendingRequest *)NULL;
PendingRequest * PendingRequest::tail = (PendingRequest *)NULL;
void
PendingRequest::enqueue (PendingRequest * pr)
{
if (head == (PendingRequest *)NULL)
{
head = pr;
tail = pr;
}
else
{
tail->next = pr;
tail = pr;
}
}
PendingRequest *
PendingRequest::dequeue ()
{
if (head == (PendingRequest *)NULL)
return head;
PendingRequest * result = head;
head = head->next;
if (tail == result)
tail = head;
return result;
}
class ProvideAttributeValueUpdatePR : public PendingRequest
{
public:
ProvideAttributeValueUpdatePR (RTI::ObjectHandle oid,
const RTI::AttributeHandleSet & attr);
virtual ~ProvideAttributeValueUpdatePR (void)
{ if (size) delete [] handleSet; }
virtual void apply (void);
private:
RTI::ObjectHandle objid;
unsigned long size;
RTI::AttributeHandle * handleSet;
};
ProvideAttributeValueUpdatePR::ProvideAttributeValueUpdatePR (RTI::ObjectHandle oid,
const RTI::AttributeHandleSet & attr)
: objid (oid), size (attr.size ())
{
handleSet = new RTI::AttributeHandle [size];
if (handleSet == NULL)
{
cerr << "Unable to create array of attribute handles, can't process attribute update request" << endl;
size = 0;
return;
}
for (int i = 0; i < size; i++)
{
handleSet[i] = attr.getHandle (i);
}
}
void
ProvideAttributeValueUpdatePR::apply ()
{
}
/******* Federate Ambassador **********/
/* Constructor */
Master_FA::Master_FA () : RTI::FederateAmbassador ()
{ }
/* Destructor */
Master_FA::~Master_FA () throw()
{ }
/* Services called by the RTI */
////////////////////////////////////
// Federation Management Services //
////////////////////////////////////
////////////////////////////////
// Object Management Services //
////////////////////////////////
void
Master_FA::reflectAttributeValues (RTI::ObjectHandle theObject,
const RTI::AttributeHandleValuePairSet & theAttributes,
const RTI::FedTime & theTime,
const char * theTag,
RTI::EventRetractionHandle theHandle)
throw (
RTI::ObjectNotKnown,
RTI::AttributeNotKnown,
RTI::FederateOwnsAttributes,
RTI::InvalidFederationTime,
RTI::FederateInternalError)
{
cerr << "Incoming attribute update for unknown object" << endl;
}
void
Master_FA::receiveInteraction (
RTI::InteractionClassHandle theInteraction,
const RTI::ParameterHandleValuePairSet & theParameters,
const RTI::FedTime & theTime,
const char * theTag,
RTI::EventRetractionHandle theHandle)
throw (
RTI::InteractionClassNotKnown,
RTI::InteractionParameterNotKnown,
RTI::InvalidFederationTime,
RTI::FederateInternalError)
{
RTIfedTime fed_time(theTime);
cout << "Incoming interaction at time " << fed_time.getTime() << endl;
if (theInteraction != reply_handle)
{
cerr << "Received unknown interaction " << theInteraction << endl;
return;
}
if (theParameters.size() != 3)
{
cerr << "Unexpected number of parameters "
<< theParameters.size() << endl;
return;
}
// extract values
RTI::ULong id;
RTI::Double change_lat;
RTI::Double change_long;
unsigned long length;
RTI::ParameterHandle handle;
char buffer [2000];
for (unsigned long i = 0; i < 3; i++)
{
handle = theParameters.getHandle (i);
if (handle == reply_parameters [Id])
{
theParameters.getValue (i, buffer, length);
Sim_Hla_Istream is (buffer, length);
is >> id;
}
else if (handle == reply_parameters [ChangeLat])
{
theParameters.getValue (i, buffer, length);
Sim_Hla_Istream is (buffer, length);
is >> change_lat;
}
else if (handle == reply_parameters [ChangeLong])
{
theParameters.getValue (i, buffer, length);
Sim_Hla_Istream is (buffer, length);
is >> change_long;
}
else
{
cerr << "Unknown handle " << handle << " for Param #" << i << endl;
return;
}
}
cout << "Node " << id << " has changed movement at "
<< change_lat << ", " << change_long << endl;
}
void
Master_FA::provideAttributeValueUpdate (
RTI::ObjectHandle theObject,
const RTI::AttributeHandleSet & theAttributes)
throw (
RTI::ObjectNotKnown,
RTI::AttributeNotKnown,
RTI::AttributeNotOwned,
RTI::FederateInternalError)
{
PendingRequest::enqueue (new ProvideAttributeValueUpdatePR (theObject,
theAttributes));
}
//////////////////////////////
// Time Management Services //
//////////////////////////////
void
Master_FA::timeRegulationEnabled (const RTI::FedTime& theFederateTime)
throw (RTI::InvalidFederationTime,
RTI::EnableTimeRegulationWasNotPending,
RTI::FederateInternalError)
{
#if 0
cout << "timeRegulationEnabled called" << endl;
#endif
time_regulation_enabled = 1;
}
void
Master_FA::timeConstrainedEnabled (const RTI::FedTime& theFederateTime)
throw (RTI::InvalidFederationTime,
RTI::EnableTimeConstrainedWasNotPending,
RTI::FederateInternalError)
{
#if 0
cout << "timeConstrainedEnabled called" << endl;
#endif
time_constrained_enabled = 1;
}
void
Master_FA::timeAdvanceGrant (const RTI::FedTime & theTime)
throw (
RTI::InvalidFederationTime,
RTI::TimeAdvanceWasNotInProgress,
RTI::FederateInternalError)
{
if (!timeAdvanceOutstanding)
cerr << "timeAdvanceGrant: Unexpected timeAdvanceGrant" << endl;
else
{
timeAdvanceOutstanding = RTI::RTI_FALSE;
RTIfedTime fed_time (theTime);
// cout << "*** Time Advance Grant received for time = " << fed_time.getTime() << endl;
currentTime = fed_time.getTime ();
}
}
/******* Federation Management Services ********/
RTI::Boolean
initiate_federation_participation(const char * const federateName)
{
short numTries = 0;
RTIfedTime cur_time;
RTIfedTime lookahead, start_time;
// first try to create a federation
try
{
ostrstream fed_file;
fed_file << fedExecName << ".fed" << ends;
rti_amb.createFederationExecution(fedExecName, fed_file.str());
delete [] fed_file.str ();
}
catch ( RTI::FederationExecutionAlreadyExists &)
{
// Not an error
// cerr << "Note: Federation execution already exists." << &e << endl;
}
catch ( RTI::Exception& e )
{
cerr << "Error:" << &e << endl;
return RTI::RTI_FALSE;
}
// then try to join the appropriate federation
cerr << "Attempting to join federation..." << endl;
while (numTries < 10)
{
try
{
federateId = rti_amb.joinFederationExecution(
(char * const) federateName,
(char * const) fedExecName,
&fedAmb);
// worked, now set up time management
break;
}
catch (RTI::FederateAlreadyExecutionMember& e)
{
cerr << "Error: " << federateName
<< " already exists in the Federation Execution "
<< fedExecName << "." << endl;
cerr << &e << endl;
return RTI::RTI_FALSE;
}
catch (RTI::FederationExecutionDoesNotExist &)
{
#if 0 /* Not really an error */
cerr << "Error: " << fedExecName << " Federation Execution "
<< "does not exist."<< endl;
cerr << "Sleeping for a few seconds ..." << endl;
#endif
#if !defined (_MSC_VER)
sleep (2);
#else
Sleep (2000);
#endif
++numTries;
}
catch (RTI::CouldNotOpenFED& e)
{
cerr << "Er
會出現如下報錯:
controller_tutorial.cpp
c:\users\hm\documents\e-controller_tutorial\controller_tutorial.cpp(149) : error C2872: 'cerr' : ambiguous symbol
c:\users\hm\documents\e-controller_tutorial\controller_tutorial.cpp(202) : error C2872: 'cerr' : ambiguous symbol
c:\users\hm\documents\e-controller_tutorial\controller_tutorial.cpp(219) : error C2872: 'cout' : ambiguous symbol
c:\users\hm\documents\e-controller_tutorial\controller_tutorial.cpp(222) : error C2872: 'cerr' : ambiguous symbol
c:\users\hm\documents\e-controller_tutorial\controller_tutorial.cpp(227) : error C2872: 'cerr' : ambiguous symbol
c:\users\hm\documents\e-controller_tutorial\controller_tutorial.cpp(263) : error C2872: 'cerr' : ambiguous symbol
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/118669.html
標籤:C++ 語言
上一篇:求助大神,實在做不出來了…
