コード例 #1
0
        public static void Run()
        {
            // Sets up and starts the simulation.
            Console.WriteLine("Process communication");
            var env = Sim.Environment();

            // For one-to-one or many-to-one type pipes, use Resources.Store.
            var pipe = Sim.Store <Message>(env);

            env.Process(MessageGenerator("Generator A", env, pipe));
            env.Process(MessageConsumer("Consumer A", env, pipe));

            Console.WriteLine();
            Console.WriteLine("One-to-one pipe communication");
            Console.WriteLine();
            env.Run(until: SimTime);

            // For one-to many, instead, use BroadcastPipe.
            // (Note: it could also be used for one-to-one, many-to-one or many-to-many)
            env = Sim.Environment();
            var bcPipe = new BroadcastPipe <Message>(env);

            env.Process(MessageGenerator("Generator A", env, bcPipe));
            env.Process(MessageConsumer("Consumer A", env, bcPipe.OutputConn()));
            env.Process(MessageConsumer("Consumer B", env, bcPipe.OutputConn()));

            Console.WriteLine();
            Console.WriteLine("One-to-many pipe communication");
            Console.WriteLine();
            env.Run(until: SimTime);
        }
コード例 #2
0
        /// <summary>
        ///   A process which randomly generates messages.
        /// </summary>
        static IEvents MessageGenerator(string name, SimEnvironment env, BroadcastPipe <Message> outPipe)
        {
            while (true)
            {
                // Waits for next transmission.
                yield return(env.Timeout(Rand.Next(6, 10)));

                // Messages are time stamped to later check
                // if the consumer was late getting them.
                var content = string.Format("{0} says hello at {1:.0}", name, env.Now);
                yield return(outPipe.Put(new Message(env.Now, content)));
            }
        }