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.
146 lines
3.0 KiB
146 lines
3.0 KiB
11 years ago
|
package main
|
||
11 years ago
|
|
||
|
import (
|
||
10 years ago
|
"encoding/json"
|
||
|
|
||
11 years ago
|
"github.com/ethereum/eth-go/ethchain"
|
||
10 years ago
|
"github.com/ethereum/eth-go/ethpipe"
|
||
11 years ago
|
"github.com/ethereum/eth-go/ethreact"
|
||
10 years ago
|
"github.com/ethereum/eth-go/ethstate"
|
||
10 years ago
|
"github.com/ethereum/go-ethereum/javascript"
|
||
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
|
|
||
11 years ago
|
blockChan chan ethreact.Event
|
||
10 years ago
|
messageChan chan ethreact.Event
|
||
11 years ago
|
quitChan chan bool
|
||
|
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 {
|
||
|
app := &ExtApplication{
|
||
10 years ago
|
ethpipe.NewJSPipe(lib.eth),
|
||
10 years ago
|
lib.eth,
|
||
10 years ago
|
make(chan ethreact.Event, 100),
|
||
|
make(chan ethreact.Event, 100),
|
||
11 years ago
|
make(chan bool),
|
||
11 years ago
|
make(chan bool),
|
||
10 years ago
|
make(map[string]*ethchain.Filter),
|
||
11 years ago
|
container,
|
||
|
lib,
|
||
|
}
|
||
|
|
||
|
return app
|
||
|
}
|
||
|
|
||
|
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
|
||
|
}
|
||
|
|
||
|
// Call the main loop
|
||
|
go app.mainLoop()
|
||
|
|
||
|
// Subscribe to events
|
||
|
reactor := app.lib.eth.Reactor()
|
||
|
reactor.Subscribe("newBlock", app.blockChan)
|
||
10 years ago
|
reactor.Subscribe("messages", app.messageChan)
|
||
11 years ago
|
|
||
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() {
|
||
|
// Clean up
|
||
|
reactor := app.lib.eth.Reactor()
|
||
|
reactor.Unsubscribe("newBlock", app.blockChan)
|
||
|
|
||
|
// Kill the main loop
|
||
|
app.quitChan <- true
|
||
11 years ago
|
app.watcherQuitChan <- true
|
||
11 years ago
|
|
||
|
close(app.blockChan)
|
||
|
close(app.quitChan)
|
||
|
|
||
|
app.container.Destroy()
|
||
|
}
|
||
|
|
||
|
func (app *ExtApplication) mainLoop() {
|
||
|
out:
|
||
|
for {
|
||
|
select {
|
||
|
case <-app.quitChan:
|
||
|
break out
|
||
|
case block := <-app.blockChan:
|
||
|
if block, ok := block.Resource.(*ethchain.Block); ok {
|
||
|
app.container.NewBlock(block)
|
||
|
}
|
||
10 years ago
|
case msg := <-app.messageChan:
|
||
|
if messages, ok := msg.Resource.(ethstate.Messages); ok {
|
||
|
for id, filter := range app.filters {
|
||
|
msgs := filter.FilterMessages(messages)
|
||
|
if len(msgs) > 0 {
|
||
|
app.container.Messages(msgs, id)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
11 years ago
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
10 years ago
|
func (self *ExtApplication) Watch(filterOptions map[string]interface{}, identifier string) {
|
||
|
self.filters[identifier] = ethchain.NewFilterFromMap(filterOptions, self.eth)
|
||
11 years ago
|
}
|
||
10 years ago
|
|
||
|
func (self *ExtApplication) GetMessages(object map[string]interface{}) string {
|
||
10 years ago
|
filter := ethchain.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)
|
||
|
}
|