Ejemplo n.º 1
0
        public void run()
        {
            CSTimer timer         = new CSTimer();
            Random  rnd           = new Random();
            int     randomTimeout = rnd.Next(1, 10) * n * 500;

            timeout = timer.read() + randomTimeout;
            Debug.WriteLine(n + "Process timeout " + randomTimeout);

            Object x = input.read();

            Console.WriteLine(n + "Read value" + x);
            while (true)
            {
                x = input.read();
                Console.WriteLine(n + "Read value" + x);

                //output.write(x);
                long currentTime = timer.read();
                if (timeout <= currentTime) //if timeout should occur now
                {
                    timeout = timer.read() + rnd.Next(1, 10) * 500;
                    Debug.WriteLine(n + "Process timout ppassed " + randomTimeout);
                    output.write(n + "For Input guard" + x + "0000000");
                    timer.after(timeout);
                    output.write(n + "Finished");
                    timeout = timer.read() + rnd.Next(1, 10) * n * 500;
                }
            }
        }
Ejemplo n.º 2
0
 /**
  * Constructor taking the name and the address of the Node
  *
  * @param nodeName
  *            Symbolic name of the Node
  * @param nodeAddress
  *            Symbolic address of the Node
  */
 internal NodeID(String nodeName, NodeAddress nodeAddress)
 {
     this.time = CSTimer.CurrentTimeMillis();
     //this.mem = System.Runtime.GetRuntime().freeMemory();
     //this.mem = GC.GetTotalMemory(true);
     this.mem = Process.GetCurrentProcess().PrivateMemorySize64;
     //this.hashCode = new Object().hashCode();
     this.hashCode = new Object().GetHashCode();
     this.name     = nodeName;
     this.address  = nodeAddress;
 }
Ejemplo n.º 3
0
        /**
         * The main body of this process.
         */
        public void run()
        {
            CSTimer tim = new CSTimer();

            while (true)
            {
                Object o = In.read();
                //Debug.WriteLine("Fixed delay sleeping for " + delayTime);
                tim.sleep(delayTime);
                Out.write(o);
            }
        }
Ejemplo n.º 4
0
        public void run()
        {
            CSTimer tim  = new CSTimer();
            int     tick = 0;

            while (true)
            {
                toConsole.write("Time: " + tick + " \n");
                tim.sleep(1000);
                tick = tick + 1;
            }
        }
Ejemplo n.º 5
0
        public void run()
        {
            CSTimer timer = new CSTimer();
            int     tick  = 0;

            while (true)
            {
                Console.WriteLine("[TICK] " + tick);
                timer.sleep(1000);
                tick++;
            }
        }
Ejemplo n.º 6
0
        public void run()
        {
            CSTimer timer   = new CSTimer();
            long    timeout = timer.read(); // read the (absolute) time once only

            while (true)
            {
                N++;
                Out.write(N);
                timeout += interval;  // set the next (absolute) timeOut
                timer.after(timeout); // wait until that (absolute) timeOut
            }
        }
        public void run()
        {
            long x       = -1;
            int  warm_up = 1000;

            Console.WriteLine("warming up ... ");
            for (int i = 0; i < warm_up; i++)
            {
                x = (long)In.read();
            }

            Console.WriteLine("last number received = " + x);

            Console.WriteLine("1000 cycles completed ... timing now starting ...");

            var csv = new StringBuilder();

            int repeatTimes = 1000;

            while (repeatTimes > 0)
            {
                long t0 = CSTimer.CurrentTimeMillis();
                for (int i = 0; i < nLoops; i++)
                {
                    x = (long)In.read();
                }

                long t1 = CSTimer.CurrentTimeMillis();

                Console.WriteLine("last number received = " + x);
                long   microseconds = (t1 - t0) * 1000;
                long   iterations   = (microseconds / ((long)nLoops));
                string first        = " microseconds / iteration";
                Console.WriteLine(iterations + first);
                long   communication = (microseconds / ((long)(4 * nLoops)));
                string second        = " microseconds / communication";
                Console.WriteLine(communication + second);
                long   contextSwitch = (microseconds / ((long)(8 * nLoops)));
                string third         = " microseconds / context switch";
                Console.WriteLine(contextSwitch + third);
                var newLine = string.Format("{0},{1},{2},{3},{4},{5}", iterations, first, communication, second,
                                            contextSwitch, third);
                csv.AppendLine(newLine);


                repeatTimes--;
            }

            File.WriteAllText(@"d:\\networkedCommsTimeTEST.csv", csv.ToString());
            Console.WriteLine("Finished");
        }
        public void run()
        {
            int n = 1 * num;

            CSTimer     timer           = new CSTimer();
            Random      rnd             = new Random();
            int         randomTimeout   = rnd.Next(1, 10) * 500;
            Alternative barrierGroupAlt = new Alternative(new Guard[] { input, barrier });

            const int INPUT = 0, GROUP = 1, TIMER = 2;

            output.write(n.ToString());


            while (true)
            {
                bool pending = input.pending();
                while (!pending)
                {
                    Debug.WriteLine((char)num + " pending = " + pending);

                    n++;
                    output.write((char)num + " " + n.ToString());  // work with the barrier
                    pending = input.pending();
                }

                input.read();                               // must consume the input
                output.write("Read click " + n.ToString()); // work with the barrier

                Boolean group = true;
                while (group)
                {
                    switch (barrierGroupAlt.priSelect())
                    {
                    case INPUT:
                        Debug.WriteLine((char)num + "barier input");
                        string x = input.read().ToString(); // must consume the input
                        Console.WriteLine(x);
                        group = false;                      // end barrier working
                        break;

                    case GROUP:
                        n--;                        // work with the barrier
                        output.write(n.ToString()); // work with the barrier
                        Debug.WriteLine((char)num + "barier wrote " + n);
                        break;
                    }
                }
            }
        }
