01: /* ====================================================================
02: * The LateralNZ Software License, Version 1.0
03: *
04: * Copyright (c) 2003 LateralNZ. All rights reserved.
05: *
06: * Redistribution and use in source and binary forms, with or without
07: * modification, are permitted provided that the following conditions
08: * are met:
09: *
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: *
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in
15: * the documentation and/or other materials provided with the
16: * distribution.
17: *
18: * 3. The end-user documentation included with the redistribution,
19: * if any, must include the following acknowledgment:
20: * "This product includes software developed by
21: * LateralNZ (http://www.lateralnz.org/) and other third parties."
22: * Alternately, this acknowledgment may appear in the software itself,
23: * if and wherever such third-party acknowledgments normally appear.
24: *
25: * 4. The names "LateralNZ" must not be used to endorse or promote
26: * products derived from this software without prior written
27: * permission. For written permission, please
28: * contact oss@lateralnz.org.
29: *
30: * 5. Products derived from this software may not be called "Panther",
31: * or "Lateral" or "LateralNZ", nor may "PANTHER" or "LATERAL" or
32: * "LATERALNZ" appear in their name, without prior written
33: * permission of LateralNZ.
34: *
35: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46: * SUCH DAMAGE.
47: * ====================================================================
48: *
49: * This software consists of voluntary contributions made by many
50: * individuals on behalf of LateralNZ. For more
51: * information on Lateral, please see http://www.lateralnz.com/ or
52: * http://www.lateralnz.org
53: *
54: */
55: package org.lateralnz.messaging.multicast;
56:
57: import java.io.Serializable;
58:
59: import java.net.MulticastSocket;
60:
61: import org.apache.log4j.Logger;
62:
63: import org.lateralnz.common.util.Constants;
64: import org.lateralnz.common.wrapper.IntHolder;
65: import org.lateralnz.messaging.broadcast.BroadcastMessageHandler;
66: import org.lateralnz.messaging.MessageHandler;
67:
68: /**
69: * a messaging service that uses IP multicast to transmit messages
70: *
71: * @author J R Briggs
72: */
73: public class MulticastMessageHandler extends BroadcastMessageHandler
74: implements MessageHandler, Constants, Serializable {
75: private static final Logger log = Logger
76: .getLogger(MulticastMessageHandler.class.getName());
77: private static final int HEADER_SIZE = 12; // size of the head (ID[3], MAX[3], ORDER[3])
78: private static final int SPACER_SIZE = 20; // a spacer (just in case)
79:
80: private boolean running = true;
81:
82: private int maxbuf; // calculated max buffer size
83: private int receiveBufSize; // the receive buffer size for the socket
84: private IntHolder msgID = new IntHolder(0); // ID number to use with fragmented packets
85:
86: protected MulticastMessageHandler(String ipaddress, int port)
87: throws Exception {
88: super (ipaddress, port, new MulticastSocket(port), false);
89:
90: MulticastSocket ms = (MulticastSocket) sock;
91: ms.joinGroup(addr);
92: ms.setTimeToLive(32);
93: // we don't want to receive local messages
94: ms.setLoopbackMode(true);
95:
96: listenerThread.start();
97: }
98: }
|