Example #1
0
        static void Main(string[] args)
        {
            RemoteServerGroup.Initiate(args); // Inicjalizacja singletona Remote Server Group

            // Tutaj powinny zostać stworzone wszystkie Monitory i zmienne warunkowe

            BufforMonitor sharedBufforMonitor = new BufforMonitor(3, RemoteServerGroup.Instance.NodeAddressNumber);

            ConditionalVariable empty = new ConditionalVariable("Empty", sharedBufforMonitor);
            ConditionalVariable full  = new ConditionalVariable("full", sharedBufforMonitor);

            RemoteServerGroup.StartRemoteExecution(); // Barriera zapewniająca, że wszystkie servery wykonają poprawnie całą inicjalizację obiektów

            if (RemoteServerGroup.Instance.NodeAddressNumber < 2)
            {
                Consumer consumer = new Consumer(empty, full, sharedBufforMonitor);
                consumer.Consume();
            }
            else
            {
                Producer producer = new Producer(empty, full, sharedBufforMonitor);
                producer.Produce();
            }
            RemoteServerGroup.Finish();
        }
        /// <summary>
        /// Performs the specified number of production iterations.
        /// A production iteration consists of:
        /// 1) Producing an object (by calling Produce)
        /// 2) Call Report, so the change can be reported.
        /// 3) Wait for a number of milli-seconds (between 0 and maxDelay)
        /// </summary>
        private async Task ProduceWithRandomDelay(int iterations, int maxDelay)
        {
            Random rng = new Random();

            for (int i = 0; i < iterations; i++)
            {
                _producer.Produce();
                _reporter.Report();
                await Task.Delay(rng.Next(maxDelay));
            }
        }