KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > protocols > SIZE


1 // $Id: SIZE.java,v 1.12 2005/01/24 13:03:05 belaban Exp $
2

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 JavaDoc;
10 import java.io.DataOutputStream JavaDoc;
11 import java.util.Map JavaDoc;
12 import java.util.Properties JavaDoc;
13 import java.util.Vector JavaDoc;
14
15
16 /**
17  * Protocol which prints out the real size of a message. To do this, the message
18  * is serialized into a byte buffer and its size read. Don't use this layer in
19  * a production stack since the costs are high (just for debugging).
20  *
21  * @author Bela Ban June 13 2001
22  */

23 public class SIZE extends Protocol {
24     final Vector JavaDoc members=new Vector JavaDoc();
25     boolean print_msg=false;
26     boolean raw_buffer=false; // just print size of message buffer
27

28     /** Min size in bytes above which msgs should be printed */
29     long min_size=0;
30
31     final ByteArrayOutputStream JavaDoc out_stream=new ByteArrayOutputStream JavaDoc(65535);
32
33
34     /**
35      * All protocol names have to be unique !
36      */

37     public String JavaDoc getName() {
38         return "SIZE";
39     }
40
41
42     public void init() {
43     }
44
45
46     /**
47      * Setup the Protocol instance acording to the configuration string
48      */

49     public boolean setProperties(Properties JavaDoc props) {super.setProperties(props);
50         String JavaDoc 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); // pass up to the layer above us
110
}
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); // Pass on to the layer below us
143
}
144
145
146     int sizeOf(Message msg) {
147         DataOutputStream JavaDoc out;
148
149         synchronized(out_stream) {
150             try {
151                 out_stream.reset();
152                 out=new DataOutputStream JavaDoc(out_stream);
153                 msg.writeTo(out);
154                 out.flush();
155                 return out_stream.size();
156             }
157             catch(Exception JavaDoc e) {
158                 return 0;
159             }
160         }
161     }
162
163     int numHeaders(Message msg) {
164         if(msg == null)
165             return 0;
166         Map JavaDoc hdrs=msg.getHeaders();
167         return hdrs !=null? hdrs.size() : 0;
168     }
169
170
171 }
172
Popular Tags