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

📄 rendaddrcompactor.java

📁 jxme的一些相关程序,主要是手机上程序开发以及手机和计算机通信的一些程序资料,程序编译需要Ant支持
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
			    if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("Exception " + e);
			    continue;
			}
		    }
		}
	    }
        } catch ( Throwable all ) {
            if (LOG.isEnabledFor(Priority.FATAL)) LOG.fatal( "Uncaught Throwable in thread: " + Thread.currentThread().getName(), all );
        }
    }

    protected void processRdvAdv (Vector rdvs) {

	if ((rdvs == null) || (rdvs.size() == 0)) {
	    // Nothing to do
	    return;
	}

	// Get all the peers, rendezvous and clients, this peer is already connected to.
	Vector connectedRdvs = new Vector();
	Enumeration rdvsEnum = null;
	rdvsEnum = rendezvous.getConnectedRendezVous();
	if ( (rdvsEnum != null) && rdvsEnum.hasMoreElements() ) {

	    while (rdvsEnum.hasMoreElements()) {
		try {
		    connectedRdvs.addElement(rdvsEnum.nextElement());
		} catch (Exception e) {
		    break;
		}
	    }
	}

	rdvsEnum = rendezvous.getConnectedPeers();
	if ( (rdvsEnum != null) && rdvsEnum.hasMoreElements() ) {

	    while (rdvsEnum.hasMoreElements()) {
		try {
		    connectedRdvs.addElement(rdvsEnum.nextElement());
		} catch (Exception e) {
		    break;
		}
	    }
	}

	Vector result = new Vector();
	// Eliminate the rdvs in the list of the peers we already are connected to.
	for (int i = 0; i < rdvs.size(); ++i) {
	    try {
		RdvAdvertisement adv = (RdvAdvertisement) rdvs.elementAt (i);
		// Check if we already have a connection with this peer.

		String peer = adv.getPeerID().toString();
		if (!connectedRdvs.contains (peer)) {
		    // Let's keep that one
		    //PDA requirements 18.02.2002
                    //java.util.Vector.add(Object obj) -> java.util.Vector.addElement(Object obj)
                    //result.add (peer);
                    result.addElement(peer);
		    //PDA requirements 18.02.2002
		}

	    } catch (Exception ez1) {
		if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug ("processRdvAdv failed ", ez1);
		continue;
	    }
	}
	connect (result, 1);
    }

    private void connect(Vector params, int nbOfThreads) {

        if ((params == null) || (params.size() == 0)) {
            // Nothing to do
            return;
        }

        String peerId = null;
        Stack addrs = new Stack();

        for (int i = 0; i < params.size(); ++i) {
            try {
                peerId = (String) params.elementAt(i);
                if (peerId.equals(localPeerId)) {
                    // this is the local peer. Discard
                    continue;
                }
                // Connect to this new rendezvous
                try {
                    if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("  Connecting...");
                    EndpointAddress address = mkAddress(peerId);
		    addrs.push(address);
                } catch (Exception e) {
                    if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("Cannot connect to Rendezvous at " + peerId);
                }
            } catch (Exception ex) {
                if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("  failed with " + ex);
                continue;
            }
        }

	// Creates threads to asynchronously connect to the rendezcvous
	for (int i = 0; i < nbOfThreads; ++i) {
	    Thread th = new Thread(new ConnectThread(addrs, rendezvous, this),
				   "Rendezvous Connect Thread");

	    th.start();
            Thread.currentThread().yield();
	}
    }


    private void searchRemote(String gid) {

        if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("searching remote");
        if (gid == null) {
            return;
        }
        // If we are here, we didn't yet had enough rendezvous. Try to find some more.
        discovery.getRemoteAdvertisements(null,
                                          DiscoveryService.ADV,
                                          "RdvGroupId",
                                          gid,
                                          MaxNbOfRdvs,
					  rdvListener);
    }

    private Vector searchLocal(String gid) {

        if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("searching local");
        if (gid == null) {
            return null;
        }
        // If we are here, we didn't yet had enough rendezvous. Try to find some more.
        Enumeration advs = null;
        try {
            advs = discovery.getLocalAdvertisements( DiscoveryService.ADV,
						     "RdvGroupId",
						     gid);
        } catch (IOException e) {
            if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("Caught IOException");
            return null;
        }

        if ((advs == null) || (!advs.hasMoreElements())) {
            return null;
        }

        Vector rdvs = new Vector();

        while (advs.hasMoreElements()) {
            try {
                RdvAdvertisement radv = (RdvAdvertisement) advs.nextElement();
                rdvs.addElement(radv.getPeerID().toString());
            } catch (Exception all) {
                if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("Failed with " + all);
            }
        }
        return rdvs;
    }

    /**
     *  Attempt to establish a connection to predefined rendezvous points
     *  on a thread for each, this is optimiztion for not blocking
     */

    protected class ConnectThread implements Runnable {

        private Stack addrs = null;
        private RendezVousService rdv = null;
        private RendAddrCompactor compactor = null;

        public ConnectThread(Stack addrs,
			     RendezVousService rdv,
                             RendAddrCompactor compactor) {
            this.addrs = addrs;
            this.rdv = rdv;
            this.compactor = compactor;
        }

        public void run() {

            Enumeration rdvsEnum = rendezvous.getConnectedRendezVous();
	    int nbOfRdvs = 0;
	    if (rdvsEnum != null) {
		try {
		    while (rdvsEnum.hasMoreElements()) {
			rdvsEnum.nextElement();
			++nbOfRdvs;
		    }
		} catch (Exception ez1) {
		    // Nothing we can do..
		}
		if (nbOfRdvs >= compactor.MaxNbOfRdvs) {
		    // we don't need to connect to this rendezvous. We already have enough
		    return;
		}
	    }

            try {
                while (addrs.size() > 0) {
                    if (compactor.stopping) return;
                    try {
                        EndpointAddress addr = (EndpointAddress) addrs.pop();
                        rdv.connectToRendezVous(addr);
                    } catch (EmptyStackException e1) {
                        // Stack is empty. Return
                        return;
                    } catch (Exception e2) {
                        // The connection has failed... next.
                        continue;
                    }
                }

            } catch ( Throwable all ) {
                if (LOG.isEnabledFor(Priority.FATAL)) LOG.fatal( "Uncaught Throwable in thread :" + Thread.currentThread().getName(), all );
            }
        }
    }


    class RdvListener implements DiscoveryListener {
	private RendAddrCompactor caller = null;

	public RdvListener (RendAddrCompactor caller) {
	    this.caller = caller;
	}

	public void discoveryEvent (DiscoveryEvent e) {

	    DiscoveryResponseMsg rep = e.getResponse();
	    Enumeration enum = rep.getResponses();
	    if ((enum == null) || (!enum.hasMoreElements())) {
		return;
	    }

	    boolean changed = false;
	    Vector rdvs = new Vector ();
	    while (enum.hasMoreElements()) {

		try {
		    // Try to get an advertisement from the responses.
		    String advString =  (String) enum.nextElement();
		    RdvAdvertisement adv = (RdvAdvertisement) AdvertisementFactory.newAdvertisement
			(new MimeMediaType("text/xml"),
			 new ByteArrayInputStream (advString.getBytes()));

		    if (adv != null) {
			//PDA requirements 18.02.2002
                        //java.util.Vector.add(Object obj) -> java.util.Vector.addElement(Object obj)
                        //rdvs.add (adv);
                        rdvs.addElement(adv);
			//PDA requirements 18.02.2002
		    }

		} catch (Exception ez1) {
		    continue;
		}
	    }
	    if (rdvs.size() > 0) {
		caller.processRdvAdv (rdvs);
	    }
	}
    }
}

⌨️ 快捷键说明

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