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 31 class DriverIn extends Driver { 32 33 34 private ProxyAgent proxy; 35 36 37 protected AgentId proxyId; 38 39 40 protected NotificationInputStream in; 41 42 43 int nbNotSent = 0; 44 45 46 int maxNotSent = 0; 47 48 49 int nbFlowControl = 0; 50 51 57 private int key = 0; 58 59 68 DriverIn(int id, 69 ProxyAgent proxy, 70 NotificationInputStream in, 71 int maxNotSent) { 72 super(id); 73 this.maxNotSent = maxNotSent; 74 this.proxy = proxy; 75 this.proxyId = proxy.getId(); 76 this.in = in; 77 this.name = proxy.getName() + ".DriverIn#" + id; 78 String classname = getClass().getName(); 80 logmon = Debug.getLogger(proxy.getLogTopic()+ '.' + 81 classname.substring(classname.lastIndexOf('.') +1)); 82 } 83 84 94 DriverIn(int id, 95 ProxyAgent proxy, 96 NotificationInputStream in, 97 int maxNotSent, 98 int key) { 99 this(id, proxy, in, maxNotSent); 100 this.key = key; 101 } 102 103 108 public String toString() { 109 return "(" + super.toString() + 110 ",key=" + key + 111 ",nbNotSent=" + nbNotSent + 112 ",maxNotSent=" + maxNotSent + 113 ",nbFlowControl=" + nbFlowControl + ")"; 114 } 115 116 117 synchronized void sendFlowControl() throws IOException { 118 nbFlowControl += 1; 119 if (logmon.isLoggable(BasicLevel.DEBUG)) 120 logmon.log(BasicLevel.DEBUG, 121 getName() + ", sendFlowControl#" + nbFlowControl); 122 if (key == 0) 124 sendTo(proxyId, new FlowControlNot(id)); 125 126 else 130 sendTo(proxyId, new FlowControlNot(id, key)); 131 while (nbFlowControl > 1) { 132 try { wait(); } catch (InterruptedException e) {} 133 } 134 } 135 136 137 synchronized void recvFlowControl(FlowControlNot not) { 138 nbFlowControl -= 1; 139 if (logmon.isLoggable(BasicLevel.DEBUG)) 140 logmon.log(BasicLevel.DEBUG, 141 getName() + ", recvFlowControl#" + nbFlowControl); 142 notify(); 143 } 144 145 146 public void run() { 147 Notification m; 148 mainLoop: 149 while (isRunning) { 150 m = null; 151 canStop = true; 152 try { 153 if (nbNotSent > maxNotSent) { 154 try { 155 sendFlowControl(); 156 } catch (IOException exc) { 157 if (! proxy.finalizing) { 158 logmon.log(BasicLevel.ERROR, 159 getName() + ", error during sendFlowControl", exc); 160 } 161 break mainLoop; 162 } 163 nbNotSent = 0; 164 } 165 m = in.readNotification(); 166 } catch (EOFException exc) { 167 break mainLoop; 169 } catch (Exception exc) { 170 if (! proxy.finalizing) { 171 logmon.log(BasicLevel.WARN, 172 getName() + ", error in readNotification", exc); 173 } 174 break mainLoop; 175 } finally { 176 Thread.interrupted(); 177 canStop = false; 178 } 179 if (m != null) { 180 if (logmon.isLoggable(BasicLevel.DEBUG)) 181 logmon.log(BasicLevel.DEBUG, getName() + ", read " + m); 182 183 proxy.driverReact(key, m); 185 nbNotSent += 1; 186 } 187 } 188 } 189 190 196 protected void end() { 197 try { 199 if (key == 0) 201 sendTo(proxyId, new DriverDone(id)); 202 203 else 207 sendTo(proxyId, new DriverDone(id, key)); 208 209 } catch (IOException exc) { 210 logmon.log(BasicLevel.ERROR, 211 getName() + ", error in reporting end", exc); 212 } 213 } 214 215 218 public void close() { 219 try { 220 in.close(); 221 } catch (Exception exc) {} 222 in = null; 223 } 224 } 225 | Popular Tags |