destinations.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package worker
  2. import (
  3. "errors"
  4. "fmt"
  5. "wkla.no-ip.biz/gogs/Willie/MsgService/MessageService/model"
  6. )
  7. //DestinationProcessor fpr every destination there must be a processor to do the work
  8. type DestinationProcessor interface {
  9. //Initialise this procssor
  10. Initialise(backend string, destination model.Destination) error
  11. //Destroy this processor
  12. Destroy(backend string, destination model.Destination) error
  13. //Store do the right storage
  14. Store(data model.JSONMap) (string, error)
  15. }
  16. //NullDestinationProcessor does nothing
  17. type NullDestinationProcessor struct {
  18. }
  19. //Initialise do nothing on initialise
  20. func (n *NullDestinationProcessor) Initialise(backend string, destination model.Destination) error {
  21. return nil
  22. }
  23. //Destroy do nothing on initialise
  24. func (n *NullDestinationProcessor) Destroy(backend string, destination model.Destination) error {
  25. return nil
  26. }
  27. //Store do nothing on store
  28. func (n *NullDestinationProcessor) Store(data model.JSONMap) (string, error) {
  29. return "noId", nil
  30. }
  31. //ErrDestinationNotFound the destination was not found in this system
  32. var ErrDestinationNotFound = errors.New("Missing destination")
  33. //DestinationList list type
  34. type DestinationList struct {
  35. destinations map[string]model.Destination
  36. processors map[string]DestinationProcessor
  37. }
  38. func GetNewDestinationProcessor(backend string, destination model.Destination) (DestinationProcessor, error) {
  39. switch destination.Type {
  40. case "mqtt":
  41. return CreateMQTTDestinationProcessor(backend, destination)
  42. case "null":
  43. return &NullDestinationProcessor{}, nil
  44. default:
  45. return &NullDestinationProcessor{}, nil
  46. }
  47. }
  48. //Destinations List off all registered destinations
  49. var Destinations = DestinationList{
  50. destinations: make(map[string]model.Destination),
  51. }
  52. //Register registering a new destination under the right name
  53. func (d *DestinationList) Register(backendName string, destination model.Destination) error {
  54. destinationNsName := GetDestinationNsName(backendName, destination.Name)
  55. d.destinations[destinationNsName] = destination
  56. return nil
  57. }
  58. //Deregister deregistering a new destination with a name
  59. func (d *DestinationList) Deregister(backendName string, destination model.Destination) error {
  60. destinationNsName := GetDestinationNsName(backendName, destination.Name)
  61. // getting the processor for this
  62. processor, ok := d.processors[destinationNsName]
  63. if ok {
  64. err := processor.Destroy(backendName, destination)
  65. if err != nil {
  66. return err
  67. }
  68. delete(d.processors, destinationNsName)
  69. }
  70. // removing the destination from the list
  71. delete(d.destinations, destinationNsName)
  72. return nil
  73. }
  74. //Store storing a message into the desired destination
  75. func (d *DestinationList) Store(backendName string, destinationName string, data model.JSONMap) error {
  76. destinationNsName := GetDestinationNsName(backendName, destinationName)
  77. destination, ok := d.destinations[destinationNsName]
  78. if !ok {
  79. return ErrDestinationNotFound
  80. }
  81. var processor DestinationProcessor
  82. processor, ok = d.processors[destinationName]
  83. if !ok {
  84. var err error
  85. processor, err = GetNewDestinationProcessor(backendName, destination)
  86. if err != nil {
  87. return err
  88. }
  89. }
  90. _, err := processor.Store(data)
  91. if err != nil {
  92. return err
  93. }
  94. //log.Infof("store object in destination %s as %s", destination, id)
  95. return nil
  96. }
  97. //GetDestinationNsName getting the unique name of a backend destination
  98. func GetDestinationNsName(backendName, destinationName string) string {
  99. return fmt.Sprintf("%s.%s", backendName, destinationName)
  100. }