Official cwtch.im peer and server implementations. https://cwtch.im
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
1.7KB

  1. package event
  2. type queue struct {
  3. infChan infiniteChannel
  4. }
  5. type simpleQueue struct {
  6. eventChannel chan Event
  7. }
  8. // Queue is a wrapper around a channel for handling Events in a consistent way across subsystems.
  9. // The expectation is that each subsystem in Cwtch will manage a given an event.Queue fed from
  10. // the event.Manager.
  11. type Queue interface {
  12. InChan() chan<- Event
  13. OutChan() <-chan Event
  14. Next() *Event
  15. Shutdown()
  16. Len() int
  17. }
  18. // NewQueue initializes an event.Queue
  19. func NewQueue() Queue {
  20. queue := &queue{infChan: *newInfiniteChannel()}
  21. return queue
  22. }
  23. // NewSimpleQueue initializes an event.Queue of the given buffer size.
  24. func NewSimpleQueue(buffer int) Queue {
  25. queue := new(simpleQueue)
  26. queue.eventChannel = make(chan Event, buffer)
  27. return queue
  28. }
  29. func (sq *simpleQueue) InChan() chan<- Event {
  30. return sq.eventChannel
  31. }
  32. func (sq *simpleQueue) OutChan() <-chan Event {
  33. return sq.eventChannel
  34. }
  35. // Backlog returns the length of the queue backlog
  36. func (sq *simpleQueue) Len() int {
  37. return len(sq.eventChannel)
  38. }
  39. // Next returns the next available event from the front of the queue
  40. func (sq *simpleQueue) Next() *Event {
  41. event := <-sq.eventChannel
  42. return &event
  43. }
  44. // Shutdown closes our eventChannel
  45. func (sq *simpleQueue) Shutdown() {
  46. close(sq.eventChannel)
  47. }
  48. func (iq *queue) InChan() chan<- Event {
  49. return iq.infChan.In()
  50. }
  51. func (iq *queue) OutChan() <-chan Event {
  52. return iq.infChan.Out()
  53. }
  54. // Out returns the next available event from the front of the queue
  55. func (iq *queue) Next() *Event {
  56. event := <-iq.infChan.Out()
  57. return &event
  58. }
  59. func (iq *queue) Len() int {
  60. return iq.infChan.Len()
  61. }
  62. // Shutdown closes our eventChannel
  63. func (iq *queue) Shutdown() {
  64. iq.infChan.Close()
  65. }