mirror of https://github.com/ethereum/go-ethereum
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
126 lines
2.6 KiB
126 lines
2.6 KiB
11 years ago
|
package main
|
||
11 years ago
|
|
||
|
import (
|
||
10 years ago
|
"encoding/json"
|
||
|
|
||
10 years ago
|
"github.com/ethereum/go-ethereum/ethchain"
|
||
|
"github.com/ethereum/go-ethereum/ethpipe"
|
||
|
"github.com/ethereum/go-ethereum/ethstate"
|
||
|
"github.com/ethereum/go-ethereum/event"
|
||
10 years ago
|
"github.com/ethereum/go-ethereum/javascript"
|
||
10 years ago
|
"github.com/ethereum/go-ethereum/ui/qt"
|
||
10 years ago
|
"gopkg.in/qml.v1"
|
||
11 years ago
|
)
|
||
|
|
||
|
type AppContainer interface {
|
||
|
Create() error
|
||
|
Destroy()
|
||
|
|
||
|
Window() *qml.Window
|
||
|
Engine() *qml.Engine
|
||
|
|
||
|
NewBlock(*ethchain.Block)
|
||
11 years ago
|
NewWatcher(chan bool)
|
||
10 years ago
|
Messages(ethstate.Messages, string)
|
||
10 years ago
|
Post(string, int)
|
||
11 years ago
|
}
|
||
|
|
||
|
type ExtApplication struct {
|
||
10 years ago
|
*ethpipe.JSPipe
|
||
10 years ago
|
eth ethchain.EthManager
|
||
11 years ago
|
|
||
10 years ago
|
events event.Subscription
|
||
11 years ago
|
watcherQuitChan chan bool
|
||
11 years ago
|
|
||
10 years ago
|
filters map[string]*ethchain.Filter
|
||
|
|
||
10 years ago
|
container AppContainer
|
||
|
lib *UiLib
|
||
11 years ago
|
}
|
||
|
|
||
|
func NewExtApplication(container AppContainer, lib *UiLib) *ExtApplication {
|
||
10 years ago
|
return &ExtApplication{
|
||
|
JSPipe: ethpipe.NewJSPipe(lib.eth),
|
||
|
eth: lib.eth,
|
||
|
watcherQuitChan: make(chan bool),
|
||
|
filters: make(map[string]*ethchain.Filter),
|
||
|
container: container,
|
||
|
lib: lib,
|
||
11 years ago
|
}
|
||
|
}
|
||
|
|
||
|
func (app *ExtApplication) run() {
|
||
|
// Set the "eth" api on to the containers context
|
||
|
context := app.container.Engine().Context()
|
||
|
context.SetVar("eth", app)
|
||
|
context.SetVar("ui", app.lib)
|
||
|
|
||
|
err := app.container.Create()
|
||
|
if err != nil {
|
||
10 years ago
|
logger.Errorln(err)
|
||
11 years ago
|
return
|
||
|
}
|
||
|
|
||
10 years ago
|
// Subscribe to events
|
||
|
mux := app.lib.eth.EventMux()
|
||
|
app.events = mux.Subscribe(ethchain.NewBlockEvent{}, ethstate.Messages(nil))
|
||
|
|
||
11 years ago
|
// Call the main loop
|
||
|
go app.mainLoop()
|
||
|
|
||
11 years ago
|
app.container.NewWatcher(app.watcherQuitChan)
|
||
|
|
||
11 years ago
|
win := app.container.Window()
|
||
|
win.Show()
|
||
|
win.Wait()
|
||
|
|
||
|
app.stop()
|
||
|
}
|
||
|
|
||
|
func (app *ExtApplication) stop() {
|
||
10 years ago
|
app.events.Unsubscribe()
|
||
11 years ago
|
|
||
|
// Kill the main loop
|
||
11 years ago
|
app.watcherQuitChan <- true
|
||
11 years ago
|
|
||
|
app.container.Destroy()
|
||
|
}
|
||
|
|
||
|
func (app *ExtApplication) mainLoop() {
|
||
10 years ago
|
for ev := range app.events.Chan() {
|
||
|
switch ev := ev.(type) {
|
||
|
case ethchain.NewBlockEvent:
|
||
|
app.container.NewBlock(ev.Block)
|
||
|
|
||
|
case ethstate.Messages:
|
||
|
for id, filter := range app.filters {
|
||
|
msgs := filter.FilterMessages(ev)
|
||
|
if len(msgs) > 0 {
|
||
|
app.container.Messages(msgs, id)
|
||
10 years ago
|
}
|
||
|
}
|
||
11 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
func (self *ExtApplication) Watch(filterOptions map[string]interface{}, identifier string) {
|
||
10 years ago
|
self.filters[identifier] = qt.NewFilterFromMap(filterOptions, self.eth)
|
||
11 years ago
|
}
|
||
10 years ago
|
|
||
|
func (self *ExtApplication) GetMessages(object map[string]interface{}) string {
|
||
10 years ago
|
filter := qt.NewFilterFromMap(object, self.eth)
|
||
10 years ago
|
|
||
|
messages := filter.Find()
|
||
|
var msgs []javascript.JSMessage
|
||
|
for _, m := range messages {
|
||
|
msgs = append(msgs, javascript.NewJSMessage(m))
|
||
|
}
|
||
|
|
||
|
b, err := json.Marshal(msgs)
|
||
|
if err != nil {
|
||
|
return "{\"error\":" + err.Error() + "}"
|
||
|
}
|
||
|
|
||
|
return string(b)
|
||
|
}
|