001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.util.transport;
022:
023: import java.io.IOException;
024:
025: import java.net.DatagramPacket;
026: import java.net.InetAddress;
027: import java.net.MulticastSocket;
028:
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031:
032: /**
033: * <a href="MulticastTransport.java.html"><b><i>View Source</i></b></a>
034: *
035: * <p>
036: * The MulticastTransport will send strings across a specified multicast
037: * address. It will also listen for messages and hand them to the appropriate
038: * DatagramHandler.
039: * </p>
040: *
041: * @author Michael C. Han
042: *
043: */
044: public class MulticastTransport extends Thread implements Transport {
045:
046: public MulticastTransport(DatagramHandler handler, String host,
047: int port) {
048: super ("MulticastListener-" + host + port);
049:
050: setDaemon(true);
051: _handler = handler;
052: _host = host;
053: _port = port;
054: }
055:
056: public synchronized void connect() throws IOException {
057: if (_socket == null) {
058: _socket = new MulticastSocket(_port);
059: } else if (_socket.isConnected() && _socket.isBound()) {
060: return;
061: }
062:
063: _address = InetAddress.getByName(_host);
064:
065: _socket.joinGroup(_address);
066:
067: _connected = true;
068:
069: start();
070: }
071:
072: public synchronized void disconnect() {
073:
074: // Interrupt all processing
075:
076: if (_address != null) {
077: try {
078: _socket.leaveGroup(_address);
079: _address = null;
080: } catch (IOException e) {
081: _log.error("Unable to leave group", e);
082: }
083: }
084:
085: _connected = false;
086:
087: interrupt();
088:
089: _socket.close();
090: }
091:
092: public synchronized void sendMessage(String msg) throws IOException {
093: _outboundPacket.setData(msg.getBytes());
094: _outboundPacket.setAddress(_address);
095: _outboundPacket.setPort(_port);
096:
097: _socket.send(_outboundPacket);
098: }
099:
100: public boolean isConnected() {
101: return _connected;
102: }
103:
104: public void run() {
105: try {
106: while (_connected) {
107: _socket.receive(_inboundPacket);
108: _handler.process(_inboundPacket);
109: }
110: } catch (IOException e) {
111: _log.error("Unable to process ", e);
112:
113: _socket.disconnect();
114:
115: _connected = false;
116:
117: _handler.errorReceived(e);
118: }
119: }
120:
121: private static final Log _log = LogFactory
122: .getLog(MulticastTransport.class);
123:
124: private final byte[] _inboundBuffer = new byte[4096];
125:
126: private final DatagramPacket _inboundPacket = new DatagramPacket(
127: _inboundBuffer, _inboundBuffer.length);
128:
129: private final byte[] _outboundBuffer = new byte[4096];
130:
131: private final DatagramPacket _outboundPacket = new DatagramPacket(
132: _outboundBuffer, _outboundBuffer.length);
133:
134: private final String _host;
135: private final DatagramHandler _handler;
136: private final int _port;
137: private boolean _connected;
138: private MulticastSocket _socket;
139: private InetAddress _address;
140:
141: }
|