KickJava   Java API By Example, From Geeks To Geeks.

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


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 import fr.dyade.aaa.util.*;
29
30 /**
31  * Output driver.
32  */

33 class DriverOut extends Driver {
34
35   /** Reference to the proxy agent */
36   protected ProxyAgent proxy;
37   /** queue of <code>Notification</code> objects to be sent */
38   protected Queue mq;
39   /** stream to write notifications to */
40   protected NotificationOutputStream out;
41
42   /** Identifies the <code>DriverOut</code> in a multi-connections context. */
43   private int key = 0;
44  
45   /**
46    * Constructor.
47    *
48    * @param id identifier local to the driver creator
49    * @param proxy associated proxy agent
50    * @param mq queue of <code>Notification</code> objects to be sent
51    * @param out stream to write notifications to
52    */

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     // Get the logging monitor using proxy topic
63
String JavaDoc classname = getClass().getName();
64     logmon = Debug.getLogger(proxy.getLogTopic()+ '.' +
65       classname.substring(classname.lastIndexOf('.') +1));
66   }
67
68   /**
69    * Constructor called by a <code>ProxyAgent</code> managing multiple
70    * connections.
71    *
72    * @param id identifier local to the driver creator
73    * @param proxy associated proxy agent
74    * @param mq queue of <code>Notification</code> objects to be sent
75    * @param out stream to write notifications to
76    * @param key key identifying the connection.
77    */

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 JavaDoc exc) {
108         canStop = false;
109         break mainLoop;
110       } finally {
111         Thread.interrupted();
112         canStop = false;
113       }
114       mq.pop();
115     }
116   }
117     
118   /**
119    * Close the OutputStream.
120    */

121   public void close() {
122     try {
123       out.close();
124     } catch (Exception JavaDoc exc) {}
125     out = null;
126   }
127
128   /**
129    * Sends a notification on the output stream.
130    *
131    * @param not notification to send
132    */

133   void sendTo(Notification not) {
134     mq.push(not);
135   }
136
137   /**
138    * Finalizes the driver.
139    *
140    * Reports driver end to the proxy agent, with a <code>DriverDone</code>
141    * notification.
142    */

143   protected void end() {
144     // report end to proxy
145
try {
146       // Single connection context.
147
if (key == 0)
148         sendTo(proxy.getId(), new DriverDone(id));
149
150       // In a multi-connections context, flagging the DriverDone
151
// notification with the key so that it is known which
152
// DriverOut to close.
153
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   /** remove all elements of queue */
163   protected void clean() {
164     mq.removeAllElements();
165   }
166 }
167
Popular Tags