All pastes #65596 Raw Edit

Miscellany

public text v1 · immutable
#65596 ·published 2006-06-14 20:00 UTC
rendered paste body
package net.sf.l2j.packetcounter;

import java.nio.ByteBuffer;
import java.util.WeakHashMap;

import net.sf.l2j.gameserver.ClientThread;
import net.sf.l2j.gameserver.CustomPacketHandlerInterface;
import net.sf.l2j.gameserver.clientpackets.ClientBasePacket;
import net.sf.l2j.gameserver.util.ExtensionFunction;

public class PacketCounter implements CustomPacketHandlerInterface, ExtensionFunction {

	private WeakHashMap<ClientThread, Counter> counters;
	private static PacketCounter instance;

	private PacketCounter() {
		counters = new WeakHashMap<ClientThread, Counter>();
	}

	public static PacketCounter getInstance() {
		if (instance == null)
			instance = new PacketCounter();
		return instance;
	}

	public ClientBasePacket handlePacket(ByteBuffer decrypted, ClientThread client) {
		Counter counter = null;
		try {
			counter = counters.get(client);
		} catch (Exception ex) {}
		if (counter == null)
			counter = new Counter();
		counter.packets++;
		counters.put(client, counter);
		return null;
	}

	public void reset() {
		counters.clear();
	}

	public String list() {
		StringBuffer buf = new StringBuffer();
		for (ClientThread thread : counters.keySet()) {
			if (thread != null && thread.getActiveChar() != null) 
			{
				//only output the players that are online
				if (thread.getActiveChar().getName() != "")
				{
					//buf.append(thread.getLoginName());
					buf.append("<table><tr><td width = 100>");
					buf.append(counters.get(thread));
					buf.append("</td><td width = 200> Player: </td><td>");
					String name = "<unknown>";
					try 
					{
						name = thread.getActiveChar().getName();
					}  
					catch (Exception ex) {}
					if (name != null)
						buf.append(name);

					buf.append("</td></tr></table>");
				}
			}
		}
		return buf.toString();
	}

	class Counter {
		public int packets;
		public long created;
		public long sec1;
		public long sec2;
		
		public Counter() {
			packets = 0;
			created = System.currentTimeMillis();
		}
		public String toString() {
			long elapsed = (System.currentTimeMillis() - created) / 1000;
			return (packets + "</td><td width = 100> packets in </td><td width = 50>" + elapsed + "</td><td width = 100></td><td>secs.  Average: " +
			String.format("%3.3f", (float)packets / elapsed));
		}
	}

	public Object get(String name) {
		return list();
	}

	public void set(String arg0, Object arg1) {
		reset();
	}
}