JXTA

net.jxta.socket
Class JxtaMulticastSocket

java.lang.Object
  extended by java.net.DatagramSocket
      extended by java.net.MulticastSocket
          extended by net.jxta.socket.JxtaMulticastSocket
All Implemented Interfaces:
EventListener, PipeMsgListener

public class JxtaMulticastSocket
extends MulticastSocket
implements PipeMsgListener

The JxtaMulticastSocket class is useful for sending and receiving JXTA multicast packets. A JxtaMulticastSocket is a (UDP) DatagramSocket, with additional capabilities for joining "groups" of other multicast hosts on the internet. A multicast group is specified within the context of PeerGroup and a propagate pipe advertisement. One would join a multicast group by first creating a MulticastSocket with the desired peer group and pipe advertisement :

  // join a Multicast group and send the group salutations
  ...
  String msg = "Hello";
  MulticastSocket s = new JxtaMulticastSocket(peergroup, propPipeAdv);
  //We are joined at this point
  DatagramPacket hi = new DatagramPacket(msg.getBytes(), msg.length());
  s.send(hi);
  // get their responses!
  byte[] buf = new byte[1000];
  DatagramPacket recv = new DatagramPacket(buf, buf.length);
  s.receive(recv);
  ...
  // OK, I'm done talking - leave the group...
  s.close();
 

One can also respond only to the sender of the datagram as follows :

  DatagramPacket res = new DatagramPacket(response.getBytes(), response.length());
  res.setAddress(recv.getAddress());
  s.send(res);
 

When one sends a message to a multicast group, all subscribing recipients to that peergroup and pipe receive the message (including themselves) When a socket subscribes to a multicast group/port, it receives datagrams sent by other hosts to the group/pipe, as do all other members of the group and pipe. A socket relinquishes membership in a group by the close() method. Multiple MulticastSocket's may subscribe to a multicast group and pipe concurrently, and they will all receive group datagrams.

When a datagram is sent it carries along with the peerid of the sender. The PeerID is represented as a InetAddress in the form of host/ipadress where host name is the peerid, and ip address is always represented as 0.0.0.0 since it is meaningless in the context of JXTA. e.g of InetAddress resembles the following:

 uuid-59616261646162614A787461503250339C6014B0F21A49DBBDF2ADBDDBCB314703/0.0.0.0
 


Field Summary
protected  boolean bound
           
protected  boolean closed
           
protected  Credential credential
           
protected  StructuredDocument credentialDoc
           
static String DATATAG
           
protected  PeerGroup group
           
protected  InputPipe in
           
protected  InetAddress localAddress
           
static String NAMESPACE
           
protected  OutputPipe outputPipe
           
protected  PipeAdvertisement pipeAdv
           
protected  PipeService pipeSvc
           
protected  ProducerBiasedQueue queue
           
protected  SocketAddress socketAddress
           
static String SRCIDTAG
           
 
Constructor Summary
JxtaMulticastSocket(PeerGroup group, PipeAdvertisement pipeAd)
          Create a multicast socket and bind it to a specific pipe within specified peer group
 
Method Summary
 void bind(SocketAddress addr)
          
 void close()
          Closes this MutlicastSocket.
protected static StructuredDocument getCredDoc(PeerGroup group)
          Obtain the credential doc from the group object
 InetAddress getLocalAddress()
          
 SocketAddress getLocalSocketAddress()
          
 int getSoTimeout()
          Gets the Timeout attribute of the JxtaMulticastSocket
 boolean isBound()
          Returns the binding state of the MutlicastSocket.
 boolean isClosed()
          Returns the closed state of the JxtaMulticastSocket.
 void joinGroup(PeerGroup group, PipeAdvertisement pipeAd)
          joins MutlicastSocket to specified pipe within the context of group
 void pipeMsgEvent(PipeMsgEvent event)
          Called for each pipe message event that occurs.
 void receive(DatagramPacket packet)
          
 void send(DatagramPacket packet)
          
 void setSoTimeout(int timeout)
          Sets the Timeout attribute of the JxtaMulticastSocket a timeout of 0 blocks forever, by default this Socket's timeout is set to 0
 
Methods inherited from class java.net.MulticastSocket
getInterface, getLoopbackMode, getNetworkInterface, getTimeToLive, getTTL, joinGroup, joinGroup, leaveGroup, leaveGroup, send, setInterface, setLoopbackMode, setNetworkInterface, setTimeToLive, setTTL
 
Methods inherited from class java.net.DatagramSocket
connect, connect, disconnect, getBroadcast, getChannel, getInetAddress, getLocalPort, getPort, getReceiveBufferSize, getRemoteSocketAddress, getReuseAddress, getSendBufferSize, getTrafficClass, isConnected, setBroadcast, setDatagramSocketImplFactory, setReceiveBufferSize, setReuseAddress, setSendBufferSize, setTrafficClass
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NAMESPACE

public static final String NAMESPACE
See Also:
Constant Field Values

DATATAG

public static final String DATATAG
See Also:
Constant Field Values

SRCIDTAG

public static final String SRCIDTAG
See Also:
Constant Field Values

pipeAdv

protected PipeAdvertisement pipeAdv

pipeSvc

protected PipeService pipeSvc

in

protected InputPipe in

group

protected PeerGroup group

socketAddress

protected SocketAddress socketAddress

localAddress

protected InetAddress localAddress

outputPipe

protected OutputPipe outputPipe

closed

protected boolean closed

bound

protected boolean bound

queue

protected ProducerBiasedQueue queue

credential

protected Credential credential

credentialDoc

protected StructuredDocument credentialDoc
Constructor Detail

JxtaMulticastSocket

public JxtaMulticastSocket(PeerGroup group,
                           PipeAdvertisement pipeAd)
                    throws IOException
Create a multicast socket and bind it to a specific pipe within specified peer group

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

joinGroup

public void joinGroup(PeerGroup group,
                      PipeAdvertisement pipeAd)
               throws IOException
joins MutlicastSocket to specified pipe within the context of group

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

getCredDoc

protected static StructuredDocument getCredDoc(PeerGroup group)
Obtain the credential doc from the group object

Parameters:
group - group context
Returns:
The credDoc value

isBound

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

Overrides:
isBound in class DatagramSocket
Returns:
true if the MutlicastSocket successfully bound to an address

close

public void close()
Closes this MutlicastSocket.

Overrides:
close in class DatagramSocket

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.

getSoTimeout

public int getSoTimeout()
Gets the Timeout attribute of the JxtaMulticastSocket

Overrides:
getSoTimeout in class DatagramSocket
Returns:
The soTimeout value

setSoTimeout

public void setSoTimeout(int timeout)
                  throws SocketException
Sets the Timeout attribute of the JxtaMulticastSocket a timeout of 0 blocks forever, by default this Socket's timeout is set to 0

Overrides:
setSoTimeout in class DatagramSocket
Parameters:
timeout - The new soTimeout value
Throws:
SocketException

isClosed

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

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

send

public void send(DatagramPacket packet)
          throws IOException

Overrides:
send in class DatagramSocket
Throws:
IOException

receive

public void receive(DatagramPacket packet)
             throws IOException

Overrides:
receive in class DatagramSocket
Throws:
IOException

getLocalAddress

public InetAddress getLocalAddress()

Overrides:
getLocalAddress in class DatagramSocket

getLocalSocketAddress

public SocketAddress getLocalSocketAddress()

Overrides:
getLocalSocketAddress in class DatagramSocket

bind

public void bind(SocketAddress addr)
          throws SocketException

Overrides:
bind in class DatagramSocket
Throws:
SocketException

JXSE