Run Loop

So I have created the foundation for the next work. I have managed to get all the run parameters​ from an XML file and loaded them into variables in main of the MCP C++ program. I had to do a little restructuring of the code as the includes for thread and time needed to be moved to the shared include as the methods in the classes used some of the same libraries.

So the main looks like this, it will not likely get much larger than this (as always the code is better spaced in the editor in AWS cloud9):

#include "mcp.cpp"

int main()
{
        
	// Print a header of execution out
	auto result = std::time(nullptr);	
	std::cout << "Start: Master Control Program " << std::endl;
	std::cout << "Local Time: " <<
	    std::asctime(std::localtime(&result)) << std::endl;
	    
    // Get running values
    std::string versionnumber, storedirectory, sharedirectory, UUID[10];
    
    MCPParms ourparms("MCP.xml"); // parms for program
    try {
        
        ourparms.load();
    }
    
    catch (std::exception const&  ex) {
        
       std::cout << ex.what() << std::endl;
       return (1);
    }
	
	ourparms.version(versionnumber);
	
	// Write out version number
	std::cout << "File version: " << versionnumber << std::endl;
	
	ourparms.UUID(UUID);
	
	ourparms.store(storedirectory);
	
	ourparms.share(sharedirectory);
	
	int client = ourparms.client();
	
	std::cout << "Client " << client;
	if (client == 0) {
	    std::cout << " Primary!";
	}
	std::cout << std::endl;
	std::cout << "Store: " << storedirectory << std::endl;
	std::cout << "Share: " << sharedirectory << std::endl;
	
	// Start run loop for MCP
	MCPRun runit(1000, storedirectory); // set wait time
	
	runit.run();
	
	// Close down task
	
	std::this_thread::sleep_for(std::chrono::milliseconds(1000));
	
	// Shutdown
	result = std::time(nullptr);
    std::cout << "Exit: Master Control Program " << std::endl;
	std::cout << "Local Time: " <<
	    std::asctime(std::localtime(&result)) << std::endl;
	    
    // Leave politely
    return 0;

}

 
The run routine is working and now starts up a loop with 1000 millisecond wait and checks if the file in the store directory named run.xml is present, well-formed​, and has a ‘run’ value of ‘Yes’. So the MCP begins to act like a master control program now as it loops until requested to a ​shutdown of fail-out if it cannot find the control file that allows it to run.

The run code:

MCPRun::MCPRun(int mywaittime, const std::string &mystore)
{
   runfile = mystore + "/run.xml"; // put hard coding inside
   storedirectory = mystore;
   waittime = mywaittime;
   shutdown = "Start";
   
   return;

}

void MCPRun::run()
/*
    This is the main control loop. It runs until the check becomes false. There
    must be a wait long enough to not cause this to take over the system, at
    least a second.
    
*/

{

   do {
   
        std::this_thread::sleep_for(std::chrono::milliseconds(waittime));
   
   } while (check());
   
   std::cout << "Ending Condition: " << shutdown << std::endl;
   
   return;

}

bool MCPRun::check()
/*
    The check opens the run.xml in the store and stops when the run value is not
    "Yes" or the file is not there or malformed.
*/

{
    std::string therun;
    therun = "No";
    try {
        
        pt::read_xml(runfile, tree); // exceptions must be handled!
        therun = tree.get("run", "No"); // no throw if not there
    }
    
    catch (std::exception const&  ex) {
        
       therun = "Fail";
       shutdown = "Fail";

    }
   //std::cout << "Check: " << therun << std::endl;
   
   return (therun == "Yes");

}

Well all of the work now begins to pay. Next, and I am having issues with this step, is to scan the share directory for files to process.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s