/NET6/Hermod/Hermod/HTTP/ServerSentEvents/HTTPEvent.cs

https://github.com/Vanaheimr/Hermod · C# · 411 lines · 172 code · 106 blank · 133 comment · 20 complexity · 5ed14e2927040bae941c7781f51186eb MD5 · raw file

  1. /*
  2. * Copyright (c) 2010-2022, GraphDefined GmbH
  3. * Author: Achim Friedland <achim.friedland@graphdefined.com>
  4. * This file is part of Vanaheimr Hermod <https://www.github.com/Vanaheimr/Hermod>
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #region Usings
  19. using System;
  20. #endregion
  21. namespace org.GraphDefined.Vanaheimr.Hermod.HTTP
  22. {
  23. /// <summary>
  24. /// A single HTTP event.
  25. /// </summary>
  26. public class HTTPEvent<T> : IEquatable<HTTPEvent<T>>,
  27. IComparable<HTTPEvent<T>>,
  28. IComparable
  29. {
  30. #region Properties
  31. /// <summary>
  32. /// The identification of the event.
  33. /// </summary>
  34. public UInt64 Id { get; }
  35. /// <summary>
  36. /// The subevent identification of the event.
  37. /// </summary>
  38. public String Subevent { get; }
  39. /// <summary>
  40. /// The timestamp of the event.
  41. /// </summary>
  42. public DateTime Timestamp { get; }
  43. /// <summary>
  44. /// The attached data of the event.
  45. /// </summary>
  46. public T Data { get; }
  47. /// <summary>
  48. /// A text-representation of the attached data of the event.
  49. /// </summary>
  50. public String SerializedHeader { get; }
  51. /// <summary>
  52. /// A text-representation of the attached data of the event.
  53. /// </summary>
  54. public String SerializedData { get; }
  55. #endregion
  56. #region Constructor(s)
  57. #region HTTPEvent(Id, Data, SerializedData)
  58. /// <summary>
  59. /// Create a new HTTP event based on the given parameters.
  60. /// </summary>
  61. /// <param name="Id">The id of the event.</param>
  62. /// <param name="Data">The attached data of the event.</param>
  63. /// <param name="SerializedHeader">The HTTP SSE header of the attached data of the event.</param>
  64. /// <param name="SerializedData">A text-representation of the attached data of the event.</param>
  65. public HTTPEvent(UInt64 Id,
  66. T Data,
  67. String SerializedHeader,
  68. String SerializedData)
  69. : this(Id,
  70. Illias.Timestamp.Now,
  71. String.Empty,
  72. Data,
  73. SerializedHeader,
  74. SerializedData)
  75. { }
  76. #endregion
  77. #region HTTPEvent(Id, Timestamp, Data, SerializedData)
  78. /// <summary>
  79. /// Create a new HTTP event based on the given parameters.
  80. /// </summary>
  81. /// <param name="Id">The id of the event.</param>
  82. /// <param name="Timestamp">The timestamp of the event.</param>
  83. /// <param name="Data">The attached data of the event.</param>
  84. /// <param name="SerializedHeader">The HTTP SSE header of the attached data of the event.</param>
  85. /// <param name="SerializedData">A text-representation of the attached data of the event.</param>
  86. public HTTPEvent(UInt64 Id,
  87. DateTime Timestamp,
  88. T Data,
  89. String SerializedHeader,
  90. String SerializedData)
  91. : this(Id,
  92. Timestamp,
  93. String.Empty,
  94. Data,
  95. SerializedHeader,
  96. SerializedData)
  97. { }
  98. #endregion
  99. #region HTTPEvent(Id, Subevent, Data, SerializedData)
  100. /// <summary>
  101. /// Create a new HTTP event based on the given parameters.
  102. /// </summary>
  103. /// <param name="Id">The id of the event.</param>
  104. /// <param name="Subevent">The subevent.</param>
  105. /// <param name="Data">The attached data of the event.</param>
  106. /// <param name="SerializedHeader">The HTTP SSE header of the attached data of the event.</param>
  107. /// <param name="SerializedData">A text-representation of the attached data of the event.</param>
  108. public HTTPEvent(UInt64 Id,
  109. String Subevent,
  110. T Data,
  111. String SerializedHeader,
  112. String SerializedData)
  113. : this(Id,
  114. Illias.Timestamp.Now,
  115. Subevent,
  116. Data,
  117. SerializedHeader,
  118. SerializedData)
  119. { }
  120. #endregion
  121. #region HTTPEvent(Id, Timestamp, Subevent, Data, SerializedData)
  122. /// <summary>
  123. /// Create a new HTTP event based on the given parameters.
  124. /// </summary>
  125. /// <param name="Id">The id of the event.</param>
  126. /// <param name="Timestamp">The timestamp of the event.</param>
  127. /// <param name="Subevent">The subevent.</param>
  128. /// <param name="Data">The attached data of the event.</param>
  129. /// <param name="SerializedHeader">The HTTP SSE header of the attached data of the event.</param>
  130. /// <param name="SerializedData">A text-representation of the attached data of the event.</param>
  131. public HTTPEvent(UInt64 Id,
  132. DateTime Timestamp,
  133. String Subevent,
  134. T Data,
  135. String SerializedHeader,
  136. String SerializedData)
  137. {
  138. this.Id = Id;
  139. this.Timestamp = Timestamp;
  140. this.Subevent = Subevent?.Trim() ?? "";
  141. this.Data = Data;
  142. this.SerializedHeader = SerializedHeader;
  143. this.SerializedData = SerializedData;
  144. }
  145. #endregion
  146. #endregion
  147. #region Operator overloading
  148. #region Operator == (HTTPEvent1, HTTPEvent2)
  149. /// <summary>
  150. /// Compares two instances of this object.
  151. /// </summary>
  152. /// <param name="HTTPEvent1">A HTTP event.</param>
  153. /// <param name="HTTPEvent2">Another HTTP event.</param>
  154. /// <returns>true|false</returns>
  155. public static Boolean operator == (HTTPEvent<T> HTTPEvent1, HTTPEvent<T> HTTPEvent2)
  156. {
  157. // If both are null, or both are same instance, return true.
  158. if (Object.ReferenceEquals(HTTPEvent1, HTTPEvent2))
  159. return true;
  160. // If one is null, but not both, return false.
  161. if (((Object) HTTPEvent1 == null) || ((Object) HTTPEvent2 == null))
  162. return false;
  163. return HTTPEvent1.Equals(HTTPEvent2);
  164. }
  165. #endregion
  166. #region Operator != (HTTPEvent1, HTTPEvent2)
  167. /// <summary>
  168. /// Compares two instances of this object.
  169. /// </summary>
  170. /// <param name="HTTPEvent1">A HTTP event.</param>
  171. /// <param name="HTTPEvent2">Another HTTP event.</param>
  172. /// <returns>true|false</returns>
  173. public static Boolean operator != (HTTPEvent<T> HTTPEvent1, HTTPEvent<T> HTTPEvent2)
  174. => !(HTTPEvent1 == HTTPEvent2);
  175. #endregion
  176. #region Operator < (HTTPEvent1, HTTPEvent2)
  177. /// <summary>
  178. /// Compares two instances of this object.
  179. /// </summary>
  180. /// <param name="HTTPEvent1">A HTTP event.</param>
  181. /// <param name="HTTPEvent2">Another HTTP event.</param>
  182. /// <returns>true|false</returns>
  183. public static Boolean operator < (HTTPEvent<T> HTTPEvent1, HTTPEvent<T> HTTPEvent2)
  184. {
  185. if ((Object) HTTPEvent1 == null)
  186. throw new ArgumentNullException(nameof(HTTPEvent1), "The given HTTPEvent1 must not be null!");
  187. return HTTPEvent1.CompareTo(HTTPEvent2) < 0;
  188. }
  189. #endregion
  190. #region Operator <= (HTTPEvent1, HTTPEvent2)
  191. /// <summary>
  192. /// Compares two instances of this object.
  193. /// </summary>
  194. /// <param name="HTTPEvent1">A HTTP event.</param>
  195. /// <param name="HTTPEvent2">Another HTTP event.</param>
  196. /// <returns>true|false</returns>
  197. public static Boolean operator <= (HTTPEvent<T> HTTPEvent1, HTTPEvent<T> HTTPEvent2)
  198. => !(HTTPEvent1 > HTTPEvent2);
  199. #endregion
  200. #region Operator > (HTTPEvent1, HTTPEvent2)
  201. /// <summary>
  202. /// Compares two instances of this object.
  203. /// </summary>
  204. /// <param name="HTTPEvent1">A HTTP event.</param>
  205. /// <param name="HTTPEvent2">Another HTTP event.</param>
  206. /// <returns>true|false</returns>
  207. public static Boolean operator > (HTTPEvent<T> HTTPEvent1, HTTPEvent<T> HTTPEvent2)
  208. {
  209. if ((Object) HTTPEvent1 == null)
  210. throw new ArgumentNullException(nameof(HTTPEvent1), "The given HTTPEvent1 must not be null!");
  211. return HTTPEvent1.CompareTo(HTTPEvent2) > 0;
  212. }
  213. #endregion
  214. #region Operator >= (HTTPEvent1, HTTPEvent2)
  215. /// <summary>
  216. /// Compares two instances of this object.
  217. /// </summary>
  218. /// <param name="HTTPEvent1">A HTTP event.</param>
  219. /// <param name="HTTPEvent2">Another HTTP event.</param>
  220. /// <returns>true|false</returns>
  221. public static Boolean operator >= (HTTPEvent<T> HTTPEvent1, HTTPEvent<T> HTTPEvent2)
  222. => !(HTTPEvent1 < HTTPEvent2);
  223. #endregion
  224. #endregion
  225. #region IEquatable<HTTPEvent> Members
  226. #region Equals(Object)
  227. /// <summary>
  228. /// Compares two instances of this object.
  229. /// </summary>
  230. /// <param name="Object">An object to compare with.</param>
  231. /// <returns>true|false</returns>
  232. public override Boolean Equals(Object Object)
  233. {
  234. if (Object is null)
  235. return false;
  236. if (!(Object is HTTPEvent<T> HTTPEvent))
  237. return false;
  238. return Equals(HTTPEvent);
  239. }
  240. #endregion
  241. #region Equals(HTTPEvent)
  242. /// <summary>
  243. /// Compares two HTTP events for equality.
  244. /// </summary>
  245. /// <param name="HTTPEvent">A HTTP event to compare with.</param>
  246. /// <returns>True if both match; False otherwise.</returns>
  247. public Boolean Equals(HTTPEvent<T> HTTPEvent)
  248. {
  249. if (HTTPEvent is null)
  250. return false;
  251. return Id.Equals(HTTPEvent.Id);
  252. }
  253. #endregion
  254. #endregion
  255. #region IComparable<HTTPEvent> Members
  256. #region CompareTo(Object)
  257. /// <summary>
  258. /// Compares two instances of this object.
  259. /// </summary>
  260. /// <param name="Object">An object to compare with.</param>
  261. public Int32 CompareTo(Object Object)
  262. {
  263. if (Object is null)
  264. throw new ArgumentNullException("The given object must not be null!");
  265. if (!(Object is HTTPEvent<T> HTTPEvent))
  266. throw new ArgumentException("The given object is not a HTTP event!");
  267. return CompareTo(HTTPEvent);
  268. }
  269. #endregion
  270. #region CompareTo(OtherHTTPEvent)
  271. /// <summary>
  272. /// Compares two instances of this object.
  273. /// </summary>
  274. /// <param name="HTTPEvent">Another HTTP event.</param>
  275. public Int32 CompareTo(HTTPEvent<T> HTTPEvent)
  276. {
  277. if (HTTPEvent is null)
  278. throw new ArgumentNullException(nameof(HTTPEvent), "The given HTTP event must not be null!");
  279. return Id.CompareTo(HTTPEvent.Id);
  280. }
  281. #endregion
  282. #endregion
  283. #region GetHashCode()
  284. /// <summary>
  285. /// Return the HashCode of this object.
  286. /// </summary>
  287. /// <returns>The HashCode of this object.</returns>
  288. public override Int32 GetHashCode()
  289. {
  290. unchecked
  291. {
  292. return Id. GetHashCode() * 3 ^
  293. Subevent.GetHashCode();
  294. }
  295. }
  296. #endregion
  297. #region (override) ToString()
  298. /// <summary>
  299. /// Returns a text representation of this object.
  300. /// </summary>
  301. public override String ToString()
  302. => SerializedData;
  303. #endregion
  304. }
  305. }