From: Robert M. <st...@us...> - 2009-07-16 21:30:44
|
Update of /cvsroot/grailrtls/grailrtls/org/grailrtls/server/commands In directory ddv4jf1.ch3.sourceforge.com:/tmp/cvs-serv13958/org/grailrtls/server/commands Added Files: PacketCmd.java Log Message: New "packet" command in the server allows packet-level statistics generation. --- NEW FILE: PacketCmd.java --- package org.grailrtls.server.commands; import org.grailrtls.server.*; import java.util.*; /** * * @author Robert S. Moore II */ public final class PacketCmd { /** The name of the command. */ public static final String COMMAND_NAME = "packet"; /** A help string for making nice user happy good. */ public static final String COMMAND_HELP = "packet REGION timing {histogram | minimum [utime]}"; /** * Runs the command. * * @param io * The I/O bundle for the client connection. * @param args * The full command-line with which this was called, including * the command invocation itself * @return A status code from {@link ReturnCode}, or <code>-1</code> if * no return code should be given. */ public static final ReturnCode runCommand(IOBundle io, String[] args) { if (args.length < 4) { return ReturnCode.makeReturnCode(ReturnCode.RET_INVALID_NUM_ARGS, "Missing arguments."); } Region region = io.getServer().getRegionByName(args[1]); if (region == null) { return ReturnCode.makeReturnCode(ReturnCode.RET_BAD_REQUEST, "Cannot find region named \"" + region.name + "\"."); } // Generate histogram for all packets in the server if (args[2].equalsIgnoreCase("timing")) { if(args[3].equalsIgnoreCase("minimum")) { HashMap<Long,Long> minimums = new HashMap<Long,Long>(); synchronized(region.transmitters) { for(NetworkTransmitter transmitter : region.transmitters) { for(Packet packet : transmitter.packets) { long[] timing = packet.getMinTimeDiffs(); if(timing == null) continue; Long existing = minimums.get(timing[0]); if(existing == null || existing < timing[1]) { minimums.put(timing[0],timing[1]); continue; } } } } List<Long> sorted_times = new ArrayList<Long>(minimums.keySet()); Collections.sort(sorted_times); for(Long timestamp : sorted_times) { if(args.length > 4 && args[4].equalsIgnoreCase("utime")) io.print(timestamp); else io.print(io.dateFormatter.format(new Date(timestamp))); io.println(" " + minimums.get(timestamp)); } return ReturnCode.makeReturnCode(ReturnCode.RET_OK); } if (args[3].equalsIgnoreCase("histogram")) { synchronized (region.transmitters) { HashMap<Integer, Integer> histogram = new HashMap<Integer, Integer>(); int min_value = Integer.MAX_VALUE; int max_value = Integer.MIN_VALUE; for (NetworkTransmitter transmitter : region.transmitters) { synchronized (transmitter.packets) { for (Packet packet : transmitter.packets) { Map<Integer, Integer> packet_histogram = packet.getHistogram(); if (packet_histogram == null) continue; for (Integer val : packet_histogram.keySet()) { if (val < min_value) min_value = val; if (val > max_value) max_value = val; if (histogram.get(val) == null) histogram.put(val, packet_histogram.get(val)); else histogram.put(val, histogram.get(val) + packet_histogram.get(val)); } } } } for (int i = min_value; i <= max_value; i++) { if (histogram.get(i) == null) continue; io.println(i + " " + histogram.get(i)); } } return ReturnCode.makeReturnCode(ReturnCode.RET_OK); } } return ReturnCode.makeReturnCode(ReturnCode.RET_BAD_REQUEST, "Invalid request."); } // Prevent instantiation because this is a static class only. private PacketCmd() { } } |