KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > driver > connectpolicy > ControllerPingThread


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2005 Emic Networks.
4  * Contact: sequoia@continuent.org
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * Initial developer(s): Emmanuel Cecchet.
19  * Contributor(s): Ralph Hannus.
20  */

21
22 package org.continuent.sequoia.driver.connectpolicy;
23
24 import java.net.Socket JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Iterator JavaDoc;
27
28 import org.continuent.sequoia.common.protocol.Commands;
29 import org.continuent.sequoia.common.stream.DriverBufferedOutputStream;
30 import org.continuent.sequoia.driver.ControllerInfo;
31 import org.continuent.sequoia.driver.SequoiaUrl;
32
33 /**
34  * This class defines a ControllerPingThread that periodically tries to connect
35  * to a failed controller to check if it is back online.
36  *
37  * @author <a HREF="mailto:emmanuel.cecchet@emicnetworks.com">Emmanuel Cecchet
38  * </a>
39  * @author <a HREF="mailto:ralph.hannus@contiuent.com">Ralph Hannus
40  * </a>
41  * @version 1.0
42  */

43 public class ControllerPingThread extends Thread JavaDoc
44 {
45   private AbstractControllerConnectPolicy policy;
46   private long retryIntervalInMs;
47   private boolean threadTerminated = false;
48   private int debugLevel;
49
50   /**
51    * Creates a new <code>ControllerPingThread</code> object.
52    *
53    * @param policy controller connection policy.
54    * @param retryIntervalInMs interval in ms between 2 pings (checks).
55    * @param debugLevel the debug level to use
56    */

57   public ControllerPingThread(AbstractControllerConnectPolicy policy,
58       long retryIntervalInMs, int debugLevel)
59   {
60     super("ControllerPingThread");
61     this.policy = policy;
62     this.retryIntervalInMs = retryIntervalInMs;
63     this.debugLevel = debugLevel;
64     setDaemon(true);
65   }
66
67   /**
68    * @see java.lang.Runnable#run()
69    */

70   public void run()
71   {
72     HashSet JavaDoc suspectedControllers = policy.getSuspectedControllers();
73     synchronized (suspectedControllers)
74     {
75       while (!suspectedControllers.isEmpty())
76       {
77         // Sleep
78
try
79         {
80           suspectedControllers.wait(retryIntervalInMs);
81         }
82         catch (InterruptedException JavaDoc ignore)
83         {
84         }
85
86         // Try to connect to each controller
87
for (Iterator JavaDoc iter = suspectedControllers.iterator(); iter.hasNext();)
88         {
89           ControllerInfo controller = (ControllerInfo) iter.next();
90           try
91           {
92             Socket JavaDoc socket = new Socket JavaDoc(controller.getHostname(), controller
93                 .getPort());
94
95             // Connection successful
96

97             // Disable Nagle algorithm else small messages are not sent
98
// (at least under Linux) even if we flush the output stream.
99
socket.setTcpNoDelay(true);
100             DriverBufferedOutputStream out = new DriverBufferedOutputStream(
101                 socket);
102             // Tell the controller not to worry, just a ping
103
out.writeInt(Commands.Ping);
104             out.flush();
105             // The socket will be closed by the controller
106

107             // Full success, remove controller from suspect list
108
policy.removeControllerFromSuspectList(controller);
109             // Regenerate iterator
110
iter = suspectedControllers.iterator();
111           }
112           catch (Exception JavaDoc failed)
113           {
114             if (debugLevel == SequoiaUrl.DEBUG_LEVEL_DEBUG)
115               System.out.println("Ping failed to controller " + controller
116                   + " (" + failed + ")");
117           }
118         }
119       } // while
120
this.threadTerminated = true;
121     } // synchronized
122
}
123
124   /**
125    * Returns true if the thread is terminated.
126    *
127    * @return true if the thread is terminated.
128    */

129   public boolean isTerminated()
130   {
131     return threadTerminated;
132   }
133 }
134
Popular Tags