📄 message.java
字号:
/** * simulator/Message.java */package simulator;import java.util.ArrayList;/** * A Message send by one node, to be delivered to another node. */public class Message{ protected int id = 0; protected Node sNode = null; protected Node dNode = null; protected double creationTime = 0; protected int dataLength = 0; protected int protocolLength = 0; protected boolean isRoutingMessage = false; protected ArrayList<Object> headers = null; protected ArrayList<History> history = new ArrayList<History>(); /** * Create a new message. * * @param id * a unique integer identifying this message * @param creationTime * the time this message is created * @param source * the node that created this message * @param dest * the node this message is being sent to * @param data * the size of user data in this message */ public Message(int id, double creationTime, Node source, Node dest, int data) { this.id = id; this.creationTime = creationTime; sNode = source; dNode = dest; dataLength = data; } /** * Create a new message. * * @param id * a unique integer identifying this message * @param creationTime * the time this message is created * @param source * the node that created this message * @param dest * the node this message is being sent to * @param data * the size of user data in this message * @param protocol * the size of protocol data in this message * @param isR * true if this message is routing message, false otherwise */ public Message(int id, double creationTime, Node source, Node dest, int data, int protocol, boolean isR) { this(id, creationTime, source, dest, data); protocolLength = protocol; isRoutingMessage = isR; } public Message(Message org) { id = org.id; creationTime = org.creationTime; sNode = org.sNode; dNode = org.dNode; dataLength = org.dataLength; protocolLength = org.protocolLength; isRoutingMessage = org.isRoutingMessage; if (org.headers != null) headers = new ArrayList<Object>(org.headers); history = new ArrayList<History>(org.history); } public boolean update(Message from) { if (from.creationTime > creationTime) { id = from.id; creationTime = from.creationTime; headers = from.headers; history = from.history; return true; } return false; } public String toString() { if (id >= 0) { if (isRoutingMessage) return "MSG_" + id + "_R_"; return "MSG_" + id + "_"; } else { if (isRoutingMessage) return "ACK_" + (-id) + "_R_"; return "ACK_" + (-id) + "_"; } } public int getId() { return id; } public double getCreationTime() { return creationTime; } public void setSourceNode(Node nSN) { sNode = nSN; } public Node getSourceNode() { return sNode; } public void setDestNode(Node nDN) { dNode = nDN; } public Node getDestNode() { return dNode; } public void setDataLength(int nLen) { if (nLen >= 0) dataLength = nLen; } public final int getDataLength() { return dataLength; } public void setProtocolLength(int nLen) { if (nLen >= 0) protocolLength = nLen; } public final int getProtocolLength() { return protocolLength; } // public static final int INTEGER_LENGTH = 4; public final int getLength() { return protocolLength + dataLength; } /** * Represents a unique message header. Create an instance of this class to * use as a key to store data in a message. This allows multiple objects to * store data in a message without conflicts. */ public static class Header { private static int nextIdx = 0; private int idx = -1; public Header() { idx = nextIdx++; } public int idx() { return idx; } } /** * Get the data associated with the header key. This returns null if there * is no such data stored. The key cannot be null. * * @throws NullPointerException * if the key is null. */ public Object getData(Header key) { if (key == null) throw new NullPointerException("the key cannot be null"); int idx = key.idx(); if (headers == null || headers.size() <= idx) return null; return headers.get(idx); } /** * Stores the data in this message, and associates it with the header key. * If data was already stored with the same key in this message, it will be * replaced. Neither the key or the data can be null. * * @throws NullPointerException * if the key or data is null. */ public void storeData(Header key, Object data) { if (key == null) throw new NullPointerException("the key cannot be null"); if (data == null) throw new NullPointerException("the data cannot be null"); int idx = key.idx(); if (headers == null) { headers = new ArrayList<Object>(idx + 1); } while (headers.size() <= idx) headers.add(null); headers.set(idx, data); } public void removeData(Header key) { if (key == null) throw new NullPointerException("the key cannot be null"); int idx = key.idx(); if (headers == null || headers.size() <= idx) return; headers.set(idx, null); } public void setIsRoutingMessage(boolean isR) { isRoutingMessage = isR; } public final boolean isRoutingMessage() { return isRoutingMessage; } public ArrayList<History> getHistory() { return history; } public void addHistory(double time, Contact ct) { history.add(new History(time, ct)); } public class History { double timeSent; Contact contact; public History(double time, Contact c) { contact = c; timeSent = time; } public String toString() { return "{" + timeSent + ":" + contact + "}"; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -