Project JXTA

net.jxta.socket
Class JxtaSocket

java.lang.Object
  extended by java.net.Socket
      extended by net.jxta.socket.JxtaSocket
All Implemented Interfaces:
EventListener, OutputPipeListener, PipeMsgListener

public class JxtaSocket
extends Socket
implements PipeMsgListener, OutputPipeListener

JxtaSocket is a bi-directional Pipe, that behaves very much like a Socket, it creates an InputPipe and listens for pipe connection request. JxtaSocket defines its own protocol. requests arrive as a JXTA Message with the following elements :

<Cred> Credentials which can be used to determine trust </Cred>

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

<remPipe> Remote pipe advertisement </remPipe>

<reqPeer> Remote peer advertisement </remPeer>

<stream> determine whether the connection is reliable, or not </stream>

<close> close request </close>

<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  String finalLock
           
protected  PeerGroup group
           
protected  InputPipe in
           
protected  String instrLock
           
protected  boolean isStream
           
protected  Messenger msgr
           
protected  StructuredDocument myCredentialDoc
           
protected  PipeAdvertisement myPipeAdv
           
protected  net.jxta.impl.util.pipe.reliable.OutgoingMsgrAdaptor outgoing
           
protected  PeerID peerid
           
protected  PipeAdvertisement pipeAdv
           
protected  PipeService pipeSvc
           
protected  net.jxta.impl.util.UnbiasedQueue queue
           
protected  net.jxta.impl.util.pipe.reliable.ReliableInputStream ris
           
protected  net.jxta.impl.util.pipe.reliable.ReliableOutputStream ros
           
protected  InputStream stream
           
protected  int timeout
           
protected  boolean waiting
           
protected  int windowSize
           
 
Constructor Summary
  JxtaSocket()
          Constructor for the JxtaSocket, this constructor does not establish a connection use this constructor when altering the default parameters, and options of the socket by default connections are unreliable, and the default timeout is 60 seconds to alter a connection a call to create(true) changes the connection to a reliable one.
protected JxtaSocket(PeerGroup group, Messenger msgr, PipeAdvertisement pipe, StructuredDocument credDoc, boolean isStream)
          Constructor for the JxtaSocket, this constructor does not establish a connection use this constructor when altering the default parameters, and options of the socket by default connections are unreliable, and the default timeout is 60 seconds to alter a connection a call to create(true) changes the connection to a reliable one.
  JxtaSocket(PeerGroup group, PeerID peerid, PipeAdvertisement pipeAd, int timeout)
          Create a JxtaSocket to any peer listening on pipeAdv this attempts establish a connection to specified pipe within a context of group and within timeout specified in milliseconds
  JxtaSocket(PeerGroup group, PeerID peerid, PipeAdvertisement pipeAd, int timeout, boolean stream)
          Create a JxtaSocket to any peer listening on pipeAdv this attempts establish a connection to specified pipe within a context of group and within timeout specified in milliseconds
  JxtaSocket(PeerGroup group, PipeAdvertisement pipeAd)
          Create a JxtaSocket to any peer listening on pipeAdv
  JxtaSocket(PeerGroup group, PipeAdvertisement pipeAd, int timeout)
          Create a JxtaSocket to any peer listening on pipeAdv this attempts establish a connection to specified pipe within the context of the specified group within timeout specified in milliseconds
 
Method Summary
protected  int available()
          Returns the number of bytes that can be read (or skipped over) from this input stream.
 void bind(SocketAddress address)
          Unsupported operation, an IOException will be thrown
 void close()
          Closes this socket.
protected  void closeCommon()
          In stream mode, closes everything but the input stream. closeFromRemote() leaves it open until EOF is reached.
protected  void closeFromRemote()
          This is called when closure is initiated on the remote side.
 void connect(PeerGroup group, PeerID peerid, PipeAdvertisement pipeAd, int timeout)
          Connects to a remote JxtaSocket on a specific peer within a timeout specified in milliseconds
 void connect(PeerGroup group, PipeAdvertisement pipeAd)
          Connects to a remote JxtaSocket on any peer within the default timeout of 60 seconds
 void connect(PeerGroup group, PipeAdvertisement pipeAd, int timeout)
          Connects to a remote JxtaSocket on any peer within a timeout specified in milliseconds
 void connect(SocketAddress address)
          Unsupported operation, an IOException will be thrown
 void connect(SocketAddress address, int i)
          Unsupported operation, an IOException will be thrown
 void create(boolean stream)
          Creates either a stream or a datagram socket. default is a datagram
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
 InputStream getInputStream()
          Returns an input stream for this socket.
 boolean getKeepAlive()
          
 OutputStream getOutputStream()
          Returns an output stream for this socket.
 int getOutputStreamBufferSize()
          Returns the OutputStream buffer size
 int getReceiveBufferSize()
          
 int getSendBufferSize()
          
 int getSoTimeout()
          Gets the Timeout attribute of the JxtaServerSocket object
 int getTrafficClass()
          
 int getWindowSize()
          When in reliable mode, gets the Reliable library window size
 boolean isBound()
          Returns the binding state of the JxtaServerSocket.
 boolean isClosed()
          Returns the closed state of the JxtaServerSocket.
 boolean isConnected()
          
 boolean isInputShutdown()
          
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 listen(int backlog)
          Sets the maximum queue length for incoming connection indications (a request to connect) to the count argument.
 void outputPipeEvent(OutputPipeEvent event)
          the output pipe event
 void pipeMsgEvent(PipeMsgEvent event)
          we got a message
protected  int read()
          Performs on behalf of JxtaSocketInputStream.
protected  int read(byte[] b, int off, int len)
          Performs on behalf of JxtaSocketInputStream.
 void sendUrgentData(int data)
          
 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 JxtaSocket object
 void setKeepAlive(boolean state)
          
 void setOOBInline(boolean state)
          
 void setOutputStreamBufferSize(int size)
          Sets the OutputStream buffer size this operation is only valid prior to any call to getOutputStream
 void setSendBufferSize(int size)
          
static void setSocketImplFactory(SocketImplFactory factory)
          Not a supported operation, an exception will be thrown
 void setSoTimeout(int timeout)
          Sets the Timeout attribute of the JxtaServerSocket a timeout of 0 blocks forever.
 void setTrafficClass(int tc)
          
 void setWindowSize(int windowSize)
          When in reliable mode, sets the Reliable library window size
 void shutdownInput()
          
 void shutdownOutput()
          
 String toString()
          
protected  void write(byte[] buf, int offset, int length)
          Performs on behalf of JxtaSocketOutputStream.
 
Methods inherited from class java.net.Socket
getChannel, getInetAddress, getLocalAddress, getLocalPort, getLocalSocketAddress, getOOBInline, getPort, getRemoteSocketAddress, getReuseAddress, getSoLinger, getTcpNoDelay, isOutputShutdown, setPerformancePreferences, setReceiveBufferSize, setReuseAddress, setSoLinger, setTcpNoDelay
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

group

protected PeerGroup group

pipeAdv

protected PipeAdvertisement pipeAdv

myPipeAdv

protected PipeAdvertisement myPipeAdv

pipeSvc

protected PipeService pipeSvc

peerid

protected PeerID peerid

in

protected InputPipe in

connectOutpipe

protected OutputPipe connectOutpipe

msgr

protected Messenger msgr

stream

protected InputStream stream

timeout

protected int timeout

windowSize

protected int windowSize

closeLock

protected final String closeLock

acceptLock

protected final String acceptLock

instrLock

protected final String instrLock

finalLock

protected final String finalLock

closed

protected boolean closed

bound

protected boolean bound

queue

protected final net.jxta.impl.util.UnbiasedQueue queue

credential

protected Credential credential

credentialDoc

protected StructuredDocument credentialDoc

myCredentialDoc

protected StructuredDocument myCredentialDoc

isStream

protected boolean isStream

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

waiting

protected boolean waiting
Constructor Detail

JxtaSocket

public JxtaSocket()
Constructor for the JxtaSocket, this constructor does not establish a connection use this constructor when altering the default parameters, and options of the socket by default connections are unreliable, and the default timeout is 60 seconds to alter a connection a call to create(true) changes the connection to a reliable one.


JxtaSocket

protected JxtaSocket(PeerGroup group,
                     Messenger msgr,
                     PipeAdvertisement pipe,
                     StructuredDocument credDoc,
                     boolean isStream)
              throws IOException
Constructor for the JxtaSocket, this constructor does not establish a connection use this constructor when altering the default parameters, and options of the socket by default connections are unreliable, and the default timeout is 60 seconds to alter a connection a call to create(true) changes the connection to a reliable one.

Parameters:
group - group context
msgr - lightweight output pipe
pipe - PipeAdvertisement
credDoc - remote node's crendetial StructuredDocument
Throws:
IOException - if an io error occurs

JxtaSocket

public JxtaSocket(PeerGroup group,
                  PipeAdvertisement pipeAd)
           throws IOException
Create a JxtaSocket to any peer listening on pipeAdv

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

JxtaSocket

public JxtaSocket(PeerGroup group,
                  PipeAdvertisement pipeAd,
                  int timeout)
           throws IOException
Create a JxtaSocket to any peer listening on pipeAdv this attempts establish a connection to specified pipe within the context of the specified group within timeout specified in milliseconds

Parameters:
group - group context
pipeAd - PipeAdvertisement
timeout - JxtaSocket timeout in milliseconds
Throws:
IOException - if an io error occurs

JxtaSocket

public JxtaSocket(PeerGroup group,
                  PeerID peerid,
                  PipeAdvertisement pipeAd,
                  int timeout)
           throws IOException
Create a JxtaSocket to any peer listening on pipeAdv this attempts establish a connection to specified pipe within a context of group and within timeout specified in milliseconds

Parameters:
group - group context
peerid - peer to connect to
pipeAd - PipeAdvertisement
timeout - JxtaSocket timeout in milliseconds
Throws:
IOException - if an io error occurs

JxtaSocket

public JxtaSocket(PeerGroup group,
                  PeerID peerid,
                  PipeAdvertisement pipeAd,
                  int timeout,
                  boolean stream)
           throws IOException
Create a JxtaSocket to any peer listening on pipeAdv this attempts establish a connection to specified pipe within a context of group and within timeout specified in milliseconds

Parameters:
group - group context
peerid - peer to connect to
pipeAd - PipeAdvertisement
timeout - JxtaSocket timeout in milliseconds
Throws:
IOException - if an io error occurs
Method Detail

create

public void create(boolean stream)
            throws IOException
Creates either a stream or a datagram socket. default is a datagram

Parameters:
stream - if true, create a stream socket; otherwise, create a datagram socket.
Throws:
IOException - if an I/O error occurs while creating the socket.

bind

public void bind(SocketAddress address)
          throws IOException
Unsupported operation, an IOException will be thrown

Overrides:
bind in class Socket
Throws:
IOException

connect

public void connect(SocketAddress address)
             throws IOException
Unsupported operation, an IOException will be thrown

Overrides:
connect in class Socket
Throws:
IOException

connect

public void connect(SocketAddress address,
                    int i)
             throws IOException
Unsupported operation, an IOException will be thrown

Overrides:
connect in class Socket
Throws:
IOException

connect

public void connect(PeerGroup group,
                    PipeAdvertisement pipeAd)
             throws IOException
Connects to a remote JxtaSocket on any peer within the default timeout of 60 seconds

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 JxtaSocket on any peer within a timeout specified in milliseconds

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

connect

public void connect(PeerGroup group,
                    PeerID peerid,
                    PipeAdvertisement pipeAd,
                    int timeout)
             throws IOException
Connects to a remote JxtaSocket on a specific peer within a timeout specified in milliseconds

Parameters:
group - group context
peerid - peer to connect to
pipeAd - PipeAdvertisement
timeout - timeout in milliseconds
Throws:
IOException - if an io error occurs

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

listen

public void listen(int backlog)
            throws IOException
Sets the maximum queue length for incoming connection indications (a request to connect) to the count argument. If a connection indication arrives when the queue is full, the connection is refused.

Parameters:
backlog - the maximum length of the queue.
Throws:
IOException - if an I/O error occurs when creating the queue.

isBound

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

Overrides:
isBound in class Socket
Returns:
true if the ServerSocket successfully bound to an address

getOutputStreamBufferSize

public int getOutputStreamBufferSize()
Returns the OutputStream buffer size

Returns:
returns output buffer size

setOutputStreamBufferSize

public void setOutputStreamBufferSize(int size)
                               throws IOException
Sets the OutputStream buffer size this operation is only valid prior to any call to getOutputStream

Throws:
IOException

getInputStream

public InputStream getInputStream()
                           throws IOException
Returns an input stream for this socket.

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

getOutputStream

public OutputStream getOutputStream()
                             throws IOException
Returns an output stream for this socket.

Overrides:
getOutputStream in class Socket
Returns:
an output stream for writing to this socket.
Throws:
IOException - if an I/O error occurs when creating the output stream.

close

public void close()
           throws IOException
Closes this socket.

Overrides:
close in class Socket
Throws:
IOException - if an I/O error occurs when closing this socket.

closeFromRemote

protected void closeFromRemote()
                        throws IOException
This is called when closure is initiated on the remote side. output is closed without any precaution since the remote side is no longer interrested, but let the input drain, so that all the packets the other side flushed before closing have been actually delivered. If needed, ris will be close itself when read first return -1.

Throws:
IOException

closeCommon

protected void closeCommon()
                    throws IOException
In stream mode, closes everything but the input stream. closeFromRemote() leaves it open until EOF is reached. That is, until all messages in the input queue have been read. At which point, ris will close() itself.

