github.com/jonasnick/go-ethereum@v0.7.12-0.20150216215225-22176f05d387/cmd/mist/ext_app.go (about)

     1  /*
     2  	This file is part of go-ethereum
     3  
     4  	go-ethereum is free software: you can redistribute it and/or modify
     5  	it under the terms of the GNU General Public License as published by
     6  	the Free Software Foundation, either version 3 of the License, or
     7  	(at your option) any later version.
     8  
     9  	go-ethereum is distributed in the hope that it will be useful,
    10  	but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  	GNU General Public License for more details.
    13  
    14  	You should have received a copy of the GNU General Public License
    15  	along with go-ethereum.  If not, see <http://www.gnu.org/licenses/>.
    16  */
    17  /**
    18   * @authors
    19   * 	Jeffrey Wilcke <i@jev.io>
    20   */
    21  package main
    22  
    23  import (
    24  	"github.com/jonasnick/go-ethereum/core"
    25  	"github.com/jonasnick/go-ethereum/core/types"
    26  	"github.com/jonasnick/go-ethereum/event"
    27  	"github.com/jonasnick/go-ethereum/ui/qt"
    28  	"github.com/jonasnick/go-ethereum/xeth"
    29  	"github.com/obscuren/qml"
    30  )
    31  
    32  type AppContainer interface {
    33  	Create() error
    34  	Destroy()
    35  
    36  	Window() *qml.Window
    37  	Engine() *qml.Engine
    38  
    39  	NewBlock(*types.Block)
    40  	NewWatcher(chan bool)
    41  	Post(string, int)
    42  }
    43  
    44  type ExtApplication struct {
    45  	*xeth.XEth
    46  	eth core.EthManager
    47  
    48  	events          event.Subscription
    49  	watcherQuitChan chan bool
    50  
    51  	filters map[string]*core.Filter
    52  
    53  	container AppContainer
    54  	lib       *UiLib
    55  }
    56  
    57  func NewExtApplication(container AppContainer, lib *UiLib) *ExtApplication {
    58  	return &ExtApplication{
    59  		XEth:            xeth.New(lib.eth),
    60  		eth:             lib.eth,
    61  		watcherQuitChan: make(chan bool),
    62  		filters:         make(map[string]*core.Filter),
    63  		container:       container,
    64  		lib:             lib,
    65  	}
    66  }
    67  
    68  func (app *ExtApplication) run() {
    69  	// Set the "eth" api on to the containers context
    70  	context := app.container.Engine().Context()
    71  	context.SetVar("eth", app)
    72  	context.SetVar("ui", app.lib)
    73  
    74  	err := app.container.Create()
    75  	if err != nil {
    76  		guilogger.Errorln(err)
    77  		return
    78  	}
    79  
    80  	// Call the main loop
    81  	go app.mainLoop()
    82  
    83  	app.container.NewWatcher(app.watcherQuitChan)
    84  
    85  	win := app.container.Window()
    86  	win.Show()
    87  	win.Wait()
    88  
    89  	app.stop()
    90  }
    91  
    92  func (app *ExtApplication) stop() {
    93  	app.events.Unsubscribe()
    94  
    95  	// Kill the main loop
    96  	app.watcherQuitChan <- true
    97  
    98  	app.container.Destroy()
    99  }
   100  
   101  func (app *ExtApplication) mainLoop() {
   102  	for ev := range app.events.Chan() {
   103  		switch ev := ev.(type) {
   104  		case core.NewBlockEvent:
   105  			app.container.NewBlock(ev.Block)
   106  
   107  			/* TODO remove
   108  			case state.Messages:
   109  				for id, filter := range app.filters {
   110  					msgs := filter.FilterMessages(ev)
   111  					if len(msgs) > 0 {
   112  						app.container.Messages(msgs, id)
   113  					}
   114  				}
   115  			*/
   116  		}
   117  	}
   118  }
   119  
   120  func (self *ExtApplication) Watch(filterOptions map[string]interface{}, identifier string) {
   121  	self.filters[identifier] = qt.NewFilterFromMap(filterOptions, self.eth)
   122  }