📄 pipeserviceimpl.java
字号:
/*
* $Id: PipeServiceImpl.java,v 1.2 2002/03/04 21:42:59 echtcherbina Exp $
*
* Copyright (c) 2001 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.pipe;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.net.URL;
import net.jxta.document.Advertisement;
import net.jxta.service.Service;
import net.jxta.protocol.PipeAdvertisement;
import net.jxta.protocol.ModuleImplAdvertisement;
import net.jxta.endpoint.Message;
import net.jxta.endpoint.EndpointService;
import net.jxta.peergroup.PeerGroup;
import net.jxta.pipe.PipeService;
import net.jxta.pipe.OutputPipe;
import net.jxta.pipe.InputPipe;
import net.jxta.pipe.PipeMsgListener;
import net.jxta.pipe.OutputPipeListener;
import net.jxta.pipe.PipeID;
import net.jxta.pipe.OutputPipeEvent;
import net.jxta.pipe.OutputPipeListener;
import net.jxta.peer.PeerID;
import net.jxta.id.IDFactory;
import net.jxta.id.ID;
import org.apache.log4j.Category; import org.apache.log4j.Priority;
/**
* This class implements the NetPipe interface.
*
*/
public class PipeServiceImpl implements PipeService {
private final static Category LOG = Category.getInstance(PipeServiceImpl.class.getName());
private static Hashtable queueTable = null;
private PeerGroup myGroup = null;
private EndpointService endpoint = null;
private PipeResolver pipeResolver = null;
private ModuleImplAdvertisement implAdvertisement = null;
private Hashtable listeners = new Hashtable();
/**
* Propagate PipeService variables
**/
private Hashtable wirePipes = new Hashtable();
/**
* Constructor: this constructor is only needed in order to create
* the listener thread of the NodePipe.
*
* @since 1.0
*/
public PipeServiceImpl() { }
/**
* Service objects are not manipulated directly to protect usage
* of the service. A Service interface is returned to access the service
* methods.
*
* @return Service public interface of the service
* @since 1.0
*/
public Service getInterface() {
return new PipeServiceInterface(this);
}
/**
* Returns the advertisement for that service.
*
* @return Advertisement the advertisement.
* @since 1.0
*/
public Advertisement getImplAdvertisement() {
return implAdvertisement;
}
/**
* Supply arguments and starts this service if it hadn't started by itself.
*
* Currently this service does not expect arguments.
*
* @param arg A table of strings arguments.
* @return int status indication.
* @since 1.0
*/
public int startApp(String[] arg) {
// create our resolver handler; it will register itself w/ the resolver.
pipeResolver = new PipeResolver(myGroup);
return 0;
}
/**
* Ask this service to stop.
*
* @since 1.0
*/
public void stopApp() {
//XXX what else do we need to do?
if (pipeResolver == null) return;
pipeResolver.stop();
pipeResolver = null;
}
/**
* Init routine
*
* @param g group associated in which this service operates in
* @param sadv the PipeService service advertisement
* @since 1.0
*/
public void init(PeerGroup pg, ID assignedID, Advertisement impl) {
implAdvertisement = (ModuleImplAdvertisement) impl;
myGroup = pg;
// Creating the hash table that will contain all the input queues.
queueTable = new Hashtable();
// Get the endpoint.
endpoint = myGroup.getEndpointService();
}
/**
* Creates an InputPipe of a NetPipe.
*
* @param adv is the public advertisement of the NetPipe.
* @return InputPipe returns a InputPipe associated to the NetPipe.
* @exception IOException Description of Exception
* @since 1.0
*/
public InputPipe createInputPipe(PipeAdvertisement adv)
throws IOException {
InputPipe ip = null;
String type = adv.getType();
PipeID pipeId = (PipeID) adv.getPipeID().clone();
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug ("createInputPipe: " + pipeId.toString());
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug (" type: " + type);
if (type.equals (PipeService.UnicastType)) {
if (pipeResolver == null) throw new IOException("PipeResolver not ready");
// create an InputPipe.
ip = new InputPipeImpl(myGroup,
pipeResolver,
pipeId);
} else if (type.equals (PipeService.UnicastSecureType)) {
if (pipeResolver == null) throw new IOException("PipeResolver not ready");
// create an InputPipe.
ip = new SecureInputPipeImpl(myGroup,
pipeResolver,
pipeId);
} else if (type.equals (PipeService.PropagateType)) {
WirePipe wirePipe = createWirePipe (adv);
if (wirePipe == null) {
throw new IOException ("Invalid propagate pipe");
}
ip = wirePipe.createInputPipe ();
} else {
// Unknown type
throw new IOException("unknown pipe type");
}
return ip;
}
/**
* create an InputPipe from a pipe Advertisement
*
* @param adv is the advertisement of the PipeServiceImpl.
* @param listener PipeMsgListener to receive msgs.
* @return InputPipe InputPipe object created
*
* @exception IOException error creating input pipe
*
* @since JXTA 1.0
*/
public InputPipe createInputPipe(
PipeAdvertisement adv,
PipeMsgListener listener)
throws IOException {
String type = adv.getType();
PipeID pipeId = (PipeID) adv.getPipeID().clone();
InputPipe ip = null;
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug ("createInputPipe: " + pipeId.toString());
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug (" type: " + type);
if (type.equals (PipeService.UnicastType)) {
if (pipeResolver == null) throw new IOException("PipeResolver not ready");
// create an InputPipe.
ip = new InputPipeImpl(myGroup,
pipeResolver,
pipeId,
listener );
} else if (type.equals (PipeService.UnicastSecureType)) {
if (pipeResolver == null) throw new IOException("PipeResolver not ready");
// create an InputPipe.
ip = new SecureInputPipeImpl(myGroup,
pipeResolver,
pipeId,
listener );
} else if (type.equals (PipeService.PropagateType)) {
WirePipe wirePipe = createWirePipe (adv);
if (wirePipe == null) {
throw new IOException ("Invalid propagate pipe");
}
ip = wirePipe.createInputPipe (listener);
} else {
// Unknown type
throw new IOException("unknown pipe type");
}
return ip;
}
/**
* Creates an OutputPipe associated to a NetPipe.
*
* @param timeout the number of milliseconds to wait during pipe creation
* @param pipeAdv Description of Parameter
* @return OutputPipe returns an OutputPipe associated to the
* destination NetPipe.
* @exception IOException Description of Exception
* @since 1.0
*/
// deprecated. To be removed when PipeService API change will be completed.
public OutputPipe createOutputPipe(PipeAdvertisement pipeAdv, int type, long timeout) throws IOException {
return createOutputPipe (pipeAdv, timeout);
}
public OutputPipe createOutputPipe(PipeAdvertisement pipeAdv, long timeout) throws IOException {
if (pipeResolver == null) throw new IOException("PipeResolver not ready");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -