Ejemplo n.º 1
0
/** Deal with any error messages generated by the call to one of the service methods
 */
void reportErrors(OSCommandLine *oscommandline, std::string errormsg, OSnl2OS* nl2OS)
{
        std::string osrl = "";
        OSResult *osresult = NULL;
        OSrLWriter *osrlwriter = NULL;
        //first check to see if we already have OSrL,
        //if so don't create a new osresult object
        std::string::size_type  pos1 = errormsg.find( "<osrl");
        if(pos1 == std::string::npos)
        {
            osrlwriter = new OSrLWriter();
            osresult = new OSResult();
            osresult->setGeneralMessage(errormsg);
            osresult->setGeneralStatusType("error");
            osrl = osrlwriter->writeOSrL(osresult);
        }
        else
        {
            osrl = errormsg;
        }
        reportResults(oscommandline, osrl, nl2OS);
}// reportErrors
int main(int argc, char **argv)
{
    WindowsErrorPopupBlocker();
    char *stub;
    // set AMPL structures
    ASL *asl;
    asl = ASL_alloc(ASL_read_fg);
    stub = argv[1];
    jac0dim((char*)stub, (fint)strlen(stub));
    OSnl2osil *nl2osil = NULL;

    std::ostringstream outStr;
	std::cout << OSAmplClientVersionInfo();
	std::string osss;

	outStr << stub << " " << getenv("OSAmplClient_options");
    osss = outStr.str();

#ifdef DEBUG_AMPL_CLIENT
    std::cout << "Input String = " << osss << std::endl;
#endif

	FileUtil *fileUtil = NULL;
	OSCommandLine *oscommandline;
	oscommandline = new OSCommandLine();
	OSCommandLineReader *clreader;
	clreader = new OSCommandLineReader();

	//parse the command line items into a CommandLine object
    try
    {		
		oscommandline = clreader->readCommandLine(osss);
		if (oscommandline->serviceMethod == "")
			oscommandline->serviceMethod = "solve";
		oscommandline->convertSolverNametoLowerCase();

	}
    catch (const ErrorClass& eclass)
    {
        OSResult *osresult = NULL;
        osresult = new OSResult();
        osresult->setGeneralMessage(eclass.errormsg);
        osresult->setGeneralStatusType("error");
        OSrLWriter *osrlwriter = NULL;
        osrlwriter = new OSrLWriter();
        std::string osrl = osrlwriter->writeOSrL(osresult);
        if (oscommandline->osrlFile != "")
        {
            fileUtil->writeFileFromString(oscommandline->osrlFile, osrl);
            if (oscommandline->browser != "")
            {
                std::string str = oscommandline->browser + "  "
                                  + oscommandline->osrlFile;
                const char *ch = &str[0];
                std::system(ch);
            }
        }
        else
        {
            std::cout << osrl << std::endl;
        }
        //catch garbage collection
        delete osresult;
        osresult = NULL;
        delete osrlwriter;
        osrlwriter = NULL;
        delete fileUtil;
        fileUtil = NULL;
        delete oscommandline;
        oscommandline = NULL;
        delete clreader;
        clreader = NULL;
        return 1;
    }

#ifdef DEBUG_AMPL_CLIENT
	std::cout << oscommandline->list_options() << std::endl;
#endif

	// deal with "action items": help, quit, listOptions
	if (oscommandline->quit || oscommandline->invokeHelp || oscommandline->writeVersion || oscommandline->listOptions)
	{
		if (oscommandline->invokeHelp) 
			std::cout << std::endl << std::endl << get_AmplClient_help() << std::endl;
		if (oscommandline->listOptions) 
			std::cout << std::endl << std::endl << oscommandline->list_options() << std::endl;
        delete oscommandline;
        oscommandline = NULL;
        return 0;
	}

	// call service method
	bool result = callServiceMethod(oscommandline);
	if (result == true)
		return 0;
	else
		return 1;
}
bool callServiceMethod(OSCommandLine* oscommandline)
{
	FileUtil *fileUtil = new FileUtil();
	try
    {
		bool result;
		OSServiceMethods *osservicemethods;
		osservicemethods = new OSServiceMethods(oscommandline);

		if (oscommandline->printModel) 
			oscommandline->osinstance->printModel();
		if (oscommandline->printRowNumberAsString != "")
			oscommandline->osinstance->printModel(atoi((oscommandline->printRowNumberAsString).c_str()));

		result = osservicemethods->executeServiceMethod(oscommandline);

		// deal with the output
		if (result == false) 
			throw ErrorClass(osservicemethods->resultString);
		else
		{
			if (oscommandline->serviceMethod == "getJobID")
			{
				std::cout << "The job ID generated by the system is:" << std::endl << std::endl;
				std::cout << osservicemethods->resultString << std::endl << std::endl;
				std::cout << "Be sure to make a record of this job ID for later." << std::endl;
				std::cout << "You will need it to retrieve the job or inquire into its status." << std::endl;
			}
			else if (oscommandline->serviceMethod == "solve" || oscommandline->serviceMethod == "retrieve")
			{
		        if (oscommandline->osrlFile != "")
				{
		            fileUtil->writeFileFromString(oscommandline->osrlFile, osservicemethods->resultString);
					if (oscommandline->browser != "")
					{
			            std::string str = oscommandline->browser + "  "
					                      + oscommandline->osrlFile;
		                const char *ch = &str[0];
				        std::system(ch);
		            }
                    std::cout <<  "\nSolve command executed. Please see " << oscommandline->osrlFile  << " for results." << std::endl;
				}
		        else
				{
		            std::cout << osservicemethods->resultString << std::endl;
				}
			}
			else if (oscommandline->serviceMethod == "kill" || oscommandline->serviceMethod == "knock")
			{
		        if (oscommandline->osplOutputFile != "")
				{
		            fileUtil->writeFileFromString(oscommandline->osplOutputFile, osservicemethods->resultString);
				}
		        else
				{
		            std::cout << osservicemethods->resultString << std::endl;
				}
			}
		}
		return 0;
    }
    catch (const ErrorClass& eclass)
    {
        OSResult *osresult = NULL;
        OSrLWriter *osrlwriter = NULL;
        osrlwriter = new OSrLWriter();
        osresult = new OSResult();
        osresult->setGeneralMessage(eclass.errormsg);
        osresult->setGeneralStatusType("error");
        std::string osrl = osrlwriter->writeOSrL(osresult);
        if (oscommandline->osrlFile != "")
        {
            fileUtil->writeFileFromString(oscommandline->osrlFile, osrl);
            if (oscommandline->browser != "")
            {
                std::string str = oscommandline->browser + "  "
                                  + oscommandline->osrlFile;
                const char *ch = &str[0];
                std::system(ch);
            }
        }
        else
        {
            std::cout << osrl << std::endl;
        }
        //catch garbage collection
        delete osresult;
        osresult = NULL;
        delete osrlwriter;
        osrlwriter = NULL;

        delete oscommandline;
        oscommandline = NULL;
        delete fileUtil;
        fileUtil = NULL;
        return 1;
    }
}
bool OSServiceMethods::executeServiceMethod(OSCommandLine *oscommandline)
{
    OSSolverAgent* osagent = NULL;
	/**
	 *  The required file conversions are all assumed 
	 *  to have been taken care of in the nonstandard constructor,
	 *  so this wrapper merely directs traffic
	 */
    try
    {
    	
    	
		/** the only local service method is solve() */
		if	(oscommandline->serviceLocation == "")
			if (oscommandline->serviceMethod == "solve")
				//resultString = runSolver(oscommandline->solverName, oscommandline->osoption, oscommandline->osinstance);
				int junk;
			else
				throw ErrorClass("No service location (URL) specified. Only \"solve\" is available locally");

		/** Here we have a remote call --- reuse as much code as possible */
		else
		{
            osagent = new OSSolverAgent(oscommandline->serviceLocation);
//solve
			if (oscommandline->serviceMethod == "solve")
	            resultString = osagent->solve(oscommandline->osil, oscommandline->osol);
//send --- first check that there is a jobID. If not, get one
			else if (oscommandline->serviceMethod == "send")
			{
                if (oscommandline->osol.find( "<jobID") == std::string::npos)
				{
	                OSoLReader *osolreader = NULL;
		            osolreader = new OSoLReader();
	                OSoLWriter *osolwriter = NULL;
		            osolwriter = new OSoLWriter();
					try
					{

						if (oscommandline->osol != "")
							oscommandline->osoption = osolreader->readOSoL(oscommandline->osol);
			            oscommandline->osoption->setJobID(osagent->getJobID(""));

						// now write the osOption object into a string
				        oscommandline->osol = osolwriter->writeOSoL(oscommandline->osoption);

		                delete osolreader;
			            osolreader = NULL;
		                delete osolwriter;
			            osolwriter = NULL;
					}
					catch (const ErrorClass& eclass)
					{
						delete osolreader;
			            osolreader = NULL;
		                delete osolwriter;
			            osolwriter = NULL;
						throw ErrorClass(eclass.errormsg);
					}
				}
				bool sendResult;
				sendResult = osagent->send(oscommandline->osil, oscommandline->osol);
				if (sendResult == false)
					throw ErrorClass("send() method failed");
            }
//retrieve
			else if (oscommandline->serviceMethod == "retrieve")
			{
	            if (oscommandline->osol.find( "<jobID") == std::string::npos)
		            throw ErrorClass("there is no JobID to retrieve");
			    resultString = osagent->retrieve(oscommandline->osol);
			}
//getJobID
			else if (oscommandline->serviceMethod == "getJobID")
	            resultString = osagent->getJobID(oscommandline->osol);
//knock
			else if (oscommandline->serviceMethod == "knock")
	            resultString = osagent->knock(oscommandline->osplInput, oscommandline->osol);
//kill
			else if (oscommandline->serviceMethod == "kill")
			{
	            if (oscommandline->osol.find( "<jobID") == std::string::npos)
		            throw ErrorClass("there is no JobID to kill");
			    resultString = osagent->kill(oscommandline->osol);
			}
			else
		            throw ErrorClass("serviceMethod not recognized");

			delete osagent;
            osagent = NULL;
		}
		return true;
    }
    catch (const ErrorClass& eclass)
    {
        OSResult *osresult = NULL;
        OSrLWriter *osrlwriter = NULL;
        osrlwriter = new OSrLWriter();
        osresult = new OSResult();
        osresult->setGeneralMessage(eclass.errormsg);
        osresult->setGeneralStatusType("error");
        resultString = osrlwriter->writeOSrL(osresult);
        //catch garbage collection
        delete osresult;
        osresult = NULL;
        delete osrlwriter;
        osrlwriter = NULL;

		if (osagent != NULL) delete osagent;
            osagent = NULL;
        return false;
    }
    
 
}//executeServiceMethod