KickJava   Java API By Example, From Geeks To Geeks.

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


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

20 package fr.dyade.aaa.agent;
21
22 import java.io.*;
23
24 import org.objectweb.util.monolog.api.BasicLevel;
25
26 import fr.dyade.aaa.util.*;
27
28 public abstract class AgentDriver extends Driver {
29
30   /** id of associated proxy agent */
31   protected AgentId proxy;
32   /** queue of <code>Notification</code> objects to be sent */
33   protected Queue mq;
34
35   /**
36    * Constructor.
37    *
38    * @param id internal id of driver
39    * @param proxy Reference to the associated proxy agent
40    * @param mq Queue of <code>Notification</code> objects to be sent
41    */

42   protected AgentDriver(int id, Agent proxy, Queue mq) {
43     super(id);
44     this.proxy = proxy.getId();
45     this.mq = mq;
46     this.name = proxy.getName() + ".AgentDriver#" + id;
47     // Get the proxy logging monitor
48
logmon = proxy.logmon;
49   }
50
51   /**
52    * Provides a string image for this object.
53    */

54   public String JavaDoc toString() {
55     StringBuffer JavaDoc output = new StringBuffer JavaDoc();
56     output.append("(");
57     output.append(super.toString());
58     output.append(",proxy=");
59     output.append(proxy);
60     output.append(",mq=");
61     output.append(mq);
62     output.append(")");
63     return output.toString();
64   }
65
66   public void run() {
67     Notification m = null;
68     mainLoop:
69     while (isRunning) {
70       try {
71     canStop = true;
72     m = (Notification) mq.get();
73     if (! isRunning) break mainLoop;
74     react(m);
75     canStop = false;
76       } catch (Exception JavaDoc exc) {
77         logmon.log(BasicLevel.ERROR,
78                    getName() + ", exception in " + this +
79                    ".react(" + m + ")", exc);
80     break mainLoop;
81       }
82       mq.pop();
83     }
84   }
85
86   public void close() {}
87
88   /**
89    * Reacts to notifications from proxy.
90    *
91    * @param not notification to react to
92    *
93    * @exception Exception
94    * unspecialized exception
95    */

96   protected abstract void react(Notification not) throws Exception JavaDoc;
97
98   /**
99    * Finalizes the driver.
100    *
101    * Reports driver end to the proxy agent, with a <code>DriverDone</code>
102    * notification.
103    */

104   protected void end() {
105     // report end to proxy
106
try {
107       sendTo(proxy, new DriverDone(id));
108     } catch (IOException exc) {
109       logmon.log(BasicLevel.ERROR,
110                    getName() + ", error in reporting end", exc);
111     }
112   }
113 }
114
Popular Tags