1 3 package org.jgroups.protocols; 4 5 import org.jgroups.Event; 6 import org.jgroups.Message; 7 import org.jgroups.stack.Protocol; 8 9 import java.io.ByteArrayOutputStream ; 10 import java.io.DataOutputStream ; 11 import java.util.Map ; 12 import java.util.Properties ; 13 import java.util.Vector ; 14 15 16 23 public class SIZE extends Protocol { 24 final Vector members=new Vector (); 25 boolean print_msg=false; 26 boolean raw_buffer=false; 28 29 long min_size=0; 30 31 final ByteArrayOutputStream out_stream=new ByteArrayOutputStream (65535); 32 33 34 37 public String getName() { 38 return "SIZE"; 39 } 40 41 42 public void init() { 43 } 44 45 46 49 public boolean setProperties(Properties props) {super.setProperties(props); 50 String str; 51 52 str=props.getProperty("print_msg"); 53 if(str != null) { 54 print_msg=Boolean.valueOf(str).booleanValue(); 55 props.remove("print_msg"); 56 } 57 58 str=props.getProperty("raw_buffer"); 59 if(str != null) { 60 raw_buffer=Boolean.valueOf(str).booleanValue(); 61 props.remove("raw_buffer"); 62 } 63 64 str=props.getProperty("min_size"); 65 if(str != null) { 66 min_size=Integer.parseInt(str); 67 props.remove("min_size"); 68 } 69 70 if(props.size() > 0) { 71 System.err.println("the following properties are not recognized:"); 72 props.list(System.out); 73 return false; 74 } 75 return true; 76 } 77 78 79 public void up(Event evt) { 80 Message msg; 81 int payload_size=0, serialized_size; 82 83 switch(evt.getType()) { 84 85 case Event.MSG: 86 msg=(Message)evt.getArg(); 87 payload_size=msg.getLength(); 88 89 if(raw_buffer) { 90 if(log.isTraceEnabled()) 91 log.trace("size of message buffer is " + payload_size + ", " + numHeaders(msg) + " headers"); 92 } 93 else { 94 serialized_size=sizeOf(msg); 95 if(serialized_size > min_size) { 96 if(log.isTraceEnabled()) 97 log.trace("size of serialized message is " + serialized_size + 98 ", " + numHeaders(msg) + " headers"); 99 100 } 101 } 102 if(print_msg) { 103 if(log.isTraceEnabled()) 104 log.trace("headers are " + msg.getHeaders() + ", payload size=" + payload_size); 105 } 106 break; 107 } 108 109 passUp(evt); } 111 112 113 public void down(Event evt) { 114 Message msg; 115 int payload_size=0, serialized_size; 116 117 switch(evt.getType()) { 118 119 case Event.MSG: 120 msg=(Message)evt.getArg(); 121 payload_size=msg.getLength(); 122 123 if(raw_buffer) { 124 if(log.isTraceEnabled()) 125 log.trace("size of message buffer is " + payload_size + ", " + numHeaders(msg) + " headers"); 126 } 127 else { 128 serialized_size=sizeOf(msg); 129 if(serialized_size > min_size) { 130 if(log.isTraceEnabled()) 131 log.trace("size of serialized message is " + serialized_size + ", " + numHeaders(msg) + " headers"); 132 133 } 134 } 135 if(print_msg) { 136 if(log.isTraceEnabled()) 137 log.trace("headers are " + msg.getHeaders() + ", payload size=" + payload_size); 138 } 139 break; 140 } 141 142 passDown(evt); } 144 145 146 int sizeOf(Message msg) { 147 DataOutputStream out; 148 149 synchronized(out_stream) { 150 try { 151 out_stream.reset(); 152 out=new DataOutputStream (out_stream); 153 msg.writeTo(out); 154 out.flush(); 155 return out_stream.size(); 156 } 157 catch(Exception e) { 158 return 0; 159 } 160 } 161 } 162 163 int numHeaders(Message msg) { 164 if(msg == null) 165 return 0; 166 Map hdrs=msg.getHeaders(); 167 return hdrs !=null? hdrs.size() : 0; 168 } 169 170 171 } 172 | Popular Tags |