// constructor for class public MultiCellBuffer(int n) { lock (this) // we want no interruptiongs { elementCount = 0; if (n <= N) { this.n = n; write_pool = new Semaphore(n, n); read_pool = new Semaphore(n, n); buffers = new BufferString[3]; for (int i = 0; i < n; i++) { buffers[i] = new BufferString(); } } else { Console.WriteLine(" MultiCellBuffer Constructor - n > N .."); } } }
// EVENT HANDLERS /// <summary> /// /// Event handler for Book Sale broadcasted by publisher thread /// /// </summary> /// <param name="price"></param> /// <param name="PubNum"></param> public void BookSale(double price, Int32 PubNum) { // books are on sale for a price. // see if I want to buy at that price OrderObject newOrder = CreateOrder(PubNum); if (StoreNumber == 0) // Every store will get this event but I only need noe store to write it out!! { Console.WriteLine(" ***************** BOOK SALE from Publisher # {0} Price{1:C} *******************\n", PubNum, price); } if (newOrder != null) { String eOrder = Encoder(newOrder); // add to multicell buffer BufferString mybString = new BufferString(); mybString.setBufferString(eOrder, PubNum); Program.mcb.setOneCell(mybString); // order the books. Console.WriteLine("\t\t ^^^ order created for store {0} for {1} books", StoreNumber, newOrder.getAmount()); } else { Console.WriteLine("\t\t *** No order created for store {0}", StoreNumber); } }
// functional thread for the bookstore. // THis thread will generate demand, check price, and create purchase orders if necessary. /// <summary> /// Functional method for thread. /// Sell Books -- This will create demand. The amount of books is random /// The process in this thread is to get the price from the publishers (use the global varialbes) /// Pick the lowest price /// See if we need to purchase books /// create and order /// Encode the order /// Push onto the multicell buffer /// Repeat... /// </summary> public void BookStoreFunc() { while (Program.BookStoreThreadRunning) { //Console.WriteLine("\n\n"); OrderObject newOrder; int PubNum = 0; //CurrentPrice = pub1.getPrice(); SellBooks(); // create demand // check for lowest price double p1 = Program.GV.get_Pub1_Price(); double P2 = Program.GV.get_Pub2_Price(); //Console.WriteLine(" pub1 price : {0}, pub2 price : {1}", p1, P2); if (p1 < P2) { PubNum = 1; CurrentPrice = p1; } else { PubNum = 2; CurrentPrice = P2; } // using the lowest price, create an order. This method can return NULL if no books are needed. newOrder = CreateOrder(PubNum); if (newOrder != null) // See if we need to move forward with the order for the store. { // encode the order object String eOrder = Encoder(newOrder); // add to multicell buffer BufferString mybString = new BufferString(); mybString.setBufferString(eOrder, PubNum); Program.mcb.setOneCell(mybString); Console.WriteLine("\t\t ^^^ order created for store {0} for {1} books", StoreNumber, newOrder.getAmount()); } else { Console.WriteLine("\t\t *** No order created for store {0}", StoreNumber); } //} LastPrice = CurrentPrice; // set the last known price Thread.Sleep(1000); // sleep for 2 seconds } Console.WriteLine("@@@@ BookStoreFunc threaded exited gracefully...Thread Name {0}", Thread.CurrentThread.Name); }
/// <summary> /// Sets one open buffer cell with data. /// If cells are all full, the calling thread is blokced. /// </summary> /// <param name="data"></param> public void setOneCell(BufferString data) { write_pool.WaitOne(); lock (this) { while (elementCount == n) { Monitor.Wait(this); } for (int i = 0; i < n; i++) { if (buffers[i].getPublisherNum() == 0) // make sure empty { buffers[i] = data; elementCount++; i = n; } } write_pool.Release(); Monitor.Pulse(this); } }