void gotConfig(void) { ArConfig *newConfig; done = true; configHandler->getConfig()->writeFile("configClient.txt"); newConfig = new ArConfig(*(configHandler->getConfig())); newConfig->writeFile("configClientNew.txt"); }
ConfigExample(): myIntParam(0), myDoubleParam(0.5), myBoolParam(false), myProcessConfigCB(this, &ConfigExample::processConfigFile) { // The global Aria class contains an ArConfig object. You can create // other instances of ArConfig, but this is how you can share one ArConfig // among various program modules. // If you want to store a config parameter in ArConfig, first you must add // it to the ArConfig object. Parameters are stored in sections, and // they affect a variable via a pointer provided in an ArConfigArg // object: ArConfig* config = Aria::getConfig(); config->setSectionComment("Example Section", "Contains parameters created by the configExample"); // Add an integer which ranges from -10 to 10: config->addParam( ArConfigArg("ExampleIntegerParameter", &myIntParam, "Example parameter integer.", -10, 10), "Example Section", ArPriority::NORMAL); // Add a floating point number which ranges from 0.0 to 1.0: config->addParam( ArConfigArg("ExampleDoubleParameter", &myDoubleParam, "Example double precision floating point number.", 0.0, 1.0), "Example Section", ArPriority::NORMAL); // Essential parameters can be placed in the "Important" priority level: config->addParam( ArConfigArg("ExampleBoolParameter", &myBoolParam, "Example boolean parameter."), "Example Section", ArPriority::IMPORTANT); // Unimportant parameters can be placed in the "Trivial" priority level: myStringParam[0] = '\0'; // make string empty config->addParam( ArConfigArg("ExampleStringParameter", myStringParam, "Example string parameter.", 256), "Example Section", ArPriority::TRIVIAL); // You can set a callback to be invoked when the configuration changes, in // case you need to respond to any changes in the parameter values: config->addProcessFileCB(&myProcessConfigCB, 0); }
AREXPORT void ArClientHandlerConfig::handleGetConfigDefaults( ArNetPacket *packet) { ArLog::log(ArLog::Normal, "%sreceived default config %s", myLogPrefix.c_str(), ((myHaveRequestedDefaultCopy) ? "(copy)" : "(reset)")); char param[1024]; char argument[1024]; char errorBuffer[1024]; myDataMutex.lock(); ArConfig *config = NULL; // If the config (or a section) is being reset to its default values, // then we don't want to remove any parameters that are not set -- i.e. // any parameters that are not contained in the default config. bool isClearUnsetValues = false; if (myHaveRequestedDefaults) { config = &myConfig; } else if (myHaveRequestedDefaultCopy) { // If we have requested a copy of the default configuration, then we // will want to remove any parameters that haven't been explicitly set. // (This is because of the next line, which copies the current config // to the default config.) isClearUnsetValues = true; // The default config is transmitted in an "abbreviated" form -- just // the section/param names and values. Copy the current config to the // default before processing the packet so that the parameter types, etc. // can be preserved. if (myDefaultConfig == NULL) { myDefaultConfig = new ArConfig(myConfig); myDefaultConfig->setConfigName("Default", myRobotName.c_str()); myDefaultConfig->setQuiet(myIsQuiet); } else { *myDefaultConfig = myConfig; } config = myDefaultConfig; } // if we didn't ask for any of these, then just return since the // data is for someone else else { myDataMutex.unlock(); return; } if (config == NULL) { ArLog::log(ArLog::Normal, "%serror determining config to populate with default values", myLogPrefix.c_str()); myDataMutex.unlock(); return; } ArArgumentBuilder *builder = NULL; ArLog::log(ArLog::Normal, "%sGot defaults", myLogPrefix.c_str()); errorBuffer[0] = '\0'; //myDataMutex.lock(); if (isClearUnsetValues) { config->clearAllValueSet(); } while (packet->getDataReadLength() < packet->getDataLength()) { packet->bufToStr(param, sizeof(param)); packet->bufToStr(argument, sizeof(argument)); builder = new ArArgumentBuilder; builder->setQuiet(myIsQuiet); builder->setExtraString(param); builder->add(argument); if ((strcasecmp(param, "Section") == 0 && !config->parseSection(builder, errorBuffer, sizeof(errorBuffer))) || (strcasecmp(param, "Section") != 0 && !config->parseArgument(builder, errorBuffer, sizeof(errorBuffer)))) { ArLog::log(ArLog::Terse, "%shandleGetConfigDefaults: Hideous problem getting defaults, couldn't parse '%s %s'", myLogPrefix.c_str(), param, argument); } else { IFDEBUG(if (strlen(param) > 0) { ArLog::log(ArLog::Normal, "%shandleGetConfigDefaults: added default '%s %s'", myLogPrefix.c_str(), param, argument); } ); } delete builder; builder = NULL; }
bool ArServerHandlerConfig::internalSetConfig(ArServerClient *client, ArNetPacket *packet) { char param[1024]; char argument[1024]; char errorBuffer[1024]; char firstError[1024]; ArNetPacket retPacket; ArConfig *config; bool ret = true; if (client != NULL) config = myConfig; else config = myDefault; if (client != NULL) lockConfig(); ArArgumentBuilder *builder = NULL; if (client != NULL) ArLog::log(ArLog::Normal, "Got new config from client %s", client->getIPString()); else ArLog::log(ArLog::Verbose, "New default config"); errorBuffer[0] = '\0'; firstError[0] = '\0'; while (packet->getDataReadLength() < packet->getDataLength()) { packet->bufToStr(param, sizeof(param)); packet->bufToStr(argument, sizeof(argument)); builder = new ArArgumentBuilder; builder->setExtraString(param); builder->add(argument); ArLog::log(ArLog::Verbose, "Config: %s %s", param, argument); // if the param name here is "Section" we need to parse sections, // otherwise we parse the argument if ((strcasecmp(param, "Section") == 0 && !config->parseSection(builder, errorBuffer, sizeof(errorBuffer))) || (strcasecmp(param, "Section") != 0 && !config->parseArgument(builder, errorBuffer, sizeof(errorBuffer)))) { if (firstError[0] == '\0') strcpy(firstError, errorBuffer); } delete builder; builder = NULL; } if (firstError[0] == '\0') { if (config->callProcessFileCallBacks(true, errorBuffer, sizeof(errorBuffer))) { if (client != NULL) ArLog::log(ArLog::Normal, "New config from client %s was fine.", client->getIPString()); else ArLog::log(ArLog::Verbose, "New default config was fine."); retPacket.strToBuf(""); writeConfig(); } else // error processing config callbacks { ret = false; if (firstError[0] == '\0') strcpy(firstError, errorBuffer); // if its still empty it means we didn't have anything good in the errorBuffer if (firstError[0] == '\0') strcpy(firstError, "Error processing"); if (client != NULL) ArLog::log(ArLog::Normal, "New config from client %s had errors processing ('%s').", client->getIPString(), firstError); else ArLog::log(ArLog::Normal, "New default config had errors processing ('%s').", firstError); retPacket.strToBuf(firstError); } } else { ret = false; if (client != NULL) ArLog::log(ArLog::Normal, "New config from client %s had at least this problem: %s", client->getIPString(), firstError); else ArLog::log(ArLog::Normal, "New default config had at least this problem: %s", firstError); retPacket.strToBuf(firstError); } //printf("Sending "); //retPacket.log(); if (client != NULL) client->sendPacketTcp(&retPacket); if (client != NULL) unlockConfig(); if (client != NULL) configUpdated(client); return ret; }
int main(int argc, char **argv) { Aria::init(); ArServerBase server; ArConfig *config; config = Aria::getConfig(); std::string section; char joy[512]; sprintf(joy, "Joy"); section = "section1"; config->addParam(ArConfigArg("int", new int, "fun int", 0), section.c_str(), ArPriority::NORMAL); config->addParam(ArConfigArg("double", new double, "fun double", 0, 1), section.c_str(), ArPriority::NORMAL); config->addParam(ArConfigArg("bool", new bool, "fun bool"), section.c_str(), ArPriority::IMPORTANT); config->addParam(ArConfigArg("string", joy, "fun string", sizeof(joy)), section.c_str(), ArPriority::TRIVIAL); section = "section8"; config->addParam(ArConfigArg("int", new int, "fun int", 0), section.c_str(), ArPriority::NORMAL); config->addParam(ArConfigArg("double", new double, "fun double", 0, 1), section.c_str(), ArPriority::NORMAL); config->addParam(ArConfigArg("doubleFOUR", (double).4, "fun double", 0.0, 1.0), section.c_str(), ArPriority::NORMAL); config->addParam(ArConfigArg("double three", new double, "fun double", 0, 1), section.c_str(), ArPriority::NORMAL); config->addParam(ArConfigArg("double two", new double, "fun double", 0, 1), section.c_str(), ArPriority::NORMAL); config->addParam(ArConfigArg("double one", new double, "fun double", 0, 1), section.c_str(), ArPriority::NORMAL); config->addParam(ArConfigArg("double", new double, "fun double", 0, 1), section.c_str(), ArPriority::NORMAL); config->addParam(ArConfigArg("bool", new bool, "fun bool"), section.c_str(), ArPriority::IMPORTANT); config->addParam(ArConfigArg("string", joy, "fun string", sizeof(joy)), section.c_str(), ArPriority::TRIVIAL); section = "some section"; config->setSectionComment("some section", "this is a random section with 4 ints"); config->addParam(ArConfigArg("int1", new int, "fun int"), section.c_str(), ArPriority::TRIVIAL); config->addParam(ArConfigArg("int2", new int, "fun int", -1, 1200), section.c_str(), ArPriority::NORMAL); config->addParam(ArConfigArg("int3", new int, "fun int"), section.c_str(), ArPriority::IMPORTANT); config->addParam(ArConfigArg("int4", new int, "fun int"), section.c_str(), ArPriority::NORMAL); config->addParam(ArConfigArg("int4", new int, "fun int"), section.c_str(), ArPriority::NORMAL); config->addParam(ArConfigArg("int1", new int, "fun int"), section.c_str(), ArPriority::TRIVIAL); section = "another section"; config->setSectionComment("another section", "this is another section with 1 of each type"); config->addParam(ArConfigArg("inta", new int, "fun int"), section.c_str(), ArPriority::NORMAL); config->addParam(ArConfigArg("doublea", new double, "fun double"), section.c_str(), ArPriority::NORMAL); config->addParam(ArConfigArg("boola", new bool, "fun bool"), section.c_str(), ArPriority::NORMAL); config->addParam(ArConfigArg("stringa", new char[512], "fun string", 512), section.c_str(), ArPriority::NORMAL); if (!server.open(7272)) { printf("Could not open server port\n"); exit(1); } config->setBaseDirectory("./"); config->writeFile("default.txt"); config->parseFile("modified.txt"); config->writeFile("modifiedModified.txt"); ArServerHandlerConfig configHandler(&server, Aria::getConfig(), "default.txt"); server.run(); Aria::shutdown(); return 0; }
int main(int argc, char **argv) { Aria::init(); ArArgumentParser argParser(&argc, argv); argParser.loadDefaultArguments(); if (argc < 2 || !Aria::parseArgs() || argParser.checkArgument("-help")) { ArLog::log(ArLog::Terse, "configExample usage: configExample <config file>.\nFor example, \"configExample examples/configExample.cfg\"."); Aria::logOptions(); Aria::shutdown(); return 1; } // Object containing config parameters, and responding to changes: ConfigExample configExample; // Load a config file given on the command line into the global // ArConfig object kept by Aria. Normally ArConfig expects config // files to be in the main ARIA directory (i.e. /usr/local/Aria or // a directory specified by the $ARIA environment variable). char error[512]; const char* filename = argParser.getArg(1); ArConfig* config = Aria::getConfig(); ArLog::log(ArLog::Normal, "configExample: loading configuration file \"%s\"...", filename); if (! config->parseFile(filename, true, false, error, 512) ) { ArLog::log(ArLog::Terse, "configExample: Error loading configuration file \"%s\" %s. Try \"examples/configExample.cfg\".", filename, error); Aria::shutdown(); return -1; } ArLog::log(ArLog::Normal, "configExample: Loaded configuration file \"%s\".", filename); // After changing a config value, you should invoke the callbacks: ArConfigSection* section = config->findSection("Example Section"); if (section) { ArConfigArg* arg = section->findParam("ExampleBoolParameter"); if (arg) { arg->setBool(!arg->getBool()); if (! config->callProcessFileCallBacks(false, error, 512) ) { ArLog::log(ArLog::Terse, "configExample: Error processing modified config: %s.", error); } else { ArLog::log(ArLog::Normal, "configExample: Successfully modified config and invoked callbacks."); } } } // You can save the configuration as well: ArLog::log(ArLog::Normal, "configExample: Saving configuration..."); if(!config->writeFile(filename)) { ArLog::log(ArLog::Terse, "configExample: Error saving configuration to file \"%s\"!", filename); } // end of program. ArLog::log(ArLog::Normal, "configExample: end of program."); Aria::shutdown(); return 0; }