Putting It All Together
Let’s now put the code together and show the lifecycle of a service request from our old friend Figure 2. As Listing 5 shows, we have a service request called "Enable Link 1", which is stored in a character array. Next, I create an instance of the class NMSServiceQueue to which I add my service request. Then I call the member function examineQueue() to look at the contents of the queue. I then call remove() to extract an item from the queue. It’s at this point that I would service the associated request. After this, I clean up by deleting the service request.
Listing 5 Service Request from Start to Finish
char serviceRequest1[] = "Enable Link 1"; NMSServiceQueue* aServiceQueue = new NMSServiceQueue(); // Let’s now create our service request aServiceQueue->add(createQueueItem(serviceRequest1)); // Our service request is now queued up ready for execution aServiceQueue->examineQueue(); // Let’s get the service request out of the queue QueueElementPtr anOperation = aServiceQueue->remove(); if (anOperation) printf("Removed a queue element: %s\n", anOperation->requiredOperation); // Code here to execute the service request // We’ve executed the service request, let’s free up the resources delete anOperation->requiredOperation; delete anOperation;
That’s the full cycle of operations! Let’s now briefly consider the queue structure in a little more detail.