1 package com.quikj.server.framework; 2 3 import java.io.*; 4 import java.net.*; 5 6 public class AceIPCServerConnection extends AceThread 7 { 8 12 protected AceIPCServerConnection(String name, 13 InetAddress client_addr, 14 int client_port, 15 int hb_interval, 16 AceIPCServer parent) 17 throws IOException 18 { 19 super(name); 20 21 clientAddress = client_addr; 22 clientPort = client_port; 23 hbInterval = hb_interval; 24 this.parent = parent; 25 } 26 27 public void dispose() 28 { 29 disposed = true; 30 dropConnection(false); 31 } 32 33 private void dropConnection(boolean send_disc_msg) 34 { 35 if (disposed == false) 36 { 37 parent.connectionClosed(this, send_disc_msg); 38 disposed = true; 39 } 40 41 try 42 { 43 AceTimer.Instance().cancelAllTimers(this); 44 } 45 catch (IOException ex) 46 { 47 System.err.println(getName() + 49 ": AceIPCServerConnection.dropConnection() -- Error canceling timers : " 50 + ex.getMessage()); 51 } 52 finally 53 { 54 if (this.isAlive() == true) 55 { 56 if (interruptWait(AceSignalMessage.SIGNAL_TERM, 57 "Normal IPC Server Connection dispose") == false) 58 { 59 System.err.println(getName() 61 + ": AceIPCServerConnection.dropConnection() -- Could not interrupt own wait : " 62 + getErrorMessage()); 63 64 super.dispose(); 65 } 66 } 67 else 68 { 69 super.dispose(); 70 71 } 75 } 76 } 77 78 public void run() 79 { 80 try 82 { 83 sendTimerId = AceTimer.Instance().startTimer(hbInterval, 84 this, 85 0); 86 87 receiveTimerId = AceTimer.Instance().startTimer((hbInterval * 88 AceIPCHeartbeatMessage.TOLERANCE_FACTOR), 89 this, 90 0); 91 if ((sendTimerId < 0) || (receiveTimerId < 0)) 92 { 93 System.err.println(getName() + 95 ": AceIPCServerConnection.run() -- Failure starting one or more timers, returned IDs = " 96 + sendTimerId + ", " 97 + receiveTimerId); 98 dropConnection(true); 99 } 100 } 101 catch (IOException ex) 102 { 103 System.err.println(getName() + 105 ": AceIPCServerConnection.run() -- IOException starting one or more timers : " 106 + ex.getMessage()); 107 dropConnection(true); 108 } 109 110 while (true) 111 { 112 AceMessageInterface message = waitMessage(); 113 if (message == null) 114 { 115 continue; 119 } 120 else if ((message instanceof AceSignalMessage) == true) 121 { 122 super.dispose(); 131 break; 132 } 133 134 if ((message instanceof AceTimerMessage) == true) 135 { 136 if (((AceTimerMessage)message).getTimerId() == sendTimerId) 137 { 138 if (resetSendTiming(false) == false) 139 { 140 dropConnection(true); 141 } 142 else 143 { 144 boolean status = parent.sendHeartbeatMessage(clientAddress, 145 clientPort); 146 } 147 } 148 else if (((AceTimerMessage)message).getTimerId() == receiveTimerId) 149 { 150 System.err.println(getName() + 152 ": AceIPCServerConnection.run() -- Receive timer expired: LOST HEARTBEAT"); 153 dropConnection(false); 154 } 155 else 156 { 157 System.err.println(getName() + 159 ": AceIPCServerConnection.run() -- Message received with unexpected timer ID = " 160 + ((AceTimerMessage)message).getTimerId()); 161 } 162 } 163 else 164 { 165 System.err.println(getName() + 167 ": AceIPCServerConnection.run() -- Unexpected Ace message type encountered : " 168 + message.messageType()); 169 } 170 171 } 172 } 173 174 private void stopTimer(int timer_id) 175 { 176 try 177 { 178 boolean status = AceTimer.Instance().cancelTimer(timer_id, this); 179 } 180 catch (IOException ex) 181 { 182 System.err.println(getName() + 184 ": AceIPCServerConnection.stopTimer() -- IOException canceling timer ID = " 185 + timer_id 186 + " : " 187 + ex.getMessage()); 188 return; 189 } 190 } 191 192 protected boolean resetSendTiming() 193 { 194 return resetSendTiming(true); 195 } 196 197 protected boolean resetSendTiming(boolean currently_running) 198 { 199 if (disposed == true) 200 { 201 return false; 202 } 203 204 synchronized (sendTimerLock) 205 { 206 if (currently_running == true) 207 { 208 stopTimer(sendTimerId); 209 } 210 211 try 212 { 213 sendTimerId = AceTimer.Instance().startTimer(hbInterval, 214 this, 215 0); 216 if (sendTimerId < 0) 217 { 218 System.err.println(getName() + 220 ": AceIPCServerConnection.resetSendTiming() -- Failure starting timer, returned ID = " 221 + sendTimerId); 222 return false; 223 } 224 } 225 catch (IOException ex) 226 { 227 System.err.println(getName() + 229 ": AceIPCServerConnection.resetSendTiming() -- IOException starting timer : " 230 + ex.getMessage()); 231 return false; 232 } 233 } 234 235 return true; 236 } 237 238 protected boolean resetReceiveTiming() 239 { 240 if (disposed == true) 241 { 242 return false; 243 } 244 245 stopTimer(receiveTimerId); 246 try 247 { 248 receiveTimerId = AceTimer.Instance().startTimer((hbInterval * 249 AceIPCHeartbeatMessage.TOLERANCE_FACTOR), 250 this, 251 0); 252 if (receiveTimerId < 0) 253 { 254 System.err.println(getName() + 256 ": AceIPCServerConnection.resetReceiveTiming() -- Failure starting timer, returned ID = " 257 + receiveTimerId); 258 return false; 259 } 260 } 261 catch (IOException ex) 262 { 263 System.err.println(getName() + 265 ": AceIPCServerConnection.resetReceiveTiming() -- IOException starting timer : " 266 + ex.getMessage()); 267 return false; 268 } 269 270 return true; 271 } 272 273 protected InetAddress getClientAddress() 274 { 275 return clientAddress; 276 } 277 278 protected int getClientPort() 279 { 280 return clientPort; 281 } 282 283 private InetAddress clientAddress; 284 private int clientPort; 285 private int hbInterval; 286 private AceIPCServer parent; 287 private boolean disposed = false; 288 289 private int sendTimerId; 290 private Object sendTimerLock = new Object (); 291 private int receiveTimerId; 292 293 } 294 | Popular Tags |