package lava.net.psyc.packages; import java.util.Calendar; import java.util.Date; import java.util.Enumeration; import java.util.Properties; import java.util.Vector; import lava.net.common.UNL; import lava.net.psyc.PSYCDeliveryException; import lava.net.psyc.PSYCMessageCenter; import lava.net.psyc.PSYCPackage; /** * **/ public class Statistics extends Skeleton { /** * **/ public final static String PackageName = "Statistics"; class DummyListener implements StatisticsListener { /** * **/ public void statisticsRequest(UNL source, String body) { try { Statistics.this.reply(source); } catch(PSYCDeliveryException e) { } } /** * **/ public void statisticsReply(UNL source, String body) { } } class Event { /** * **/ long millis = 0; /** * **/ int loglevel = 0; /** * **/ String event = null; /** * **/ String string = null; /** * **/ Event(long millis, int loglevel, String event) { this.millis = millis; this.loglevel = normalize(loglevel); this.event = event; } /** * **/ public String toString() { if(string != null) return string; if(cal == null) cal = Calendar.getInstance(); StringBuffer buf = new StringBuffer(); cal.clear(); cal.setTime(new Date(millis)); int tmp = cal.get(Calendar.MONTH) + 1; if(tmp < 10) buf.append("0"); buf.append(tmp); buf.append("/"); tmp = cal.get(Calendar.DAY_OF_MONTH); if(tmp < 10) buf.append("0"); buf.append(tmp); buf.append(" "); tmp = cal.get(Calendar.HOUR_OF_DAY); if(tmp < 10) buf.append("0"); buf.append(tmp); buf.append(":"); tmp = cal.get(Calendar.MINUTE); if(tmp < 10) buf.append("0"); buf.append(tmp); buf.append(" "); buf.append(loglevel); if(event != null) { buf.append(" "); buf.append(event); } string = buf.toString(); return string; } } /** * **/ public final static String requestStatisticsTag = "_request_statistics"; /** * **/ private final static String[] requestStatisticsAliases = // {"request_statistics"}; /** * **/ public final static String infoStatisticsTag = // "_info_statistics"; /** * **/ private final static String[] infoStatisticsAliases = // {"info_statistics"}; /** * **/ private StatisticsListener listener = new DummyListener(); /** * **/ private static Calendar cal = null; /** * **/ private Vector eventLog = new Vector(); /** * **/ public Statistics() { this (null); } /** * **/ public Statistics(StatisticsListener listener) { super(); setPackageName(PackageName); addPackageMethod(requestStatisticsTag,requestStatisticsAliases,true); addPackageMethod(infoStatisticsTag,infoStatisticsAliases,// true); if(listener != null) this.listener = listener; } /** * **/ public boolean understands() { return !(listener instanceof DummyListener); } /** * **/ public void received(UNL source, String method, String body) { if(requestStatisticsTag.equals(method)) listener.statisticsRequest(source,body); else if(infoStatisticsTag.equals(method)) listener.statisticsReply(source,body); } /** * **/ private int normalize(int loglevel) { if(loglevel < 0) loglevel *= -1; return loglevel % 10; } /** * **/ public void resetEventLog() { eventLog.removeAllElements(); } /** * **/ public void addEvent(int loglevel, String event) { eventLog.addElement(new Event(System.currentTimeMillis(),// loglevel,event)); } /** * **/ public void addEvent(String event) { addEvent(0,event); } /** * **/ public void request(UNL target, String body) throws PSYCDeliveryException { center.sendChecked(target,PackageName,requestStatisticsTag,body); } /** * **/ public void request(UNL target) throws PSYCDeliveryException { request(target,null); } /** * **/ public void reply(UNL target, String body) throws PSYCDeliveryException { center.sendChecked(target,PackageName,infoStatisticsTag,// body); } /** * **/ public void reply(UNL target) throws PSYCDeliveryException { reply(target,getDefaultStatistics()); } /** * **/ public String getDefaultStatistics() { StringBuffer stats = new StringBuffer(); appendMemoryStatistics(stats); //appendSystemProperties(stats); appendPeerStatistics(stats); appendEventLog(stats); return stats.toString(); } /** * **/ public void appendMemoryStatistics(StringBuffer stats) { if(stats == null) return; Runtime machine = Runtime.getRuntime(); stats.append("\nJava System - Memory Statistics\n"); stats.append(" Memory total: " + machine.totalMemory() + "\n"); stats.append(" Memory free : " + machine.freeMemory() + "\n"); } /** * **/ public void appendSystemProperties(StringBuffer stats) { if(stats == null) return; Properties p = System.getProperties(); String n; if(p != null) { stats.append("\nJava System - Properties:\n"); for(Enumeration e = p.propertyNames();e.hasMoreElements();) { n = (String)e.nextElement(); stats.append(" "); stats.append(n); stats.append("="); stats.append(p.getProperty(n)); stats.append("\n"); } } } /** * **/ public void appendPeerStatistics(StringBuffer stats) { if(stats == null) return; stats.append("\nMMP System - Known Peer Statistics\n"); for(Enumeration e = center.getMMPCenter().getAllRemotes();// e.hasMoreElements();) stats.append(" " + e.nextElement() + "\n"); } /** * **/ public void appendEventLog(StringBuffer stats, int from, int to) { if(stats == null) return; stats.append("\nPSYC System - Event log\n"); if(eventLog.size() <= 0) return; from = normalize(from); to = normalize(to); Event event; for(Enumeration e = eventLog.elements();e.hasMoreElements();) { event = (Event)e.nextElement(); if(event.loglevel < from || event.loglevel > to) continue; stats.append(" " + event + "\n"); } } /** * **/ public void appendEventLog(StringBuffer stats, int from) { appendEventLog(stats,from,9); } /** * **/ public void appendEventLog(StringBuffer stats) { appendEventLog(stats,0,9); } }