1 21 22 package org.continuent.sequoia.driver.connectpolicy; 23 24 import java.net.Socket ; 25 import java.util.HashSet ; 26 import java.util.Iterator ; 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 43 public class ControllerPingThread extends Thread 44 { 45 private AbstractControllerConnectPolicy policy; 46 private long retryIntervalInMs; 47 private boolean threadTerminated = false; 48 private int debugLevel; 49 50 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 70 public void run() 71 { 72 HashSet suspectedControllers = policy.getSuspectedControllers(); 73 synchronized (suspectedControllers) 74 { 75 while (!suspectedControllers.isEmpty()) 76 { 77 try 79 { 80 suspectedControllers.wait(retryIntervalInMs); 81 } 82 catch (InterruptedException ignore) 83 { 84 } 85 86 for (Iterator iter = suspectedControllers.iterator(); iter.hasNext();) 88 { 89 ControllerInfo controller = (ControllerInfo) iter.next(); 90 try 91 { 92 Socket socket = new Socket (controller.getHostname(), controller 93 .getPort()); 94 95 97 socket.setTcpNoDelay(true); 100 DriverBufferedOutputStream out = new DriverBufferedOutputStream( 101 socket); 102 out.writeInt(Commands.Ping); 104 out.flush(); 105 107 policy.removeControllerFromSuspectList(controller); 109 iter = suspectedControllers.iterator(); 111 } 112 catch (Exception failed) 113 { 114 if (debugLevel == SequoiaUrl.DEBUG_LEVEL_DEBUG) 115 System.out.println("Ping failed to controller " + controller 116 + " (" + failed + ")"); 117 } 118 } 119 } this.threadTerminated = true; 121 } } 123 124 129 public boolean isTerminated() 130 { 131 return threadTerminated; 132 } 133 } 134 | Popular Tags |