Queue.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace EVCB_OCPP.Packet.Messages.Basic
  7. {
  8. public class Queue
  9. {
  10. //public Dictionary<string, Type> requestQueue;
  11. public Dictionary<string, IRequest> requestQueue;
  12. public Queue()
  13. {
  14. //requestQueue = new Dictionary<string, Type>();
  15. requestQueue = new Dictionary<string, IRequest>();
  16. }
  17. /// <summary>
  18. /// Store a {Request} and get a unique identifier to fetch it later on.
  19. /// param request: the {Request}.
  20. /// return: a unique identifier used to fetch the request.
  21. /// </summary>
  22. //public String store(Type request)
  23. public string store(IRequest request)
  24. {
  25. string UniqueId = Guid.NewGuid().ToString();
  26. requestQueue.Add(UniqueId, request);
  27. return UniqueId;
  28. }
  29. /// <summary>
  30. /// Store a {Request} and get a unique identifier to fetch it later on.
  31. /// param request: the {Request}.
  32. /// return: a unique identifier used to fetch the request.
  33. /// </summary>
  34. //public String store(Type request)
  35. public void store(IRequest request, string UniqueId)
  36. {
  37. requestQueue.Add(UniqueId, request);
  38. }
  39. /// <summary>
  40. /// Restore a {Request} using a unique identifier.
  41. /// The identifier can only be used once.
  42. /// If no Request was found, null is returned.
  43. /// param ticket: unique identifier returned when {Request} was initially stored.
  44. /// return: the stored {Request}
  45. /// </summary>
  46. //public Type restoreRequest(String ticket)
  47. public IRequest RestoreRequest(string UniqueId)
  48. {
  49. //Type request = null;
  50. IRequest request = null;
  51. try
  52. {
  53. if(requestQueue.ContainsKey(UniqueId))
  54. request = requestQueue[UniqueId];
  55. requestQueue.Remove(UniqueId);
  56. }
  57. catch (Exception ex)
  58. {
  59. throw ex;
  60. }
  61. return request;
  62. }
  63. }
  64. }