plugin.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package plugins
  2. import (
  3. "github.com/coredhcp/coredhcp/handler"
  4. "github.com/coredhcp/coredhcp/logger"
  5. )
  6. var log = logger.GetLogger()
  7. // Plugin represents a plugin object.
  8. // Setup6 and Setup4 are the setup functions for DHCPv6 and DHCPv4 handlers
  9. // respectively. Both setup functions can be nil.
  10. type Plugin struct {
  11. Name string
  12. Setup6 SetupFunc6
  13. Setup4 SetupFunc4
  14. }
  15. // RegisteredPlugins maps a plugin name to a Plugin instance.
  16. var RegisteredPlugins = make(map[string]*Plugin)
  17. // SetupFunc6 defines a plugin setup function for DHCPv6
  18. type SetupFunc6 func(args ...string) (handler.Handler6, error)
  19. // SetupFunc4 defines a plugin setup function for DHCPv6
  20. type SetupFunc4 func(args ...string) (handler.Handler4, error)
  21. // RegisterPlugin registers a plugin by its name and setup functions.
  22. func RegisterPlugin(name string, setup6 SetupFunc6, setup4 SetupFunc4) {
  23. log.Printf("Registering plugin \"%s\"", name)
  24. if _, ok := RegisteredPlugins[name]; ok {
  25. // TODO this highlights that asking the plugins to register themselves
  26. // is not the right approach. Need to register them in the main program.
  27. log.Panicf("Plugin '%s' is already registered", name)
  28. }
  29. plugin := Plugin{
  30. Name: name,
  31. Setup6: setup6,
  32. Setup4: setup4,
  33. }
  34. RegisteredPlugins[name] = &plugin
  35. }