Throws:
IOException

setInputPipe

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

Parameters:
in - The new inputPipe value

pipeMsgEvent

public void pipeMsgEvent(PipeMsgEvent event)
we got a message

Specified by:
pipeMsgEvent in interface PipeMsgListener
Parameters:
event - the message event

outputPipeEvent

public void outputPipeEvent(OutputPipeEvent event)
the output pipe event

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 Advertisment
Returns:
Messenger

getSoTimeout

public int getSoTimeout()
                 throws SocketException
Gets the Timeout attribute of the JxtaServerSocket object

Overrides:
getSoTimeout in class Socket
Returns:
The soTimeout value
Throws:
IOException - if an I/O error occurs
SocketException

setSoTimeout

public void setSoTimeout(int timeout)
                  throws SocketException
Sets the Timeout attribute of the JxtaServerSocket a timeout of 0 blocks forever. In reliable mode it is possible for this call to block trying to obtain a lock on reliable input stream

Overrides:
setSoTimeout in class Socket
Parameters:
timeout - The new soTimeout value
Throws:
IOException - if an I/O error occurs
SocketException

getWindowSize

public int getWindowSize()
When in reliable mode, gets the Reliable library window size

Returns:
The windowSize value
Throws:
IOException - if an I/O error occurs

setWindowSize

public void setWindowSize(int windowSize)
                   throws SocketException
When in reliable mode, sets the Reliable library window size

Parameters:
windowSize - The new window size value
Throws:
IOException - if an I/O error occurs
SocketException

isClosed

public boolean isClosed()
Returns the closed state of the JxtaServerSocket.

Overrides:
isClosed in class Socket
Returns:
true if the socket has been closed

write

protected void write(byte[] buf,
                     int offset,
                     int length)
              throws IOException
Performs on behalf of JxtaSocketOutputStream.

Throws:
IOException
See Also:
OutputStream.write(int)

read

protected int read()
            throws IOException
Performs on behalf of JxtaSocketInputStream.

Throws:
IOException
See Also:
InputStream.read()

read

protected int read(byte[] b,
                   int off,
                   int len)
            throws IOException
Performs on behalf of JxtaSocketInputStream.

Throws:
IOException
See Also:
InputStream.read()

available

protected int available()
                 throws IOException
Returns the number of bytes that can be read (or skipped over) from this input stream. It's possible for this call to block, when called for the very first time before the internal stream has ever been created (i.e. no data receieved)

Returns:
the number of bytes that can be read from this input stream without blocking
Throws:
IOException - - if an I/O error occurs.

getSendBufferSize

public int getSendBufferSize()
                      throws SocketException

Overrides:
getSendBufferSize in class Socket
Throws:
SocketException

setSendBufferSize

public void setSendBufferSize(int size)
                       throws SocketException

Overrides:
setSendBufferSize in class Socket
Throws:
SocketException

getReceiveBufferSize

public int getReceiveBufferSize()
                         throws SocketException

Overrides:
getReceiveBufferSize in class Socket
Throws:
SocketException

getKeepAlive

public boolean getKeepAlive()
                     throws SocketException

Overrides:
getKeepAlive in class Socket
Throws:
SocketException

getTrafficClass

public int getTrafficClass()
                    throws SocketException

Overrides:
getTrafficClass in class Socket
Throws:
SocketException

setTrafficClass

public void setTrafficClass(int tc)
                     throws SocketException

Overrides:
setTrafficClass in class Socket
Throws:
SocketException

isInputShutdown

public boolean isInputShutdown()

Overrides:
isInputShutdown in class Socket

sendUrgentData

public void sendUrgentData(int data)
                    throws IOException

Overrides:
sendUrgentData in class Socket
Throws:
IOException

setOOBInline

public void setOOBInline(boolean state)
                  throws SocketException

Overrides:
setOOBInline in class Socket
Throws:
SocketException

setKeepAlive

public void setKeepAlive(boolean state)
                  throws SocketException

Overrides:
setKeepAlive in class Socket
Throws:
SocketException

setSocketImplFactory

public static void setSocketImplFactory(SocketImplFactory factory)
                                 throws IOException
Not a supported operation, an exception will be thrown

Throws:
IOException

shutdownInput

public void shutdownInput()
                   throws IOException

Overrides:
shutdownInput in class Socket
Throws:
IOException

shutdownOutput

public void shutdownOutput()
                    throws IOException

Overrides:
shutdownOutput in class Socket
Throws:
IOException

isConnected

public boolean isConnected()

Overrides:
isConnected in class Socket

toString

public String toString()

Overrides:
toString in class Socket

JXTA J2SE