import java.io.*; import java.util.*; import java.lang.*; import java.sql.Time; /** This class represents the messages sent and received by LPs. * @author Yin Xiong * @version 2.0 (10/2006) */ public class Message implements Serializable, Comparable { /** * Constants representing categories of messages that can be sent. * */ public enum MsgType { SIM_REGULAR, SIM_ANTI, SIM_STRAGGLER, SYSTEM_MONITOR, SYSTEM_STEER, SYSTEM_REPORT, SYSTEM_ACKNOWLEDGE,SIM_ENDSIM } /**Tag that indicate whether this message has already been acknowledged by the receiver */ int tag; /**Type of the message, matching one of the constants defined in this class. */ MsgType msgType; /**(Simulation) time this message should occur.*/ //double timeStamp; int timeStamp; /**Message content*/ Object content; /**Sender's {@link LogicalProcess#APPid APPid}, if sender is a * {@link LogicalProcess}, or identifier, if sender is a monitoring/steering * application. */ String senderAPPid; /**Sender's LP number*/ int senderLP; /**Sender's PE number*/ int senderPE; /**Receiver's {@link LogicalProcess#APPid APPid}, if receiver is a * {@link LogicalProcess}, or identifier, if receiver is a monitoring/steering * application. */ String receiverAPPid; /**Receiver's LP number*/ int receiverLP; /**Receiver's PE number*/ int receiverPE; /**Receiver's machine name*/ String receiverMN; /**Preferred constructor; constructs a Message object of undefined origin and *destination. *The sender and receiver info must be set later. *@param time The local sim time this message occurs. *@param typ MsgType of this Message. *@param cont Object the content of the message. */ public Message(int time, MsgType typ, Object cont) { tag=0; //0=not acknowledged yet timeStamp=time; msgType=typ; content=cont; senderAPPid = null; senderLP = -1; senderPE = -1; receiverAPPid = null; receiverLP = -1; receiverPE = -1; receiverMN=null; }//end constructor /**Sets the machine name of the receiver of this message. *@param mn a String object representing the machine name. */ void setReceiverMN(String mn) { receiverMN=mn; } /**Returns the machine name of the receive of this message. */ public String getReceiverMN() { return receiverMN; }//end getReceiverMN /**Constructs a Message object with preliminary (not complete) addressing. * senderLP, senderPE, receiverLP, and receiverPE must be specified later! *@param time The local sim time this message occurs. *@param typ MsgType of this Message. *@param cont Object the content of the message. *@param sender String the APPid of the sender LP. *@param receiver String the APPid of the receiver LP. */ public Message(int time, MsgType typ, Object cont, String sender, String receiver) { timeStamp=time; msgType=typ; content=cont; senderAPPid=sender; senderLP = -1; senderPE = -1; receiverAPPid=receiver; receiverLP = -1; receiverPE = -1; }//end constructor /**Sets the message type; *@param typ MsgType */ void setMsgType(MsgType typ) { msgType=typ; }//end setMsgType /**Returns a copy of the passed-in message with a negative sign. */ Message makeAntiMsg() { Message antiMsg=new Message(this.timeStamp, Message.MsgType.SIM_ANTI, this.content, this.senderAPPid, this.receiverAPPid); antiMsg.senderLP=this.senderLP; antiMsg.senderPE=this.senderPE; antiMsg.receiverLP=this.receiverLP; antiMsg.receiverPE=this.receiverPE; return antiMsg; }//end makeAntiMsg /**Returns the PE id of the receiver of the message. */ public int getReceiverPE() { return receiverPE; }//end getsReceiverPE /**Sets the sender's PE number. *@param sPE int the PE where the sender resides. */ void setSenderPE(int sPE) { senderPE=sPE; }//end setSenderPE /**Sets the sender's LP number. *@param sLP int the LP id of the sender. */ void setSenderLP(int sLP) { senderLP=sLP; }//end setSenderLP /**Sets the receiver's PE number. *@param rPE int the PE where the receiver resides. */ void setReceiverPE(int rPE) { receiverPE=rPE; }//end setReceiverPE /**Sets the receiver's LP number. *@param rLP int the id of the receiver LP. */ void setReceiverLP(int rLP) { receiverLP=rLP; }//end setReceiverLP /**Sets the sender APPid.*/ void setSenderAPPid(String said) { senderAPPid=said; }//end setsSenderName /**Sets the receiver APPid.*/ void setReceiverAPPid(String raid) { receiverAPPid=raid; }//end setsReceiverName /**gets the receiver LP.*/ public int getReceiverLP() { return receiverLP; }//end getsReceiverLP /**gets the sender LP.*/ public int getSenderLP() { return senderLP; }//end getsSenderLP /**Returns the sender name.*/ public String getSenderAPPid() { return senderAPPid; }//end getSenderName /**Returns the receiver name.*/ public String getReceiverAPPid() { return receiverAPPid; }//end getReceiverName /**Returns the type of the message.*/ public MsgType getMsgType() { return msgType; } /**Returns the time stamp of the message.*/ public double getTimeStamp() { return timeStamp; } /**Returns the content of the message.*/ public Object getContent() { return content; } /**Returns a string representation of the Message object. */ public String toString() { return "Message of type "+msgType+" with timestamp "+timeStamp+ " from ("+senderAPPid+","+senderLP+","+senderPE+") to ("+ receiverAPPid+","+receiverLP+","+receiverPE+") with content "+ content; } /**Compares two Messages' time stamps. If this Message has an earlier time * stamp than the other, it is "less than" it, and a negative number is * returned. If this message has a later time stamp, it is "greater" and a * positive number is returned. If they have the same time stamp, zero is * returned (marking them as "equal"). As such, note: this class has a natural * ordering that is inconsistent with equals. */ public int compareTo(Message O) { //can't just return (sendTimeStamp - O.sendTimeStamp) due to rounding... if (timeStamp < O.timeStamp) return -1; else if (timeStamp > O.timeStamp) return 1; return 0; } }//end class Message