plugin.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package core
  2. import (
  3. "context"
  4. "github.com/cnlh/nps/bridge"
  5. "net"
  6. )
  7. // Plugin interface, all plugins must implement those functions.
  8. type Plugin interface {
  9. GetConfigName() *NpsConfigs
  10. GetConfigLevel() ConfigLevel
  11. GetStage() Stage
  12. Start(ctx context.Context, config map[string]string) (context.Context, error)
  13. Run(ctx context.Context, config map[string]string) (context.Context, error)
  14. End(ctx context.Context, config map[string]string) (context.Context, error)
  15. }
  16. type NpsPlugin struct {
  17. Version string
  18. }
  19. func (npsPlugin *NpsPlugin) GetConfigName() *NpsConfigs {
  20. return nil
  21. }
  22. // describe the config level
  23. func (npsPlugin *NpsPlugin) GetConfigLevel() ConfigLevel {
  24. return CONFIG_LEVEL_PLUGIN
  25. }
  26. // describe the stage of the plugin
  27. func (npsPlugin *NpsPlugin) GetStage() Stage {
  28. return STAGE_RUN
  29. }
  30. func (npsPlugin *NpsPlugin) Start(ctx context.Context, config map[string]string) (context.Context, error) {
  31. return ctx, nil
  32. }
  33. func (npsPlugin *NpsPlugin) Run(ctx context.Context, config map[string]string) (context.Context, error) {
  34. return ctx, nil
  35. }
  36. func (npsPlugin *NpsPlugin) End(ctx context.Context, config map[string]string) (context.Context, error) {
  37. return ctx, nil
  38. }
  39. func (npsPlugin *NpsPlugin) GetClientConn(ctx context.Context) net.Conn {
  40. return ctx.Value(CLIENT_CONNECTION).(net.Conn)
  41. }
  42. func (npsPlugin *NpsPlugin) GetBridge(ctx context.Context) *bridge.Bridge {
  43. return ctx.Value(BRIDGE).(*bridge.Bridge)
  44. }
  45. func (npsPlugin *NpsPlugin) GetClientId(ctx context.Context) int {
  46. return ctx.Value(CLIENT_ID).(int)
  47. }
  48. type Plugins struct {
  49. pgs []Plugin
  50. }
  51. func NewPlugins() *Plugins {
  52. p := &Plugins{}
  53. p.pgs = make([]Plugin, 0)
  54. return p
  55. }
  56. func (pl *Plugins) Add(plugins ...Plugin) {
  57. for _, plugin := range plugins {
  58. pl.pgs = append(pl.pgs, plugin)
  59. }
  60. }