1 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 31 protected AgentId proxy; 32 33 protected Queue mq; 34 35 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 logmon = proxy.logmon; 49 } 50 51 54 public String toString() { 55 StringBuffer output = new StringBuffer (); 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 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 96 protected abstract void react(Notification not) throws Exception ; 97 98 104 protected void end() { 105 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 |