private void AddToRequestsQueue() { while (true) { try { if (listener.IsListening) { var context = listener.GetContext(); if (queueLength < QueueSize) { RequestsQueue.Enqueue(context); Interlocked.Increment(ref queueLength); } else { var response = new StatusCode(429); //429 response.Execute(context.Response); } } } catch (ThreadAbortException) { return; } catch (Exception error) { Console.WriteLine(error.Message); } } }
// Complete the elevator requests public void DoRequests() { if (RequestsQueue.Count > 0) { // Make sure queue is sorted before any request is completed SortRequestsQueue(); Request requestToComplete = RequestsQueue[0]; // Go to requested floor if (Door.Status != "closed") { Door.CloseDoor(); } NextFloor = requestToComplete.Floor; GoToNextFloor(); // Remove request after it is complete Door.OpenDoor(); RequestsQueue.Remove(requestToComplete); // Automatically close door Door.CloseDoor(); } // Automatically go idle temporarily if 0 requests or at the end of request Movement = "idle"; }
/// <summary> /// Gets the data context. /// </summary> /// <param name="disposalRequestLibId">The disposal request library identifier.</param> /// <param name="list">The list.</param> /// <param name="context">The context.</param> internal void GetDataContext(int disposalRequestLibId, List <CustomsWarehouseDisposal> list, DataContextAsync context, Action dataLoaded) { m_DisposalRequestLibId = disposalRequestLibId; IEnumerable <IGrouping <string, CustomsWarehouseDisposal> > _requests = list.GroupBy <CustomsWarehouseDisposal, string>(x => x.CWL_CWDisposal2CustomsWarehouseID.Batch); RequestsQueue _Queue = new RequestsQueue(this, context, dataLoaded); _Queue.DoAsync(_requests); }
// Sort requests, for added efficiency private void SortRequestsQueue() { // Remove any requests which are useless i.e. requests that are already on their desired floor foreach (var req in RequestsQueue.ToArray()) { if (req.Floor == CurrentFloor) { RequestsQueue.Remove(req); } } // Decide if elevator is going up, down or is staying idle SetMovement(); // Sort if (RequestsQueue.Count > 1) { if (Movement == "up") { // Sort the queue in ascending order RequestsQueue.Sort((x, y) => x.Floor.CompareTo(y.Floor)); // Push any request to the end of the queue that would require a direction change foreach (var req in RequestsQueue.ToArray()) { if (req.Direction != Movement || req.Floor < CurrentFloor) { RequestsQueue.Remove(req); RequestsQueue.Add(req); } } } else { // Reverse the sorted queue (will now be in descending order) RequestsQueue.Sort((x, y) => y.Floor.CompareTo(x.Floor)); // Push any request to the end of the queue that would require a direction change foreach (var req in RequestsQueue.ToArray()) { if (req.Direction != Movement || req.Floor > CurrentFloor) { RequestsQueue.Remove(req); RequestsQueue.Add(req); } } } } }
// Change properties of elevator in one line - USE ONLY FOR TESTING public void ChangeProperties(int newCurrentFloor, int newNextFloor) { CurrentFloor = newCurrentFloor; NextFloor = newNextFloor; if (CurrentFloor > NextFloor) { Movement = "down"; } else { Movement = "up"; } RequestsQueue.Add(new Request(NextFloor, Movement)); }
/// <summary> /// The main method which turns on all the pieces in the application. /// </summary> /// <param name="args"> /// The command line arguments. /// </param> static void Main(string[] args) { // create the server "building blocks" MazeGameGenerator generator = new MazeGameGenerator(new DFSMazeGenerator()); RequestsQueue queue = new RequestsQueue(); Model model = new Model(generator, MazeGame.FromString); Controller controller = new Controller(model, queue); View view = new View(controller); Server server = new Server(int.Parse(ConfigurationManager.AppSettings["Port"]), view); // start getting clients server.Start(); // close the server Console.WriteLine("Press any button to close the server."); Console.ReadKey(); server.Stop(); //CancellationTokenSource tokenSource2 = new CancellationTokenSource(); //Task t = new Task(() => Console.WriteLine("First")), tcopy; //tcopy = t; ////tcopy.Start(); //Thread.Sleep(1000); //t = t.ContinueWith(dummy => Console.WriteLine("Second"), tokenSource2.Token); //t = t.ContinueWith(dummy => Console.WriteLine("Third"), tokenSource2.Token); //t = t.ContinueWith(dummy => Console.WriteLine("Fourth"), tokenSource2.Token); //t = t.ContinueWith(dummy => Console.WriteLine("Fifth")); //t = t.ContinueWith(dummy => Console.WriteLine("Sixth"), tokenSource2.Token); ////return; //tcopy.Start(); //tokenSource2.Cancel(); //Task<string> strTask = new Task<string>(() => "First "), strTaskCopy; //strTaskCopy = strTask; //strTask = strTask.ContinueWith(str => str.Result + "Second "); //strTask = strTask.ContinueWith(str => str.Result + "Third "); //strTask = strTask.ContinueWith(str => str.Result + "Fourth "); //strTask = strTask.ContinueWith(str => str.Result + "Fifth "); //strTask = strTask.ContinueWith(str => str.Result + "Sixth"); ////tcopy.Start(); //strTaskCopy.Start(); //Console.WriteLine(strTask.Result); }
public CraeteRequest(RequestsQueue requestsQueue, IGrouping <string, CustomsWarehouseDisposal> grouping) : base(grouping) { m_RequestsQueue = requestsQueue; }
// Send new request to its request queue public void SendRequest(int stopFloor, string btnDirection) { var request = new Request(stopFloor, btnDirection); RequestsQueue.Add(request); }