1 4 package com.tc.net.protocol.tcm; 5 6 import EDU.oswego.cs.dl.util.concurrent.SynchronizedLong; 7 8 import com.tc.text.StringFormatter; 9 10 import java.util.HashMap ; 11 import java.util.Iterator ; 12 import java.util.Map ; 13 14 public class MessageMonitorImpl implements MessageMonitor { 15 16 private final Map counters = new HashMap (); 17 private final StringFormatter formatter = new StringFormatter(); 18 19 public void newIncomingMessage(TCMessage message) { 20 getOrCreateMessageCounter(message.getMessageType()).newIncomingMessage(message); 21 } 22 23 public void newOutgoingMessage(TCMessage message) { 24 getOrCreateMessageCounter(message.getMessageType()).newOutgoingMessage(message); 25 } 26 27 private MessageCounter getOrCreateMessageCounter(Object key) { 28 synchronized (counters) { 29 MessageCounter rv = (MessageCounter) counters.get(key); 30 if (rv == null) { 31 rv = new MessageCounter(formatter, key.toString()); 32 counters.put(key, rv); 33 } 34 return rv; 35 } 36 } 37 38 public String toString() { 39 StringBuffer rv = new StringBuffer (); 40 String nl = System.getProperty("line.separator"); 41 rv.append("Message monitor").append(nl); 42 synchronized (counters) { 43 for (Iterator i=counters.values().iterator(); i.hasNext();) { 44 rv.append(i.next()).append(nl); 45 } 46 } 47 rv.append(nl); 48 return rv.toString(); 49 } 50 51 private static class MessageCounter { 52 53 private final String name; 54 private final SynchronizedLong incomingCount = new SynchronizedLong(0); 55 private final SynchronizedLong incomingData = new SynchronizedLong(0); 56 57 private final SynchronizedLong outgoingCount = new SynchronizedLong(0); 58 private final SynchronizedLong outgoingData = new SynchronizedLong(0); 59 private final StringFormatter formatter; 60 61 private MessageCounter(StringFormatter formatter, String name) { 62 this.formatter = formatter; 63 this.name = formatter.rightPad(25, name); 64 } 65 66 private synchronized void newIncomingMessage(TCMessage message) { 67 incomingCount.increment(); 68 incomingData.add(message.getTotalLength()); 69 } 70 71 private synchronized void newOutgoingMessage(TCMessage message) { 72 outgoingCount.increment(); 73 outgoingData.add(message.getTotalLength()); 74 } 75 76 public String toString() { 77 return name + "| IN: " + formatter.leftPad(30, incomingCount) + ", " + formatter.leftPad(30, incomingData) + "b" 78 + "| OUT: " + formatter.leftPad(30, outgoingCount) + ", " + formatter.leftPad(30, outgoingData) + "b"; 79 80 } 81 } 82 } 83 | Popular Tags |