Kinetica   C#   API  Version 7.2.3.0
Protocol.cs
Go to the documentation of this file.
1 
18 using System;
19 using System.Collections.Generic;
20 using System.Linq;
21 using System.Text;
22 using Newtonsoft.Json;
23 using Newtonsoft.Json.Linq;
24 
25 namespace Avro
26 {
27  public class Protocol
28  {
32  public string Name { get; set; }
33 
37  public string Namespace { get; set; }
38 
42  public string Doc { get; set; }
43 
47  public IList<Schema> Types { get; set; }
48 
52  public IDictionary<string,Message> Messages { get; set; }
53 
54  private byte[] md5;
55  public byte[] MD5
56  {
57  get
58  {
59  try
60  {
61  if (md5 == null)
62  md5 = System.Security.Cryptography.MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(ToString()));
63  }
64  catch (Exception ex)
65  {
66  throw new AvroRuntimeException("MD5 get exception", ex);
67  }
68  return md5;
69  }
70  }
71 
80  public Protocol(string name, string space,
81  string doc, IEnumerable<Schema> types,
82  IDictionary<string,Message> messages)
83  {
84  if (string.IsNullOrEmpty(name)) throw new ArgumentNullException("name", "name cannot be null.");
85  if (null == types) throw new ArgumentNullException("types", "types cannot be null.");
86  if (null == messages) throw new ArgumentNullException("messages", "messages cannot be null.");
87 
88  this.Name = name;
89  this.Namespace = space;
90  this.Doc = doc;
91  this.Types = new List<Schema>(types);
92  this.Messages = new Dictionary<string, Message>(messages);
93  }
94 
100  public static Protocol Parse(string jstring)
101  {
102  if (string.IsNullOrEmpty(jstring)) throw new ArgumentNullException("json", "json cannot be null.");
103 
104  JToken jtok = null;
105  try
106  {
107  jtok = JObject.Parse(jstring);
108  }
109  catch (Exception ex)
110  {
111  throw new ProtocolParseException("Invalid JSON format: " + jstring, ex);
112  }
113  return Parse(jtok);
114  }
115 
121  private static Protocol Parse(JToken jtok)
122  {
123  string name = JsonHelper.GetRequiredString(jtok, "protocol");
124  string space = JsonHelper.GetOptionalString(jtok, "namespace");
125  string doc = JsonHelper.GetOptionalString(jtok, "doc");
126 
127  var names = new SchemaNames();
128 
129  JToken jtypes = jtok["types"];
130  var types = new List<Schema>();
131  if (jtypes is JArray)
132  {
133  foreach (JToken jtype in jtypes)
134  {
135  var schema = Schema.ParseJson(jtype, names, space);
136  types.Add(schema);
137  }
138  }
139 
140  var messages = new Dictionary<string,Message>();
141  JToken jmessages = jtok["messages"];
142  if (null != jmessages)
143  {
144  foreach (JProperty jmessage in jmessages)
145  {
146  var message = Message.Parse(jmessage, names, space);
147  messages.Add(message.Name, message);
148  }
149  }
150 
151  return new Protocol(name, space, doc, types, messages);
152  }
153 
158  public override string ToString()
159  {
160  using (System.IO.StringWriter sw = new System.IO.StringWriter())
161  {
162  using (Newtonsoft.Json.JsonTextWriter writer = new Newtonsoft.Json.JsonTextWriter(sw))
163  {
164  #if(DEBUG)
165  writer.Formatting = Newtonsoft.Json.Formatting.Indented;
166  #endif
167 
168  WriteJson(writer, new SchemaNames());
169  writer.Flush();
170  return sw.ToString();
171  }
172  }
173  }
174 
180  internal void WriteJson(Newtonsoft.Json.JsonTextWriter writer, SchemaNames names)
181  {
182  writer.WriteStartObject();
183 
184  JsonHelper.writeIfNotNullOrEmpty(writer, "protocol", this.Name);
185  JsonHelper.writeIfNotNullOrEmpty(writer, "namespace", this.Namespace);
186  JsonHelper.writeIfNotNullOrEmpty(writer, "doc", this.Doc);
187 
188  writer.WritePropertyName("types");
189  writer.WriteStartArray();
190 
191  foreach (Schema type in this.Types)
192  type.WriteJson(writer, names, this.Namespace);
193 
194  writer.WriteEndArray();
195 
196  writer.WritePropertyName("messages");
197  writer.WriteStartObject();
198 
199  foreach (KeyValuePair<string,Message> message in this.Messages)
200  {
201  writer.WritePropertyName(message.Key);
202  message.Value.writeJson(writer, names, this.Namespace);
203  }
204 
205  writer.WriteEndObject();
206  writer.WriteEndObject();
207  }
208 
214  public override bool Equals(object obj)
215  {
216  if (obj == this) return true;
217  if (!(obj is Protocol)) return false;
218 
219  Protocol that = obj as Protocol;
220 
221  return this.Name.Equals(that.Name) && this.Namespace.Equals(that.Namespace) &&
222  TypesEquals(that.Types) && MessagesEquals(that.Messages);
223  }
224 
232  private bool TypesEquals(IList<Schema> that)
233  {
234  if (Types.Count != that.Count) return false;
235  foreach (Schema schema in Types)
236  if (!that.Contains(schema)) return false;
237  return true;
238  }
239 
247  private bool MessagesEquals(IDictionary<string, Message> that)
248  {
249  if (Messages.Count != that.Count) return false;
250  foreach (KeyValuePair<string, Message> pair in Messages)
251  {
252  if (!that.ContainsKey(pair.Key))
253  return false;
254  if (!pair.Value.Equals(that[pair.Key]))
255  return false;
256  }
257  return true;
258  }
259 
264  public override int GetHashCode()
265  {
266  return Name.GetHashCode() + Namespace.GetHashCode() +
267  GetTypesHashCode() + GetMessagesHashCode();
268  }
269 
274  private int GetTypesHashCode()
275  {
276  int hash = Types.Count;
277  foreach (Schema schema in Types)
278  hash += schema.GetHashCode();
279  return hash;
280  }
281 
286  private int GetMessagesHashCode()
287  {
288  int hash = Messages.Count;
289  foreach (KeyValuePair<string, Message> pair in Messages)
290  hash += (pair.Key.GetHashCode() + pair.Value.GetHashCode());
291  return hash;
292  }
293  }
294 }
override int GetHashCode()
Hash code function
Definition: Schema.cs:272
IList< Schema > Types
List of schemas objects representing the different schemas defined under the 'types' attribute
Definition: Protocol.cs:47
Base class for all schema types
Definition: Schema.cs:29
string Doc
Documentation for the protocol
Definition: Protocol.cs:42
string Name
Name of the protocol
Definition: Protocol.cs:32
static string GetRequiredString(JToken jtok, string field)
Retrieves the required string property value for the given property name from the given JSON object.
Definition: JsonHelper.cs:57
virtual internal void WriteJson(JsonTextWriter writer, SchemaNames names, string encspace)
Writes schema object in JSON format
Definition: Schema.cs:248
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.
A class that contains a list of named schemas.
Definition: SchemaName.cs:146
static string GetOptionalString(JToken jtok, string field)
Retrieves the optional string property value for the given property name from the given JSON object.
Definition: JsonHelper.cs:35
override int GetHashCode()
Returns the hash code of this protocol object
Definition: Protocol.cs:264
override string ToString()
Writes Protocol in JSON format
Definition: Protocol.cs:158
static Protocol Parse(string jstring)
Parses the given JSON string to create a Protocol object
Definition: Protocol.cs:100
Protocol(string name, string space, string doc, IEnumerable< Schema > types, IDictionary< string, Message > messages)
Constructor for Protocol class
Definition: Protocol.cs:80
IDictionary< string, Message > Messages
List of message objects representing the different schemas defined under the 'messages' attribute
Definition: Protocol.cs:52
string Namespace
Namespace of the protocol
Definition: Protocol.cs:37
override bool Equals(object obj)
Tests equality of this protocol object with the passed object
Definition: Protocol.cs:214
byte [] MD5
Definition: Protocol.cs:56