Project JXTA

net.jxta.util
Class JxtaBiDiPipe

java.lang.Object
  extended bynet.jxta.util.JxtaBiDiPipe
All Implemented Interfaces:
EventListener, OutputPipeListener, PipeMsgListener, net.jxta.impl.util.pipe.reliable.ReliableInputStream.MsgListener

public class JxtaBiDiPipe
extends Object
implements PipeMsgListener, OutputPipeListener, net.jxta.impl.util.pipe.reliable.ReliableInputStream.MsgListener

JxtaBiDiPipe is a bi-directional Pipe, it creates an InputPipe for incoming Messages, and EndpointMessenger for outgoing messages JxtaBiDiPipe defines its own protocol for negotiating connections. Connection requests arrive as a JXTA Message with the following elements :

<Credential> to determine whether requestor has the proper access to be granted a connection

<reqPipe> Requestor's pipe advertisement </reqPipe>

<remPipe> remote pipe advertisement </remPipe>

<remPeer> remote peer advertisement </remPeer>

<reliable> Reliability setting ("true", or "false") </reliable>

<data> data <data>


Field Summary
protected  String acceptLock
           
protected  boolean bound
           
protected  boolean closed
           
protected  String closeLock
           
protected  OutputPipe connectOutpipe
           
protected  Credential credential
           
protected  StructuredDocument credentialDoc
           
protected  PipeEventListener eventListener
           
protected  String finalLock
           
protected  PeerGroup group
           
protected  InputPipe in
           
protected  boolean isReliable
           
protected  PipeMsgListener listener
           
protected  Messenger msgr
           
protected  StructuredDocument myCredentialDoc
           
protected  PipeAdvertisement myPipeAdv
           
protected  net.jxta.impl.util.pipe.reliable.OutgoingMsgrAdaptor outgoing
           
static int PIPE_CLOSED_EVENT
          Pipe close Event
protected  PipeAdvertisement pipeAdv
           
protected  PipeService pipeSvc
           
protected  net.jxta.impl.util.pipe.reliable.ReliableInputStream ris
           
protected  net.jxta.impl.util.pipe.reliable.ReliableOutputStream ros
           
protected  InputStream stream
           
protected  boolean waiting
           
 
Constructor Summary
  JxtaBiDiPipe()
          JxtaBiDiPipe A bidirectional pipe Creates a new object with a default timeout of 60,000ms, and a reliability setting of false
protected JxtaBiDiPipe(PeerGroup group, Messenger msgr, PipeAdvertisement pipe, StructuredDocument credDoc, boolean isReliable)
          JxtaBiDiPipe A bidirectional pipe
  JxtaBiDiPipe(PeerGroup group, PipeAdvertisement pipeAd, int timeout, PipeMsgListener listener)
          attempts to create a bidirectional connection to remote peer within default timeout of 1 minutes, and initiates a connection
  JxtaBiDiPipe(PeerGroup group, PipeAdvertisement pipeAd, int timeout, PipeMsgListener listener, boolean reliable)
          attempts to create a bidirectional connection to remote peer within default timeout of 60,000ms, and initiates a connection
  JxtaBiDiPipe(PeerGroup group, PipeAdvertisement pipeAd, PipeMsgListener listener)
          attempts to create a bidirectional connection to remote peer within default timeout of 60,000ms, and initiates a connection
 
Method Summary
protected  void accept(JxtaBiDiPipe s)
          Accepts a connection
protected  boolean checkCred(StructuredDocument cred)
          Not implemented yet
 void close()
          Closes this pipe.
protected  void closePipe()
           
 void connect(PeerGroup group, PeerID peerid, PipeAdvertisement pipeAd, int timeout, PipeMsgListener listener)
          Connects to a remote JxtaBiDiPipe
 void connect(PeerGroup group, PeerID peerid, PipeAdvertisement pipeAd, int timeout, PipeMsgListener listener, boolean reliable)
          Connects to a remote JxtaBiDiPipe
 void connect(PeerGroup group, PipeAdvertisement pipeAd)
          Connect to JxtaBiDiPipe with default timeout
 void connect(PeerGroup group, PipeAdvertisement pipeAd, int timeout)
          Connects to a remote JxtaBiDiPipe
protected  Message createOpenMessage(PeerGroup group, PipeAdvertisement pipeAd)
          Create a connection request message
