void watcher(zhandle_t *zzh, int type, int state, const char *path, void* context) { /* Be careful using zh here rather than zzh - as this may be mt code * the client lib may call the watcher before zookeeper_init returns */ fprintf(stderr, "Watcher %s state = %s", type2String(type), state2String(state)); if (path && strlen(path) > 0) { fprintf(stderr, " for path %s", path); } fprintf(stderr, "\n"); if (type == ZOO_SESSION_EVENT) { if (state == ZOO_CONNECTED_STATE) { const clientid_t *id = zoo_client_id(zzh); if (myid.client_id == 0 || myid.client_id != id->client_id) { myid = *id; fprintf(stderr, "Got a new session id: 0x%llx\n", _LL_CAST_ myid.client_id); if (clientIdFile) { FILE *fh = fopen(clientIdFile, "w"); if (!fh) { perror(clientIdFile); } else { int rc = fwrite(&myid, sizeof(myid), 1, fh); if (rc != sizeof(myid)) { perror("writing client id"); } fclose(fh); } } } } else if (state == ZOO_AUTH_FAILED_STATE) { fprintf(stderr, "Authentication failure. Shutting down...\n"); zookeeper_close(zzh); shutdownThisThing=1; zh=0; } else if (state == ZOO_EXPIRED_SESSION_STATE) { fprintf(stderr, "Session expired. Shutting down...\n"); zookeeper_close(zzh); shutdownThisThing=1; zh=0; } } }
Species::Species(const std::string& _name, SPECIES_TYPE t) : name( _name ), maxAge( 0 ), maxHealth( 0), maxSpeed( 0 ), Reach( 0 ), resistance( 0 ), breedingSpeed( 0 ), type( t ), foodRequirement( 0 ), waterRequirement( 0 ), optimalTemperature( 0 ), ImageSetName(""), Frame( t ) { std::string typestring = type2String( type ); Reach = Engine::getCfg()->get<float>("sim.species.defaults.reach") * Engine::getCfg()->get<float>("sim.species.defaults.reach." + typestring); breedingSpeed = Engine::getCfg()->get<float>("sim.species.defaults.breedingSpeed") * Engine::getCfg()->get<int>("sim.species.defaults.breedingSpeed." + typestring); //Engine::out() << "species!" << std::endl; maxAge = Engine::getCfg()->get<int>("sim.species.defaults.maxAge"); maxHealth = Engine::getCfg()->get<int>("sim.species.defaults.maxHealth"); maxSpeed = Engine::getCfg()->get<float>("sim.species.defaults.maxSpeed") * Engine::getCfg()->get<int>("sim.species.defaults.maxSpeed." + typestring); resistance = Engine::getCfg()->get<float>("sim.species.defaults.resistance"); maxRegeneration = Engine::getCfg()->get<float>("sim.species.defaults.maxRegeneration") * Engine::getCfg()->get<float>("sim.species.defaults.maxRegeneration." + typestring); foodRequirement = Engine::getCfg()->get<float>("sim.species.defaults.foodRequirement") * Engine::getCfg()->get<float>("sim.species.defaults.foodRequirement." + typestring); waterRequirement = Engine::getCfg()->get<float>("sim.species.defaults.waterRequirement") * Engine::getCfg()->get<float>("sim.species.defaults.waterRequirement." + typestring); optimalTemperature = Engine::getCfg()->get<int>("sim.species.defaults.optimalTemperature"); ImageSetName = Engine::getCfg()->get<std::string>("system.renderer.creatureImageSet"); }
// Called when there is some state change wrt zk handle // Note:- This is called under the context of the zk thread // So be sure that anything that you do is threadsafe void ZKClient::GlobalWatcher(zhandle_t* _zh, sp_int32 _type, sp_int32 _state, const char* _path) { // Be careful using zk_handler_ here rather than _zzh; // the client lib may call the watcher before zookeeper_init returns LOG(INFO) << "ZKClient GlobalWatcher called with type " << type2String(_type) << " and state " << state2String(_state); if (_path && strlen(_path) > 0) { LOG(INFO) << " for path " << _path; } if (_type == ZOO_SESSION_EVENT) { if (_state == ZOO_CONNECTED_STATE) { const clientid_t* id = zoo_client_id(_zh); if (zk_clientid_.client_id == 0 || zk_clientid_.client_id != id->client_id) { zk_clientid_ = *id; LOG(INFO) << "Got a new session id: " << zk_clientid_.client_id << "\n"; } } if (_state == ZOO_AUTH_FAILED_STATE) { LOG(FATAL) << "ZKClient Authentication failure. Shutting down...\n"; } else if (_state == ZOO_EXPIRED_SESSION_STATE) { // If client watcher is set, notify it about the session expiry // instead of shutting down. if (client_global_watcher_cb_) { const ZkWatchEvent event = {_type, _state, _path}; SendWatchEvent(event); } else { // We need to close and re-establish // There are watches, etc that need to be set again. // So the simpler option here is to kill ourselves LOG(FATAL) << "Session expired. Shutting down...\n"; } } else if (_state == ZOO_CONNECTING_STATE) { // We are still in the process of connecting LOG(INFO) << "Re-connecting to the zookeeper\n"; } else if (_state == ZOO_ASSOCIATING_STATE) { // Connection process still ongoing } } }
void Cluster::OnInitWatcher(zhandle_t *zzh, int type, int state, const char *path) { INFO_LOG("Watcher %s state = %s for path %s", type2String(type), state2String(state), (path && strlen(path) > 0) ? path : ""); if (type == ZOO_SESSION_EVENT) { if (state == ZOO_CONNECTED_STATE) { const clientid_t *id = zoo_client_id(zzh); if (m_zk_clientid.client_id == 0 || m_zk_clientid.client_id != id->client_id) { m_zk_clientid = *id; INFO_LOG("Got a new session id: 0x%llx", (long long )m_zk_clientid.client_id); if (!g_db->GetConf().zk_clientid_file.empty()) { std::string content; content.assign((const char*) (&m_zk_clientid), sizeof(m_zk_clientid)); file_write_content(g_db->GetConf().zk_clientid_file, content); } } m_state = ZK_STATE_CONNECTED; } else if (state == ZOO_AUTH_FAILED_STATE) { WARN_LOG("Zookeeper server authentication failure. Shutting down..."); zookeeper_close(zzh); m_zk = NULL; m_state = ZK_STATE_DISCONNECT; } else if (state == ZOO_EXPIRED_SESSION_STATE) { WARN_LOG("Session expired. Shutting down..."); zookeeper_close(zzh); m_zk = NULL; m_state = ZK_STATE_DISCONNECT; } } }