Esempio n. 1
0
        public void run()
        {
            Guard[] guards = new Guard[In.Length + 1];
            Array.Copy(In, 0, guards, 0, In.Length);

            CSTimer tim        = new CSTimer();
            int     timerIndex = In.Length;

            guards[timerIndex] = tim;

            Alternative alt = new Alternative(guards);

            Boolean running = true;

            tim.setAlarm(tim.read() + timeout);
            while (running)
            {
                int index = alt.fairSelect();
                if (index == timerIndex)
                {
                    running = false;
                }
                else
                {
                    Out.write(In[index].read());
                }
            }

            Console.WriteLine("Goodbye from FairPlexTime ...");
            //System.exit(0);
        }
Esempio n. 2
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;
                }
            }
        }
        /**
         * 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;
                }
            }
        }
Esempio n. 4
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
            }
        }