protected static StructuredDocument getCredDoc(PeerGroup group)
          obtain the cred doc from the group object
 StructuredDocument getCredentialDoc()
          get the remote credential doc
 InputPipe getInputPipe()
          Returns an input stream for this socket.
 PipeMsgListener getListener()
          Returns the message listener for this pipe
 Message getMessage(int timeout)
          Gets a message from the queue.
 Messenger getMessenger()
          Deprecated. use sendMessage instead
 PipeAdvertisement getPipeAdvertisement()
          Returns the Assigned PipeAdvertisement
 PeerAdvertisement getRemotePeerAdvertisement()
          Returns remote PeerAdvertisement
 PipeAdvertisement getRemotePipeAdvertisement()
          Returns remote PipeAdvertisement
 boolean isBound()
          Returns the binding state of the JxtaServerPipe.
protected static Messenger lightweightOutputPipe(PeerGroup group, PipeAdvertisement pipeAdv, PeerAdvertisement peer)
          A lightweight output pipe constructor, note the return type Since all the info needed is available, there's no need for to use the pipe service to resolve the pipe we have all we need to construct a messenger.
 void outputPipeEvent(OutputPipeEvent event)
          Called when a input pipe has been located for a previously registered pipe. The event contains an OutputPipe which can be used to communicate with the remote peer.
 void pipeMsgEvent(PipeMsgEvent event)
          Called for each pipe message event that occurs.
 void processIncomingMessage(Message message)
          /** This method is invoked by the Reliablity library for each incoming data message
 boolean sendMessage(Message msg)
          Send a message
 void setCredentialDoc(StructuredDocument doc)
          Sets the connection credential doc If no credentials are set, the default group credential will be used
protected  void setInputPipe(InputPipe in)
          Sets the inputPipe attribute of the JxtaBiDiPipe object
 void setListener(PipeEventListener listener)
          Sets a Pipe event listener, set listener to null to unset the listener
 void setListener(PipeMsgListener listener)
          Sets message listener for a pipe spawned by the JxtaServerPipe.
 void setReliable(boolean reliable)
          Toggles reliability
protected  void setRemotePeerAdvertisement(PeerAdvertisement peer)
          Sets the remote PeerAdvertisement
protected  void setRemotePipeAdvertisement(PipeAdvertisement pipe)
          Sets the remote PipeAdvertisement
protected  void waiter(int timeMilisecs)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

group

protected PeerGroup group

pipeAdv

protected PipeAdvertisement pipeAdv

myPipeAdv

protected PipeAdvertisement myPipeAdv

pipeSvc

protected PipeService pipeSvc

in

protected InputPipe in

connectOutpipe

protected OutputPipe connectOutpipe

msgr

protected Messenger msgr

stream

protected InputStream stream

closeLock

protected final String closeLock

acceptLock

protected final String acceptLock

finalLock

protected final String finalLock

closed

protected boolean closed

bound

protected boolean bound

listener

protected PipeMsgListener listener

eventListener

protected PipeEventListener eventListener

credential

protected Credential credential

waiting

protected boolean waiting

isReliable

protected boolean isReliable

outgoing

protected net.jxta.impl.util.pipe.reliable.OutgoingMsgrAdaptor outgoing

ris

protected net.jxta.impl.util.pipe.reliable.ReliableInputStream ris

ros

protected net.jxta.impl.util.pipe.reliable.ReliableOutputStream ros

credentialDoc

protected StructuredDocument credentialDoc

myCredentialDoc

protected StructuredDocument myCredentialDoc

PIPE_CLOSED_EVENT

public static final int PIPE_CLOSED_EVENT
Pipe close Event

See Also:
Constant Field Values
Constructor Detail

JxtaBiDiPipe

protected JxtaBiDiPipe(PeerGroup group,
                       Messenger msgr,
                       PipeAdvertisement pipe,
                       StructuredDocument credDoc,
                       boolean isReliable)
                throws IOException
JxtaBiDiPipe A bidirectional pipe

Parameters:
group - group context
msgr - lightweight output pipe
pipe - PipeAdvertisement
isReliable - Whether the connection is reliable or not
Throws:
IOException - if an io error occurs

JxtaBiDiPipe

public JxtaBiDiPipe()
JxtaBiDiPipe A bidirectional pipe Creates a new object with a default timeout of 60,000ms, and a reliability setting of false


JxtaBiDiPipe

public JxtaBiDiPipe(PeerGroup group,
                    PipeAdvertisement pipeAd,
                    PipeMsgListener listener)
             throws IOException
attempts to create a bidirectional connection to remote peer within default timeout of 60,000ms, and initiates a connection

Parameters:
group - group context
pipeAd - PipeAdvertisement
listener - application PipeMsgListener
Throws:
IOException - if an io error occurs

JxtaBiDiPipe

public JxtaBiDiPipe(PeerGroup group,
                    PipeAdvertisement pipeAd,
                    int timeout,
                    PipeMsgListener listener)
             throws IOException
attempts to create a bidirectional connection to remote peer within default timeout of 1 minutes, and initiates a connection

Parameters:
group - group context
pipeAd - PipeAdvertisement
listener - application PipeMsgListener
Throws:
IOException - if an io error occurs

JxtaBiDiPipe

public JxtaBiDiPipe(PeerGroup group,
                    PipeAdvertisement pipeAd,
                    int timeout,
                    PipeMsgListener listener,
                    boolean reliable)
             throws IOException
attempts to create a bidirectional connection to remote peer within default timeout of 60,000ms, and initiates a connection

Parameters:
group - group context
pipeAd - PipeAdvertisement
listener - application PipeMsgListener
Throws:
IOException - if an io error occurs
Method Detail

connect

public void connect(PeerGroup group,
                    PipeAdvertisement pipeAd)
             throws IOException
Connect to JxtaBiDiPipe with default timeout

Parameters:
group - group context
pipeAd - PipeAdvertisement
Throws:
IOException - if an io error occurs

connect

public void connect(PeerGroup group,
                    PipeAdvertisement pipeAd,
                    int timeout)
             throws IOException
Connects to a remote JxtaBiDiPipe

Parameters:
group - group context
pipeAd - PipeAdvertisement
timeout - timeout in ms, also reset object default timeout to that of timeout
Throws:
IOException - if an io error occurs

connect

public void connect(PeerGroup group,
                    PeerID peerid,
                    PipeAdvertisement pipeAd,
                    int timeout,
                    PipeMsgListener listener)
             throws IOException
Connects to a remote JxtaBiDiPipe

Parameters:
group - group context
peerid - peer to connect to
pipeAd - PipeAdvertisement
timeout - timeout in ms, also reset object default timeout to that of timeout
Throws:
IOException - if an io error occurs

connect

public void connect(PeerGroup group,
                    PeerID peerid,
                    PipeAdvertisement pipeAd,
                    int timeout,
                    PipeMsgListener listener,
                    boolean reliable)
             throws IOException
Connects to a remote JxtaBiDiPipe

Parameters:
group - group context
peerid - peer to connect to
pipeAd - PipeAdvertisement
timeout - timeout in ms, also reset object default timeout to that of timeout
reliable - Reliable connection
Throws:
IOException - if an io error occurs

setReliable

public void setReliable(boolean reliable)
                 throws IOException
Toggles reliability

Parameters:
reliable - Toggles reliability to reliable
Throws:
IOEXecption - if pipe is bound
IOException

getCredDoc

protected static StructuredDocument getCredDoc(PeerGroup group)
obtain the cred doc from the group object

Parameters:
group - group context
Returns:
The credDoc value

getCredentialDoc

public StructuredDocument getCredentialDoc()
get the remote credential doc

Returns:
Credential StructuredDocument

setCredentialDoc

public void setCredentialDoc(StructuredDocument doc)
Sets the connection credential doc If no credentials are set, the default group credential will be used

Parameters:
doc - Credential StructuredDocument

createOpenMessage

protected Message createOpenMessage(PeerGroup group,
                                    PipeAdvertisement pipeAd)
                             throws IOException
Create a connection request message

Parameters:
group - group context
pipeAd - pipe advertisement
Returns:
the Message object
Throws:
IOException

accept

protected void accept(JxtaBiDiPipe s)
               throws IOException
Accepts a connection

Parameters:
s - the accepted connection.
Throws:
IOException - if an I/O error occurs when accepting the connection.

isBound

public boolean isBound()
Returns the binding state of the JxtaServerPipe.

Returns:
true if the ServerSocket successfully bound to an address

getInputPipe

public InputPipe getInputPipe()
                       throws IOException
Returns an input stream for this socket.

Returns:
a stream for reading from this socket.
Throws:
IOException - if an I/O error occurs when creating the input stream.

getMessenger

public Messenger getMessenger()
                       throws IOException
Deprecated. use sendMessage instead

Returns the messenger to the remote pipe Note that this method will block until a messenger is created. This is especially likely when this is a server and are waiting to connect to a client.

Returns:
the pipe messenger
Throws:
IOException - if an I/O error occurs when

waiter

protected void waiter(int timeMilisecs)

getRemotePeerAdvertisement

public PeerAdvertisement getRemotePeerAdvertisement()
Returns remote PeerAdvertisement

Returns:
remote PeerAdvertisement

getRemotePipeAdvertisement

public PipeAdvertisement getRemotePipeAdvertisement()
Returns remote PipeAdvertisement

Returns:
remote PipeAdvertisement

setRemotePeerAdvertisement

protected void setRemotePeerAdvertisement(PeerAdvertisement peer)
Sets the remote PeerAdvertisement

Parameters:
peer - Remote PeerAdvertisement

setRemotePipeAdvertisement

protected void setRemotePipeAdvertisement(PipeAdvertisement pipe)
Sets the remote PipeAdvertisement

Parameters:
pipe - PipeAdvertisement

close

public void close()
           throws IOException
Closes this pipe.

Throws:
IOException - if an I/O error occurs when closing this socket.

closePipe

protected void closePipe()
                  throws IOException
Throws:
IOException

setInputPipe

protected void setInputPipe(InputPipe in)
Sets the inputPipe attribute of the JxtaBiDiPipe object

Parameters:
in - The new inputPipe value

pipeMsgEvent

public void pipeMsgEvent(PipeMsgEvent event)
Called for each pipe message event that occurs.

Specified by:
pipeMsgEvent in interface PipeMsgListener
Parameters:
event - The event being received.

processIncomingMessage

public void processIncomingMessage(Message message)
/** This method is invoked by the Reliablity library for each incoming data message

Specified by:
processIncomingMessage in interface net.jxta.impl.util.pipe.reliable.ReliableInputStream.MsgListener
Parameters:
message - Incoming message

sendMessage

public boolean sendMessage(Message msg)
                    throws IOException
Send a message

Parameters:
msg - Message to send to the remote side
Throws:
IOException

outputPipeEvent

public void outputPipeEvent(OutputPipeEvent event)
Called when a input pipe has been located for a previously registered pipe. The event contains an OutputPipe which can be used to communicate with the remote peer.

Specified by:
outputPipeEvent in interface OutputPipeListener
Parameters:
event - the event

lightweightOutputPipe

protected static Messenger lightweightOutputPipe(PeerGroup group,
                                                 PipeAdvertisement pipeAdv,
                                                 PeerAdvertisement peer)
A lightweight output pipe constructor, note the return type Since all the info needed is available, there's no need for to use the pipe service to resolve the pipe we have all we need to construct a messenger.

Parameters:
group - group context
pipeAdv - Remote Pipe Advertisement
peer - Remote Peer advertisement
Returns:
Messenger

checkCred

protected boolean checkCred(StructuredDocument cred)
Not implemented yet


getListener

public PipeMsgListener getListener()
Returns the message listener for this pipe

Returns:
PipeMsgListener

setListener

public void setListener(PipeMsgListener listener)
Sets message listener for a pipe spawned by the JxtaServerPipe. There is a window where a message could arrive prior to listener being registered therefore a message queue is created to queue messages, once a listener is registered these messages will be dequeued by calling the listener until the queue is empty

Parameters:
listener - New value of property listener.

setListener

public void setListener(PipeEventListener listener)
Sets a Pipe event listener, set listener to null to unset the listener

Parameters:
listener - New value of property listener.

getMessage

public Message getMessage(int timeout)
                   throws InterruptedException
Gets a message from the queue. If no Object is immediately available, then wait the specified amount of time for a message to be inserted.

Parameters:
timeout - Amount of time to wait in milliseconds for an object to be available. Per Java convention, a timeout of zero (0) means wait an infinite amount of time. Negative values mean do not wait at all.
Returns:
The next message in the queue., if a listener is registered calls to this method will return null
Throws:
InterruptedException - if the operation is interrupted before the timeout interval is completed.

getPipeAdvertisement

public PipeAdvertisement getPipeAdvertisement()
Returns the Assigned PipeAdvertisement

Returns:
the Assigned PipeAdvertisement

JXTA J2SE