001: // $Id: TotalTokenProtocolObserver.java,v 1.6 2006/08/13 09:05:10 mimbert Exp $
002: package org.jgroups.blocks;
003:
004: import org.apache.commons.logging.Log;
005: import org.apache.commons.logging.LogFactory;
006: import org.jgroups.Event;
007: import org.jgroups.JChannel;
008: import org.jgroups.Message;
009: import org.jgroups.stack.Protocol;
010: import org.jgroups.stack.ProtocolObserver;
011:
012: import java.util.Vector;
013:
014: public class TotalTokenProtocolObserver implements ProtocolObserver {
015: String name;
016: static Log logger = LogFactory
017: .getLog(TotalTokenProtocolObserver.class);
018:
019: public TotalTokenProtocolObserver(JChannel c) {
020: this .name = c.getLocalAddress().toString();
021:
022: Vector prots = c.getProtocolStack().getProtocols();
023: for (int i = 0; i < prots.size(); i++) {
024: Protocol prot = (Protocol) prots.elementAt(i);
025: if ("TOTAL_TOKEN".equals(prot.getName())) {
026: prot.setObserver(this );
027: }
028: }
029: }
030:
031: public void setProtocol(Protocol prot) {
032: }
033:
034: public boolean up(Event evt, int num_evts) {
035: return true;
036: }
037:
038: public boolean passUp(Event evt) {
039: Object obj = null;
040: Message msg;
041:
042: if (evt.getType() != Event.MSG) {
043: logger.debug("For channel " + name + " received event:"
044: + evt);
045: return true;
046: }
047:
048: msg = (Message) evt.getArg();
049: if (msg.getLength() > 0) {
050: try {
051: obj = msg.getObject();
052: } catch (ClassCastException cast_ex) {
053: logger
054: .debug("For channel " + name + " received:"
055: + msg);
056: return true;
057: } catch (Exception e) {
058: logger.error(e);
059: }
060:
061: logger.debug("For channel " + name + " received:" + obj);
062: } else
063: logger
064: .debug("For channel " + name
065: + " received null msg from " + msg.getSrc()
066: + ", headers are "
067: + msg.printObjectHeaders() + ')');
068:
069: return true;
070: }
071:
072: public boolean down(Event evt, int num_evts) {
073: Object obj = null;
074: Message msg;
075:
076: if (evt.getType() != Event.MSG) {
077: logger.debug("For channel " + name + " sent event:" + evt);
078: return true;
079: }
080:
081: msg = (Message) evt.getArg();
082: if (msg.getLength() > 0) {
083: try {
084: obj = msg.getObject();
085: } catch (ClassCastException cast_ex) {
086: logger.debug("For channel " + name + " sent:" + msg);
087: return true;
088: } catch (Exception e) {
089: logger.error(e);
090: }
091: logger.debug("For channel " + name + " sent:" + obj);
092:
093: } else
094: logger.debug("For channel " + name + " sent null msg to "
095: + msg.getDest() + ", headers are "
096: + msg.printObjectHeaders() + " )");
097: return true;
098: }
099:
100: public boolean passDown(Event evt) {
101: return true;
102: }
103:
104: }
|