ext_app.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. "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/xeth"
  24. "github.com/obscuren/qml"
  25. )
  26. type AppContainer interface {
  27. Create() error
  28. Destroy()
  29. Window() *qml.Window
  30. Engine() *qml.Engine
  31. NewBlock(*types.Block)
  32. NewWatcher(chan bool)
  33. Post(string, int)
  34. }
  35. type ExtApplication struct {
  36. *xeth.XEth
  37. eth core.Backend
  38. events event.Subscription
  39. watcherQuitChan chan bool
  40. filters map[string]*core.Filter
  41. container AppContainer
  42. lib *UiLib
  43. }
  44. func NewExtApplication(container AppContainer, lib *UiLib) *ExtApplication {
  45. return &ExtApplication{
  46. XEth: xeth.New(lib.eth),
  47. eth: lib.eth,
  48. watcherQuitChan: make(chan bool),
  49. filters: make(map[string]*core.Filter),
  50. container: container,
  51. lib: lib,
  52. }
  53. }
  54. func (app *ExtApplication) run() {
  55. // Set the "eth" api on to the containers context
  56. context := app.container.Engine().Context()
  57. context.SetVar("eth", app)
  58. context.SetVar("ui", app.lib)
  59. err := app.container.Create()
  60. if err != nil {
  61. guilogger.Errorln(err)
  62. return
  63. }
  64. // Call the main loop
  65. go app.mainLoop()
  66. app.container.NewWatcher(app.watcherQuitChan)
  67. win := app.container.Window()
  68. win.Show()
  69. win.Wait()
  70. app.stop()
  71. }
  72. func (app *ExtApplication) stop() {
  73. app.events.Unsubscribe()
  74. // Kill the main loop
  75. app.watcherQuitChan <- true
  76. app.container.Destroy()
  77. }
  78. func (app *ExtApplication) mainLoop() {
  79. for ev := range app.events.Chan() {
  80. switch ev := ev.(type) {
  81. case core.NewBlockEvent:
  82. app.container.NewBlock(ev.Block)
  83. /* TODO remove
  84. case state.Messages:
  85. for id, filter := range app.filters {
  86. msgs := filter.FilterMessages(ev)
  87. if len(msgs) > 0 {
  88. app.container.Messages(msgs, id)
  89. }
  90. }
  91. */
  92. }
  93. }
  94. }