1 21 package fr.dyade.aaa.agent; 22 23 import java.io.*; 24 import java.util.*; 25 26 import org.objectweb.util.monolog.api.BasicLevel; 27 import org.objectweb.util.monolog.api.Logger; 28 29 import fr.dyade.aaa.util.*; 30 31 39 public class Channel { 40 static Channel channel = null; 41 42 47 static Channel newInstance() throws Exception { 48 String cname = System.getProperty("Channel", 49 "fr.dyade.aaa.agent.Channel"); 50 Class cclass = Class.forName(cname); 51 channel = (Channel) cclass.newInstance(); 52 return channel; 53 } 54 55 protected Logger logmon = null; 56 57 61 protected Channel() { 62 consumers = new Vector(); 63 64 logmon = Debug.getLogger(Debug.A3Engine + 66 ".#" + AgentServer.getServerId()); 67 logmon.log(BasicLevel.DEBUG, toString() + " created."); 68 } 69 70 static Vector consumers = null; 71 72 95 public final static void sendTo(AgentId to, 96 Notification not) { 97 108 if (Thread.currentThread() == AgentServer.engine.thread) { 110 AgentServer.engine.push(AgentServer.engine.agent.getId(), to, not); 111 } else { 112 channel.directSendTo(AgentId.localId, to, not); 113 } 114 } 115 116 123 static final void post(Message msg) throws Exception { 124 try { 125 MessageConsumer cons = AgentServer.getConsumer(msg.to.getTo()); 126 if (! consumers.contains(cons)) { 127 consumers.add(cons); 128 } 129 cons.post(msg); 130 } catch (UnknownServerException exc) { 131 channel.logmon.log(BasicLevel.WARN, 132 channel.toString() + ", can't post message: " + msg, 133 exc); 134 } 136 } 137 138 141 static final void save() throws IOException { 142 for (int i=0; i<consumers.size(); i++) { 143 ((MessageConsumer) consumers.elementAt(i)).save(); 144 } 145 } 146 147 158 static final void validate() { 159 for (int i=0; i<consumers.size(); i++) { 160 ((MessageConsumer) consumers.elementAt(i)).validate(); 161 } 162 consumers.clear(); 163 } 164 165 182 void directSendTo(AgentId from, 183 AgentId to, 184 Notification not) { 185 MessageConsumer consumer = null; 186 Message msg = null; 187 188 if (logmon.isLoggable(BasicLevel.DEBUG)) 189 logmon.log(BasicLevel.DEBUG, 190 toString() + ".directSendTo(" + from + ", " + to + ", " + not + ")"); 191 192 if ((to == null) || to.isNullId()) 193 return; 194 195 msg = Message.alloc(from, to, not); 196 try { 197 consumer = AgentServer.getConsumer(to.to); 198 } catch (UnknownServerException exc) { 199 channel.logmon.log(BasicLevel.ERROR, 200 toString() + ", can't post message: " + msg, 201 exc); 202 return; 204 } 205 206 try { 207 AgentServer.getTransaction().begin(); 208 consumer.post(msg); 209 consumer.save(); 210 AgentServer.getTransaction().commit(); 211 consumer.validate(); 213 AgentServer.getTransaction().release(); 214 } catch (Exception exc) { 215 logmon.log(BasicLevel.FATAL, 217 toString() + ", Transaction problem.", exc); 218 throw new TransactionError(toString() + ", " + exc.getMessage()); 219 } 220 } 221 222 227 public final String toString() { 228 StringBuffer strbuf = new StringBuffer (); 229 strbuf.append("Channel#").append(AgentServer.getServerId()); 230 return strbuf.toString(); 231 } 232 } 233 | Popular Tags |