KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > clustering > unicast > Unicast


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.unicast;
11
12 import java.util.Map JavaDoc;
13
14 import org.mmbase.core.event.NodeEvent;
15 import org.mmbase.clustering.ClusterManager;
16 import org.mmbase.util.logging.Logger;
17 import org.mmbase.util.logging.Logging;
18 import org.mmbase.util.xml.UtilReader;
19
20
21 /**
22  * @javadoc
23  *
24  * @author Nico Klasens
25  * @version $Id: Unicast.java,v 1.9 2006/06/21 05:46:20 michiel Exp $
26  */

27 public class Unicast extends ClusterManager {
28
29     private static final Logger log = Logging.getLoggerInstance(Unicast.class);
30
31     public static final String JavaDoc CONFIG_FILE = "unicast.xml";
32
33     /** Port on which the talking between nodes take place.*/
34     private int unicastPort = 4243;
35
36     /** Timeout of the connection.*/
37     private int unicastTimeout = 10 * 1000;
38
39
40     /** Sender which reads the nodesToSend Queue amd puts the message on the line */
41     private ChangesSender ucs;
42     /** Receiver which reads the message from the line and puts message in the nodesToSpawn Queue */
43     private ChangesReceiver ucr;
44
45     /**
46      * @since MMBase-1.8.1
47      */

48     private final UtilReader reader = new UtilReader(CONFIG_FILE,
49                                                      new Runnable JavaDoc() {
50                                                          public void run() {
51                                                              synchronized(Unicast.this) {
52                                                                  stopCommunicationThreads();
53                                                                  readConfiguration(reader.getProperties());
54                                                                  startCommunicationThreads();
55                                                              }
56                                                          }
57                                                      });
58
59
60
61     public Unicast(){
62         readConfiguration(reader.getProperties());
63         start();
64     }
65
66     protected synchronized void readConfiguration(Map JavaDoc configuration) {
67         super.readConfiguration(configuration);
68
69         String JavaDoc tmp = (String JavaDoc) configuration.get("unicastport");
70         if (tmp != null && !tmp.equals("")) {
71             try {
72                 unicastPort = Integer.parseInt(tmp);
73             } catch (Exception JavaDoc e) {}
74         }
75         tmp = (String JavaDoc) configuration.get(org.mmbase.module.core.MMBase.getMMBase().getMachineName() + ".unicastport");
76         if (tmp != null && !tmp.equals("")) {
77             try {
78                 unicastPort = Integer.parseInt(tmp);
79             } catch (Exception JavaDoc e) {}
80         }
81
82         tmp = (String JavaDoc) configuration.get("unicasttimeout");
83         if (tmp != null && !tmp.equals("")) {
84             try {
85                 unicastTimeout = Integer.parseInt(tmp);
86             } catch (Exception JavaDoc e) {}
87         }
88
89         log.info("unicast port: " + unicastPort);
90         log.info("unicast timeout: " + unicastTimeout);
91
92     }
93
94     /**
95      * @see org.mmbase.clustering.ClusterManager#startCommunicationThreads()
96      */

97     protected synchronized void startCommunicationThreads() {
98         ucs = new ChangesSender(reader.getProperties(), unicastPort, unicastTimeout, nodesToSend, send);
99         try {
100             ucr = new ChangesReceiver(unicastPort, nodesToSpawn);
101         } catch (java.io.IOException JavaDoc ioe) {
102             log.error(ioe);
103         }
104     }
105
106     /**
107      * @see org.mmbase.clustering.ClusterManager#stopCommunicationThreads()
108      */

109     protected synchronized void stopCommunicationThreads() {
110         if (ucs != null) {
111             ucs.stop();
112             log.service("Stopped communication sender " + ucs);
113             ucs = null;
114         }
115         if (ucr != null) {
116             ucr.stop();
117             log.service("Stopped communication receiver " + ucr);
118             ucr = null;
119         }
120     }
121
122     // javadoc inherited
123
public void changedNode(NodeEvent event) {
124         byte[] message = createMessage(event);
125         nodesToSend.append(message);
126         //Multicast receives his own message. Unicast now too.
127
nodesToSpawn.append(message);
128         if (log.isDebugEnabled()) {
129             log.debug("message: " + event);
130         }
131         return;
132     }
133
134 }
135
Popular Tags