static void initConfig (int argc, char **argv) { /* Initialize */ server.fd = -1; if (pthread_mutex_init (&server.reconfig_lock, NULL) != 0) { perror ("Can not initialize pthread_mutex"); exit (EXIT_FAILURE); } server.arc_ref = initializeArcRef (); if (server.arc_ref == NULL) { perror ("Can not initialize arc_ref_t"); exit (EXIT_FAILURE); } server.reconfig_tid = 0; server.client_tid = NULL; server.reconfig_signal = 0; server.shutdown_signal = 0; server.config_file = NULL; server.conf = NULL; /* Config file */ if (argc != 2) { usage (); exit (EXIT_FAILURE); } server.config_file = strdup (argv[1]); server.conf = initFromConfigFile (); if (server.conf == NULL) { perror ("Can not initialize from config file"); exit (EXIT_FAILURE); } server.query_timeout_millis = server.conf->query_timeout_millis; }
//================= private ================== void FigureItem::initFromConfigFile(const FigureKey& figureKey) { assert(figureKey.isValid()); std::string fileName = FigureResourceManager::getInstance()->constructPath((figureKey.isMale ? "boy/" : "girl/") + figureKey.type + "/" + figureKey.id + "/boneConfig.json"); cocos2d::Data fileData = cocos2d::FileUtils::getInstance()->getDataFromFile(fileName); if (fileData.getSize() <= 0) { cocos2d::log("Fail opening file:%s\n", fileName.c_str()); return; } assert(fileData.getBytes() != nullptr); std::string jsonString((char*)fileData.getBytes(), fileData.getSize()); rapidjson::Document document; document.Parse<0>(jsonString.c_str()); if (document.HasParseError() || document.IsObject() == false) { cocos2d::log("json ParseError %s\n", document.GetParseError()); return; } if (document.HasMember("COMPONENTS")) { const rapidjson::Value& components = document["COMPONENTS"]; assert(components.IsArray()); for (rapidjson::SizeType i = 0; i < components.Size(); ++i) { const rapidjson::Value& item = components[i]; assert(item.HasMember("TYPE")); assert(item.HasMember("ID")); std::string type = item["TYPE"].GetString(); std::string id = item["ID"].GetString(); assert(mSonTypeSet.find(type) == mSonTypeSet.end()); mSonTypeSet.insert(type); mSonFigureKeys.push_back(FigureKey(figureKey.isMale, type, id)); FigureKey sonFigureKey = FigureKey(figureKey.isMale, type, id); initFromConfigFile(sonFigureKey); } return; } assert(document.HasMember("BONE_CONFIGS")); const rapidjson::Value& boneConfigs = document["BONE_CONFIGS"]; assert(boneConfigs.IsArray()); for (rapidjson::SizeType i = 0; i < boneConfigs.Size(); ++i) { const rapidjson::Value& boneConfigJson = boneConfigs[i]; std::string boneName = boneConfigJson["BONE_NAME"].GetString(); const rapidjson::Value& bonePicture = boneConfigJson["BONE_PICTURE"]; BoneConfig boneConfig; boneConfig.boneName = boneName; if (bonePicture.IsArray()) { for (rapidjson::SizeType i = 0; i < bonePicture.Size(); ++i) { const rapidjson::Value& item = bonePicture[i]; assert(item.HasMember("PATH")); std::string picturePath = FigureResourceManager::getInstance()->constructPath(item["PATH"].GetString()); assert(cocos2d::FileUtils::getInstance()->isFileExist(picturePath)); boneConfig.pictures.push_back(picturePath); } } else { //bonePicture.GetString() == boy-or-girl/type/id/bone.png std::string picturePath = FigureResourceManager::getInstance()->constructPath(bonePicture.GetString()); assert(cocos2d::FileUtils::getInstance()->isFileExist(picturePath)); boneConfig.pictures.push_back(picturePath); } mBoneConfigArray.push_back(boneConfig); } }
FigureItem::FigureItem(const FigureKey& figureKey) : mFigureKey(figureKey) { initFromConfigFile(figureKey); }
static void * reconfigThread (arc_t * initial_arc) { pthread_mutex_lock (&server.reconfig_lock); server.arc_ref->arc = initial_arc; server.arc_ref->refcount = 1; pthread_mutex_unlock (&server.reconfig_lock); while (!server.shutdown_signal) { /* Set reconfig arc_t */ if (server.reconfig_signal) { conf_t *new_conf, *tmp_conf; arc_ref_t *new_arc_ref, *tmp_arc_ref; /* initialize config setting from updated conf file */ new_conf = initFromConfigFile (); if (new_conf == NULL || strcmp (server.conf->cluster_name, new_conf->cluster_name) || server.conf->port != new_conf->port || server.conf->daemonize != new_conf->daemonize) { perror ("Reconfig canceled. Can not change certain properties(cluster name, port, daemonize)"); freeConfigStructure (new_conf); server.reconfig_signal = 0; continue; } /* initialize new arc_t from new Configuration */ new_arc_ref = initializeArcRef (); if (new_arc_ref == NULL) { perror ("Reconfig canceled, Can not allocate memory for arc_ref_t"); freeConfigStructure (new_conf); server.reconfig_signal = 0; continue; } new_arc_ref->arc = initCapi (new_conf); if (new_arc_ref->arc == NULL) { perror ("Reconfig canceled, Can not create new arc_t"); freeConfigStructure (new_conf); free (new_arc_ref); server.reconfig_signal = 0; continue; } /* Set reconfiguration arc_t */ pthread_mutex_lock (&server.reconfig_lock); tmp_arc_ref = server.arc_ref; server.arc_ref = new_arc_ref; server.arc_ref->refcount = 1; tmp_conf = server.conf; server.conf = new_conf; server.query_timeout_millis = server.conf->query_timeout_millis; pthread_mutex_unlock (&server.reconfig_lock); release_arc_ref (tmp_arc_ref); freeConfigStructure (tmp_conf); server.reconfig_signal = 0; } sleep (1); } return NULL; }