Ejemplo n.º 1
0
        static void ReceiveDemo()
        {
            // demonstrate receiving - Receive processes whatever messages happen to be on the queue at the time of the call

            Console.WriteLine("\nPress a key to read message/s");
            Console.ReadLine();

            IBus bus = new BusBuilder()
                       .WithLogging(new FileLogger())
                       .InstallMsmqIfNeeded()
                       .DefineErrorQueue("MiniBus.errors")
                       .DefineReadQueue("MiniBus.messages1")
                       .CreateLocalQueuesAutomatically()
                       .EnlistInAmbientTransactions()
                       .JsonSerialization()
                       .NumberOfRetries(3)
                       .CreateBus();

            // pass true to have a message fail and moved to the error queue
            bus.RegisterHandler(new PersonHandler(false));

            bus.Receive <Person>();

            Console.WriteLine("\nPress a key to exit");
            Console.ReadLine();
            bus.Dispose();
        }
Ejemplo n.º 2
0
        public Passenger(int _pid, bool _isMissionary) // TODO: call this from main with data received from args[]
        {
            this.PID    = _pid;
            this._TYPE_ = _isMissionary ? PassengerType.Missionary : PassengerType.Cannibal;

            // create a bus for sending messages
            IBus bus = new BusBuilder()
                       .WithLogging(new ConsoleLogger())
                       .DefineErrorQueue("Passenger.errors")
                       .DefineWriteQueue("Boat")
                       .CreateLocalQueuesAutomatically()
                       .JsonSerialization()

                       .CreateBus();

            // create your message type
            var msg = new Message {
                PID = this.PID, Value = (int)_TYPE_
            };

            // send it
            bus.Send(msg);

            // create a bus for receiving messages
            bus = new BusBuilder()
                  .WithLogging(new ConsoleLogger())
                  .DefineErrorQueue("Passenger.errors")
                  .DefineReadQueue("Passenger." + PID)
                  //.DefineWriteQueue("Boat")
                  .CreateLocalQueuesAutomatically()
                  .JsonSerialization()
                  .CreateBus();

            // register one or more message handlers
            bus.RegisterHandler <bool>(new ResponseHandler());
            // process messages on the read queue synchronously
            bus.Receive <bool>();

            // process messages on the read queue asynchronously
            //bus.ReceiveAsync<PlaceOrder>();

            // DIE in handler
        }
Ejemplo n.º 3
0
        static void ReceiveDemoAsync()
        {
            // demonstrate receiving asynchrnously - ReceiveAsync processes messages as and when they arrive.
            // Open another instance to send messages while the other receives.

            IBus bus = new BusBuilder()
                       .WithLogging(new ConsoleLogger())
                       .InstallMsmqIfNeeded()
                       .DefineErrorQueue("MiniBus.errors")
                       .DefineReadQueue("MiniBus.messages1")
                       .CreateLocalQueuesAutomatically()
                       .EnlistInAmbientTransactions()
                       .JsonSerialization()
                       .NumberOfRetries(3)
                       .CreateBus();

            bus.RegisterHandler(new PersonHandler(false));
            bus.ReceiveAsync <Person>();

            Console.WriteLine("\nPress a key to exit");
            Console.ReadLine();
            bus.Dispose();
        }
Ejemplo n.º 4
0
        //static void Main(string[] args)
        public Boat(int M, int N)
        {
            string[] ms          = new string[M + N];
            var      processInfo = new ProcessStartInfo
            {
                //UseShellExecute = false, // redirect all input/output streams to parent's console
                FileName  = Constants.FILENAME_PASSENGER,
                Arguments = Constants.ARGUMENT_PASSENGER_M_MISIONARA
            };

            for (int i = 0; i < M; i++)
            {
                // create missionary
                var pid = i;
                //new Passenger(pid, false); // TO DO: start process
                processInfo.Arguments = pid + " " + Constants.ARGUMENT_PASSENGER_M_MISIONARA;
                childProcesses.Add(Process.Start(processInfo));

                ms.Append(pre + pid);
            }

            for (int i = 0; i < N; i++)
            {
                // create cannibal
                var pid = i + M;
                //new Passenger(pid, false); // TO DO: start process
                processInfo.Arguments = pid + " " + Constants.ARGUMENT_PASSENGER_M_MISIONARA;
                childProcesses.Add(Process.Start(processInfo));

                ms.Append(pre + pid);
            }


            // create a bus for receiving messages
            IBus bus = new BusBuilder()
                       .WithLogging(new ConsoleLogger())
                       .DefineErrorQueue("Boat.errors")
                       .DefineReadQueue("Boat")
                       //.DefineWriteQueues(ms)
                       .CreateLocalQueuesAutomatically()
                       .JsonSerialization()

                       .CreateBus();

            // register one or more message handlers
            bus.RegisterHandler <Message>(new PassengerHandler());
            // process messages on the read queue synchronously
            bus.ReceiveAsync <Message>(); // bus.ReceiveAsync<PlaceOrder>();

            aTimer = new Timer(Constants.BOAT_CHECK_PERIOD);

            aTimer.Elapsed += (sender, args) =>
            {
                //bus.StopReceiving();
                if (!ProcessPassengers())
                {
                    //    bus.Receive<Message>();
                    //}
                    //else
                    //{
                    // die
                    aTimer.Enabled = false;
                    Console.WriteLine("Press enter to exit...");
                    Console.ReadLine();
                }
            };

            aTimer.AutoReset = true;
            aTimer.Enabled   = true;
        }