001: /*
002: * Copyright (c) 2001 by The Regents of the University of California.
003: * All rights reserved.
004: *
005: * Permission to use, copy, modify, and distribute this software and its
006: * documentation for any purpose, without fee, and without written agreement is
007: * hereby granted, provided that the above copyright notice and the following
008: * two paragraphs appear in all copies of this software.
009: *
010: * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
011: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
012: * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
013: * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
014: *
015: * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
016: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
017: * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
018: * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
019: * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
020: *
021: * Author: Rob von Behren <jrvb@cs.berkeley.edu>
022: *
023: */
024:
025: package seda.nbio;
026:
027: import java.io.*;
028: import java.net.*;
029:
030: /**
031: * NonblockingMulticastSocket provides non-blocking multicast datagram I/O.
032: *
033: * NOTE: packets cannot be received on a socket once connect() is
034: * called, due to the semantics of connect() for multicast sockets.
035: * Instead, applications should generally use joinGroup(), and then
036: * explicitly specify the group address as the destination in all the
037: * outbound packets.
038: **/
039: public class NonblockingMulticastSocket extends
040: NonblockingDatagramSocket {
041:
042: InetAddress multicast_interface = null;
043: int multicast_ttl = -1;
044:
045: /**
046: * Create a NonblockingMulticastSocket bound to any available port.
047: */
048: public NonblockingMulticastSocket() throws IOException {
049: /* bind socket to any available port */
050: this (0, null);
051: }
052:
053: public NonblockingMulticastSocket(int port) throws IOException {
054: this (port, null);
055: }
056:
057: /**
058: * Create a NonblockingMulticastSocket bound to the given port and
059: * the given local address.
060: */
061: public NonblockingMulticastSocket(int port, InetAddress laddr)
062: throws IOException {
063: super (port, laddr);
064: }
065:
066: /**
067: * Join a multicast group
068: **/
069: public void joinGroup(InetAddress addr) throws IOException {
070: impl.joinGroup(addr);
071: }
072:
073: /**
074: * Leave a multicast group
075: **/
076: public void leaveGroup(InetAddress addr) throws IOException {
077: impl.leaveGroup(addr);
078: }
079:
080: /**
081: * get the multicast ttl
082: **/
083: public int getTimeToLive() throws IOException {
084: if (multicast_ttl == -1)
085: multicast_ttl = impl.getTimeToLive();
086:
087: return multicast_ttl;
088: }
089:
090: /**
091: * set the time to live
092: **/
093: public void setTimeToLive(int ttl) throws IOException {
094: multicast_ttl = ttl;
095: impl.setTimeToLive(ttl);
096: }
097:
098: /**
099: * Get the interface associated with this multicast socket
100: **/
101: public InetAddress getInterface() {
102: // FIXME: this is done, b/c it is easier than creating the new
103: // object from w/in the Jni layer. This is probably faster
104: // anyway, so this should be OK.
105: return multicast_interface;
106: }
107:
108: /**
109: * Set the interface associated with this socket
110: **/
111: public void setInterface(InetAddress addr) throws IOException {
112: impl.setInterface(addr);
113: }
114:
115: /**
116: * This sets the state of the IP_MULTICAST_LOOP option on the
117: * underlying socket. If state==true, the socket will receive
118: * packets it sends out. If false, it will not.<p>
119: *
120: * NOTE: The behavior of this is somewhat strange for two multicast
121: * listeners on the same physical machine. Ideally, this should be
122: * an incoming filter - each socket should throw out packets that it
123: * sent out, and not deliver them to the application.<p>
124: *
125: * Unfortunately, this instead seems to be an outbound filter - all
126: * packets sent out on a socket with IP_MULTICAST_LOOP turned off
127: * will be invisible to all sockets on the local machine -
128: * regardless of whether or not these other sockets have specified
129: * IP_MULTICAST_LOOP=false.
130: **/
131: public void seeLocalMessages(boolean state) throws IOException {
132: impl.seeLocalMessages(state);
133: }
134:
135: }
|