Ejemplo n.º 9
0
        public void run()
        {
            CSTimer timer = new CSTimer();

            while (true)
            {
                //Console.WriteLine("Inside GPrint timer");
                if (delay != 0)
                {
                    timer.after(timer.read() + delay);
                    Console.WriteLine(heading.Split(' ')[0] + " \t" + inChannel.read());
                }
            }
        }
Ejemplo n.º 10
0
        public void run()
        {
            CSTimer timer  = new CSTimer();
            long    second = 1000; // JCSP timer units are milliseconds
            int     busy   = id + 1;

            while (true)
            {
                timer.sleep(busy * second); // application specific work
                Console.WriteLine("Player " + id + " at the barrier ...");
                barrier.sync();
                Console.WriteLine("\t\t\t... Player " + id + " over the barrier");
                busy = (nPlayers + 1) - busy; // just to make it more interesting
            }
        }
Ejemplo n.º 11
0
        public void run()
        {
            CSTimer tim      = new CSTimer();
            int     CHICKENS = 4;

            toConsole.write("Starting ... \n");
            while (true)
            {
                toConsole.write("Cooking ... \n"); // cook 4 chickens;
                tim.after(tim.read() + 2000);      // this takes 2 seconds to cook;
                toConsole.write(CHICKENS + "chickens ready ... \n");
                supply.write(CHICKENS);
                toConsole.write("Taking chickens to Canteen ... \n");
                supply.write(0);
            }
        }
        public void run()
        {
            //initialize internal arrays of 2-dimensional array
            int[][] n = new int[c.Length][];
            for (int i = 0; i < n.Length; i++)
            {
                n[i] = new int[nWritersPerChannel];
            }

            //setup variables
            const int      initialWait = 3000;
            int            iterations = nMessages * nChannels * nWritersPerChannel;
            long           t0 = 0, t1 = 0, microseconds = 0;
            Alternative    alt               = new Alternative(c);
            CSTimer        timer             = new CSTimer();
            int            channelFairSelect = 0;
            StressedPacket stressedPacket;
            var            csv = new StringBuilder();


            //set the timer to wait for 5 seconds to make sure that all the readers are idle in writing state
            Console.WriteLine("Waiting 3 seconds...");
            timer.after(initialWait + timer.read());
            Console.WriteLine("Waiting is over. Measuring time");


            //perform read and measure the time
            t0 = CSTimer.CurrentTimeMillis();
            for (int i = 0; i < iterations; i++)
            {
                channelFairSelect = alt.fairSelect();
                stressedPacket    = (StressedPacket)c[channelFairSelect].read();
                n[channelFairSelect][stressedPacket.writer] = stressedPacket.n;
            }

            t1           = CSTimer.CurrentTimeMillis();
            microseconds = (t1 - t0) * 1000;
            if (microseconds > 0)
            {
                Console.WriteLine("Reading time for " + iterations + " iterations: " + microseconds);
                var newLine = string.Format("{0},{1},{2},{3},{4}", nChannels, nWritersPerChannel, nMessages, iterations,
                                            microseconds);
                csv.AppendLine(newLine);
                File.AppendAllText(@"d:\\NetworkedstressedAlt_Test" + nChannels + "x" + nWritersPerChannel + ".csv",
                                   csv.ToString());
            }
        }
