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;
                    }
                }
            }
        }
Example #2
0
        public void run()
        {
            /*final*/
            Alternative clickGroup =
                new Alternative(new Guard[] { click, group });

            const int CLICK = 0, GROUP = 1;

            int n = 0;

            configure.write(n.ToString());

            while (true)
            {
                configure.write(Color.Green); // pretty

                while (!click.pending())
                {
                    n++;                           // work on our own
                    configure.write(n.ToString()); // work on our own
                }

                click.read();               // must consume the click

                configure.write(Color.Red); // pretty

                Boolean group = true;
                while (group)
                {
                    switch (clickGroup.priSelect())
                    {
                    case CLICK:
                        click.read();     // must consume the click
                        group = false;    // end group working
                        break;

                    case GROUP:
                        n--;                           // work with the group
                        configure.write(n.ToString()); // work with the group
                        break;
                    }
                }
            }
        }
        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;
                }
            }
        }
        /**
         * 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;
                }
            }
        }
Example #5
0
        public void run()
        {
            Guard[]     altChans = { in0, in1 };
            Alternative alt      = new Alternative(altChans);

            while (true)
            {
                switch (alt.select())
                {
                case 0:
                    Console.WriteLine("in0 read " + in0.read());
                    break;

                case 1:
                    Console.WriteLine("in1 read " + in1.read());
                    break;
                }
                //Thread.Sleep(100);
            }
        }
Example #6
0
        /**
         * Receives some data back from the server after
         * <code>request(Object)</code> has been called.
         *
         * @return the <code>Object</code> sent from the server.
         */
        public Object reply()// throws
        {
            try
            {
                //moved it out from the if statement below and had to initialize it with empty object - KP
                ConnectionServerMessage serverReply = new ConnectionServerMessage();

                if (currentClientState != CLIENT_STATE_MADE_REQ)
                {
                    try
                    {
                        serverReply = (ConnectionServerMessage)fromServer.read();
                    }
                    catch (InvalidOperationException)
                    {
                        throw new InvalidOperationException("Cannot call reply() on a ConnectionClient that is not waiting for a reply.");
                    }
                }

                //check whether the server closed the connection
                currentClientState = serverReply.open ? CLIENT_STATE_OPEN : CLIENT_STATE_CLOSED;
                if (serverReply.open)
                {
                    currentClientState = CLIENT_STATE_OPEN;
                }
                else
                {
                    currentClientState = CLIENT_STATE_CLOSED;
                    release();
                }
                return(serverReply.data);
            }
            catch (InvalidOperationException)
            {
                throw;
            }
        }
Example #7
0
        public void run()
        {
            Skip skip = new Skip();

            Guard[]     guards = { In0, In1, In2, In3, In4, skip };
            Alternative alt    = new Alternative(guards);

            while (true)
            {
                switch (alt.priSelect())
                {
                case 0:
                    // ...  process data pending on channel In0
                    Out.write(In0.read());
                    Debug.WriteLine(" 0 ready");
                    break;

                case 1:
                    // ...  process data pending on channel In1
                    Out.write(In1.read());
                    Debug.WriteLine(" 1 ready");

                    break;

                case 2:
                    // ...  process data pending on channel In2
                    Out.write(In2.read());
                    Debug.WriteLine(" 2 ready");

                    break;

                case 3:
                    // ...  process data pending on channel In2
                    Out.write(In3.read());
                    Debug.WriteLine(" 3 ready");

                    break;

                case 4:
                    // ...  process data pending on channel In2
                    Out.write(In4.read());
                    Debug.WriteLine(" 4 ready");

                    break;

                case 5:
                    // ...  nothing available for the above ...
                    // ...  so get on with something else for a while ...
                    // ...  then loop around and poll again ...
                    try
                    {
                        Debug.WriteLine(" Skip ready");
                        Thread.Sleep(400);
                    }
                    catch (ThreadInterruptedException e)
                    {
                    }

                    Out.write("...  so getting on with something else for a while ...");
                    break;
                }
            }
        }
Example #8
0
 /**
  * Read an Object from the channel.
  *
  * @return the object read from the channel
  */
 public override Object read()
 {
     return(channel.read());
 }