KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > dyade > aaa > agent > DriverIn


1 /*
2  * Copyright (C) 2001 - 2006 ScalAgent Distributed Technologies
3  * Copyright (C) 1996 - 2000 BULL
4  * Copyright (C) 1996 - 2000 INRIA
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA.
20  */

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 /**
29  * Input driver.
30  */

31 class DriverIn extends Driver {
32
33   /** Proxy this <code>DriverIn<code> belongs to. */
34   private ProxyAgent proxy;
35
36   /** Id of proxy this <code>DriverIn</code> belongs to. */
37   protected AgentId proxyId;
38
39   /** stream to read notifications from */
40   protected NotificationInputStream in;
41
42   /** number of notifications sent since last sent <code>FlowControl</code> */
43   int nbNotSent = 0;
44
45   /** max number of notifications between two <code>FlowControl</code>s */
46   int maxNotSent = 0;
47
48   /** number of <code>FlowControl</code>s sent and not received by proxy */
49   int nbFlowControl = 0;
50
51   /**
52    * Identifies this <code>DriverIn</code> among the drivers of a multi
53    * connections proxy.
54    * If <code>key</code> equals 0, this driver does not belong to a multi
55    * connections proxy.
56    */

57   private int key = 0;
58
59   /**
60    * Constructor.
61    *
62    * @param id identifier local to the driver creator
63    * @param proxy proxy agent to forward notifications to
64    * @param in stream to read notifications from
65    * @param maxNotSent max number of notifications between
66    * <code>FlowControl</code>s
67    */

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     // Get the logging monitor using proxy topic
79
String JavaDoc classname = getClass().getName();
80     logmon = Debug.getLogger(proxy.getLogTopic()+ '.' +
81       classname.substring(classname.lastIndexOf('.') +1));
82   }
83
84   /**
85    * Constructor called by a <code>ProxyAgent</code> managing multiple
86    * connections.
87    * @param id identifier local to the driver creator
88    * @param proxy proxy agent to forward notifications to
89    * @param in stream to read notifications from
90    * @param maxNotSent max number of notifications between
91    * <code>FlowControl</code>s
92    * @param key key identifying the connection.
93    */

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   /**
104    * Provides a string image for this object.
105    *
106    * @return printable image of this object
107    */

108   public String JavaDoc 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     // Single connection context:
123
if (key == 0)
124       sendTo(proxyId, new FlowControlNot(id));
125
126     // In a multi-connections context, flagging the FlowControlNot
127
// notification with the key so that it is known which
128
// DriverIn to control.
129
else
130       sendTo(proxyId, new FlowControlNot(id, key));
131     while (nbFlowControl > 1) {
132       try { wait(); } catch (InterruptedException JavaDoc 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         // End of input flow.
168
break mainLoop;
169       } catch (Exception JavaDoc 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         // "Passes" the notification to the proxy:
184
proxy.driverReact(key, m);
185         nbNotSent += 1;
186       }
187     }
188   }
189
190   /**
191    * Finalizes the driver.
192    *
193    * Reports driver end to the proxy agent, with a <code>DriverDone</code>
194    * notification.
195    */

196   protected void end() {
197     // report end to proxy
198
try {
199       // Single connection context
200
if (key == 0)
201         sendTo(proxyId, new DriverDone(id));
202
203       // In a multi-connections context, flagging the DriverDone
204
// notification with the key so that it is known which
205
// DriverIn to close.
206
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   /**
216    * Close the OutputStream.
217    */

218   public void close() {
219     try {
220       in.close();
221     } catch (Exception JavaDoc exc) {}
222     in = null;
223   }
224 }
225
Popular Tags