1 21 package fr.dyade.aaa.agent; 22 23 import java.io.*; 24 25 import org.objectweb.util.monolog.api.BasicLevel; 26 import org.objectweb.util.monolog.api.Logger; 27 28 import fr.dyade.aaa.util.*; 29 30 33 class DriverOut extends Driver { 34 35 36 protected ProxyAgent proxy; 37 38 protected Queue mq; 39 40 protected NotificationOutputStream out; 41 42 43 private int key = 0; 44 45 53 DriverOut(int id, 54 ProxyAgent proxy, 55 Queue mq, 56 NotificationOutputStream out) { 57 super(id); 58 this.proxy = proxy; 59 this.mq = mq; 60 this.out = out; 61 this.name = proxy.getName() + ".DriverOut#" + id; 62 String classname = getClass().getName(); 64 logmon = Debug.getLogger(proxy.getLogTopic()+ '.' + 65 classname.substring(classname.lastIndexOf('.') +1)); 66 } 67 68 78 DriverOut(int id, 79 ProxyAgent proxy, 80 Queue mq, 81 NotificationOutputStream out, 82 int key) 83 { 84 this(id, proxy, mq, out); 85 this.key = key; 86 } 87 88 public void run() { 89 Notification m = null; 90 mainLoop: 91 while (isRunning) { 92 try { 93 canStop = true; 94 m = (Notification) mq.get(); 95 if (! isRunning) break; 96 if (logmon.isLoggable(BasicLevel.DEBUG)) 97 logmon.log(BasicLevel.DEBUG, getName() + ", write: " + m); 98 canStop = false; 99 out.writeNotification(m); 100 } catch (IOException exc) { 101 canStop = false; 102 if (! proxy.finalizing) { 103 logmon.log(BasicLevel.WARN, 104 getName() + ", write failed" + m, exc); 105 } 106 break mainLoop; 107 } catch (InterruptedException exc) { 108 canStop = false; 109 break mainLoop; 110 } finally { 111 Thread.interrupted(); 112 canStop = false; 113 } 114 mq.pop(); 115 } 116 } 117 118 121 public void close() { 122 try { 123 out.close(); 124 } catch (Exception exc) {} 125 out = null; 126 } 127 128 133 void sendTo(Notification not) { 134 mq.push(not); 135 } 136 137 143 protected void end() { 144 try { 146 if (key == 0) 148 sendTo(proxy.getId(), new DriverDone(id)); 149 150 else 154 sendTo(proxy.getId(), new DriverDone(id, key)); 155 156 } catch (IOException exc) { 157 logmon.log(BasicLevel.ERROR, 158 getName() + ", error in reporting end", exc); 159 } 160 } 161 162 163 protected void clean() { 164 mq.removeAllElements(); 165 } 166 } 167 | Popular Tags |