1 3 package org.jgroups.protocols; 4 5 import org.jgroups.Event; 6 import org.jgroups.Message; 7 import org.jgroups.View; 8 import org.jgroups.stack.Protocol; 9 import org.jgroups.stack.ProtocolObserver; 10 11 import java.util.Properties ; 12 import java.util.Vector ; 13 14 15 35 public class PERF extends Protocol { 36 boolean details=false; 37 final Vector members=new Vector (); 38 Vector protocols=null; 39 static final String name="PERF"; 40 41 42 45 public String getName() { 46 return name; 47 } 48 49 50 public boolean setProperties(Properties props) { 51 super.setProperties(props); 52 String str; 53 54 str=props.getProperty("details"); 55 if(str != null) { 56 details=Boolean.valueOf(str).booleanValue(); 57 props.remove("details"); 58 } 59 if(props.size() > 0) { 60 System.err.println("PERF.setProperties(): these properties are not recognized:"); 61 props.list(System.out); 62 return false; 63 } 64 return true; 65 } 66 67 68 public void start() throws Exception { 69 protocols=stack != null ? stack.getProtocols() : null; 70 setupObservers(); 71 } 72 73 74 public void up(Event evt) { 75 Message msg; 76 PerfHeader hdr; 77 78 switch(evt.getType()) { 79 80 case Event.MSG: 81 msg=(Message)evt.getArg(); 82 hdr=removePerfHeader(msg); 83 if(hdr != null) { 84 hdr.setEndTime(); 85 hdr.setDone(name, PerfHeader.UP); if(log.isTraceEnabled()) 87 log.trace(hdr.printContents(details, protocols) + "\n---------------------------------\n"); 88 } 89 break; 90 } 91 92 passUp(evt); } 94 95 96 public void down(Event evt) { 97 Message msg; 98 99 switch(evt.getType()) { 100 101 case Event.TMP_VIEW: 102 case Event.VIEW_CHANGE: 103 Vector new_members=((View)evt.getArg()).getMembers(); 104 synchronized(members) { 105 members.removeAllElements(); 106 members.addAll(new_members); 107 } 108 passDown(evt); 109 break; 110 111 case Event.MSG: 112 msg=(Message)evt.getArg(); 113 initializeMessage(msg); break; 115 } 116 117 passDown(evt); } 119 120 121 122 123 126 void setupObservers() { 127 Protocol p=null; 128 String pname; 129 PerfObserver po=null; 130 131 for(int i=0; i < protocols.size(); i++) { 132 p=(Protocol)protocols.elementAt(i); 133 pname=p.getName(); 134 if(pname != null) { 135 po=new PerfObserver(pname); 136 p.setObserver(po); 137 } 138 } 139 } 140 141 142 void initializeMessage(Message msg) { 143 PerfHeader hdr=new PerfHeader(msg.getSrc(), msg.getDest()); 144 Protocol p; 145 String pname=null; 146 147 if(protocols == null) { 148 System.err.println("PERF.initializeMessage(): 'protocols' variable is null"); 149 return; 150 } 151 152 for(int i=0; i < protocols.size(); i++) { 153 p=(Protocol)protocols.elementAt(i); 154 pname=p.getName(); 155 if(pname != null) { 156 hdr.addEntry(pname); 157 } 158 } 159 hdr.setReceived(name, PerfHeader.DOWN); msg.putHeader(name, hdr); 161 } 162 163 164 167 PerfHeader removePerfHeader(Message m) { 168 Object hdr=null; 169 170 if(m == null || (hdr=m.removeHeader(name)) == null) 171 return null; 172 return (PerfHeader)hdr; 173 } 174 175 176 180 190 191 } 192 193 194 197 class PerfObserver implements ProtocolObserver { 198 final String prot_name; 199 boolean bottom=false; 200 201 PerfObserver(String prot_name) { 202 this.prot_name=prot_name; 203 } 204 205 206 public void setProtocol(Protocol prot) { 207 if(prot != null && prot.getDownProtocol() == null) 208 bottom=true; 209 } 210 211 212 public boolean up(Event evt, int num_evts) { 213 PerfHeader hdr; 214 if(evt.getType() == Event.MSG) { 215 hdr=getPerfHeader((Message)evt.getArg()); 216 if(hdr != null) { 217 hdr.setReceived(prot_name, PerfHeader.UP); 218 if(bottom) 219 hdr.setNetworkReceived(); 220 } 221 } 222 return true; 223 } 224 225 226 public boolean passUp(Event evt) { 227 PerfHeader hdr; 228 if(evt.getType() == Event.MSG) { 229 hdr=getPerfHeader((Message)evt.getArg()); 230 if(hdr != null) { 231 hdr.setDone(prot_name, PerfHeader.UP); 232 } 233 } 234 return true; 235 } 236 237 238 public boolean down(Event evt, int num_evts) { 239 PerfHeader hdr; 240 if(evt.getType() == Event.MSG) { 241 hdr=getPerfHeader((Message)evt.getArg()); 242 if(hdr != null) { 243 hdr.setReceived(prot_name, PerfHeader.DOWN); 244 } 245 } 246 return true; 247 } 248 249 250 public boolean passDown(Event evt) { 251 PerfHeader hdr; 252 if(evt.getType() == Event.MSG) { 253 hdr=getPerfHeader((Message)evt.getArg()); 254 if(hdr != null) { 255 hdr.setDone(prot_name, PerfHeader.DOWN); 256 if(bottom) 257 hdr.setNetworkSent(); 258 } 259 } 260 return true; 261 } 262 263 264 PerfHeader getPerfHeader(Message m) { 265 Object hdr=null; 266 267 if(m == null || (hdr=m.getHeader(PERF.name)) == null) 268 return null; 269 return (PerfHeader)hdr; 270 } 271 272 273 } 274 | Popular Tags |