ext_app.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package ethui
  2. import (
  3. "fmt"
  4. "github.com/ethereum/eth-go/ethchain"
  5. "github.com/ethereum/eth-go/ethpub"
  6. "github.com/ethereum/eth-go/ethutil"
  7. "github.com/go-qml/qml"
  8. )
  9. type AppContainer interface {
  10. Create() error
  11. Destroy()
  12. Window() *qml.Window
  13. Engine() *qml.Engine
  14. NewBlock(*ethchain.Block)
  15. ObjectChanged(*ethchain.StateObject)
  16. StorageChanged(*ethchain.StorageState)
  17. NewWatcher(chan bool)
  18. }
  19. type ExtApplication struct {
  20. *ethpub.PEthereum
  21. blockChan chan ethutil.React
  22. changeChan chan ethutil.React
  23. quitChan chan bool
  24. watcherQuitChan chan bool
  25. container AppContainer
  26. lib *UiLib
  27. registeredEvents []string
  28. }
  29. func NewExtApplication(container AppContainer, lib *UiLib) *ExtApplication {
  30. app := &ExtApplication{
  31. ethpub.NewPEthereum(lib.eth),
  32. make(chan ethutil.React, 1),
  33. make(chan ethutil.React, 1),
  34. make(chan bool),
  35. make(chan bool),
  36. container,
  37. lib,
  38. nil,
  39. }
  40. return app
  41. }
  42. func (app *ExtApplication) run() {
  43. // Set the "eth" api on to the containers context
  44. context := app.container.Engine().Context()
  45. context.SetVar("eth", app)
  46. context.SetVar("ui", app.lib)
  47. err := app.container.Create()
  48. if err != nil {
  49. fmt.Println(err)
  50. return
  51. }
  52. // Call the main loop
  53. go app.mainLoop()
  54. // Subscribe to events
  55. reactor := app.lib.eth.Reactor()
  56. reactor.Subscribe("newBlock", app.blockChan)
  57. app.container.NewWatcher(app.watcherQuitChan)
  58. win := app.container.Window()
  59. win.Show()
  60. win.Wait()
  61. app.stop()
  62. }
  63. func (app *ExtApplication) stop() {
  64. // Clean up
  65. reactor := app.lib.eth.Reactor()
  66. reactor.Unsubscribe("newBlock", app.blockChan)
  67. for _, event := range app.registeredEvents {
  68. reactor.Unsubscribe(event, app.changeChan)
  69. }
  70. // Kill the main loop
  71. app.quitChan <- true
  72. app.watcherQuitChan <- true
  73. close(app.blockChan)
  74. close(app.quitChan)
  75. close(app.changeChan)
  76. app.container.Destroy()
  77. }
  78. func (app *ExtApplication) mainLoop() {
  79. out:
  80. for {
  81. select {
  82. case <-app.quitChan:
  83. break out
  84. case block := <-app.blockChan:
  85. if block, ok := block.Resource.(*ethchain.Block); ok {
  86. app.container.NewBlock(block)
  87. }
  88. case object := <-app.changeChan:
  89. if stateObject, ok := object.Resource.(*ethchain.StateObject); ok {
  90. app.container.ObjectChanged(stateObject)
  91. } else if storageObject, ok := object.Resource.(*ethchain.StorageState); ok {
  92. app.container.StorageChanged(storageObject)
  93. }
  94. }
  95. }
  96. }
  97. func (app *ExtApplication) Watch(addr, storageAddr string) {
  98. var event string
  99. if len(storageAddr) == 0 {
  100. event = "object:" + string(ethutil.Hex2Bytes(addr))
  101. app.lib.eth.Reactor().Subscribe(event, app.changeChan)
  102. } else {
  103. event = "storage:" + string(ethutil.Hex2Bytes(addr)) + ":" + string(ethutil.Hex2Bytes(storageAddr))
  104. app.lib.eth.Reactor().Subscribe(event, app.changeChan)
  105. }
  106. app.registeredEvents = append(app.registeredEvents, event)
  107. }