diff --git a/Programs/examples/TestClient/Commands/System/LogPacketCommand.cs b/Programs/examples/TestClient/Commands/System/LogPacketCommand.cs new file mode 100644 index 00000000..4d460431 --- /dev/null +++ b/Programs/examples/TestClient/Commands/System/LogPacketCommand.cs @@ -0,0 +1,81 @@ +using System; +using System.IO; +using OpenMetaverse; +using OpenMetaverse.Packets; + +namespace OpenMetaverse.TestClient +{ + public class PacketLogCommand : Command + { + private TestClient m_client; + private bool m_isLogging; + private int m_packetsToLogRemaining; + private StreamWriter m_logStreamWriter; + + public PacketLogCommand(TestClient testClient) + { + Name = "logpacket"; + Description = "Logs a given number of packets to a file. For example, packetlog 10 tenpackets.xml"; + Category = CommandCategory.TestClient; + + m_client = testClient; + } + + public override string Execute(string[] args, UUID fromAgentID) + { + if (args.Length != 2) + return string.Format("Usage: {0} no-of-packets filename", Name); + + string rawNumberOfPackets = args[0]; + string path = args[1]; + int numberOfPackets; + + if (!int.TryParse(args[0], out numberOfPackets) || numberOfPackets <= 0) + return string.Format( + "{0} is not a valid number of packets for {1}", rawNumberOfPackets, m_client.Self.Name); + + if (m_isLogging) + return string.Format( + "Still waiting to finish logging {0} packets for {1}", + m_packetsToLogRemaining, m_client.Self.Name); + + try + { + lock (this) + m_logStreamWriter = new StreamWriter(path); + } + catch (Exception e) + { + return string.Format("Could not open file with path [{0}], exception {1}", path, e); + } + + m_isLogging = true; + m_packetsToLogRemaining = numberOfPackets; + m_client.Network.RegisterCallback(PacketType.Default, HandlePacket); + return string.Format("Now logging {0} packets for {1}", m_packetsToLogRemaining, m_client.Self.Name); + } + + /// + /// Logs the packet received for this client. + /// + /// + /// This handler assumes that packets are processed one at a time. + /// + /// Sender. + /// Arguments. + private void HandlePacket(object sender, PacketReceivedEventArgs args) + { +// Console.WriteLine( +// "Received packet {0} from {1} for {2}", args.Packet.Type, args.Simulator.Name, m_client.Self.Name); + m_logStreamWriter.Write(PacketDecoder.PacketToString(args.Packet)); + + if (--m_packetsToLogRemaining <= 0) + { + m_client.Network.UnregisterCallback(PacketType.Default, HandlePacket); + m_logStreamWriter.Close(); + Console.WriteLine("Finished logging packets for {0}", m_client.Self.Name); + m_isLogging = false; + } + } + } +} \ No newline at end of file diff --git a/Programs/examples/TestClient/Commands/System/PacketLogCommand.cs b/Programs/examples/TestClient/Commands/System/PacketLogCommand.cs deleted file mode 100644 index 8e2ff94f..00000000 --- a/Programs/examples/TestClient/Commands/System/PacketLogCommand.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System; -using OpenMetaverse; - -namespace OpenMetaverse.TestClient -{ - public class PacketLogCommand : Command - { - public PacketLogCommand(TestClient testClient) - { - Name = "packetlog"; - Description = "Logs a given number of packets to an xml file. Usage: packetlog 10 tenpackets.xml"; - Category = CommandCategory.TestClient; - } - - public override string Execute(string[] args, UUID fromAgentID) - { - if (args.Length != 2) - return "Usage: packetlog 10 tenpackets.xml"; - - return "This function is currently unimplemented"; - } - } -}