1 23 package fr.dyade.aaa.agent; 24 25 import java.io.*; 26 import java.util.Hashtable ; 27 import java.util.Enumeration ; 28 29 import org.objectweb.util.monolog.api.BasicLevel; 30 31 import fr.dyade.aaa.util.*; 32 33 public abstract class ProxyAgent extends Agent { 34 35 public static final int DRIVER_IN = 1; 36 public static final int DRIVER_OUT = 2; 37 38 39 protected boolean blockingCnx = true; 40 41 protected boolean multipleCnx = false; 42 43 protected int inFlowControl = 20; 44 45 protected transient Queue qout; 46 47 48 private transient DriverIn drvIn = null; 49 50 private transient DriverOut drvOut = null; 51 52 private transient DriverConnect drvCnx; 53 54 55 protected boolean multiConn = false; 56 63 protected transient Hashtable driversTable; 64 68 private int driversKey = 1; 69 70 71 boolean finalizing = false; 72 73 78 protected String getLogTopic() { 79 String classname = getClass().getName(); 80 return Debug.A3Proxy + '.' + 81 classname.substring(classname.lastIndexOf('.') +1); 82 } 83 84 public ProxyAgent() { 85 this(null); 86 } 87 88 public ProxyAgent(String n) { 89 this(AgentServer.getServerId(), n); 90 } 91 92 public ProxyAgent(short to, String n) { 93 super(to, n, true); 95 drvIn = null; 96 drvOut = null; 97 drvCnx = null; 98 } 99 100 106 public ProxyAgent(String name, int stamp) { 107 super(name, true, stamp); 108 drvIn = null; 109 drvOut = null; 110 drvCnx = null; 111 } 112 113 118 public String toString() { 119 return "(" + super.toString() + 120 ",blockingCnx=" + blockingCnx + 121 ",multipleCnx=" + multipleCnx + 122 ",inFlowControl=" + inFlowControl + 123 ",multiConn=" + multiConn + 124 ",qout=" + qout + 125 ",driversKey=" + driversKey + ")"; 126 } 127 128 132 public void setMultiConn() { 133 multiConn = true; 134 } 135 136 149 protected void agentInitialize(boolean firstTime) throws Exception { 150 super.agentInitialize(firstTime); 151 152 if (!multiConn) 154 qout = new Queue(); 155 else 157 driversTable = new Hashtable (); 158 159 reinitialize(); 160 } 161 162 169 protected void reinitialize() throws IOException { 170 if (drvIn != null || drvOut != null) { 171 if (!multiConn) throw new IllegalStateException (); 172 } 173 174 drvCnx = new DriverConnect(this, blockingCnx, multipleCnx); 175 drvCnx.start(); 176 177 if (multiConn) { 180 DriverMonitor dMonitor = new DriverMonitor(drvIn, drvOut, qout, ois, oos, 181 drvCnx); 182 183 driversTable.put(new Integer (driversKey), dMonitor); 184 driversKey++; 185 } 186 } 187 188 189 protected transient NotificationInputStream ois = null; 190 191 protected transient NotificationOutputStream oos = null; 192 193 200 public abstract void connect() throws Exception ; 201 202 208 public abstract void disconnect() throws Exception ; 209 210 221 void createDrivers() throws Exception { 222 drvCnx.canStop = true; 223 try { 224 connect(); 225 } catch (InterruptedException exc) { 226 logmon.log(BasicLevel.DEBUG, getName() + "InterruptedException"); 227 } finally { 228 if (drvCnx != null) 229 drvCnx.canStop = false; 230 } 231 232 if ((drvCnx == null) || (! drvCnx.isRunning)) return; 233 234 if (! multiConn) { 235 if (logmon.isLoggable(BasicLevel.DEBUG)) 236 logmon.log(BasicLevel.DEBUG, getName() + ", connected"); 237 if (oos != null) { 238 drvOut = new DriverOut(DRIVER_OUT, this, qout, oos); 239 drvOut.start(); 240 } 241 if (ois != null) { 242 drvIn = new DriverIn(DRIVER_IN, this, ois, inFlowControl); 243 drvIn.start(); 244 } 245 } 246 else { 249 if (logmon.isLoggable(BasicLevel.DEBUG)) 250 logmon.log(BasicLevel.DEBUG, "connected - driversKey=" + driversKey); 251 if (ois != null) { 252 drvIn = new DriverIn(DRIVER_IN, this, ois, inFlowControl, driversKey); 253 drvIn.start(); 254 } 255 if (oos != null) { 256 qout = new Queue(); 257 drvOut = new DriverOut(DRIVER_OUT, this, qout, oos, driversKey); 258 drvOut.start(); 259 } 260 } 261 } 262 263 267 protected void stop() { 268 if (multiConn) return; 269 270 if (drvCnx != null) { 271 drvCnx.stop(); 272 drvCnx = null; 273 } 274 if (drvIn != null) { 275 drvIn.stop(); 276 drvIn = null; 277 } 278 if (drvOut != null) { 279 drvOut.stop(); 280 drvOut = null; 281 } 282 if (logmon.isLoggable(BasicLevel.DEBUG)) 283 logmon.log(BasicLevel.DEBUG, "stopped"); 284 } 285 286 291 protected void stop(int drvKey) { 292 DriverMonitor dMonitor = 293 (DriverMonitor) driversTable.get(new Integer (drvKey)); 294 295 if (dMonitor != null) { 296 if (dMonitor.drvCnx != null) { 297 (dMonitor.drvCnx).stop(); 298 dMonitor.drvCnx = null; 299 } 300 if (dMonitor.drvIn != null) { 301 (dMonitor.drvIn).stop(); 302 dMonitor.drvIn = null; 303 } 304 if (dMonitor.drvOut != null) { 305 (dMonitor.drvOut).stop(); 306 dMonitor.drvOut = null; 307 } 308 if (logmon.isLoggable(BasicLevel.DEBUG)) 309 logmon.log(BasicLevel.DEBUG, "stopped - driversKey=" + driversKey); 310 } 311 } 312 313 314 public void cleanDriverOut() { 315 if (! multiConn) { 316 if (drvOut != null) 317 drvOut.clean(); 318 } 319 } 320 321 327 public void cleanDriverOut(int drvKey) { 328 DriverMonitor dMonitor = 329 (DriverMonitor) driversTable.get(new Integer (drvKey)); 330 if (dMonitor != null) { 331 if (dMonitor.drvOut != null) 332 (dMonitor.drvOut).clean(); 333 } 334 } 335 336 337 protected void closeAllConnections() { 338 Enumeration keys = driversTable.keys(); 339 while (keys.hasMoreElements()) { 340 Integer key = (Integer ) keys.nextElement(); 341 342 DriverMonitor dMonitor = (DriverMonitor) driversTable.get(key); 343 if (dMonitor != null) { 344 if (dMonitor.ois != null) { 345 try { 346 (dMonitor.ois).close(); 347 } catch (IOException exc) {} 348 dMonitor.ois = null; 349 } 350 if (dMonitor.oos != null) { 351 try { 352 (dMonitor.oos).close(); 353 } catch (IOException exc) {} 354 dMonitor.oos = null; 355 dMonitor.qout = null; 356 } 357 stop(key.intValue()); 358 } 359 } 360 driversTable.clear(); 361 } 362 363 374 protected void driverReact(int key, Notification not) { 375 if (logmon.isLoggable(BasicLevel.DEBUG)) 376 logmon.log(BasicLevel.DEBUG, "Proxy " + this + " gets not " + not 377 + " from driver " + key); 378 379 sendTo(this.getId(), not); 380 } 381 382 391 protected void sendOut(int key, Notification not) throws Exception { 392 if (logmon.isLoggable(BasicLevel.DEBUG)) 393 logmon.log(BasicLevel.DEBUG, "Proxy " + this + " gets not " + not 394 + " to pass to driver " + key); 395 try { 396 DriverMonitor dMon = (DriverMonitor) driversTable.get(new Integer (key)); 397 (dMon.getQout()).push(not); 398 } 399 catch (Exception e ) { 400 throw new Exception ("Can't forward notification " + not 401 + " to driver out " + key + ": " + e); 402 } 403 } 404 405 417 public void react(AgentId from, Notification not) throws Exception { 418 int drvKey; 419 DriverMonitor dMon; 420 DriverIn dIn; 421 Queue qo; 422 try { 423 if (not instanceof DriverDone) 424 driverDone((DriverDone) not); 425 else if (not instanceof FlowControlNot) { 426 drvKey = ((FlowControlNot) not).driverKey; 428 if (drvKey != 0) { 430 dMon = (DriverMonitor) driversTable.get(new Integer (drvKey)); 431 dIn = dMon.drvIn; 432 dIn.recvFlowControl((FlowControlNot) not); 433 } 434 else 435 drvIn.recvFlowControl((FlowControlNot) not); 436 } 437 else if (not instanceof DeleteNot) { 438 closeAllConnections(); 439 super.react(from, not); 440 } 441 else if (! from.equals(this.getId())) 443 qout.push(not); 444 else 445 super.react(from, not); 446 } catch (Exception exc) { 447 if (logmon.isLoggable(BasicLevel.ERROR)) 448 logmon.log(BasicLevel.ERROR, 449 "error in " + this + ".react(" + from + ", " + not + ")", 450 exc); 451 stop(); 452 } 454 } 455 456 465 protected void driverDone(DriverDone not) throws IOException { 466 if (!multiConn) { 467 switch (not.getDriver()) { 468 case DRIVER_IN: 469 try { 470 ois.close(); 471 } catch (Exception e) {} 472 ois = null; 473 drvIn = null; 474 break; 475 case DRIVER_OUT: 476 try { 477 oos.close(); 478 } catch (Exception e) {} 479 oos = null; 480 drvOut = null; 481 break; 482 } 483 } 484 else { 487 int drvKey = not.getDriverKey(); 488 DriverMonitor dMonitor = (DriverMonitor) 489 driversTable.get(new Integer (drvKey)); 490 491 if (dMonitor != null) { 492 switch (not.getDriver()) { 493 case DRIVER_IN: 494 if (dMonitor.drvIn != null) 495 (dMonitor.drvIn).close(); 496 dMonitor.ois = null; 497 dMonitor.drvIn = null; 498 break; 499 case DRIVER_OUT: 500 if (dMonitor.drvOut != null) 501 (dMonitor.drvOut).close(); 502 dMonitor.oos = null; 503 dMonitor.drvOut = null; 504 break; 505 } 506 if (dMonitor.drvIn == null && dMonitor.drvOut == null) 509 driversTable.remove(new Integer (drvKey)); 510 } 511 } 512 } 513 514 520 public void agentFinalize(boolean lastime) { 521 if (logmon.isLoggable(BasicLevel.DEBUG)) 522 logmon.log(BasicLevel.DEBUG, 523 toString() + " agentFinalize -> " + drvCnx); 524 525 finalizing = true; 526 527 if (multiConn) 528 closeAllConnections(); 529 else { 530 try { 531 ois.close(); 532 } catch (Exception exc) {} 533 try { 534 oos.close(); 535 } catch (Exception exc) {} 536 } 537 538 try { 539 disconnect(); 540 } catch (Exception exc) {} 541 542 stop(); 543 544 qout = null; 545 ois = null; 546 oos = null; 547 } 548 } 549 | Popular Tags |