//
// JobPacketCommand.cs
//
// Author:
//       Carsten Sonne Larsen <cs@innolan.dk>
//
// Copyright (c) 2013 Carsten Sonne Larsen
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using Ntp.Process;
using System.Collections.Generic;

namespace Ntp.Monitor.Server.PacketCommand
{
    /*
    public sealed class JobPacketCommand : PacketCommand
    {
        public override string CommandText {
            get { return "bjob"; }
        }

        public override string CommandDescription {
            get { return "Binary command."; }
        }

        protected override byte[] ExecuteBinary ()
        {
            int packetSize = Marshal.SizeOf (typeof(JobPacket));
            int packetCount = ApplicationState.Scheduler.Count ();

            MemoryStream stream = new MemoryStream (packetCount * packetSize + 2);
            stream.WriteByte (Convert.ToByte (packetCount));

            foreach (JobDescription job in ApplicationState.Scheduler) {
                JobPacket packet = new JobPacket (job);

                byte[] buffer = new byte[packetSize];
                GCHandle handle = GCHandle.Alloc (buffer, GCHandleType.Pinned);
                Marshal.StructureToPtr (packet, handle.AddrOfPinnedObject (), false);
                handle.Free ();

                stream.Write (buffer, 0, packetSize);
            }

            return stream.ToArray ();
        }

        public static JobPacket[] Deserialize (byte[] data)
        {
            int size = Marshal.SizeOf (typeof(JobPacket));
            byte[] buffer = new byte[size];

            GCHandle handle = GCHandle.Alloc (buffer, GCHandleType.Pinned);
            IntPtr ptr = handle.AddrOfPinnedObject ();

            MemoryStream stream = new MemoryStream (data);
            int c = Convert.ToInt32 (stream.ReadByte ());
            JobPacket[] packets = new JobPacket[c];

            for (int i = 0; i <= c; i++) {
                stream.Read (buffer, 0, size);
                object res = Marshal.PtrToStructure (ptr, typeof(JobPacket));
                packets [i] = (JobPacket)res;
            }

            return packets;
        }

        public static JobPacket[] DeserializeOld (byte[] data)
        {
            int size = Marshal.SizeOf (typeof(JobPacket));
            byte[] buffer = new byte[size];

            GCHandle handle = GCHandle.Alloc (buffer, GCHandleType.Pinned);
            IntPtr ptr = handle.AddrOfPinnedObject ();

            MemoryStream stream = new MemoryStream (data);
            List<JobPacket> packets = new List<JobPacket> ();

            while (stream.Position != stream.Length) {
                stream.Read (buffer, 0, size);
                object res = Marshal.PtrToStructure (ptr, typeof(JobPacket));
                JobPacket newPacket = (JobPacket)res;
                packets.Add (newPacket);
            }

            return packets.ToArray ();
        }
    }
    */
}