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

📄 pipeserviceimpl.java

📁 jxme的一些相关程序,主要是手机上程序开发以及手机和计算机通信的一些程序资料,程序编译需要Ant支持
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

        // Recover the PipeId from the PipeServiceImpl Advertisement
        PipeID pipeId = (PipeID) pipeAdv.getPipeID().clone();
	String type = pipeAdv.getType();
	if (type == null) {
	    if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug ("No type - use UnicastType as default");
	    type = PipeService.UnicastType;
	}

        // create a OutputPipe
        OutputPipe op = null;

	if (type.equals (PipeService.UnicastType)) {

	    op = new NonBlockingOutputPipe(myGroup,
					   pipeResolver,
					   pipeAdv,
					   null,
					   timeout);
	} else if (type.equals (PipeService.UnicastSecureType)) {
	    op = new SecureOutputPipe(myGroup,
				      pipeResolver,
				      pipeAdv,
				      null,
				      timeout);
	} else if (type.equals (PipeService.PropagateType)) {
	    WirePipe wirePipe = createWirePipe (pipeAdv);
	    if (wirePipe == null) {
		throw new IOException ("Invalid propagate pipe");
	    }
	    op = wirePipe.createOutputPipe (timeout);

	} else {
	    // Unknown type
	    throw new IOException("unknown pipe type");
        }
        return op;
    }


    /**
     * create an OutputPipe from the pipe Advertisement giving a PeerId(s)
     * where the corresponding InputPipe is supposed to be.
     *
     * @param  adv           is the advertisement of the NetPipe.
     * @param  peers         is an enumeration of the PeerId of the peers where to look
     * for the corresponding Pipes
     * @param  timeout       the number of milliseconds to wait for during pipe creation
     * @return               OuputPipe
     * @since 1.0
     * @throws  IOException  if none of the peers in the enumeration has the
     * corresponding OutputPipe
     */


    // deprecated. To be removed when PipeService API change will be completed.
    public OutputPipe createOutputPipe(PipeAdvertisement adv,
				       int type,
				       Enumeration peers,
				       long timeout) 
	throws IOException 
{
	return createOutputPipe (adv, peers, timeout);
    }



    public OutputPipe createOutputPipe(PipeAdvertisement adv,
				       Enumeration peers,
				       long timeout)
     throws IOException {

        if (pipeResolver == null) throw new IOException("PipeResolver not ready");

        // Recover the PipeId from the PipeServiceImpl Advertisement
        PipeID pipeId = (PipeID) adv.getPipeID().clone();

	String type = adv.getType();
	if (type == null) {
	    if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug ("No type - use UnicastType as default");
	    type = PipeService.UnicastType;
	}

        // create a OutputPipe
        OutputPipe op = null;

        // For the time being, only support one element in the Enumeration
        // XXX: to be fixed when one to many pipe will be implemented. lomax@jxta.org
        String peer = null;
        if ((peers == null) || (!peers.hasMoreElements())) {
            throw new IOException("null peers");
        }

        try {
            peer = ((PeerID) peers.nextElement()).toString();
        } catch (Exception e) {
        }

	if (type.equals (PipeService.UnicastType)) {

                op = new NonBlockingOutputPipe(myGroup,
                        pipeResolver,
                        adv,
                        peer,
                        timeout);
	} else if (type.equals (PipeService.UnicastSecureType)) {

                op = new SecureOutputPipe(myGroup,
					  pipeResolver,
					  adv,
					  peer,
					  timeout);
	} else if (type.equals (PipeService.PropagateType)) {
	    WirePipe wirePipe = createWirePipe (adv);
	    if (wirePipe == null) {
		throw new IOException ("Invalid propagate pipe");
	    }
	    op = wirePipe.createOutputPipe (peers, timeout);

	} else {
	    // Unknown type
	    throw new IOException("unknown pipe type");
        }
        return op;
    }


    /**
     * registers a listener for a NetPipe.
     *
     * @param  pipeAdv          Input PipeService advertisement
     * @param  listener         to be called back when a pipe is resolved
     * @exception  IOException  Description of Exception
     * @since 1.0
     */

    // deprecated. To be removed when PipeService API change will be completed.
    public void createOutputPipe(PipeAdvertisement pipeAdv,
				 int type,
				 OutputPipeListener listener)
	throws IOException {

	createOutputPipe (pipeAdv, listener);
    }


    public void createOutputPipe(PipeAdvertisement pipeAdv,
				 OutputPipeListener listener)
      throws IOException {

        if (pipeResolver == null) throw new IOException("PipeResolver not ready");

        // Recover the PipeId from the PipeServiceImpl Advertisement
        PipeID pipeId = (PipeID) pipeAdv.getPipeID().clone();

	String type = pipeAdv.getType();
	if (type == null) {
	    if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug ("No type - use UnicastType as default");
	    type = PipeService.UnicastType;
	}

	if (type.equals (PipeService.UnicastType) ||
	    type.equals (PipeService.UnicastSecureType)) {

              listeners.put(pipeId.toString(),listener);
              // need to create the listener first
              PipeListener myListener = new PipeListener (pipeAdv, myGroup.getPeerID());
	      pipeResolver.addListener(pipeId.toString(), myListener);

	      pipeResolver.find(pipeAdv, 1, false);

	} else if (type.equals (PipeService.PropagateType)) {
	    // No listener can be set to propated pipes.
	    throw new IOException("Propagated PipeService do not allow OutputPipe listener" );
	} else {
	    // Unknown type
	    throw new IOException("unknown pipe type");
        }
    }


    protected class PipeListener implements PipeResolver.Listener {

	protected PipeAdvertisement pAdv = null;
	private PeerID localPeerID = null;

	private Category LOG = Category.getInstance(PipeServiceImpl.class.getName());

	public PipeListener (PipeAdvertisement pAdv, PeerID localPeerID) {
	    this.pAdv = pAdv;
	    this.localPeerID = localPeerID;
	}

	public void pipeResolverEvent(PipeResolver.Event e) {
	    // create op
	    PipeID pipeId=null;
	    try {
		pipeId = (PipeID) IDFactory.fromURL(new URL( e.getPipeID()));
	    } catch (Exception ex) {
		if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug ("Malformed pipe id : \n"
								 + e.getPipeID(), ex);
		ex.printStackTrace();
	    }
	    try {
		String pstr = e.getPipeID();
		if ( listeners.containsKey( pstr ) ) {
		    
		    OutputPipe op = null;
		    String type = pAdv.getType();
		    boolean isLocal = e.getPeerID().equals (localPeerID.toString());

		    if (type.equals (PipeService.UnicastType) || (isLocal)) {
			// In the local case, even if the pipe is secure, we return a non
			// secure OutputPipe
			op = new NonBlockingOutputPipe(myGroup,
						       pipeResolver,
						       pAdv,
						       e.getPeerID());

		    } else if (type.equals (PipeService.UnicastSecureType) || isLocal) {
			// If the destination peer is local, return a non secure outputpipe
			op = new SecureOutputPipe(myGroup,
						  pipeResolver,
						  pAdv,
						  e.getPeerID());
		    }
		    if (op != null) {
			// Only generate an event when the output pipe was succesfully opened.
			OutputPipeEvent newevent = new OutputPipeEvent (this,
									op,
									pipeId.toString(),
									e.getQueryID() );
			
			OutputPipeListener pl = (OutputPipeListener) listeners.get( pstr );
			pl.outputPipeEvent( newevent );
		    }
		}
	    } catch (IOException ie) {
		if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug ("Error creating output pipe :",ie);
	    }
	}
    }

    /**
     * Creates a new Message
     *
     * @return    Message returns a newly allocated Message.
     * @since 1.0
     */
    public Message createMessage() {
        return endpoint.newMessage();
    }


    /**
     *Adds a feature to the Addresses attribute of the PipeServiceImpl object
     *
     * @param  adv    The feature to be added to the Addresses attribute
     * @param  addrs  The feature to be added to the Addresses attribute
     * @since 1.0
     */
    public void addAddresses(PipeAdvertisement adv, Enumeration addrs) {

        if ((adv == null) || (addrs == null)) {
            return;
        }
        // Not yet implemented. In fact, this might not be implemented, and
        // instead, removed from the public API.
        // XXX: to be revisited: lomax@jxta.org
    }


    /**
     *  remove a Output pipe listener
     *
     * @param  pipeID              Description of Parameter
     * @param  listener            Description of Parameter
     * @return                     OutputPipeListener the value to which the key had been
     *  mapped in this hashtable, or null if the key did not have a mapping.
     * @since 1.0
     */

    public synchronized OutputPipeListener removeOutputPipeListener(String pipeID,
            OutputPipeListener listener) {

        if (pipeResolver == null) return null;

        pipeResolver.removeListener(pipeID);
        return ((OutputPipeListener) listeners.remove(listener));
    }

    /**
     * PropagateType pipes
     **/

    private synchronized WirePipe createWirePipe (PipeAdvertisement adv) {

	String pid = adv.getPipeID().toString();

	// First see if we have already a WirePipe for this pipe
	if (wirePipes.containsKey (pid)) {
	    // Just return it
	    return (WirePipe) wirePipes.get (pid);
	}

	// No.. There is none. Create a new one.
	WirePipe wirePipe = null;
	try {
	    wirePipe = new WirePipe (myGroup, adv);
	} catch (IllegalArgumentException ez1) {
	    if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug ("wrong pipe advertisement");
	    return null;
	}
	wirePipes.put (pid, wirePipe);
	return wirePipe;
    }

}

⌨️ 快捷键说明

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