Ejemplo n.º 13
0
        public void run()
        {
            CSTimer tim   = new CSTimer();
            CSTimer reset = new CSTimer();

            Guard[]   guards = { reset, tim, In as Guard }; // prioritised order
            const int RESET  = 0;                           // index into guards
            const int TIM    = 1;                           // index into guards
            const int IN     = 2;                           // index into guards

            Alternative alt = new Alternative(guards);

            int  x       = 0;
            long timeout = tim.read() + 3000;

            tim.setAlarm(timeout);

            long resetting = reset.read() + 5000;

            reset.setAlarm(resetting);

            while (true)
            {
                switch (alt.priSelect())
                {
                case RESET:
                    resetting = reset.read() + 5000;
                    reset.setAlarm(resetting);
                    Out.write("Numbers reset");
                    break;

                // fall through
                case TIM:
                    timeout = tim.read();
                    Out.write(x * 10);
                    timeout += 3000;
                    tim.setAlarm(timeout);
                    break;

                case IN:
                    x = (int)In.read();
                    Out.write(x);
                    break;
                }
            }
        }
Ejemplo n.º 14
0
        public void run()
        {
            CSTimer timer = new CSTimer();

            int n_chickens;

            Console.WriteLine("            Chef    : starting ... ");
            while (true)
            {
                // cook 4 chickens
                Console.WriteLine("            Chef    : cooking ... ");
                timer.after(timer.read() + 2000); // this takes 3 seconds to cook
                n_chickens = 4;
                Console.WriteLine("            Chef    : " + n_chickens + " chickens, ready-to-go ... ");
                supply.write(n_chickens); // supply the chickens
                supply.write(0);          // wait till they're set down
            }
        }
Ejemplo n.º 15
0
        /**
         * The main body of this process.
         */
        public void run()
        {
            CSTimer tim = new CSTimer();

            Guard[]   guards = { reset, tim, In };           // prioritised order
            const int RESET  = 0;                            // index into guards
            const int TIM    = 1;                            // index into guards
            const int IN     = 2;                            // index into guards

            Alternative alt = new Alternative(guards);

            Object x = 1;                                      // holding object

            long interval = initialInterval;

            long timeout = tim.read() + interval;

            tim.setAlarm(timeout);

            while (true)
            {
                switch (alt.priSelect())
                {
                case RESET:
                    //interval = ((long)reset.read()).longValue();
                    interval = (long)reset.read();
                    timeout  = tim.read();                             // fall through
                    Out.write(x);
                    timeout += interval;
                    tim.setAlarm(timeout);
                    break;

                case TIM:
                    Out.write(x);
                    timeout += interval;
                    tim.setAlarm(timeout);
                    break;

                case IN:
                    x = In.read();
                    break;
                }
            }
        }
        public void run()
        {
            Guard[]   canteenGuards = { supply as Guard, service as Guard };
            Boolean[] precondition  = { true, false };
            var       canteenAlt    = new Alternative(canteenGuards);

            const int SUPPLY  = 0;
            const int SERVICE = 1;

            CSTimer tim      = new CSTimer();
            int     chickens = 0;

            toConsole.write("Canteen : starting ... \n");

            while (true)
            {
                precondition[SERVICE] = (chickens > 0);
                if (chickens == 0)
                {
                    toConsole.write("Waiting for chickens ...\n");
                }

                switch (canteenAlt.fairSelect(precondition))
                {
                case SUPPLY:
                    int value = (int)(long)supply.read();
                    toConsole.write("Chickens on the way ...\n");
                    tim.after(tim.read() + 3000);
                    chickens = chickens + value;
                    toConsole.write(chickens + " chickens now available ...\n");
                    supply.read();
                    break;

                case SERVICE:
                    int id = (int)(long)service.read();
                    chickens = chickens - 1;
                    toConsole.write("chicken ready for Philosoper " + id + " ... " + chickens +
                                    " chickens left \n");
                    deliver.write(1);
                    break;
                }
            }
        }
        public void run()
        {
            long    halfSecond = 500;
            long    second     = 1000;
            CSTimer tim        = new CSTimer();
            long    timeout    = tim.read();

            while (true)
            {
                timeout += 5000;
                tim.after(timeout);
                Console.WriteLine("                    <== now every second");
                reset.Out().write(second);
                timeout += 5000;
                tim.after(timeout);
                Console.WriteLine("                    <== now every half second");
                reset.Out().write(halfSecond);
            }
        }
