ext_app.go 3.42 KB
Newer Older
Felix Lange's avatar
Felix Lange committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Copyright (c) 2013-2014, Jeffrey Wilcke. All rights reserved.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
// MA 02110-1301  USA

obscuren's avatar
obscuren committed
18
package main
19 20

import (
obscuren's avatar
obscuren committed
21 22
	"encoding/json"

obscuren's avatar
obscuren committed
23 24
	"github.com/ethereum/go-ethereum/core"
	"github.com/ethereum/go-ethereum/core/types"
25
	"github.com/ethereum/go-ethereum/event"
obscuren's avatar
obscuren committed
26
	"github.com/ethereum/go-ethereum/javascript"
obscuren's avatar
obscuren committed
27
	"github.com/ethereum/go-ethereum/state"
28
	"github.com/ethereum/go-ethereum/ui/qt"
29
	"github.com/ethereum/go-ethereum/xeth"
30
	"gopkg.in/qml.v1"
31 32 33 34 35 36 37 38 39
)

type AppContainer interface {
	Create() error
	Destroy()

	Window() *qml.Window
	Engine() *qml.Engine

40
	NewBlock(*types.Block)
41
	NewWatcher(chan bool)
obscuren's avatar
obscuren committed
42
	Messages(state.Messages, string)
obscuren's avatar
obscuren committed
43
	Post(string, int)
44 45 46
}

type ExtApplication struct {
47
	*xeth.JSXEth
obscuren's avatar
obscuren committed
48
	eth core.EthManager
49

Felix Lange's avatar
Felix Lange committed
50
	events          event.Subscription
51
	watcherQuitChan chan bool
52

obscuren's avatar
obscuren committed
53
	filters map[string]*core.Filter
obscuren's avatar
obscuren committed
54

obscuren's avatar
obscuren committed
55 56
	container AppContainer
	lib       *UiLib
57 58 59
}

func NewExtApplication(container AppContainer, lib *UiLib) *ExtApplication {
Felix Lange's avatar
Felix Lange committed
60
	return &ExtApplication{
61
		JSXEth:          xeth.NewJSXEth(lib.eth),
Felix Lange's avatar
Felix Lange committed
62 63
		eth:             lib.eth,
		watcherQuitChan: make(chan bool),
obscuren's avatar
obscuren committed
64
		filters:         make(map[string]*core.Filter),
Felix Lange's avatar
Felix Lange committed
65 66
		container:       container,
		lib:             lib,
67 68 69 70 71 72 73 74 75 76 77
	}
}

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 {
obscuren's avatar
obscuren committed
78
		guilogger.Errorln(err)
79 80 81
		return
	}

Felix Lange's avatar
Felix Lange committed
82 83
	// Subscribe to events
	mux := app.lib.eth.EventMux()
obscuren's avatar
obscuren committed
84
	app.events = mux.Subscribe(core.NewBlockEvent{}, state.Messages(nil))
Felix Lange's avatar
Felix Lange committed
85

86 87 88
	// Call the main loop
	go app.mainLoop()

89 90
	app.container.NewWatcher(app.watcherQuitChan)

91 92 93 94 95 96 97 98
	win := app.container.Window()
	win.Show()
	win.Wait()

	app.stop()
}

func (app *ExtApplication) stop() {
Felix Lange's avatar
Felix Lange committed
99
	app.events.Unsubscribe()
100 101

	// Kill the main loop
102
	app.watcherQuitChan <- true
103 104 105 106 107

	app.container.Destroy()
}

func (app *ExtApplication) mainLoop() {
Felix Lange's avatar
Felix Lange committed
108 109
	for ev := range app.events.Chan() {
		switch ev := ev.(type) {
obscuren's avatar
obscuren committed
110
		case core.NewBlockEvent:
Felix Lange's avatar
Felix Lange committed
111 112
			app.container.NewBlock(ev.Block)

obscuren's avatar
obscuren committed
113
		case state.Messages:
Felix Lange's avatar
Felix Lange committed
114 115 116 117
			for id, filter := range app.filters {
				msgs := filter.FilterMessages(ev)
				if len(msgs) > 0 {
					app.container.Messages(msgs, id)
obscuren's avatar
obscuren committed
118 119
				}
			}
120 121 122 123
		}
	}
}

obscuren's avatar
obscuren committed
124
func (self *ExtApplication) Watch(filterOptions map[string]interface{}, identifier string) {
125
	self.filters[identifier] = qt.NewFilterFromMap(filterOptions, self.eth)
126
}
obscuren's avatar
obscuren committed
127 128

func (self *ExtApplication) GetMessages(object map[string]interface{}) string {
129
	filter := qt.NewFilterFromMap(object, self.eth)
obscuren's avatar
obscuren committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143

	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)
}