📄 routemessage.java
字号:
} return 0; } return -1; } /** * Sets the PrevNode attribute of the RouteMessage object * * @param n The new PrevNode value */ public void setPrevNode(NodeHandle n) { prevNode = n; } /** * Sets the NextHop attribute of the RouteMessage object * * @param nh The new NextHop value */ public void setNextHop(NodeHandle nh) { nextHop = nh; } /** * Sets the DestinationId attribute of the RouteMessage object * * @param id The new DestinationId value */ public void setDestinationId(rice.p2p.commonapi.Id id) { target = (Id) id; } /** * Sets the NextHopHandle attribute of the RouteMessage object * * @param nextHop The new NextHopHandle value */ public void setNextHopHandle(rice.p2p.commonapi.NodeHandle nextHop) { this.nextHop = (NodeHandle) nextHop; } /** * Sets the Message attribute of the RouteMessage object * * @param message The new Message value */ public void setMessage(rice.p2p.commonapi.Message message) { ((PastryEndpointMessage) unwrap()).setMessage(message); } /** * Sets the Message attribute of the RouteMessage object * * @param message The new Message value */ public void setMessage(RawMessage message) { ((PastryEndpointMessage) unwrap()).setMessage(message); } /** * Routes the messages if the next hop has been set up. * * @param localHandle DESCRIBE THE PARAMETER * @return true if the message got routed, false otherwise. */ public boolean routeMessage(NodeHandle localHandle) { if (nextHop == null) { return false; } setSender(localHandle); NodeHandle handle = nextHop; nextHop = null; prevNode = localHandle; if (localHandle.equals(handle)) { localHandle.getLocalNode().send(handle, internalMsg); } else { localHandle.getLocalNode().send(handle, this); } return true; } /** * The wrapped message. * * @return the wrapped message. * @deprecated use unwrap(MessageDeserializer) */ public Message unwrap() { if (internalMsg != null) { return internalMsg; } try { endpointDeserializer.setSubDeserializer(endpointDeserializer); return unwrap(endpointDeserializer); //pn.getEnvironment().getLogManager().getLogger(RouteMessage.class, null))); } catch (IOException ioe) { throw new RuntimeException(ioe); } } /** * DESCRIBE THE METHOD * * @return DESCRIBE THE RETURN VALUE */ public String toString() { String str = ""; str += "[ " + internalMsg + " ]"; return str; } /** * DESCRIBE THE METHOD * * @param buf DESCRIBE THE PARAMETER * @exception IOException DESCRIBE THE EXCEPTION */ public void serialize(OutputBuffer buf) throws IOException { buf.writeByte((byte) 0); buf.writeInt(auxAddress); target.serialize(buf); prevNode.serialize(buf); if (serializedMsg != null) { // optimize this, possibly by extending InternalBuffer interface to access the raw underlieing bytes byte[] raw = new byte[serializedMsg.bytesRemaining()]; serializedMsg.read(raw); buf.write(raw, 0, raw.length); serializedMsg = null; // note, this leaves the RouteMessage in a busted state no rawInternalMsg, internalMsg, serializedMsg } else { if (rawInternalMsg == null) { rawInternalMsg = convert(internalMsg); }// address was already peeled off as the auxAddress// different wire to deserialize the Address and eliminate unneeded junk// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+// +bool hasSender + Priority + Type (Application specifc) + // zero is java serialization// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+//// optional// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+// + NodeHandle sender +// + +// ... flexable size// + +// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ NodeHandle sender = rawInternalMsg.getSender(); boolean hasSender = (sender != null); if (hasSender) { buf.writeBoolean(true); } else { buf.writeBoolean(false); } buf.writeByte(rawInternalMsg.getPriority()); buf.writeShort(rawInternalMsg.getType()); if (hasSender) { sender.serialize(buf); } rawInternalMsg.serialize(buf); } } /** * DESCRIBE THE METHOD * * @param md DESCRIBE THE PARAMETER * @return DESCRIBE THE RETURN VALUE * @exception IOException DESCRIBE THE EXCEPTION */ public Message unwrap(MessageDeserializer md) throws IOException { if (internalMsg != null) { return internalMsg; }//// if (internalMsg.getType() == 0) {// PJavaSerializedMessage pjsm = (PJavaSerializedMessage)internalMsg;// return pjsm.getMessage();// }// return internalMsg;// } // deserialize using md// address was already peeled off as the auxAddress// different wire to deserialize the Address and eliminate unneeded junk// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+// +bool hasSender + Priority + Type (Application specifc) + // zero is java serialization// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+//// optional// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+// + NodeHandle sender +// + +// ... flexable size// + +// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ NodeHandle internalSender = null; boolean hasSender = serializedMsg.readBoolean(); byte internalPriority = serializedMsg.readByte(); short internalType = serializedMsg.readShort(); if (hasSender) { internalSender = nhf.readNodeHandle(serializedMsg); } internalMsg = (Message) md.deserialize(serializedMsg, internalType, internalPriority, internalSender); // the serializedMsg is now dirty, because the unwrapper may change the internal message serializedMsg = null; nhf = null; return internalMsg; } /** * DESCRIBE THE METHOD * * @param msg DESCRIBE THE PARAMETER * @return DESCRIBE THE RETURN VALUE */ private static PRawMessage convert(Message msg) { if (msg instanceof PRawMessage) { PRawMessage prm = (PRawMessage) msg; if (prm.getType() == 0) { throw new RuntimeException("Cannot route a PJavaSerializedMessage, this is used internally in RouteMessage."); } return prm; } return new PJavaSerializedMessage(msg); } /** * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + int * auxAddress + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * + Id target + + + + + + + + + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * + NodeHandle prev + + (used to repair routing table during routing) + + + * ... + + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * Internal Message + + (see below) + + + * * @param buf * @param nhf DESCRIBE THE PARAMETER * @param pn DESCRIBE THE PARAMETER * @return * @exception IOException DESCRIBE THE EXCEPTION */ public static RouteMessage build(InputBuffer buf, NodeHandleFactory nhf, PastryNode pn) throws IOException { byte version = buf.readByte(); switch (version) { case 0: int auxAddress = buf.readInt(); Id target = Id.build(buf); NodeHandle prev = nhf.readNodeHandle(buf); return new RouteMessage(target, auxAddress, prev, buf, nhf, pn); default: throw new IOException("Unknown Version: " + version); } } /** * DESCRIBE THE CLASS * * @version $Id: pretty.settings 2305 2005-03-11 20:22:33Z jeffh $ * @author jeffh */ class RMDeserializer extends PJavaSerializedDeserializer { MessageDeserializer sub; /** * Constructor for RMDeserializer. */ public RMDeserializer() { // the late binding of pn is pretty problematic, we'll set it right before we deserialize // the thing is, we usually won't even need it super(null); } /** * Sets the SubDeserializer attribute of the RMDeserializer object * * @param md The new SubDeserializer value */ public void setSubDeserializer(MessageDeserializer md) { sub = md; } /** * DESCRIBE THE METHOD * * @param buf DESCRIBE THE PARAMETER * @param type DESCRIBE THE PARAMETER * @param priority DESCRIBE THE PARAMETER * @param sender DESCRIBE THE PARAMETER * @return DESCRIBE THE RETURN VALUE * @exception IOException DESCRIBE THE EXCEPTION */ public Message deserialize(InputBuffer buf, short type, byte priority, NodeHandle sender) throws IOException { // just in case we have to do java serialization pn = RouteMessage.this.pn; switch (type) { case PastryEndpointMessage.TYPE: return new PastryEndpointMessage(auxAddress, buf, sub, sender); } return null; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -