watcher.go raw

   1  package gui
   2  
   3  import (
   4  	"time"
   5  
   6  	"github.com/p9c/p9/pkg/btcjson"
   7  
   8  	"github.com/p9c/p9/pkg/qu"
   9  )
  10  
  11  // Watcher keeps the chain and wallet and rpc clients connected
  12  func (wg *WalletGUI) Watcher() qu.C {
  13  	var e error
  14  	I.Ln("starting up watcher")
  15  	quit := qu.T()
  16  	// start things up first
  17  	if !wg.node.Running() {
  18  		D.Ln("watcher starting node")
  19  		wg.node.Start()
  20  	}
  21  	if wg.ChainClient == nil {
  22  		D.Ln("chain client is not initialized")
  23  		var e error
  24  		if e = wg.chainClient(); E.Chk(e) {
  25  		}
  26  	}
  27  	if !wg.wallet.Running() {
  28  		D.Ln("watcher starting wallet")
  29  		wg.wallet.Start()
  30  		D.Ln("now we can open the wallet")
  31  		if e = wg.writeWalletCookie(); E.Chk(e) {
  32  		}
  33  	}
  34  	if wg.WalletClient == nil || wg.WalletClient.Disconnected() {
  35  	allOut:
  36  		for {
  37  			if e = wg.walletClient(); !E.Chk(e) {
  38  			out:
  39  				for {
  40  					// keep trying until shutdown or the wallet client connects
  41  					I.Ln("attempting to get blockchain info from wallet")
  42  					var bci *btcjson.GetBlockChainInfoResult
  43  					if bci, e = wg.WalletClient.GetBlockChainInfo(); E.Chk(e) {
  44  						select {
  45  						case <-time.After(time.Second):
  46  							continue
  47  						case <-wg.quit:
  48  							return nil
  49  						}
  50  					}
  51  					D.S(bci)
  52  					break out
  53  				}
  54  			}
  55  			wg.unlockPassword.Wipe()
  56  			select {
  57  			case <-time.After(time.Second):
  58  				break allOut
  59  			case <-wg.quit:
  60  				return nil
  61  			}
  62  		}
  63  	}
  64  	go func() {
  65  		
  66  		watchTick := time.NewTicker(time.Second)
  67  		var e error
  68  	totalOut:
  69  		for {
  70  		disconnected:
  71  			for {
  72  				D.Ln("top of watcher loop")
  73  				select {
  74  				case <-watchTick.C:
  75  					if e = wg.Advertise(); E.Chk(e) {
  76  					}
  77  					if !wg.node.Running() {
  78  						D.Ln("watcher starting node")
  79  						wg.node.Start()
  80  					}
  81  					if wg.ChainClient.Disconnected() {
  82  						if e = wg.chainClient(); E.Chk(e) {
  83  							continue
  84  						}
  85  					}
  86  					if !wg.wallet.Running() {
  87  						D.Ln("watcher starting wallet")
  88  						wg.wallet.Start()
  89  					}
  90  					if wg.WalletClient == nil {
  91  						D.Ln("wallet client is not initialized")
  92  						if e = wg.walletClient(); E.Chk(e) {
  93  							continue
  94  							// } else {
  95  							// 	break disconnected
  96  						}
  97  					}
  98  					if wg.WalletClient.Disconnected() {
  99  						if e = wg.WalletClient.Connect(1); D.Chk(e) {
 100  							continue
 101  							// } else {
 102  							// 	break disconnected
 103  						}
 104  					} else {
 105  						D.Ln(
 106  							"chain, chainclient, wallet and client are now connected",
 107  							wg.node.Running(),
 108  							!wg.ChainClient.Disconnected(),
 109  							wg.wallet.Running(),
 110  							!wg.WalletClient.Disconnected(),
 111  						)
 112  						wg.updateChainBlock()
 113  						wg.processWalletBlockNotification()
 114  						break disconnected
 115  					}
 116  				case <-quit.Wait():
 117  					break totalOut
 118  				case <-wg.quit.Wait():
 119  					break totalOut
 120  				}
 121  			}
 122  			if wg.cx.Config.Controller.True() {
 123  				if wg.ChainClient != nil {
 124  					if e = wg.ChainClient.SetGenerate(
 125  						wg.cx.Config.Controller.True(),
 126  						wg.cx.Config.GenThreads.V(),
 127  					); !E.Chk(e) {
 128  					}
 129  				}
 130  			}
 131  		connected:
 132  			for {
 133  				select {
 134  				case <-watchTick.C:
 135  					if !wg.wallet.Running() {
 136  						D.Ln(">>>>>>>>>>>>>>>>>>>>>>>>>>>>> wallet not running, breaking out")
 137  						break connected
 138  					}
 139  					if wg.WalletClient == nil || wg.WalletClient.Disconnected() {
 140  						D.Ln(">>>>>>>>>>>>>>>>>>>>>>>>>>>>> wallet client disconnected, breaking out")
 141  						break connected
 142  					}
 143  				case <-quit.Wait():
 144  					break totalOut
 145  				case <-wg.quit.Wait():
 146  					break totalOut
 147  				}
 148  			}
 149  		}
 150  		D.Ln("shutting down watcher")
 151  		if wg.WalletClient != nil {
 152  			wg.WalletClient.Disconnect()
 153  			wg.WalletClient.Shutdown()
 154  		}
 155  		wg.wallet.Stop()
 156  	}()
 157  	return quit
 158  }
 159