⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 endpointroutermessage.java

📁 jxta_src_2.41b jxta 2.41b 最新版源码 from www.jxta.org
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * * $Id: EndpointRouterMessage.java,v 1.18 2006/07/20 16:16:36 hamada Exp $ * * Copyright (c) 2001-2006 Sun Microsystems, Inc.  All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in *    the documentation and/or other materials provided with the *    distribution. * * 3. The end-user documentation included with the redistribution, *    if any, must include the following acknowledgment: *       "This product includes software developed by the *       Sun Microsystems, Inc. for Project JXTA." *    Alternately, this acknowledgment may appear in the software itself, *    if and wherever such third-party acknowledgments normally appear. * * 4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA" *    must not be used to endorse or promote products derived from this *    software without prior written permission. For written *    permission, please contact Project JXTA at http://www.jxta.org. * * 5. Products derived from this software may not be called "JXTA", *    nor may "JXTA" appear in their name, without prior written *    permission of Sun. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED.  IN NO EVENT SHALL SUN MICROSYSTEMS OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of Project JXTA.  For more * information on Project JXTA, please see * <http://www.jxta.org/>. * * This license is based on the BSD license adopted by the Apache Foundation. */package net.jxta.impl.endpoint.router;import java.util.Enumeration;import java.util.Vector;import net.jxta.document.AdvertisementFactory;import net.jxta.document.Element;import net.jxta.document.MimeMediaType;import net.jxta.document.StructuredDocumentFactory;import net.jxta.document.StructuredDocumentUtils;import net.jxta.document.XMLDocument;import net.jxta.document.XMLElement;import net.jxta.endpoint.EndpointAddress;import net.jxta.endpoint.Message;import net.jxta.endpoint.MessageElement;import net.jxta.endpoint.TextDocumentMessageElement;import net.jxta.protocol.AccessPointAdvertisement;import net.jxta.protocol.RouteAdvertisement;import org.apache.log4j.Level;import org.apache.log4j.Logger;/** * Message element Router. This element is added to every * message to carry route information for the EndpointRouter service */public class EndpointRouterMessage {    /**     *    Log4j Category     */    private static final Logger LOG = Logger.getLogger(EndpointRouterMessage.class.getName());    public static final String MESSAGE_NS = "jxta";    public static final String MESSAGE_NAME = "EndpointRouterMsg";    public static final String Name = "jxta:ERM";    public static final String SrcTag = "Src";    public static final String DestTag = "Dest";    public static final String LastHopTag = "Last";    public static final String GatewayForwardTag = "Fwd";    public static final String GatewayReverseTag = "Rvs";    private String srcAddress = null; // PeerID-based EndpointAddress    private String destAddress = null; // PeerID-based EndpointAddress    private String lastHop = null; // Plain PeerID    private transient Vector forwardGateways = null;    private transient Vector forwardCache = null;    private transient Vector reverseGateways = null;    private transient Vector reverseCache = null;    private transient RouteAdvertisement radv = null;    // A flag that represents the existence of data.  Which is    // different from all fields being empty.    private transient boolean rmExists = false;    // A flag that tells us that the message is not uptodate compared to    // This object.    private transient boolean rmDirty = false;    // Keep tied to one and only one message.    private final transient Message message;    // Cache the element. At the minimum it simplifies removal.    private transient MessageElement rmElem = null;    public boolean msgExists() {        return rmExists;    }    public boolean isDirty() {        return rmDirty;    }    public EndpointRouterMessage(Message message, boolean removeMsg) {        this.message = message;        try {            rmElem = message.getMessageElement(MESSAGE_NS, MESSAGE_NAME);            if (rmElem == null) {                return;            }            // We have an element, but until we read it, no data to            // match (rmExists == false). If the data cannot be read            // from the element, the element is scheduled for removal.            rmDirty = true;            // If we have been instructed so, do not parse any existing            // element, and leave it marked for removal from the message            // as if it were invalid.            if (removeMsg) {                return;            }            XMLDocument doc = (XMLDocument) StructuredDocumentFactory.newStructuredDocument(rmElem.getMimeType(),                              rmElem.getStream());            Enumeration each = null;            XMLElement e = null;            each = doc.getChildren();            if (!each.hasMoreElements()) {                // results in rmExists being false.                return;            }            while (each.hasMoreElements()) {                try {                    e = (XMLElement) each.nextElement();                    if (e.getName().equals(SrcTag)) {                        srcAddress = e.getTextValue();                        continue;                    }                    if (e.getName().equals(DestTag)) {                        destAddress = e.getTextValue();                        continue;                    }                    if (e.getName().equals(LastHopTag)) {                        lastHop = e.getTextValue();                        continue;                    }                    if (e.getName().equals(GatewayForwardTag)) {                        for (Enumeration eachXpt = e.getChildren(); eachXpt.hasMoreElements();) {                            if (forwardGateways == null) {                                forwardGateways = new Vector();                            }                            if (forwardCache == null) {                                forwardCache = new Vector();                            }                            XMLElement aXpt = (XMLElement) eachXpt.nextElement();                            AccessPointAdvertisement xptAdv = (AccessPointAdvertisement)                                                              AdvertisementFactory.newAdvertisement(aXpt);                            forwardGateways.addElement(xptAdv);                            // Save the original element                            forwardCache.addElement(aXpt);                        }                        continue;                    }                    if (e.getName().equals(GatewayReverseTag)) {                        for (Enumeration eachXpt = e.getChildren(); eachXpt.hasMoreElements();) {                            if (reverseGateways == null) {                                reverseGateways = new Vector();                            }                            if (reverseCache == null) {                                reverseCache = new Vector();                            }                            XMLElement aXpt = (XMLElement) eachXpt.nextElement();                            AccessPointAdvertisement xptAdv = (AccessPointAdvertisement)                                                              AdvertisementFactory.newAdvertisement(aXpt);                            reverseGateways.addElement(xptAdv);                            // Save the original element                            reverseCache.addElement(aXpt);                        }                        continue;                    }                    if (e.getName().equals(RouteAdvertisement.getAdvertisementType())) {                        radv = (RouteAdvertisement)                               AdvertisementFactory.newAdvertisement(e);                    }                } catch (Exception ee) {                    // keep going                }            }            // XXX 20040929 bondolo Should be doing validation here.            // All parsed ok, we're in sync.            rmExists = true;            rmDirty = false;        } catch (Exception eee) {            // give up. The dirty flag will get the element removed            // from the message (if there was one) and we'll report            // there was none.        }    }    public void updateMessage() {        if (!rmDirty) {            return;        }        if (!rmExists) {            // The change was to remove it.            // If there was an rmElem, remove it and make sure to remove            // all of them. We may have found one initialy but there may be            // several. (just a sanity check for outgoing messages).            while (rmElem != null) {                message.removeMessageElement(MESSAGE_NS, rmElem);                rmElem = message.getMessageElement(MESSAGE_NS, MESSAGE_NAME);            }            if (rmElem != null) {                message.removeMessageElement(MESSAGE_NS, rmElem);            }            rmElem = null;            rmDirty = false;            return;        }        // The element was either created or changed. Replace whatever

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -