/// <summary> /// This method places an order. It creates an order object, encodes it and places it in a MultiCellBuffer /// </summary> public void placeOrder() { OrderClass order = new OrderClass(); order.cardNumber = this.cardNumber; order.amount = rng.Next(1, 10); //calculates the quantity of chiken to be ordered by generating a random number order.senderId = this.name; order.timeStamp = DateTime.Now; //stores the timestamp at which the order was placed String orderObjString = EncodeDecode.encode(order); ResourcePool.countEmptySpaces.WaitOne(); //Semaphore allows the thread if buffer has space else blocks it myApplication.mcb.setOneCell(orderObjString); }
/// <summary> /// This method is started as a thread by the farmerFunc thread. /// It checks the MultiCellBuffer once in every 500ms /// </summary> public void startAcceptOrder() { Console.WriteLine("looking for orders"); while (myApplication.runApp) //thread accepts order as long as the farmer thread is running { //myApplication.runApp is bool that checks if farmer thread is alive Thread.Sleep(500); ResourcePool.countFilledSpaces.WaitOne(); //Semaphore allows the thread if there is something in the buffer to process String objString = myApplication.mcb.getOneCell(); OrderClass orderObj = EncodeDecode.decode(objString); Int32 currPrice = this.getPrice(); Thread orderProcessor = new Thread(() => orderProcessing(orderObj, currPrice)); orderProcessor.Start(); } }