Ejemplo n.º 18
0
        public void run()
        {
            int currentFactor = 0;
            var timer         = new CSTimer();
            var timeout       = timer.read();

            while (true)
            {
                timeout = timeout + testInterval;
                timer.after(timeout);
                suspend.write(0);
                currentFactor = (int)factor.read();

                currentFactor = currentFactor + addition;
                timer.sleep(computeInterval);

                injector.write(currentFactor);
            }
        }
        public void run()
        {
            Random  rng   = new Random();
            CSTimer timer = new CSTimer();

            while (true)
            {
                Console.WriteLine("\t\tReindeer" + number + ": on holiday ... wish you were here, :)");
                timer.sleep(holidayTime + rng.Next(holidayTime));
                Console.WriteLine("\t\tReindeer " + number + ": back from holiday ... ready for work, :(");
                stable.sync();
                harness.write(number);
                harnessed.read();
                Console.WriteLine("\t\tReindeer " + number + ": delivering toys ... la-di-da-di-da-di-da, :)");
                returned.read();
                Console.WriteLine("\t\tReindeer " + number + ": all toys delivered ... want a holiday, :(");
                unharness.read();
            }
        }
Ejemplo n.º 20
0
        public void run()
        {
            CSTimer tim = new CSTimer();

            Console.WriteLine("      Philosopher " + id + "  : starting ... ");
            while (true)
            {
                // everyone, bar Philosopher 0, has a little think
                if (id > 0)
                {
                    tim.after(tim.read() + 3000); // thinking
                }

                // want chicken
                Console.WriteLine("      Philosopher " + id + "  : gotta eat ... ");
                service.write(0);
                deliver.read();
                Console.WriteLine("      Philosopher " + id + "  : mmm ... that's good ... ");
            }
        }
Ejemplo n.º 21
0
        /**
         * @param clazz
         * @param message
         */
        public /*synchronized*/ void log(Type clazz, String message) //changed Java Class to Object
        {
            //Console.WriteLine(message);
            if (this.logger == null)
            {
                return;
            }
            DateTime date = new DateTime(CSTimer.CurrentTimeMillis());

            try
            {
                this.logger.WriteLine("(" + date.ToString() + ")-" + clazz.GetType().Name + ":");
                this.logger.WriteLine("\t\"" + message + "\"");
                this.logger.Flush();
            }
            catch (Exception e)
            {
                // Do nothing
            }
        }
Ejemplo n.º 22
0
        public void run()
        {
            Alternative alt = new Alternative(new Guard[] { supply, service });

            Boolean[] precondition = { true, false };
            const int SUPPLY       = 0;
            const int SERVICE      = 1;

            CSTimer timer = new CSTimer();

            int nChickens = 0;

            Console.WriteLine("            Canteen : starting ... ");
            while (true)
            {
                precondition[SERVICE] = (nChickens > 0);
                switch (alt.fairSelect(precondition))
                {
                case SUPPLY:
                    int value = (int)supply.read();     // new batch of chickens from the Chef
                    Console.WriteLine("            Canteen : ouch ... make room ... this dish is very hot ... ");
                    timer.after(timer.read() + 3000);   // this takes 3 seconds to put down
                    nChickens += value;
                    Console.WriteLine("            Canteen : more chickens ... " +
                                      nChickens + " now available ... ");
                    supply.read();     // let the Chef get back to cooking
                    break;

                case SERVICE:
                    service.read();     // Philosopher wants a chicken
                    Console.WriteLine("      Canteen : one chicken coming down ... " +
                                      (nChickens - 1) + " left ... ");
                    deliver.write(1);     // serve one chicken
                    nChickens--;
                    break;
                }
            }
        }
Ejemplo n.º 23
0
        public void run()
        {
            Random  rng   = new Random();
            CSTimer timer = new CSTimer();

            while (true)
            {
                Console.WriteLine("\tElf " + number + ": working, :)");
                timer.sleep(workingTime + rng.Next(workingTime));
                needToConsult.write(number);
                int group = (int)joinGroup.read();
                groups[group].fallInto();
                // process will wait until flushed by vestibule
                // it is guaranteed by vestibule that three Elf proceses will be started
                consult.write(number);
                Console.WriteLine("\tElf " + number + ": need to consult Santa, :(");
                consulting.read();
                Console.WriteLine("\tElf " + number + ": about these toys ... ???");
                negotiating.write(1);
                consulted.read();
                Console.WriteLine("\tElf " + number + ": OK ... we will build it, bye, :(");
            }
        }
Ejemplo n.º 24
0
    public override void OnInspectorGUI()
    {
        mCSTimer = target as CSTimer;
        if (mCSTimer == null)
        {
            return;
        }

        var cdTasks = mCSTimer.CdTasks;

        GUILayout.Label("=========================");
        _coolDownToggle = EditorGUILayout.Foldout(_coolDownToggle, string.Format("CoolDown:{0}", cdTasks.Count));
        GUILayout.Label("=========================");
        if (_coolDownToggle)
        {
            for (int i = 0; i < cdTasks.Count; ++i)
            {
                DrawCoolDownTask(cdTasks [i]);
            }
        }

        var timerTasks = mCSTimer.TimerTasks;

        GUILayout.Label("=========================");
        _timerToggle = EditorGUILayout.Foldout(_timerToggle, string.Format("Timer:{0}", timerTasks.Count));
        GUILayout.Label("=========================");
        if (_timerToggle)
        {
            for (int i = 0; i < timerTasks.Count; ++i)
            {
                DrawTimerTask(timerTasks [i]);
            }
        }

        this.Repaint();
    }
Ejemplo n.º 25
0
        public void run()
        {
            int innerCycleTime = n * start;

            CSTimer tim     = new CSTimer();
            long    timeout = tim.read();

            while (true)
            {
                int interval = start;
                while (interval >= stop)
                {
                    int innerCycles = innerCycleTime / interval;
                    for (int i = 0; i < innerCycles; i++)
                    {
                        Out.write(interval);
                        timeout += (long)interval;
                        tim.after(timeout);
                    }

                    interval /= 2;
                }
            }
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Run Writers started.");

            int nChannels = 1;

            Console.WriteLine("Please enter number of writers per channel");
            int nWritersPerChannel = Int32.Parse(Console.ReadLine());
            int nMessages          = 2;

            Console.WriteLine("Please enter writer ID");
            int writerID = Int32.Parse(Console.ReadLine());


            Console.WriteLine("Please enter IP address for this node.");
            var writersChannelNodeIP = Console.ReadLine();

            Console.WriteLine("Please enter IP address for Reader.");
            var readerNodeIP = Console.ReadLine();


            var readerNodeAddr = new TCPIPNodeAddress(readerNodeIP, 3300);
            var fromReader     = NetChannel.net2one();

            var writersChannelNodesAddresses = new TCPIPNodeAddress(writersChannelNodeIP, 3300);

            Node.getInstance().init(writersChannelNodesAddresses);

            var readersChannelNumberForThisWriter = Int32.Parse("5" + (writerID - 1));
            var writers2network = NetChannel.any2net(readerNodeAddr, readersChannelNumberForThisWriter);

            Console.WriteLine("writers2network location = " + writers2network.getLocation().ToString());

            Console.WriteLine("Informing reader that Writer" + writerID + " is ready and sending its IP");
            //writers2network.write(GetLocalIPAddress.ConvertLocalIPAddressToString()); //use while all nodes are on different machines
            writers2network.write(writersChannelNodeIP); //use for local loopback addresses

            CSTimer timer = new CSTimer();

            timer.sleep(2000);
            Console.WriteLine("Waiting for the signal from Reader...");
            fromReader.read();

            Console.WriteLine("Read signal from Reader");

            //====================== Running the test
            StressedWriterPerformance[] writers = new StressedWriterPerformance[nChannels * nWritersPerChannel];

            for (int channel = 0; channel < nChannels; channel++)
            {
                for (int i = 0; i < nWritersPerChannel; i++)
                {
                    writers[(channel * nWritersPerChannel) + i] =
                        new StressedWriterPerformance(writers2network, channel, i, writerID);
                    writerID++;
                }
            }

            for (int i = 0; i < 100; i++)
            {
                new CSPParallel(
                    new IamCSProcess[]
                {
                    new CSPParallel(writers),
                }
                    ).run();
            }

            Console.WriteLine("Finished all");
        }
Ejemplo n.º 27
0
        public void run()
        {
            const int   REINDEER = 0;
            const int   ELVES    = 1;
            Random      rng      = new Random();
            CSTimer     timer    = new CSTimer();
            Alternative santaAlt = new Alternative(new Guard[] { stable, consult as Guard });

            openForBusiness.write(1);
            int index = -1;

            while (true)
            {
                index = santaAlt.priSelect();
                int[] id = new int[9]; //size of the array must be provided. For loops no greater than 9 - Karol Pasierb

                switch (index)
                {
                case REINDEER:
                    Console.WriteLine("Santa: ho-ho-ho ... the reindeer are back");
                    for (int i = 0; i < 9; i++)
                    {
                        ;
                        id[i] = (int)harness.read();
                        Console.WriteLine("Santa: harnessing reindeer " + id[i] + " ...");
                    }

                    Console.WriteLine("Santa: mush mush ...");
                    for (int i = 0; i < 9; i++)
                    {
                        harnessed.write(1);
                    }

                    // simulate time to undertake present deliveries
                    timer.sleep(deliveryTime + rng.Next(deliveryTime));
                    Console.WriteLine("Santa: woah ... we are back home");
                    for (int i = 0; i < 9; i++)
                    {
                        returned.write(1);
                    }

                    for (int i = 0; i < 9; i++)
                    {
                        Console.WriteLine("Santa: unharnessing reindeer " + id[i]);
                        unharnessList[id[i]].write(1);
                    }

                    break;

                case ELVES:
                    id[0] = (int)consult.read();
                    for (int i = 1; i < 3; i++)
                    {
                        id[i] = (int)consult.read();
                    }

                    Console.WriteLine("Santa: ho-ho-ho ... some elves are here!");
                    for (int i = 0; i < 3; i++)
                    {
                        consulting[id[i]].write(1);
                        Console.WriteLine("Santa: hello elf " + id[i] + " ...");
                    }

                    for (int i = 0; i < 3; i++)
                    {
                        negotiating.read();
                    }

                    Console.WriteLine("Santa: consulting with elves ...");
                    timer.sleep(consultationTime + rng.Next(consultationTime));
                    Console.WriteLine("Santa: OK, all done - thanks!");
                    for (int i = 0; i < 3; i++)
                    {
                        consulted[id[i]].write(1);
                        Console.WriteLine("Santa: goodbye elf " + id[i] + " ...");
                    }

                    consultationOver.write(1);
                    break;
                }
            }
        }
        public void run()
        {
            const int seconds     = 1000;
            const int initialWait = 5;

            //commented out as it doesn't add any value - Karol Pasierb
            //Console.WriteLine("\nWait (" + initialWait +
            //                    " seconds) for all the writers to get going ...");
            //CSTimer tim = new CSTimer();
            //long timeout = tim.read() + (initialWait * seconds);
            //tim.after(timeout);
            //Console.WriteLine("OK - that should be long enough ...\n");
            int[][] n = new int[c.Length][];
            for (int i = 0; i < n.Length; i++)
            {
                n[i] = new int[nWritersPerChannel];
            }

            //commented out for performance testing - Karol Pasierb
            //for (int channel = 0; channel < c.Length; channel++)
            //{
            //    for (int i = 0; i < nWritersPerChannel; i++)
            //    {
            //        StressedPacket stressedPacket = (StressedPacket)c[channel].read();
            //        n[channel][stressedPacket.writer] = stressedPacket.n;
            //        for (int chan = 0; chan < channel; chan++) Console.Write("  ");
            //        Console.WriteLine("channel " + channel +
            //                            " writer " + stressedPacket.writer +
            //                            " read " + stressedPacket.n);
            //    }
            //}
            Alternative alt = new Alternative(c);
            int         counter = 0, tock = 0;
            long        t0 = 0, t1 = 0, microseconds = 0;

            while (true)
            {
                if (counter == 0)
                {
                    /*t1 = CSPTimeMillis.CurrentTimeMillis();
                     * microseconds = (t1 - t0);
                     * Console.WriteLine("Reading time for tock " + tock + ": " + microseconds);*/

                    Console.Write("Tock " + tock + " : ");
                    int total = 0;
                    for (int channel = 0; channel < n.Length; channel++)
                    {
                        Console.Write(n[channel][tock % nWritersPerChannel] + " ");
                        for (int i = 0; i < nWritersPerChannel; i++)
                        {
                            total += n[channel][i];
                        }
                    }

                    Console.WriteLine(": " + total);
                    tock++;
                    counter = 10000;
//                    t0 = CSPTimeMillis.CurrentTimeMillis();
                    //Debug.WriteLine("Read time at counter 10000");
                }

                counter--;

                t0 = CSTimer.CurrentTimeMillis();
                int            channelFairSelect = alt.fairSelect();
                StressedPacket stressedPacket    = (StressedPacket)c[channelFairSelect].read();
                n[channelFairSelect][stressedPacket.writer] = stressedPacket.n;
                t1           = CSTimer.CurrentTimeMillis();
                microseconds = (t1 - t0) * 1000;
                if (microseconds > 0)
                {
                    Debug.WriteLine("Reading time for tock " + tock + ": " + microseconds);
                }


                // for (int chan = 0; chan < channel; chan++) System.out.print ("  ");
                // Console.WriteLine ("channel " + channel +
                //                     " writer " + stressedPacket.writer +
                //                     " read " + stressedPacket.n);
            }
        }
