runapp.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package api
  2. import (
  3. "fmt"
  4. "launcher/cmd"
  5. "launcher/conf"
  6. "time"
  7. "github.com/energye/energy/v2/cef"
  8. "github.com/energye/energy/v2/cef/ipc"
  9. "github.com/energye/golcl/lcl"
  10. "github.com/energye/golcl/lcl/types"
  11. )
  12. type RunningApp struct {
  13. Guid string
  14. Title string
  15. Key string
  16. }
  17. type RunningNativeApp struct {
  18. Key string
  19. Entry string
  20. Params []string
  21. StartTime time.Time
  22. Guid string
  23. Title string
  24. Cmd *cmd.NativeExe
  25. }
  26. type RunningCmdSvcApp struct {
  27. Entry string
  28. Params []string
  29. StartTime time.Time
  30. Guid string
  31. Title string
  32. Key string
  33. Web *cef.LCLBrowserWindow
  34. Cmd *cmd.CmdSvcExe
  35. }
  36. var running_natives = make(map[string]*RunningNativeApp)
  37. var running_svcs = make(map[string]*RunningCmdSvcApp)
  38. func StopRunningApp() {
  39. for k, app := range running_natives {
  40. app.Cmd.Stop()
  41. delete(running_natives, k)
  42. }
  43. }
  44. func GetAppName(guid string) string {
  45. for _, app := range running_natives {
  46. if app.Guid == guid {
  47. return app.Title
  48. }
  49. }
  50. return ""
  51. }
  52. func _runNativeApp(guid, entryFile string, params []string, title string, key string) string {
  53. params = append(params,
  54. fmt.Sprintf("--port=%d", conf.AppConfig.NatsWsPort),
  55. fmt.Sprintf("--data=%s", conf.AppConfig.DataDir),
  56. fmt.Sprintf("--guid=%s", guid))
  57. cmd := &cmd.NativeExe{
  58. ExeFile: entryFile,
  59. Params: params,
  60. MonitorQuit: true,
  61. Quit: make(chan int),
  62. }
  63. err := cmd.Start()
  64. if err != nil {
  65. fmt.Println("start ", entryFile, "error", err.Error())
  66. return err.Error()
  67. }
  68. go func() {
  69. qflag := <-cmd.Quit
  70. fmt.Println("native app quite ", qflag)
  71. if running_natives[guid] != nil {
  72. delete(running_natives, guid)
  73. }
  74. StopApp(guid)
  75. }()
  76. running_natives[guid] = &RunningNativeApp{
  77. Guid: guid,
  78. Key: key,
  79. Entry: entryFile,
  80. Params: params,
  81. StartTime: time.Now(),
  82. Cmd: cmd,
  83. Title: title,
  84. }
  85. StartApp(guid)
  86. return ""
  87. }
  88. func RegRunApp(event *cef.BrowserEvent, window cef.IBrowserWindow, bw *cef.LCLBrowserWindow) {
  89. ipc.On("GetRunningApp", func() []RunningApp {
  90. out := []RunningApp{}
  91. for _, app := range running_natives {
  92. out = append(out, RunningApp{Guid: app.Guid, Title: app.Title, Key: app.Key})
  93. }
  94. for _, app := range running_svcs {
  95. out = append(out, RunningApp{Guid: app.Guid, Title: app.Title, Key: app.Key})
  96. }
  97. fmt.Println(running_svcs)
  98. return out
  99. })
  100. ipc.On("RunCmdSvcApp", func(guid, webUrl, entryFile string, params []string, title string, key string) string {
  101. window.RunOnMainThread(func() {
  102. params = append(params,
  103. fmt.Sprintf("--port=%d", conf.AppConfig.NatsWsPort),
  104. fmt.Sprintf("--data=%s", conf.AppConfig.DataDir),
  105. fmt.Sprintf("--guid=%s", guid))
  106. b := NewWeb(webUrl, entryFile, 1280, 720)
  107. app := &RunningCmdSvcApp{
  108. Guid: guid,
  109. Entry: entryFile,
  110. Params: params,
  111. Key: key,
  112. Title: title,
  113. StartTime: time.Now(),
  114. Web: b,
  115. }
  116. cmdsvc := &cmd.CmdSvcExe{
  117. NatsMsgName: fmt.Sprintf("%s.cmdmsg", guid),
  118. ExeFile: entryFile,
  119. Params: params,
  120. MonitorQuit: true,
  121. Quit: make(chan int),
  122. }
  123. err := cmdsvc.Start()
  124. if err != nil {
  125. fmt.Println("start ", entryFile, "error", err.Error())
  126. return
  127. }
  128. quiting := 0
  129. cmdsvcQuited := 0
  130. go func() {
  131. qflag := <-cmdsvc.Quit
  132. fmt.Println("cmd svc app quite ", qflag)
  133. if running_svcs[guid] != nil {
  134. delete(running_svcs, guid)
  135. }
  136. StopApp(guid)
  137. if quiting == 0 {
  138. cmdsvcQuited = 1
  139. b.Close()
  140. }
  141. }()
  142. if b != nil {
  143. b.SetOnClose(func(sender lcl.IObject, action *types.TCloseAction) bool {
  144. if cmdsvcQuited == 0 {
  145. quiting = 1
  146. cmdsvc.Stop()
  147. fmt.Println("web closed")
  148. }
  149. *action = types.CaFree
  150. return false
  151. })
  152. }
  153. running_svcs[guid] = app
  154. StartApp(guid)
  155. })
  156. return ""
  157. })
  158. ipc.On("RunNativeApp", _runNativeApp)
  159. ipc.On("StopNativeApp", func(guid string) bool {
  160. running := running_natives[guid]
  161. if running == nil {
  162. return true
  163. }
  164. running.Cmd.Stop()
  165. return true
  166. })
  167. }