plugin.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package core
  2. import (
  3. "context"
  4. "ehang.io/nps/bridge"
  5. "net"
  6. )
  7. // Plugin interface, all plugins must implement those functions.
  8. type Plugin interface {
  9. GetConfigName() *NpsConfigs
  10. InitConfig(globalConfig, clientConfig, pluginConfig map[string]string, pgCnf []*Config)
  11. GetStage() []Stage
  12. Start(ctx context.Context) (context.Context, error)
  13. Run(ctx context.Context) (context.Context, error)
  14. End(ctx context.Context) (context.Context, error)
  15. }
  16. type NpsPlugin struct {
  17. Version string
  18. Configs map[string]string
  19. }
  20. func (npsPlugin *NpsPlugin) GetConfigName() *NpsConfigs {
  21. return nil
  22. }
  23. func (npsPlugin *NpsPlugin) InitConfig(globalConfig, clientConfig, pluginConfig map[string]string, pgCnf []*Config) {
  24. npsPlugin.Configs = make(map[string]string)
  25. for _, cfg := range pgCnf {
  26. switch cfg.ConfigLevel {
  27. case CONFIG_LEVEL_PLUGIN:
  28. npsPlugin.Configs[cfg.ConfigName] = pluginConfig[cfg.ConfigName]
  29. case CONFIG_LEVEL_CLIENT:
  30. npsPlugin.Configs[cfg.ConfigName] = clientConfig[cfg.ConfigName]
  31. case CONFIG_LEVEL_GLOBAL:
  32. npsPlugin.Configs[cfg.ConfigName] = globalConfig[cfg.ConfigName]
  33. }
  34. }
  35. return
  36. }
  37. // describe the stage of the plugin
  38. func (npsPlugin *NpsPlugin) GetStage() []Stage {
  39. return []Stage{STAGE_RUN}
  40. }
  41. func (npsPlugin *NpsPlugin) Start(ctx context.Context) (context.Context, error) {
  42. return ctx, nil
  43. }
  44. func (npsPlugin *NpsPlugin) Run(ctx context.Context) (context.Context, error) {
  45. return ctx, nil
  46. }
  47. func (npsPlugin *NpsPlugin) End(ctx context.Context) (context.Context, error) {
  48. return ctx, nil
  49. }
  50. func (npsPlugin *NpsPlugin) GetClientConn(ctx context.Context) net.Conn {
  51. return ctx.Value(CLIENT_CONNECTION).(net.Conn)
  52. }
  53. func (npsPlugin *NpsPlugin) SetClientConn(ctx context.Context, conn net.Conn) context.Context {
  54. return context.WithValue(ctx, CLIENT_CONNECTION, conn)
  55. }
  56. func (npsPlugin *NpsPlugin) GetBridge(ctx context.Context) *bridge.Bridge {
  57. return ctx.Value(BRIDGE).(*bridge.Bridge)
  58. }
  59. func (npsPlugin *NpsPlugin) GetClientId(ctx context.Context) int {
  60. return ctx.Value(CLIENT_ID).(int)
  61. }
  62. type Plugins struct {
  63. StartPgs []Plugin
  64. RunPgs []Plugin
  65. EndPgs []Plugin
  66. AllPgs []Plugin
  67. }
  68. func NewPlugins() *Plugins {
  69. p := &Plugins{}
  70. p.StartPgs = make([]Plugin, 0)
  71. p.RunPgs = make([]Plugin, 0)
  72. p.EndPgs = make([]Plugin, 0)
  73. p.AllPgs = make([]Plugin, 0)
  74. return p
  75. }
  76. func (pl *Plugins) Add(plugins ...Plugin) {
  77. for _, plugin := range plugins {
  78. for _, v := range plugin.GetStage() {
  79. pl.AllPgs = append(pl.RunPgs, plugin)
  80. switch v {
  81. case STAGE_RUN:
  82. pl.RunPgs = append(pl.RunPgs, plugin)
  83. case STAGE_END:
  84. pl.EndPgs = append(pl.EndPgs, plugin)
  85. case STAGE_START:
  86. pl.StartPgs = append(pl.StartPgs, plugin)
  87. }
  88. }
  89. }
  90. }
  91. func RunPlugin(ctx context.Context, pgs []Plugin, stage Stage) (context.Context, error) {
  92. var err error
  93. for _, pg := range pgs {
  94. switch stage {
  95. case STAGE_RUN:
  96. ctx, err = pg.Run(ctx)
  97. case STAGE_START:
  98. ctx, err = pg.Start(ctx)
  99. case STAGE_END:
  100. ctx, err = pg.End(ctx)
  101. }
  102. if err != nil {
  103. return ctx, err
  104. }
  105. }
  106. return ctx, nil
  107. }