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

📄 rendezvousserviceimpl.java

📁 jxme的一些相关程序,主要是手机上程序开发以及手机和计算机通信的一些程序资料,程序编译需要Ant支持
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
                // don't worry about it for now. It'll still work.
            }
        }

        if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("RendezVousService is initialized");
    }


    private void publishInParentGroup (Advertisement adv) {
        // Publish into the parent group.
        RefPeerGroup parent = ((GenericPeerGroup) group).getParentGroup();
        if (parent == null) {
            // No parent... nothing to do.
            return;
        }
        DiscoveryService parentDiscovery = parent.getDiscoveryService();
        if (parentDiscovery == null) {
            if (LOG.isEnabledFor(Priority.WARN)) LOG.warn("Cannot access parent's DiscoveryService Service");
            return;
        }

        try {
            // This is not our own peer adv so we must not keep it
            // longer than its expiration time.
            parentDiscovery.publish(adv, DiscoveryService.PEER,
                                    DiscoveryService.DEFAULT_EXPIRATION,
                                    DiscoveryService.DEFAULT_EXPIRATION);

        } catch (IOException e) {
            if (LOG.isEnabledFor(Priority.WARN)) LOG.warn("Cannot locally publish advertisementin parent group");
        }
    }

    /**
     * This portion of the API is for the peers that are connected
     * to a RendezVousService peer.
     *
     * Add a peer as a new RendezVousService point.
     *
     * @param  adv              Description of Parameter
     * @exception  IOException  Description of Exception
     * @throws  IOException     when the RendezVousService peer is not reachable
     */
    public void connectToRendezVous(PeerAdvertisement adv)
             throws IOException {

	if (LOG.isDebugEnabled()) {
	    if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("connectToRendezVous with advertisement");
	}
        if (adv == null) {
            // Sanity check
            return;
        }

        String peerId = adv.getPeerID().toString();
        if (peerId.equals(localPeerId)) {
            // No connection to itself
            return;
        }

        // Try to get an EndpointMessenger to that peer.

        // First publish the advertisement
        try {
            // This is not our own peer adv so we must not keep it
            // longer than its expiration time.
            group.getDiscoveryService().publish(adv,
                                                DiscoveryService.PEER,
                                                DiscoveryService.DEFAULT_EXPIRATION,
                                                DiscoveryService.DEFAULT_EXPIRATION);
        } catch (Exception e) {
            if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("   publish failed with " + e);
        }

        // Make sure that EndpointService has also the document
	// This is not necessary, especially since the parent group is very likely
	// to have the peer advertisement already, and if it doesn't, it will query for it,
	// but this optimization makes sure that the local peer will not have to query.
        publishInParentGroup (adv);

        // Check that the EndpointService can talk to that peer
        if (!endpoint.ping(mkAddress(peerId))) {
            throw new IOException();
        }

        try {
            EndpointMessenger messenger =
                    endpoint.getMessenger(mkAddress(peerId,
						    pName,
						    pParam));
            if (messenger == null) {
                throw new IOException();
            }
            connectToRendezVous(messenger);
        } catch (IOException e) {
            throw e;
        }

        return;
    }


    /**
     * Connect to a rendezvous described by an endpoint address.
     *
     * NB: The address is modified: the serviceName memeber is set to
     * "RendezvousService" and the serviceParameter member is nillified.
     *
     * @param  addr             The address.
     * @exception  IOException  Description of Exception
     */
    public void connectToRendezVous(EndpointAddress addr)
             throws IOException {

	if (LOG.isDebugEnabled()) {
	    if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("connectToRendezVous with EndpointAddress");
	}
        if (addr == null) {
            if (LOG.isEnabledFor(Priority.WARN)) LOG.warn("bad endpoint address");
            return;
        }

	if (LOG.isDebugEnabled()) {
	    try {
		if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("    to = " + addr.getProtocolName() + "://" + addr.getProtocolAddress());
	    } catch (Exception e) {
	    }
	}
        String proto = addr.getProtocolName();

        if (!proto.equals("jxta")) {
            // Check if this address is reachable
            if (!endpoint.ping(addr)) {
                throw new IOException("Cannot ping rendezvous");
            }
        } else {
            String peerId = addr.getProtocolName() +
                    "://" + addr.getProtocolAddress();
            if (peerId.equals(localPeerAddr)) {
                // No connection to itself
                return;
            }
        }

        addr.setServiceName(pName);
        addr.setServiceParameter(pParam);

        try {
            EndpointMessenger messenger = endpoint.getMessenger(addr);
            if (messenger == null) {
                if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("no messenger");
                throw new IOException("no messenger");
            }
            connectToRendezVous(messenger);
        } catch (Exception e) {
            if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("connectToRendezVous failed", e);
            throw new IOException("connectToRendezVous failed");
        }
        return;
    }


    /**
     *Description of the Method
     *
     * @param  peer  Description of Parameter
     */
    public void reconnectToRendezVous(String peer) {

	if (LOG.isDebugEnabled()) {
	    if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("reconnectToRendezVous to " + peer);
	}
        if (peer.equals(localPeerAddr)) {
            return;
        }

        try {
            EndpointMessenger messenger =
                    endpoint.getMessenger(mkAddress(peer,
						    pName,
						    pParam));
            if (messenger == null) {
                if (LOG.isEnabledFor(Priority.WARN)) LOG.warn("Could not get messenger. aborting");
                throw new IOException("Could not get messenger. aborting");
            }

            connectToRendezVous(messenger);
        } catch (Exception e) {
            if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("reconnecteRendezVous failed ", e);
        }
        return;
    }


    /**
     * Remove a RendezVousService point.
     *
     * @param  rdv           Description of Parameter
     * @throws  IOException  if rendezVous is not connected.
     */
    public void disconnectFromRendezVous(PeerID rdv) {

        if (!isClient || (rdv == null)) {
            // Sanity check
            return;
        }

        String peerId = rdv.toString();

        // First try to get an EndpointMessenger to that peer.
        try {
            EndpointMessenger messenger =
                    endpoint.getMessenger(mkAddress(peerId,
						    pName,
						    pParam));
            if (messenger != null) {
                disconnectFromRendezVous(messenger);
            }
        } catch (Exception e) {
        }

        removeRdv(peerId);

        if (rendezVous.size() == 0) {
            // No more rendezVous
            rendezVous = null;
            isClient = false;
        }
        return;
    }


    /**
     * Returns an Enumeration of the PeerID all the RendezVousService on which this
     * Peer is currentely connected.
     *
     * @return    Description of the Returned Value
     */

    public synchronized Enumeration getConnectedRendezVous() {
        Vector result = new Vector();

	if (LOG.isDebugEnabled()) {
	    if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("getConnectedRendezVous");
	}
        if (!isClient) {
            // Sanity check
            return result.elements();
        }

        if ((rendezVous == null) || (rendezVous.size() == 0)) {
            return result.elements();
        }

        for (int i = 0; i < rendezVous.size(); ++i) {
            try {
                PeerConnection pConn = (PeerConnection) rendezVous.elementAt(i);
                String peerId = pConn.getPeer();
                net.jxta.id.ID id = IDFactory.fromURL(new URL(peerId));
                result.addElement(id);
            } catch (Exception ignored) {
		if (LOG.isEnabledFor(Priority.DEBUG))
			LOG.debug("getConnectedRendezVous failed to construct peerid ", ignored);
            }
        }

        return result.elements();
    }


    /**
     * Returns an Enumeration of the PeerID all the RendezVousService on which this
     * Peer failed to connect to.
     *
     * @return    Description of the Returned Value
     */

    public synchronized Enumeration getDisconnectedRendezVous() {
        Vector result = new Vector();

        if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("getDisconnectedRendezVous");
        if (!isClient) {
            // Sanity check
            return result.elements();
        }

        if (removedRendezVous.size() == 0) {
            return result.elements();
        }

        for (int i = 0; i < removedRendezVous.size(); ++i) {
            try {
                PeerConnection pConn = (PeerConnection)
                        removedRendezVous.elementAt(i);
                String peerId = pConn.getPeer();
                net.jxta.id.ID id = IDFactory.fromURL(new URL(peerId));
                result.addElement(id);
            } catch (Exception ignored) {
            }
        }

        return result.elements();
    }


    /**
     * Sends advertisement about other RendezVousService to a given peer
     *
     * @param  destPeer  is the advertisement of the peer to which to
     * send the RendezVousService advertisement.
     * @param  rdv       Description of Parameter
     */

    public void sendRendezVousAdv(PeerAdvertisement destPeer,
                                  PeerAdvertisement rdv) {

        // Try to get an EndpointMessenger to that peer.
        // First publish the advertisement
        try {
            // This is not our own peer adv so we must not keep it
            // longer than its expiration time.
            group.getDiscoveryService().publish(destPeer,
                                                DiscoveryService.PEER,
                                                DiscoveryService.DEFAULT_EXPIRATION,
                                                DiscoveryService.DEFAULT_EXPIRATION);

        } catch (Exception e) {
            if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("publish failed with " + e);
        }

        // Make sure that EndpointService has also the document
        publishInParentGroup (destPeer);

        String peer = destPeer.getPeerID().toString();

        // Check that the EndpointService can talk to that peer
        if (!endpoint.ping(endpoint.newEndpointAddress(peer))) {
            return;
        }

        try {
            EndpointMessenger messenger =
                    endpoint.getMessenger(mkAddress(peer,
						    pName,
						    pParam));
            if (messenger == null) {
                return;
            }

            Message msg = endpoint.newMessage();

            msg.addElement(msg.newMessageElement (RdvAdvReply,
						  new MimeMediaType("text/xml"),
						  rdv.getDocument(
								  new MimeMediaType("text/xml")).getStream()));

            messenger.sendMessage(msg);
        } catch (Exception e) {
        }
    }


    /**
     * Start the local peer as a RendezVousService peer. The caller must provide
     * a handler to an authorization manager.
     *
     * @param  handler       is the RendezVousManager that will be invoked each
     * time a new Peer request to be connected.
     * @throws  IOException  when a handler has already been connected
     */
    public synchronized void startRendezVous(RendezVousManager handler)
             throws IOException {

        if ((isRendezVous) || (handler == null)) {
            // We are already a rendez vous. Throw an exception
            if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("startRendezvous: already a rendezvous");
            throw new IOException();
        }

        manager = handler;
        clients = new Vector();
        createLocalEndpoint();
        isRendezVous = true;

        // Update the peeradv with that information:
        try {
            StructuredTextDocument params = (StructuredTextDocument)
                StructuredDocumentFactory.newStructuredDocument(

⌨️ 快捷键说明

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