kmodule.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // package kmodule -- модуль на основе ядра.
  2. package kmodule
  3. import (
  4. "time"
  5. "gitp78su.ipnodns.ru/svi/kern/v4/lev0"
  6. "gitp78su.ipnodns.ru/svi/kern/v4/lev1"
  7. "gitp78su.ipnodns.ru/svi/kern/v4/lev1/comp_spec"
  8. "gitp78su.ipnodns.ru/svi/kern/v4/lev2/kctx"
  9. "gitp78su.ipnodns.ru/svi/kern/v4/lev2/kmodule/mod_stat"
  10. "gitp78su.ipnodns.ru/svi/kern/v4/lev2/kspec"
  11. "gitp78su.ipnodns.ru/svi/kern/v4/lev2/lti/bus_ent"
  12. "gitp78su.ipnodns.ru/svi/kern/v4/lev2/lti/bus_ent/topic"
  13. "gitp78su.ipnodns.ru/svi/kern/v4/lev2/lti/bus_spec"
  14. "gitp78su.ipnodns.ru/svi/kern/v4/lev2/lti/kbus_local"
  15. "gitp78su.ipnodns.ru/svi/kern/v4/lev3/mod_ent/module_name"
  16. "gitp78su.ipnodns.ru/svi/kern/v4/lev3/mod_spec"
  17. )
  18. // kModule -- модуль на основе ядра.
  19. type kModule struct {
  20. kCtx kspec.IKernelCtx
  21. ctx comp_spec.ILocalCtx
  22. name *module_name.LModuleName
  23. bus bus_spec.IKernelBus
  24. timePhase *lev0.MSafeNum
  25. txtLive *lev0.MTxt
  26. stat mod_spec.IModuleStat
  27. }
  28. // NewKernelModule -- возвращает новый модуль на основе ядра.
  29. func NewKernelModule(name *module_name.LModuleName) mod_spec.IKernelModule {
  30. lev0.If(name == nil).Hassert("NewKernelModule(): name==nil", []any{})
  31. kCtx := kctx.GetKernelCtx()
  32. sf := &kModule{
  33. kCtx: kCtx,
  34. ctx: lev1.NewLocalCtx(kCtx.Ctx()),
  35. name: name,
  36. bus: kbus_local.GetKernelBusLocal(),
  37. timePhase: lev0.MutSafeNum(1000), // 1000 msec
  38. txtLive: lev0.MutTxt(""),
  39. stat: mod_stat.NewModStat(name),
  40. }
  41. go sf.sigLive()
  42. return sf
  43. }
  44. // Stat -- возвращает статистику модуля.
  45. func (sf *kModule) Stat() mod_spec.IModuleStat {
  46. return sf.stat
  47. }
  48. // Log -- возвращает буферный лог.
  49. func (sf *kModule) Log() comp_spec.ILogBuf {
  50. return sf.ctx.Log()
  51. }
  52. // Ctx -- возвращает контекст модуля.
  53. func (sf *kModule) Ctx() comp_spec.ILocalCtx {
  54. return sf.ctx
  55. }
  56. // Run -- запускает модуль в работу.
  57. func (sf *kModule) Run() {
  58. lev0.If(true).
  59. Hassert("kModule.Run(): module='%v', parent not realized this method",
  60. []any{sf.name})
  61. }
  62. // Name -- возвращает уникальное имя модуля.
  63. func (sf *kModule) Name() *module_name.LModuleName {
  64. return sf.name
  65. }
  66. // IsWork -- возвращает признак состояния работы.
  67. func (sf *kModule) IsWork() lev0.Bool {
  68. lev0.If(true).
  69. Hassert("kModule.IsWork(): module='%v', parent not realized this method",
  70. []any{sf.name})
  71. return false
  72. }
  73. // Live -- возвращает индикатор жизни модуля.
  74. func (sf *kModule) Live() *lev0.LTxt {
  75. return sf.txtLive.Let()
  76. }
  77. var (
  78. f0 = lev0.ATxt("|")
  79. f1 = lev0.ATxt("/")
  80. f2 = lev0.ATxt("-")
  81. f3 = lev0.ATxt("\\")
  82. )
  83. // Сигнал жизни, каждые 5 сек публикует в шину метку.
  84. func (sf *kModule) sigLive() {
  85. var (
  86. topic = topic.LetTopic(bus_ent.ATopic(sf.name.Get()) + "_live")
  87. iPhase = 0
  88. res *lev0.Result[lev0.Bool]
  89. )
  90. fnPhase := func() {
  91. time.Sleep(time.Millisecond * time.Duration(sf.timePhase.Get()))
  92. select {
  93. case <-sf.kCtx.Ctx().Done():
  94. return
  95. default:
  96. switch iPhase {
  97. case 0:
  98. sf.txtLive.Set(f0)
  99. res = sf.bus.Publish(topic, sf.txtLive.Byte())
  100. case 1:
  101. sf.txtLive.Set(f1)
  102. res = sf.bus.Publish(topic, sf.txtLive.Byte())
  103. case 2:
  104. sf.txtLive.Set(f2)
  105. res = sf.bus.Publish(topic, sf.txtLive.Byte())
  106. case 3:
  107. sf.txtLive.Set(f3)
  108. res = sf.bus.Publish(topic, sf.txtLive.Byte())
  109. iPhase = -1
  110. }
  111. res.Hassert("kModule.sigLive(): name=%v, in publish live", sf.Name())
  112. iPhase++
  113. sf.stat.Add(1)
  114. }
  115. }
  116. for {
  117. select {
  118. case <-sf.kCtx.Ctx().Done():
  119. return
  120. default:
  121. fnPhase()
  122. }
  123. }
  124. }