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

📄 piperesolver.java

📁 jxme的一些相关程序,主要是手机上程序开发以及手机和计算机通信的一些程序资料,程序编译需要Ant支持
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
    }
    
    
    /**
     *  if ipid still in cache return the peer associated with it
     *
     * @param  ipId  input pipe id
     * @return       peerid
     * @since 1.0
     */
    private String findCached(String ipId) {

        PipeEntry entry = (PipeEntry) cachedPipes.get(ipId);
        if (entry == null) {
	   if (LOG.isEnabledFor(Priority.DEBUG)) {
		LOG.debug("findCached did not find " + ipId);
	   }
          return null;
        }
        // Something has asked to find this pipe. Update the lease
        resetLease(entry);
	if (LOG.isEnabledFor(Priority.DEBUG)) {
		LOG.debug("findCached found" + ipId);
	}
        return entry.peer;
    }
    
    
    
    /**
     *  Attempt to resolve the pipe on the net
     *
     * @param  adv      Pipe Advertisement
     * @param  timeout  Description of Parameter
     * @param  nocache  no cached responses
     * @return          peerid
     * @since 1.0
     */
    private String findRemote(PipeAdvertisement adv, long timeout, boolean nocache) {
        
        String ipId = adv.getPipeID().toString();
        String type = adv.getType();
        
        if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("findRemote " + ipId);
        try {
            // First create the request message.
            StructuredTextDocument doc = (StructuredTextDocument)
            StructuredDocumentFactory.newStructuredDocument(
            new MimeMediaType("text/xml"),
            "jxta:PipeResolver");
            
            Element e = null;
            e = doc.createElement(MsgTypeTag, QueryMsgType);
            doc.appendChild(e);
            e = doc.createElement(PipeIdTag, ipId);
            doc.appendChild(e);
            e = doc.createElement(TypeTag, type);
            doc.appendChild(e);
            if (nocache) {
                e = doc.createElement(CachedTag, "false");
                doc.appendChild(e);
            }
            StringWriter dumped = new StringWriter();
            doc.sendToWriter(dumped);
            
            ResolverQuery query = new ResolverQuery(PipeResolverName,
            null,
            localPeerId,
            dumped.toString(), qid++);
            
            resolver.sendQuery(null, query);
            
        } catch (Exception ee) {
            if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("findRemote failed with " + ee);
            return null;
        }
        // Wait until either the given timeout or something arrived
        if (timeout == 0) {
            // no timeout, just return
            if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("findRemote no timeout");
            return null;
        }
        
        long endTime = System.currentTimeMillis() + timeout;
        
        String peer = null;
        
        while (true) {
            // check the cache.
            peer = findLocal(ipId);
            if (peer != null) {
                if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("findRemote found " + peer);
                return peer;
            }
            peer = findCached(ipId);
            if (peer != null) {
                if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("findRemote found " + peer);
                return peer;
            }
            
            if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("findRemote waiting...");
            if (timeout < 0) {
                goWait(Long.MAX_VALUE);
            } else {
                long remain = endTime - System.currentTimeMillis();
                if (remain <= 0) {
                    break;
                }
                goWait(remain);
            }
        }
        // Timeout has been reached
        if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("findRemote timeout.");
        return null;
    }
    
    
    
    /**
     *  wait for timeout
     *
     * @param  timeout  Description of Parameter
     * @since 1.0
     */
    private synchronized void goWait(long timeout) {
        try {
            wait(timeout);
        } catch (Exception e) {
        }
    }
    
    
    /**
     *Description of the Method
     *
     * @since 1.0
     */
    private synchronized void notifyWaitingThreads() {
        notifyAll();
    }
    
    
    /**
     *  utility to  retrieve and covert a peer adv to a string
     *
     * @param  peer id
     * @return       string representation of the adv
     * @since 1.0
     */
    private String advString(String peer) {
        
        DiscoveryService discovery = myGroup.getDiscoveryService();
        Enumeration enum;
        try {
            enum = discovery.getLocalAdvertisements(DiscoveryService.PEER, "PID", peer);
        } catch (IOException e) {
            // we don't have the peerAdv
            return null;
        }
        
        if (enum.hasMoreElements()) {
            Advertisement adv = (Advertisement) enum.nextElement();
            StringWriter out = new StringWriter();
            MimeMediaType displayAs = new MimeMediaType("text/xml");
            
            try {
                StructuredTextDocument doc = (StructuredTextDocument) adv.getDocument(displayAs);
                doc.sendToWriter(out);
                return out.toString();
            } catch (Exception e) {
                if (LOG.isEnabledFor(Priority.ERROR)) LOG.error("Bad Peer Adverisement, or error during toString conversion");
                return null;
            }
        } else {
            // we have no result
            return null;
        }
    }
    
    /**
     * This is an optimization step, if we get a peer adv as part of the
     * pipe resolution let's publish it locally, it will be used by the router
     * later on
     * @param  adv  Description of Parameter
     * @since
     */
    private void saveAdv(String adv) {
        
        PeerAdvertisement padv = null;
        discovery = myGroup.getDiscoveryService();
        
        try {
            padv = (PeerAdvertisement) AdvertisementFactory.newAdvertisement(
            new MimeMediaType("text/xml"),
            new ByteArrayInputStream(adv.getBytes()));
            if (!(padv.getPeerID().toString()).equals(localPeerId)) {
                // This is not our own peer adv so we must not keep it
                // longer than its expiration time.
                discovery.publish(padv, DiscoveryService.PEER,
                DiscoveryService.DEFAULT_EXPIRATION,
                DiscoveryService.DEFAULT_EXPIRATION);
            }
            //        if (endpoint.addPeerRoute(padv.getPid())){
            //          if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("Added endpoint route"+padv.getPid());
            //        }
        } catch (Exception e) {
            if (LOG.isEnabledFor(Priority.ERROR)) LOG.error("Error Saving Peer Advertisement during pipe resolution");
        }
    }
    
    
    /**
     *Description of the Class
     *
     */
    public class PipeEntry {
        
        /**
         *Description of the Field
         *
         * @since 1.0
         */
        protected String peer = null;
        /**
         *Description of the Field
         *
         * @since 1.0
         */
        protected long lease = 0;
        
        
        /**
         *Constructor for the PipeEntry object
         *
         * @param  peer  Description of Parameter
         * @since 1.0
         */
        public PipeEntry(String peer) {
            
            this.peer = peer;
            this.lease = DefaultPipeLease;
        }
        
        
        /**
         *Description of the Method
         *
         * @param  obj  Description of Parameter
         * @return      Description of the Returned Value
         * @since 1.0
         */
        public boolean equals(Object obj) {
            
            PipeEntry tmp = (PipeEntry) obj;
            return (peer.equals(tmp.peer));
        }
    }
    
    /**
     *  Add a pipe resolver listener
     *
     * @param  listener  listener
     * @since 1.0
     */
    
    public synchronized void addListener(
    String pipeid,
    Listener listener) {
        
        listeners.put(pipeid, listener);
    }
    
    
    /**
     *  remove a pipe resolver listener
     *
     * @param  listener  to remove
     * @return   listener object removed
     * @since 1.0
     */
    
    public synchronized Listener removeListener( String pipeid) {
        
        return ( (Listener) listeners.remove(pipeid));
    }
    
    public interface Listener extends java.util.EventListener {
        
        /**
         * PipeResolver Event
         * @param PipeResolverEvent event the PipeResolver Event
         */
        void pipeResolverEvent( PipeResolver.Event event );
        
    }
    
    public class Event extends java.util.EventObject {
        
        private String peerid=null;
        private String pipeid=null;
        private int queryID=-1;
        private String type=null;
        
        /**
         *  Creates a new event
         *  @param source The object on which the Event initially occurred
         *  @param queryid The query id associated with the response returned in this event
         */
        
        public Event( Object source,
        String peerid,
        String pipeid,
        String type,
        int queryid) {
            
            super( source );
            this.peerid = peerid;
            this.pipeid = pipeid;
            this.type = type;
            this.queryID = queryid;
        }
        
        /**
         *  Returns the response associated with the event
         *  @return peerid
         */
        public String getPeerID() {
            
            return new String(peerid);
        }
        /**
         *  Returns the response associated with the event
         *  @return pipeid
         */
        public String getPipeID() {
            
            return new String(pipeid);
        }
        
        /**
         *  Returns the type of the pipe that is associated with the event
         *  @return pipeid
         */
        public String getType() {
            return type;
        }
        
        /**
         *  Returns The query id associated with the response returned in this event
         *  @return query id associated with the response
         */
        public int getQueryID() {
            
            return queryID;
        }
        
    }
    
}

⌨️ 快捷键说明

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