//--------------------------------------------------------------------- public void subscribeExists(string path, zkOpeHandler handler = null, Dictionary <string, object> param = null) { if (path != null && path != "" && !_existsListener.ContainsKey(path)) { EbLog.Note("Subscribed Exists changes for:" + path); zkHandlerParam hp = new zkHandlerParam(); hp.handler = handler; hp.param = param; _existsListener.Add(path, hp); aexists(path, true, handler, param); } }
//--------------------------------------------------------------------- public void subscribeChildChanges(string path, zkOpeHandler handler = null, Dictionary <string, object> param = null) { if (path != null && path != "" && !_childListener.ContainsKey(path)) { zkHandlerParam hp = new zkHandlerParam(); hp.handler = handler; hp.param = param; _childListener.Add(path, hp); awatchForChilds(path, handler, param); EbLog.Note("Subscribed child changes for:" + path); } }
//--------------------------------------------------------------------- public int awriteData(string path, string data, zkOpeHandler handler = null, Dictionary <string, object> param = null) { int id = 0; if (handler != null) { id = zookeeper.generateId(); zkHandlerParam hp = new zkHandlerParam(); hp.handler = handler; hp.param = param; _handlerDic.TryAdd(id, hp); } return(mConnection.awriteData(path, data, _setCompletion, id)); }
//--------------------------------------------------------------------- public int aexists(string path, bool watch, zkOpeHandler handler = null, Dictionary <string, object> param = null) { int id = 0; if (handler != null) { id = zookeeper.generateId(); zkHandlerParam hp = new zkHandlerParam(); hp.handler = handler; hp.param = param; _handlerDic.TryAdd(id, hp); } return(mConnection.aexists(path, watch, _existsCompletion, id)); }
//--------------------------------------------------------------------- public int acreate(string path, string data, int mode, zkOpeHandler handler = null, Dictionary <string, object> param = null) { if (path == null || data == null) { EbLog.Error("path must not be null."); return(-1); } EbLog.Note("acreate node :" + path); int id = 0; if (handler != null) { id = zookeeper.generateId(); zkHandlerParam hp = new zkHandlerParam(); hp.handler = handler; hp.param = param; _handlerDic.TryAdd(id, hp); } return(mConnection.acreate(path, data, mode, _createCompletion, id)); }
//--------------------------------------------------------------------- public void update() { // 处理process消息. while (_events.Count > 0) { ResponseEvent response; _events.TryDequeue(out response); if (null == response) { continue; } //if (response.returnCode != (int)Zk.ZOO_ERRORS.ZOK) if (response.id <= 0) { EbLog.Note("Zk.ZkClient.update ,path:" + response.id + ",Reason:" + ((Zk.ZOO_ERRORS)response.returnCode).ToString()); continue; } zkHandlerParam hp = null; //_handlerDic.TryGetValue(response.id, out handler); _handlerDic.TryRemove(response.id, out hp); if (null == hp || null == hp.handler) { EbLog.Error("zk update error , hp or handler is null !"); continue; } int eventId = _eventId++; EbLog.Note("Delivering event #" + eventId + ",type:" + ((ZOO_OPE)(response.opeType)).ToString() + ",id:" + response.id); switch ((ZOO_OPE)response.opeType) { case ZOO_OPE.ZOO_CREATE_OP: //onOpeResult.onCreated(response.path, response.data, response.returnCode); hp.handler(response.returnCode, response.data, null, hp.param); break; case ZOO_OPE.ZOO_DELETE_OP: //onOpeResult.onDeleted(response.path, response.returnCode); hp.handler(response.returnCode, null, null, hp.param); break; case ZOO_OPE.ZOO_EXISTS_OP: //onOpeResult.onExists(response.path, response.returnCode); hp.handler(response.returnCode, null, null, hp.param); break; case ZOO_OPE.ZOO_GETDATA_OP: //onOpeResult.onGet(response.path, response.returnCode, response.data); hp.handler(response.returnCode, response.data, null, hp.param); break; case ZOO_OPE.ZOO_SETDATA_OP: //onOpeResult.onSet(response.path, response.returnCode); hp.handler(response.returnCode, null, null, hp.param); break; case ZOO_OPE.ZOO_GETCHILDREN_OP: //onOpeResult.onGetChildren(response.path, response.returnCode, response.children); hp.handler(response.returnCode, null, response.children, hp.param); break; } EbLog.Note("Delivering event #" + eventId + " done"); } while (_watchEvents.Count > 0) { WatchedEvent wcEvent; _watchEvents.TryDequeue(out wcEvent); if (null == wcEvent) { continue; } int eventId = _eventId++; EbLog.Note("Delivering event #" + eventId + "," + wcEvent); //if(null != onOpeResult) //{ foreach (var func in _handlerList) { // 如果已经处理了,就不用遍历了. if (func(wcEvent)) { break; } } //onOpeResult.handler(wcEvent); //} EbLog.Note("Delivering event #" + eventId + " done"); } }