KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > clustering > multicast > Multicast


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.clustering.multicast;
11
12 import java.util.Map JavaDoc;
13
14 import org.mmbase.clustering.ClusterManager;
15 import org.mmbase.util.logging.Logger;
16 import org.mmbase.util.logging.Logging;
17 import org.mmbase.util.xml.UtilReader;
18
19
20 /**
21  * Multicast is a thread object that reads the receive queue
22  * and spawns them to call the objects (listeners) who need to know.
23  * The Multicast start two threads to handle the sending and receiving of
24  * multicast messages.
25  *
26  * @author Daniel Ockeloen
27  * @author Rico Jansen
28  * @author Nico Klasens
29  * @version $Id: Multicast.java,v 1.10 2006/07/06 11:40:48 michiel Exp $
30  */

31 public class Multicast extends ClusterManager {
32
33     private static final Logger log = Logging.getLoggerInstance(Multicast.class);
34
35     public static final String JavaDoc CONFIG_FILE = "multicast.xml";
36
37     /**
38      * Defines what 'channel' we are talking to when using multicast.
39      */

40     private String JavaDoc multicastHost = "ALL-SYSTEMS.MCAST.NET";
41
42     /**
43      * Determines on what port does this multicast talking between nodes take place.
44      * This can be set to any port but check if something else on
45      * your network is allready using multicast when you have problems.
46      */

47     private int multicastPort = 4243;
48
49     /** Determines the Time To Live for a multicast datapacket */
50     private int multicastTTL = 1;
51
52     /** Datapacket receive size */
53     private int dpsize = 64 * 1024;
54
55     /** Sender which reads the nodesToSend Queue amd puts the message on the line */
56     private ChangesSender mcs;
57     /** Receiver which reads the message from the line and puts message in the nodesToSpawn Queue */
58     private ChangesReceiver mcr;
59
60     /**
61      * @since MMBase-1.8.1
62      */

63     private final UtilReader reader = new UtilReader(CONFIG_FILE,
64                                                             new Runnable JavaDoc() {
65                                                                 public void run() {
66                                                                     synchronized(Multicast.this) {
67                                                                         stopCommunicationThreads();
68                                                                         readConfiguration(reader.getProperties());
69                                                                         startCommunicationThreads();
70                                                                     }
71                                                                 }
72                                                             });
73
74     /**
75      */

76
77     public Multicast(){
78         readConfiguration(reader.getProperties());
79         start();
80     }
81
82     /**
83      * @since MMBase-1.8.1
84      */

85     protected synchronized void readConfiguration(Map JavaDoc configuration) {
86         super.readConfiguration(configuration);
87
88         String JavaDoc tmp = (String JavaDoc) configuration.get("multicastport");
89         if (tmp != null && !tmp.equals("")) {
90             try {
91                 multicastPort = Integer.parseInt(tmp);
92             } catch (Exception JavaDoc e) {}
93         }
94
95         tmp = (String JavaDoc) configuration.get("multicasthost");
96         if (tmp != null && !tmp.equals("")) {
97             multicastHost = tmp;
98         }
99
100         tmp = (String JavaDoc) configuration.get("multicastTTL");
101         if (tmp != null && !tmp.equals("")) {
102             try {
103                 multicastTTL = Integer.parseInt(tmp);
104             } catch (Exception JavaDoc e) {}
105         }
106
107         tmp = (String JavaDoc) configuration.get("dpsize");
108         if (tmp != null && !tmp.equals("")) {
109             try {
110                 dpsize = Integer.parseInt(tmp);
111             } catch (Exception JavaDoc e) {}
112         }
113
114         log.info("multicast host: " + multicastHost +
115                  ", port: " + multicastPort +
116                  ", TTL: " + multicastTTL +
117                  ", datapacketsize: " + dpsize);
118
119     }
120
121     protected synchronized void startCommunicationThreads() {
122         try {
123             mcs = new ChangesSender(multicastHost, multicastPort, multicastTTL, nodesToSend, send);
124         } catch (java.net.UnknownHostException JavaDoc e) {
125             log.error(e);
126         }
127         try {
128             mcr = new ChangesReceiver(multicastHost, multicastPort, dpsize, nodesToSpawn);
129         } catch (java.net.UnknownHostException JavaDoc e) {
130             log.error(e);
131         }
132     }
133
134     protected synchronized void stopCommunicationThreads() {
135         if (mcs != null) {
136             mcs.stop();
137             log.service("Stopped communication sender " + mcs);
138             mcs = null;
139         }
140         if (mcr != null) {
141             mcr.stop();
142             log.service("Stopped communication receiver " + mcr);
143             mcr = null;
144         }
145     }
146
147 }
148
Popular Tags