001: // $Id: SIZE.java,v 1.16.2.1 2007/04/27 08:03:51 belaban Exp $
002:
003: package org.jgroups.protocols;
004:
005: import org.jgroups.Event;
006: import org.jgroups.Message;
007: import org.jgroups.util.Util;
008: import org.jgroups.stack.Protocol;
009:
010: import java.io.ByteArrayOutputStream;
011: import java.io.DataOutputStream;
012: import java.util.Map;
013: import java.util.Properties;
014: import java.util.Vector;
015:
016: /**
017: * Protocol which prints out the real size of a message. To do this, the message
018: * is serialized into a byte buffer and its size read. Don't use this layer in
019: * a production stack since the costs are high (just for debugging).
020: *
021: * @author Bela Ban June 13 2001
022: */
023: public class SIZE extends Protocol {
024: final Vector members = new Vector();
025: boolean print_msg = false;
026: boolean raw_buffer = false; // just print size of message buffer
027:
028: /** Min size in bytes above which msgs should be printed */
029: long min_size = 0;
030:
031: final ByteArrayOutputStream out_stream = new ByteArrayOutputStream(
032: 65535);
033:
034: /**
035: * All protocol names have to be unique !
036: */
037: public String getName() {
038: return "SIZE";
039: }
040:
041: public void init() {
042: }
043:
044: /**
045: * Setup the Protocol instance acording to the configuration string
046: */
047: public boolean setProperties(Properties props) {
048: super .setProperties(props);
049: String str;
050:
051: str = props.getProperty("print_msg");
052: if (str != null) {
053: print_msg = Boolean.valueOf(str).booleanValue();
054: props.remove("print_msg");
055: }
056:
057: str = props.getProperty("raw_buffer");
058: if (str != null) {
059: raw_buffer = Boolean.valueOf(str).booleanValue();
060: props.remove("raw_buffer");
061: }
062:
063: str = props.getProperty("min_size");
064: if (str != null) {
065: min_size = Integer.parseInt(str);
066: props.remove("min_size");
067: }
068:
069: if (props.size() > 0) {
070: log.error("the following properties are not recognized: "
071: + props);
072:
073: return false;
074: }
075: return true;
076: }
077:
078: public void up(Event evt) {
079: Message msg;
080: int payload_size = 0, serialized_size;
081:
082: switch (evt.getType()) {
083:
084: case Event.MSG:
085: msg = (Message) evt.getArg();
086: payload_size = msg.getLength();
087:
088: if (raw_buffer) {
089: if (log.isTraceEnabled())
090: log.trace("size of message buffer is "
091: + payload_size + ", " + numHeaders(msg)
092: + " headers");
093: } else {
094: serialized_size = sizeOf(msg);
095: if (serialized_size > min_size) {
096: if (log.isTraceEnabled())
097: log.trace("size of serialized message is "
098: + serialized_size + ", "
099: + numHeaders(msg) + " headers");
100:
101: }
102: }
103: if (print_msg) {
104: if (log.isTraceEnabled())
105: log.trace("headers are " + msg.getHeaders()
106: + ", payload size=" + payload_size);
107: }
108: break;
109: }
110:
111: passUp(evt); // pass up to the layer above us
112: }
113:
114: public void down(Event evt) {
115: Message msg;
116: int payload_size = 0, serialized_size;
117:
118: switch (evt.getType()) {
119:
120: case Event.MSG:
121: msg = (Message) evt.getArg();
122: payload_size = msg.getLength();
123:
124: if (raw_buffer) {
125: if (log.isTraceEnabled())
126: log.trace("size of message buffer is "
127: + payload_size + ", " + numHeaders(msg)
128: + " headers");
129: } else {
130: serialized_size = sizeOf(msg);
131: if (serialized_size > min_size) {
132: if (log.isTraceEnabled())
133: log.trace("size of serialized message is "
134: + serialized_size + ", "
135: + numHeaders(msg) + " headers");
136:
137: }
138: }
139: if (print_msg) {
140: if (log.isTraceEnabled())
141: log.trace("headers are " + msg.getHeaders()
142: + ", payload size=" + payload_size);
143: }
144: break;
145: }
146:
147: passDown(evt); // Pass on to the layer below us
148: }
149:
150: int sizeOf(Message msg) {
151: DataOutputStream out = null;
152:
153: synchronized (out_stream) {
154: try {
155: out_stream.reset();
156: out = new DataOutputStream(out_stream);
157: msg.writeTo(out);
158: out.flush();
159: return out_stream.size();
160: } catch (Exception e) {
161: return 0;
162: } finally {
163: Util.close(out);
164: }
165: }
166: }
167:
168: int numHeaders(Message msg) {
169: if (msg == null)
170: return 0;
171: Map hdrs = msg.getHeaders();
172: return hdrs != null ? hdrs.size() : 0;
173: }
174:
175: }
|