

using System;
using FBPLib;




namespace Components
{

    /** Component to generate a stream of 'n' packets, where 'n' is
    * specified in an InitializationConnection.
    */
    [InPort("COUNT")]
    [OutPort("OUT")]
    [ComponentDescription("Generate packets based on count, inserting brackets between every 5 packets")]

    public class GenSS : Component
    {

        internal static string _copyright =
            "Copyright 2007, 2008, 2009, J. Paul Morrison.  At your option, you may copy, " +
            "distribute, or make derivative works under the terms of the Clarified Artistic License, " +
            "based on the Everything Development Company's Artistic License.  A document describing " +
            "this License may be found at http://www.jpaulmorrison.com/fbp/artistic2.htm. " +
            "THERE IS NO WARRANTY; USE THIS PRODUCT AT YOUR OWN RISK.";

        OutputPort _outport;
        IInputPort _count;


        public override void Execute() /* throws Throwable */ {
            Packet ctp = _count.Receive();

            string param = ctp.Content.ToString();
            Int32 ct = Int32.Parse(param);
            Drop(ctp);
            _count.Close();

            Packet p = Create(Packet.Types.Open, "");
            _outport.Send(p);

            for (int i = 0; i < ct; i++)
            {
                if (i % 20 == 4)
                {
                    p = Create(Packet.Types.Close, "");
                    _outport.Send(p);
                    p = Create(Packet.Types.Open, "");
                    _outport.Send(p);
                }
                int j = 100 - i;
                string s = String.Format("{0:d4}", j) + " abc";

                p = Create(s);
                _outport.Send(p);
            }
            p = Create(Packet.Types.Close, "");
            _outport.Send(p);
            // output.close();
            // terminate();
        }
        /*
        public override System.Object[] Introspect()
        {

            return new Object[] {
		"generates a set of Packets under control of a counter" ,
		"OUT", "output", Type.GetType("System.String"),
			"lines read",
		"COUNT", "parameter", Type.GetType("System.String"),
			"Count of number of entities to be generated"};
        }
        */
        public override void OpenPorts()
        {

            _outport = OpenOutput("OUT");
           // _outport.SetType(Type.GetType("System.String"));

            _count = OpenInput("COUNT");
          //  _count.SetType(Type.GetType("System.String"));

        }
    }
}
