ext_app.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. This file is part of go-ethereum
  3. go-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. go-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /**
  15. * @authors
  16. * Jeffrey Wilcke <i@jev.io>
  17. */
  18. package main
  19. import (
  20. "encoding/json"
  21. "github.com/ethereum/go-ethereum/core"
  22. "github.com/ethereum/go-ethereum/core/types"
  23. "github.com/ethereum/go-ethereum/event"
  24. "github.com/ethereum/go-ethereum/javascript"
  25. "github.com/ethereum/go-ethereum/state"
  26. "github.com/ethereum/go-ethereum/ui/qt"
  27. "github.com/ethereum/go-ethereum/xeth"
  28. "gopkg.in/qml.v1"
  29. )
  30. type AppContainer interface {
  31. Create() error
  32. Destroy()
  33. Window() *qml.Window
  34. Engine() *qml.Engine
  35. NewBlock(*types.Block)
  36. NewWatcher(chan bool)
  37. Messages(state.Messages, string)
  38. Post(string, int)
  39. }
  40. type ExtApplication struct {
  41. *xeth.JSXEth
  42. eth core.EthManager
  43. events event.Subscription
  44. watcherQuitChan chan bool
  45. filters map[string]*core.Filter
  46. container AppContainer
  47. lib *UiLib
  48. }
  49. func NewExtApplication(container AppContainer, lib *UiLib) *ExtApplication {
  50. return &ExtApplication{
  51. JSXEth: xeth.NewJSXEth(lib.eth),
  52. eth: lib.eth,
  53. watcherQuitChan: make(chan bool),
  54. filters: make(map[string]*core.Filter),
  55. container: container,
  56. lib: lib,
  57. }
  58. }
  59. func (app *ExtApplication) run() {
  60. // Set the "eth" api on to the containers context
  61. context := app.container.Engine().Context()
  62. context.SetVar("eth", app)
  63. context.SetVar("ui", app.lib)
  64. err := app.container.Create()
  65. if err != nil {
  66. guilogger.Errorln(err)
  67. return
  68. }
  69. // Subscribe to events
  70. mux := app.lib.eth.EventMux()
  71. app.events = mux.Subscribe(core.NewBlockEvent{}, state.Messages(nil))
  72. // Call the main loop
  73. go app.mainLoop()
  74. app.container.NewWatcher(app.watcherQuitChan)
  75. win := app.container.Window()
  76. win.Show()
  77. win.Wait()
  78. app.stop()
  79. }
  80. func (app *ExtApplication) stop() {
  81. app.events.Unsubscribe()
  82. // Kill the main loop
  83. app.watcherQuitChan <- true
  84. app.container.Destroy()
  85. }
  86. func (app *ExtApplication) mainLoop() {
  87. for ev := range app.events.Chan() {
  88. switch ev := ev.(type) {
  89. case core.NewBlockEvent:
  90. app.container.NewBlock(ev.Block)
  91. case state.Messages:
  92. for id, filter := range app.filters {
  93. msgs := filter.FilterMessages(ev)
  94. if len(msgs) > 0 {
  95. app.container.Messages(msgs, id)
  96. }
  97. }
  98. }
  99. }
  100. }
  101. func (self *ExtApplication) Watch(filterOptions map[string]interface{}, identifier string) {
  102. self.filters[identifier] = qt.NewFilterFromMap(filterOptions, self.eth)
  103. }
  104. func (self *ExtApplication) GetMessages(object map[string]interface{}) string {
  105. filter := qt.NewFilterFromMap(object, self.eth)
  106. messages := filter.Find()
  107. var msgs []javascript.JSMessage
  108. for _, m := range messages {
  109. msgs = append(msgs, javascript.NewJSMessage(m))
  110. }
  111. b, err := json.Marshal(msgs)
  112. if err != nil {
  113. return "{\"error\":" + err.Error() + "}"
  114. }
  115. return string(b)
  116. }