Ejemplo n.º 29
0
        public void run()
        {
            int SECOND          = 1000;
            int DOUBLE_INTERVAL = 5 * SECOND;

            const int NORMAL_SUSPEND   = 0;
            const int NORMAL_TIMER     = 1;
            const int NORMAL_IN        = 2;
            const int SUSPENDED_INJECT = 0;
            const int SUSPENDED_IN     = 1;

            var timer = new CSTimer();

            var normalAlt    = new Alternative(new Guard[] { suspend as Guard, timer as Guard, inChannel as Guard });
            var suspendedAlt = new Alternative(new Guard[] { injector as Guard, inChannel as Guard });
            var timeout      = timer.read() + DOUBLE_INTERVAL;

            timer.setAlarm(timeout);

            int        inValue;
            ScaledData result;

            while (true)
            {
                switch (normalAlt.priSelect())
                {
                case NORMAL_SUSPEND:
                    suspend.read();        // its a signal, no data content;
                    factor.write(scaling); //reply with current value of scaling;
                    bool suspended = true;
                    Console.WriteLine("\n\tSuspended");
                    while (suspended)
                    {
                        switch (suspendedAlt.priSelect())
                        {
                        case SUSPENDED_INJECT:
                            scaling = (int)injector.read();         //this is the resume signal as well;
                            Console.WriteLine("\n\tInjected scaling is " + scaling);
                            suspended = false;
                            timeout   = timer.read() + DOUBLE_INTERVAL;
                            timer.setAlarm(timeout);
                            break;

                        case SUSPENDED_IN:
                            inValue         = (int)inChannel.read();
                            result          = new ScaledData();
                            result.Original = inValue;
                            result.Scaled   = inValue;
                            outChannel.write(result.ToString());
                            break;
                        } // end-switch
                    }     //end-while

                    break;

                case NORMAL_TIMER:
                    timeout = timer.read() + DOUBLE_INTERVAL;
                    timer.setAlarm(timeout);
                    scaling = scaling * multiplier;
                    Console.WriteLine("\n\tNormal Timer: new scaling is " + scaling);
                    break;

                case NORMAL_IN:
                    inValue         = (int)inChannel.read();
                    result          = new ScaledData();
                    result.Original = inValue;
                    result.Scaled   = inValue * scaling;
                    outChannel.write(result.ToString());
                    break;
                } //end-switch
            }
        }