/******************************************************************* * * DESCRIPTION: definitions of messages ( Y, *, D, X, I ) * * AUTHOR: Amir Barylko & Jorge Beyoglonian * Version 2: Daniel A. Rodriguez * * EMAIL: mailto://amir@dc.uba.ar * mailto://jbeyoglo@dc.uba.ar * mailto://drodrigu@dc.uba.ar * * DATE: 27/6/1998 * DATE: 25/4/1999 (v2) * *******************************************************************/ #ifndef __MESSAGE_H #define __MESSAGE_H /** include files **/ #include "time.h" // class Time #include "value.h" // class Value #include "process.h" // class Processor, ProcId #include "procadm.h" // SingleProcessorAdmin #include "port.h" #include "strutil.h" #include "cellpos.h" #include "cppwrapper.h" #include "strutil.h" /** foward declarations **/ class Port ; class Processor ; class SingleCPPWrapper; /** definitions **/ class Message { public: virtual ~Message() //Destructor {} //** Queries **// const Time &time() const {return t;} const ProcId &procId() const { return proc; } //** Modifiers **// Message &time( const Time &time ) { t = time; return *this; } Message &procId( const ProcId &p ) { proc = p; return *this; } virtual Message *clone() const = 0 ; virtual Message &sendTo( Processor & ) = 0 ; virtual Message &sendRemoteTo( const MachineId &, const ProcId &) = 0; // virtual Message &sendRemoteTo(const MachineId &, const ProcId &) = 0; virtual const string type() const = 0 ; virtual const string asString() const { MachineId mid = SingleCPPWrapper::Instance().getMachineID(); string desc ; if ( procId() != ProcessorAdmin::rootId ) { Model &model = SingleProcessorAdmin::Instance().model( procId() ); desc = model.description(); } else { desc = "Root"; } if (SingleCPPWrapper::Instance().machineForProcId( procId() ) == mid ) return int2Str(mid) + " / L / " + type() + " / " + time().asString() + " / " + desc + "(" + procId() + ")" ; else return int2Str(mid) + " / R / " + type() + " / " + time().asString() + " / " + desc + "(" + procId() + ")" ; } protected: Message( const Time &time, const ProcId &id ) // constructor : t(time) , proc(id) {} Message( const Message &msg) // Copy constructor : t(msg.t) , proc(msg.proc) {} Message& operator=( const Message& ); // Assignment operator int operator==( const Message& ) const; // Equality operator private: Time t; ProcId proc; }; // class Message class InitMessage : public Message { public: InitMessage( const Time &tm, const ProcId &m) // Default constructor :Message( tm, m) {} virtual Message *clone() const { return new InitMessage(*this);} virtual Message &sendTo( Processor &proc ) { proc.receive(*this); return *this;} virtual Message &sendRemoteTo(const MachineId &mid, const ProcId &pid) { SingleCPPWrapper::Instance().sendRemoteMessage(*this, mid, pid); return *this; } virtual const string type() const {return "I" ;} }; // class InitMessage class EchoMessage : public Message { public: EchoMessage( const Time &tm, const ProcId &m, long sec, long usec) :Message( tm, m), sendTimeSec(sec), sendTimeUSec(usec) {} EchoMessage( const Time &tm, const ProcId &m) // Default constructor :Message( tm, m), sendTimeSec(-1), sendTimeUSec(-1) {} virtual Message *clone() const { return new EchoMessage(*this);} virtual Message &sendTo( Processor &proc ) { return *this;} virtual Message &sendRemoteTo(const MachineId &mid, const ProcId &pid) { SingleCPPWrapper::Instance().sendEchoMessage(*this, mid); return *this; } virtual const string type() const {return "E" ;} const long getSendTimeSec() const { return sendTimeSec; } const long getSendTimeUSec() const { return sendTimeUSec ; } Message& setSendTimeSec( long sec ) { sendTimeSec = sec; return *this; } Message& setSendTimeUSec( long usec ) { sendTimeUSec = usec ; return *this; } private: long sendTimeSec; long sendTimeUSec; }; // class EchoMessage class InternalMessage : public Message { public: InternalMessage( const Time &tm = Time::Zero, const ProcId &m = 0 ) //Default constructor :Message(tm,m) {} virtual Message *clone() const {return new InternalMessage(*this);} virtual Message &sendTo( Processor &proc ) {proc.receive(*this); return *this;} virtual Message &sendRemoteTo( const MachineId &mid, const ProcId &pid) { SingleCPPWrapper::Instance().sendRemoteMessage( *this, mid, pid); return *this; } virtual const string type() const {return "*" ;} protected: InternalMessage& operator=(const InternalMessage&); //Assignment operator int operator==(const InternalMessage&) const; //Equality operator private: }; // class InternalMessage class DoneMessage : public Message { public: DoneMessage(); //Default constructor DoneMessage( const Time &tm, const ProcId &m, const Time &nextChange , bool fromSlave) :Message(tm,m) ,next(nextChange) , slaveSync( fromSlave ) {} const Time &nextChange() const {return next;} Message &nextChange( const Time & ); virtual Message *clone() const {return new DoneMessage(*this);} virtual Message &sendTo( Processor &proc ) {proc.receive(*this); return *this;} virtual Message &sendRemoteTo( const MachineId &mid, const ProcId &pid) { SingleCPPWrapper::Instance().sendRemoteMessage(*this, mid, pid) ; return *this; } virtual const string type() const {return "D" ;} virtual const string asString() const { return Message::asString() + " / " + nextChange().asString(); } bool isFromSlave() const { return slaveSync; } Message &isFromSlave(bool fromSlave ) { slaveSync = fromSlave ; return *this ; } protected: DoneMessage(const DoneMessage &msg) //Copy constructor :Message(msg) ,next(msg.next) , slaveSync(msg.slaveSync) {} DoneMessage& operator=(const DoneMessage&); //Assignment operator int operator==(const DoneMessage&) const; //Equality operator private: Time next; bool slaveSync; }; // class DoneMessage //Rami: class BasicPortMessage : public Message { public: BasicPortMessage() : Message(Time::Zero, Processor::InvalidId) , p(NULL) , v(0) {} BasicPortMessage(const Time &tm, const ProcId &m, const Port &port,const Value &value) : Message(tm,m) ,p(&port) ,v(value) {} BasicPortMessage(const BasicPortMessage &msg) :Message(msg) ,p(msg.p) ,v(msg.v) {} const Value &value() const { return v;} Message &value(const Value &val) { v = val; return *this; } const Port &port() const { /*assert(p);*/ return *p; } Message &port(const Port &port) { p = &port ; return *this; } virtual const string asString() const { return Message::asString() + " / " + port().asString() + " / " + Value2StrReal(value()); } protected: BasicPortMessage& operator=(const BasicPortMessage&); int operator==(const BasicPortMessage&) const; private: const Port *p; Value v; }; // class BasicPortMessage class OutputMessage: public BasicPortMessage { public: OutputMessage() {} OutputMessage(const Time &tm, const ProcId &m,const Port &port,const Value &value) : BasicPortMessage(tm,m,port,value) {} OutputMessage(const OutputMessage &msg) : BasicPortMessage(msg) {} virtual const string type() const { return "Y" ; } virtual Message *clone() const { return new OutputMessage(*this); } virtual Message &sendTo(Processor &proc) { proc.receive(*this); return *this; } virtual Message &sendRemoteTo( const MachineId &mid, const ProcId &pid) { SingleCPPWrapper::Instance().sendRemoteMessage(*this, mid, pid) ; return *this; } virtual const string asString() const { return BasicPortMessage::asString() ; } // {return Message::asString() + " / " + port().asString() + " / " + Value2StrReal(value()); } protected: OutputMessage & operator=(const OutputMessage&); int operator==(const OutputMessage&) const; }; // class OutputMessage class ExternalMessage : public BasicPortMessage { public: ExternalMessage() {} ExternalMessage(const Time &tm, const ProcId &m, const Port &port, const Value &value, const ModelId &senderId, const CellPosition pos) : BasicPortMessage(tm, m, port,value) , sender(senderId), senderCellPos(pos) {} ExternalMessage(const Time &tm, const ProcId &m, const Port &port, const Value &value, const ModelId &senderId) : BasicPortMessage(tm, m, port,value) , sender(senderId) {} ExternalMessage(const Time &tm, const ProcId &m, const Port &port, const Value &value) : BasicPortMessage(tm, m, port,value) {} ExternalMessage(const ExternalMessage& msg) : BasicPortMessage(msg) { this->sender = msg.sender; this->senderCellPos = msg.senderCellPosition(); } virtual const string type() const { return "X" ; } virtual Message *clone() const { return new ExternalMessage(*this); } virtual Message &sendTo(Processor &proc) { proc.receive(*this); return *this; } virtual Message &sendRemoteTo( const MachineId &mid, const ProcId &pid) { SingleCPPWrapper::Instance().sendRemoteMessage(*this, mid, pid) ; return *this; } virtual const string asString() const { return BasicPortMessage::asString(); } // { return Message::asString() + " / " + port().asString() + " / " + Value2StrReal(value()); } Message &senderModelId( const ModelId &id) { sender = id ; return *this; } const ModelId &senderModelId() const { return sender ; } Message &senderCellPosition(const CellPosition& pos) { senderCellPos = pos; return *this; } const CellPosition senderCellPosition() const { return senderCellPos; } protected: ExternalMessage& operator=(const ExternalMessage&); int operator==(const ExternalMessage&) const; ModelId sender; CellPosition senderCellPos ; // Rami: used only for FlatCoupledCell models }; // class ExternalMessage // Rami class CollectMessage : public Message { public: CollectMessage(const Time &tm=Time::Zero, const ProcId &m = 0) : Message(tm,m) {} virtual CollectMessage *clone() const { return new CollectMessage(*this); } virtual Message &sendTo(Processor &proc) { proc.receive(*this); return *this; } virtual Message &sendRemoteTo( const MachineId &mid, const ProcId &pid) { SingleCPPWrapper::Instance().sendRemoteMessage(*this, mid, pid) ; return *this; } virtual const string type() const {return "@" ; } protected: CollectMessage& operator=(const CollectMessage&); int operator==(const CollectMessage&) const; private: }; // class CollectMessage #endif //__MESSAGE_H