KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > blocks > TotalTokenProtocolObserver


1 // $Id: TotalTokenProtocolObserver.java,v 1.5 2004/07/05 14:15:02 belaban Exp $
2
package org.jgroups.blocks;
3
4 import org.apache.log4j.Logger;
5 import org.jgroups.Event;
6 import org.jgroups.JChannel;
7 import org.jgroups.Message;
8 import org.jgroups.stack.Protocol;
9 import org.jgroups.stack.ProtocolObserver;
10
11 import java.util.Vector JavaDoc;
12
13 public class TotalTokenProtocolObserver implements ProtocolObserver
14 {
15     String JavaDoc name;
16     static Logger logger = Logger.getLogger(TotalTokenProtocolObserver.class.getName());
17
18     
19     public TotalTokenProtocolObserver(JChannel c)
20     {
21         this.name = c.getLocalAddress().toString();
22         
23         Vector JavaDoc prots = c.getProtocolStack().getProtocols();
24         for (int i = 0; i < prots.size(); i++)
25         {
26             Protocol prot = (Protocol) prots.elementAt(i);
27             if ("TOTAL_TOKEN".equals(prot.getName()))
28             {
29                 prot.setObserver(this);
30             }
31         }
32     }
33
34     public void setProtocol(Protocol prot)
35     {
36     }
37
38     public boolean up(Event evt, int num_evts)
39     {
40         return true;
41     }
42
43     public boolean passUp(Event evt)
44     {
45         Object JavaDoc obj = null;
46         Message msg;
47
48         if (evt.getType() != Event.MSG)
49         {
50             logger.debug("For channel "+name+" received event:"+evt);
51             return true;
52         }
53
54         msg = (Message) evt.getArg();
55         if (msg.getLength() > 0)
56         {
57             try
58             {
59                 obj=msg.getObject();
60             }
61             catch (ClassCastException JavaDoc cast_ex)
62             {
63                 logger.debug("For channel "+name+" received:"+msg);
64                 return true;
65             }
66             catch (Exception JavaDoc e)
67             {
68                 logger.error(e);
69             }
70
71             logger.debug("For channel "+name+" received:"+obj);
72         }
73         else
74             logger.debug(
75                 "For channel "+name+" received null msg from "
76                     + msg.getSrc()
77                     + ", headers are "
78                     + msg.printObjectHeaders()
79                     + ')');
80
81         return true;
82     }
83
84     public boolean down(Event evt, int num_evts)
85     {
86         Object JavaDoc obj=null;
87         Message msg;
88
89         if(evt.getType() != Event.MSG) {
90             logger.debug("For channel "+name+" sent event:"+evt);
91             return true;
92         }
93
94         msg=(Message)evt.getArg();
95         if(msg.getLength() > 0) {
96             try {
97                 obj=msg.getObject();
98             }
99             catch(ClassCastException JavaDoc cast_ex) {
100                 logger.debug("For channel "+name+" sent:"+msg);
101                 return true;
102             }
103             catch(Exception JavaDoc e) {
104             logger.error(e);
105             }
106             logger.debug("For channel "+name+" sent:"+obj);
107
108         }
109         else
110             logger.debug("For channel "+name+" sent null msg to " + msg.getDest() + ", headers are " +
111                        msg.printObjectHeaders() + " )");
112         return true;
113     }
114     
115     public boolean passDown(Event evt)
116     {
117         return true;
118     }
119
120 }
121
Popular Tags