ext_app.go 3.4 KB

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