1 13 14 package com.go.teaservlet.util.cluster; 15 16 import java.io.IOException ; 17 import java.rmi.Naming ; 18 import java.net.DatagramPacket ; 19 import com.go.trove.log.Syslog; 20 21 27 public class AutomaticClusterManagementThread extends Thread { 28 29 private final boolean DEBUG = true; 30 31 private boolean exit; 32 private boolean mActive; 33 private ClusterManager mClusterManager; 34 35 protected AutomaticClusterManagementThread(ClusterManager manager) { 36 super(); 37 mClusterManager = manager; 38 mActive = true; 39 } 40 41 protected AutomaticClusterManagementThread(ClusterManager manager, 42 boolean active) { 43 super(); 44 mClusterManager = manager; 45 mActive = active; 46 } 47 48 protected AutomaticClusterManagementThread(ClusterManager manager, 49 String name) { 50 super(name); 51 mClusterManager = manager; 52 mActive = true; 53 } 54 55 protected AutomaticClusterManagementThread(ClusterManager manager, 56 String name, 57 boolean active) { 58 super(name); 59 mClusterManager = manager; 60 mActive = active; 61 } 62 63 public void kill() { 64 exit = true; 65 } 66 67 public void run() { 68 exit = false; 69 while (!exit) { 70 try { 71 72 String packetString = obtainPacketString(); 73 74 if (packetString.indexOf("quit") >= 0) { 75 exit = true; 76 } 77 else { 78 updateCluster(packetString); 79 } 80 } 81 catch (Exception e) { 82 e.printStackTrace(); 83 exit = true; 84 } 85 } 86 } 87 88 protected String obtainPacketString() throws Exception { 89 90 DatagramPacket pack = mClusterManager.getNextPacket(); 91 byte[] packetData = pack.getData(); 92 int len = pack.getLength(); 93 94 Syslog.debug(new String (packetData,0,len)); 95 96 return new String (packetData,0,len,"ISO-8859-1"); 97 } 98 99 protected void updateCluster(String packetString) throws Exception { 100 101 int commandIndex = packetString.indexOf('~'); 102 int clusterIndex = packetString.lastIndexOf('~'); 103 104 if (commandIndex > 0 && clusterIndex > commandIndex) { 105 106 String command = packetString 107 .substring(0,commandIndex); 108 int commandCode = -1; 109 110 if ("join".equals(command)) { 111 commandCode = 'j'; 112 } 113 else if ("accept".equals(command)) { 114 commandCode = 'a'; 115 } 116 else if ("ping".equals(command)) { 117 commandCode = 'p'; 118 } 119 else if ("leave".equals(command)) { 120 commandCode = 'L'; 121 } 122 123 if (commandCode > 0) { 124 String clusterName = packetString 125 .substring(commandIndex+1,clusterIndex); 126 String hostName = packetString 127 .substring(clusterIndex+1); 128 129 String namingURL = 130 ("//" + hostName + ':' + 131 mClusterManager.getRMIPort() 132 + '/' + clusterName); 133 134 135 if (mClusterManager.getCluster().getClusterName() 136 .equals(clusterName)) { 137 138 Syslog.debug("Looking Up " + namingURL); 139 140 Clustered bcl = null; 141 try { 142 bcl = (Clustered)Naming.lookup(namingURL); 143 } 144 catch (Exception e) { 145 Syslog.debug(e.getMessage()); 146 } 147 148 if (bcl != null) { 149 Syslog.debug(namingURL + " Found on " 150 + bcl.getServerName()); 151 152 if (commandCode >= 'a') { 153 if (commandCode < 'p' && 154 !mClusterManager.getCluster().containsPeer(bcl)) { 155 mClusterManager.getCluster().addPeer(bcl); 156 } 157 158 162 if (commandCode > 'a' && !bcl.getServerName() 163 .equals(mClusterManager.getCluster() 164 .getServerName()) 165 && mActive) { 166 try { 167 mClusterManager.send 168 (("accept~" + clusterName + '~' 169 + mClusterManager.getCluster() 170 .getServerName()).getBytes()); 171 } 172 catch (Exception e) { 173 e.printStackTrace(); 174 } 175 } 176 } 177 else if (commandCode == 'L') { 178 try { 179 mClusterManager.getCluster() 180 .removePeer(bcl); 181 } 182 catch (Exception e) { 183 Syslog.debug("error removing " + hostName 184 + " from the cluster"); 185 } 186 } 187 } 188 } 189 } 190 } 191 } 192 } 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | Popular Tags |