Example #1
0
        /*
         * Method to decrypt the string using the mentioned encryption service and
         * decode encrypted string into order object.
         */
        public Order decryptOrder(MultiCellBuffer buffer, int threadID)
        {
            String decryptedOrderString;

            String[] orderValues;
            Order    order = new Order();

            /*
             * Decryption service hosted in
             * http://neptune.fulton.ad.asu.edu/WSRepository/Services/EncryptionWcf/Service.svc
             */
            CryptoService.ServiceClient serviceClient = new CryptoService.ServiceClient();
            decryptedOrderString = serviceClient.Decrypt(buffer.getOneCell(threadID));

            /*
             * Disclose every field of the order string using | to get back the order object
             */
            orderValues = decryptedOrderString.Split('|');

            /*
             * Values to be assigned based on the order it was encoded.
             * Get the associated value from the respective index and
             * assign it to the associated attribute of the order object
             */
            order.OrderNo      = Convert.ToInt32(orderValues[0]);
            order.SenderID     = Convert.ToInt32(orderValues[1]);
            order.CardNo       = Convert.ToInt32(orderValues[2]);
            order.Amount       = Convert.ToInt32(orderValues[3]);
            order.SellingPrice = Convert.ToInt32(orderValues[4]);
            return(order);
        }
Example #2
0
        /*
         * A function to handle the price cut event
         */
        public void chickenOnSale(Int32 oldPrice, int price, Int32 availableChickens, Int32 priceDrop)
        {
            Console.WriteLine("________________________________________________________________________________");
            Console.WriteLine("\tOld Price: ${0} New Price : ${1} Price Dropped by : ${2} \n\tNew Order requested by Thread{3}", oldPrice, price, priceDrop, threadID);

            ReaderWriterLock rwl     = new ReaderWriterLock();
            Encoder          encoder = new Encoder();

            TimeStamp timestamp = new TimeStamp();

            MultiCellBuffer buffer = new MultiCellBuffer(threadID);

            try
            {
                acquireLock(rwl);

                Order order = setOrderObject(threadID, price, availableChickens, priceDrop);

                /*
                 * Encoding the order object and writing the data in the buffer cell
                 */
                String encryptedOrder = encoder.encryptOrder(threadID, order);
                buffer.setOneCell(threadID, encryptedOrder);

                /*
                 * A class to process the order and send back the status of the order
                 */
                OrderProcessing orderProcessClass = new OrderProcessing();
                orderProcessClass.processOrder(timestamp, buffer, threadID);

                /*
                 * Time stamp to calculate the time taken to process the order
                 */
                timestamp.End = DateTime.Now;

                Console.Write("   Time Elapsed : ");
                Console.Write("{1} ms", threadID, timestamp.timeStampDifference());
            }
            finally
            {
                releaseLock(rwl);
                Console.WriteLine("\n________________________________________________________________________________");
            }
        }
Example #3
0
        /*
         * A function to process the order
         */
        public Boolean processOrder(TimeStamp ts, MultiCellBuffer buffer, Int32 threadId)
        {
            Encoder decoder = new Encoder();

            acquireLock();

            /*
             * Decrypting the order read from the buffer and calculating
             * the totalCost using the calculateSellingPrice method.
             */
            Order order = decoder.decryptOrder(buffer, threadId);

            order.TotalAmount = calculateSellingPrice(order);
            display(order);

            Console.Write("   Order Status : ");

            try
            {
                if (isValidCard(order.CardNo))
                {
                    /*
                     * Check if the order can be placed
                     */
                    if (checkFeasibilityWithQuantity(order.Amount))
                    {
                        Retailer retailer = new Retailer();
                        updateTotalOrder(order.Amount);

                        /*
                         * Subscribing to order confirmation event
                         */
                        orderConfirmation += new orderConfirmationEvent(retailer.orderConfirmationCallBack);

                        /*
                         * Status information about the order and chicken availability is sent to the handler.
                         */
                        orderConfirmation(threadId, currentChickenAvailability());

                        /*
                         * Unsubscribing to order confirmation event
                         */
                        orderConfirmation -= new orderConfirmationEvent(retailer.orderConfirmationCallBack);

                        return(true);
                    }

                    else
                    {
                        /*
                         * If it cannot be placed notify the possible reason
                         * due to the sold out chicken or requesting more chicken than
                         * the available chickens.
                         */
                        if (ChickenFarm.chickensPurchased == 100)
                        {
                            Console.WriteLine("Order cannot be processed, all {0} chickens have been sold out.", ChickenFarm.totalChickenCount);
                        }
                        else
                        {
                            Console.WriteLine("Order cannot be processed, chicken amount needs to be <= {0}", currentChickenAvailability());
                        }

                        return(false);
                    }
                }

                else
                {
                    Console.WriteLine("Order is not in the range");
                    return(false);
                }
            }

            finally
            {
                releaseLock();
                Console.WriteLine("  ____________________________________________________________________________");
            }
        }