1 23 24 package org.objectweb.cjdbc.driver.connectpolicy; 25 26 import java.net.Socket ; 27 import java.util.HashSet ; 28 import java.util.Iterator ; 29 30 import org.objectweb.cjdbc.common.stream.CJDBCOutputStream; 31 import org.objectweb.cjdbc.driver.CjdbcUrl; 32 import org.objectweb.cjdbc.driver.ControllerInfo; 33 import org.objectweb.cjdbc.driver.protocol.Commands; 34 35 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 } 65 66 69 public void run() 70 { 71 HashSet suspectedControllers = policy.getSuspectedControllers(); 72 synchronized (suspectedControllers) 73 { 74 while (!suspectedControllers.isEmpty()) 75 { 76 try 78 { 79 suspectedControllers.wait(retryIntervalInMs); 80 } 81 catch (InterruptedException ignore) 82 { 83 } 84 85 for (Iterator iter = suspectedControllers.iterator(); iter.hasNext();) 87 { 88 ControllerInfo controller = (ControllerInfo) iter.next(); 89 try 90 { 91 Socket socket = new Socket (controller.getHostname(), controller 92 .getPort()); 93 94 96 socket.setTcpNoDelay(true); 99 CJDBCOutputStream out = new CJDBCOutputStream(socket); 100 out.writeInt(Commands.Ping); 102 out.flush(); 103 105 policy.removeControllerFromSuspectList(controller); 107 iter = suspectedControllers.iterator(); 109 } 110 catch (Exception failed) 111 { 112 if (debugLevel == CjdbcUrl.DEBUG_LEVEL_DEBUG) 113 System.out.println("Ping failed to controller " + controller 114 + " (" + failed + ")"); 115 } 116 } 117 } this.threadTerminated = true; 119 } } 121 122 127 public boolean isTerminated() 128 { 129 return threadTerminated; 130 } 131 } 132 | Popular Tags |