/// <summary> /// Set the data for the node of the given path if such a node exists and the /// given version matches the version of the node (if the given version is /// -1, it matches any node's versions). Return the stat of the node. /// /// This operation, if successful, will trigger all the watches on the node /// of the given path left by getData calls. /// /// A KeeperException with error code KeeperException.NoNode will be thrown /// if no node with the given path exists. /// /// A KeeperException with error code KeeperException.BadVersion will be /// thrown if the given version does not match the node's version. /// /// The maximum allowable size of the data array is 1 MB (1,048,576 bytes). /// Arrays larger than this will cause a KeeperExecption to be thrown. /// @param path /// the path of the node /// @param data /// the data to set /// @param version /// the expected matching version /// @return the state of the node /// @throws InterruptedException If the server transaction is interrupted. /// @throws KeeperException If the server signals an error with a non-zero error code. /// @throws IllegalArgumentException if an invalid path is specified /// </summary> public Stat SetData(string path, byte[] data, int version) { string clientPath = path; PathUtils.ValidatePath(clientPath); string serverPath = PrependChroot(clientPath); RequestHeader h = new RequestHeader(); h.Type = (int)OpCode.SetData; SetDataRequest request = new SetDataRequest(); request.Path = serverPath; request.Data = data; request.Version = version; SetDataResponse response = new SetDataResponse(); ReplyHeader r = cnxn.SubmitRequest(h, request, response, null); if (r.Err != 0) { throw KeeperException.Create((KeeperException.Code)Enum.ToObject(typeof(KeeperException.Code), r.Err), clientPath); } return(response.Stat); }
/// <summary> /// Return the stat of the node of the given path. Return null if no such a /// node exists. /// /// If the watch is non-null and the call is successful (no exception is thrown), /// a watch will be left on the node with the given path. The watch will be /// triggered by a successful operation that creates/delete the node or sets /// the data on the node. /// </summary> /// <param name="path">The path.</param> /// <param name="watcher">The watcher.</param> /// <returns>the stat of the node of the given path; return null if no such a node exists.</returns> public Stat Exists(string path, IWatcher watcher) { string clientPath = path; PathUtils.ValidatePath(clientPath); // the watch contains the un-chroot path WatchRegistration wcb = null; if (watcher != null) { wcb = new ExistsWatchRegistration(watchManager, watcher, clientPath); } string serverPath = PrependChroot(clientPath); RequestHeader h = new RequestHeader(); h.Type = (int)OpCode.Exists; ExistsRequest request = new ExistsRequest(serverPath, watcher != null); SetDataResponse response = new SetDataResponse(); ReplyHeader r = cnxn.SubmitRequest(h, request, response, wcb); if (r.Err != 0) { if (r.Err == (int)KeeperException.Code.NONODE) { return(null); } throw KeeperException.Create((KeeperException.Code)Enum.ToObject(typeof(KeeperException.Code), r.Err), clientPath); } return(response.Stat.Czxid == -1 ? null : response.Stat); }
public static SetDataResponse create(JObject jObject) { JProperty handshakeProperty = jObject.Property("handshake"); JProperty valuesProperty = jObject.Property("values"); if (handshakeProperty == null) { return(null); } if (valuesProperty == null) { return(null); } JToken responseTypeProperty = valuesProperty.Value["responseType"]; JToken keyProperty = valuesProperty.Value["key"]; JToken valueProperty = valuesProperty.Value["value"]; JToken simIdProperty = valuesProperty.Value["simId"]; JToken errorProperty = valuesProperty.Value["error"]; // Check all properties are present and valid if (responseTypeProperty == null && responseTypeProperty.Type == JTokenType.String) { return(null); } if (keyProperty == null && keyProperty.Type == JTokenType.String) { return(null); } if (valueProperty == null && valueProperty.Type == JTokenType.String) { return(null); } if (simIdProperty == null && simIdProperty.Type == JTokenType.String) { return(null); } SetDataResponse setDataResponse = new SetDataResponse(); setDataResponse.handshake = handshakeProperty.Value.ToObject <SimCapiHandshake>(); setDataResponse.responseType = responseTypeProperty.ToObject <string>(); setDataResponse.key = keyProperty.ToObject <string>(); setDataResponse.value = valueProperty.ToObject <string>(); setDataResponse.simId = simIdProperty.ToObject <string>(); setDataResponse.error = "No Error Present"; if (errorProperty != null && errorProperty.Type == JTokenType.String) { setDataResponse.error = errorProperty.ToObject <string>(); } return(setDataResponse); }
public override void WriteObject(XmlWriter writer, object graph) { SetDataResponse SetDataResponseField = ((SetDataResponse)(graph)); if (WriteParentElement(writer, true, true, graph)) { writer.WriteEndElement(); } return; }
public override object ReadObject(XmlReader reader) { SetDataResponse SetDataResponseField = null; if (IsParentStartElement(reader, false, true)) { SetDataResponseField = new SetDataResponse(); reader.Read(); reader.ReadEndElement(); } return(SetDataResponseField); }
public void deserialize(InputArchive archive, string tag) { results = new List <OpResult>(); archive.startRecord(tag); MultiHeader h = new MultiHeader(); ((Record)h).deserialize(archive, tag); while (!h.getDone()) { ZooDefs.OpCode opcode = EnumUtil <ZooDefs.OpCode> .DefinedCast(h.get_Type()); switch (opcode) { case ZooDefs.OpCode.create: CreateResponse cr = new CreateResponse(); ((Record)cr).deserialize(archive, tag); results.Add(new OpResult.CreateResult(cr.getPath())); break; case ZooDefs.OpCode.delete: results.Add(new OpResult.DeleteResult()); break; case ZooDefs.OpCode.setData: SetDataResponse sdr = new SetDataResponse(); ((Record)sdr).deserialize(archive, tag); results.Add(new OpResult.SetDataResult(sdr.getStat())); break; case ZooDefs.OpCode.check: results.Add(new OpResult.CheckResult()); break; case ZooDefs.OpCode.error: //FIXME: need way to more cleanly serialize/deserialize exceptions ErrorResponse er = new ErrorResponse(); ((Record)er).deserialize(archive, tag); results.Add(new OpResult.ErrorResult(er.getErr())); break; default: throw new IOException("Invalid type " + h.get_Type() + " in MultiResponse"); } ((Record)h).deserialize(archive, tag); } archive.endRecord(tag); }
private async Task <Stat> SetDataAsyncInternal(string path, byte[] data, int version, bool sync) { string clientPath = path; PathUtils.ValidatePath(clientPath); string serverPath = PrependChroot(clientPath); RequestHeader h = new RequestHeader(); h.Type = (int)OpCode.SetData; SetDataRequest request = new SetDataRequest(serverPath, data, version); SetDataResponse response = new SetDataResponse(); ReplyHeader r = cnxn.SubmitRequest(h, request, response, null); if (r.Err != 0) { throw KeeperException.Create((KeeperException.Code)Enum.ToObject(typeof(KeeperException.Code), r.Err), clientPath); } return(response.Stat); }
public override object ReadObject(XmlReader reader) { SetDataResponse SetDataResponseField = null; if (IsParentStartElement(reader, false, true)) { SetDataResponseField = new SetDataResponse(); reader.Read(); reader.ReadEndElement(); } return SetDataResponseField; }
public static System.Object deserialize(string jsonString, ref SimCapiMessageType messageType) { JObject jObject; try { jObject = JsonConvert.DeserializeObject <JObject>(jsonString); } catch (System.Exception) { return(null); } JProperty type = jObject.Property("type"); if (type == null) { throw new System.Exception("Invalid message structure"); } JToken typeValue = jObject["type"]; if (typeValue.Type != JTokenType.Integer) { throw new System.Exception("type property should be an Integer"); } messageType = typeValue.ToObject <SimCapiMessageType>(); switch (messageType) { case SimCapiMessageType.HANDSHAKE_RESPONSE: return(HandshakeResponse.create(jObject)); case SimCapiMessageType.VALUE_CHANGE: return(ValueChange.create(jObject)); case SimCapiMessageType.CONFIG_CHANGE: return(ConfigChange.create(jObject)); case SimCapiMessageType.VALUE_CHANGE_REQUEST: return(ValueChangeRequest.create(jObject)); case SimCapiMessageType.CHECK_COMPLETE_RESPONSE: return(CheckCompleteResponse.create(jObject)); case SimCapiMessageType.CHECK_START_RESPONSE: return(CheckStartResponse.create(jObject)); case SimCapiMessageType.GET_DATA_RESPONSE: return(GetDataResponse.create(jObject)); case SimCapiMessageType.SET_DATA_RESPONSE: return(SetDataResponse.create(jObject)); case SimCapiMessageType.API_CALL_RESPONSE: return(null); case SimCapiMessageType.INITIAL_SETUP_COMPLETE: return(InitialSetupComplete.create(jObject)); case SimCapiMessageType.RESIZE_PARENT_CONTAINER_RESPONSE: return(ResizeParentContainerResponse.create(jObject)); case SimCapiMessageType.ALLOW_INTERNAL_ACCESS: return(AllowInternalAccess.create(jObject)); case SimCapiMessageType.REGISTERED_LOCAL_DATA_CHANGED: return(RegisteredLocalDataChanged.create(jObject)); } throw new System.Exception("Invalid message type"); }