github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/eth/downloader/api.go (about) 1 // This file is part of the go-sberex library. The go-sberex library is 2 // free software: you can redistribute it and/or modify it under the terms 3 // of the GNU Lesser General Public License as published by the Free 4 // Software Foundation, either version 3 of the License, or (at your option) 5 // any later version. 6 // 7 // The go-sberex library is distributed in the hope that it will be useful, 8 // but WITHOUT ANY WARRANTY; without even the implied warranty of 9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 10 // General Public License <http://www.gnu.org/licenses/> for more details. 11 12 package downloader 13 14 import ( 15 "context" 16 "sync" 17 18 sberex "github.com/Sberex/go-sberex" 19 "github.com/Sberex/go-sberex/event" 20 "github.com/Sberex/go-sberex/rpc" 21 ) 22 23 // PublicDownloaderAPI provides an API which gives information about the current synchronisation status. 24 // It offers only methods that operates on data that can be available to anyone without security risks. 25 type PublicDownloaderAPI struct { 26 d *Downloader 27 mux *event.TypeMux 28 installSyncSubscription chan chan interface{} 29 uninstallSyncSubscription chan *uninstallSyncSubscriptionRequest 30 } 31 32 // NewPublicDownloaderAPI create a new PublicDownloaderAPI. The API has an internal event loop that 33 // listens for events from the downloader through the global event mux. In case it receives one of 34 // these events it broadcasts it to all syncing subscriptions that are installed through the 35 // installSyncSubscription channel. 36 func NewPublicDownloaderAPI(d *Downloader, m *event.TypeMux) *PublicDownloaderAPI { 37 api := &PublicDownloaderAPI{ 38 d: d, 39 mux: m, 40 installSyncSubscription: make(chan chan interface{}), 41 uninstallSyncSubscription: make(chan *uninstallSyncSubscriptionRequest), 42 } 43 44 go api.eventLoop() 45 46 return api 47 } 48 49 // eventLoop runs an loop until the event mux closes. It will install and uninstall new 50 // sync subscriptions and broadcasts sync status updates to the installed sync subscriptions. 51 func (api *PublicDownloaderAPI) eventLoop() { 52 var ( 53 sub = api.mux.Subscribe(StartEvent{}, DoneEvent{}, FailedEvent{}) 54 syncSubscriptions = make(map[chan interface{}]struct{}) 55 ) 56 57 for { 58 select { 59 case i := <-api.installSyncSubscription: 60 syncSubscriptions[i] = struct{}{} 61 case u := <-api.uninstallSyncSubscription: 62 delete(syncSubscriptions, u.c) 63 close(u.uninstalled) 64 case event := <-sub.Chan(): 65 if event == nil { 66 return 67 } 68 69 var notification interface{} 70 switch event.Data.(type) { 71 case StartEvent: 72 notification = &SyncingResult{ 73 Syncing: true, 74 Status: api.d.Progress(), 75 } 76 case DoneEvent, FailedEvent: 77 notification = false 78 } 79 // broadcast 80 for c := range syncSubscriptions { 81 c <- notification 82 } 83 } 84 } 85 } 86 87 // Syncing provides information when this nodes starts synchronising with the Sberex network and when it's finished. 88 func (api *PublicDownloaderAPI) Syncing(ctx context.Context) (*rpc.Subscription, error) { 89 notifier, supported := rpc.NotifierFromContext(ctx) 90 if !supported { 91 return &rpc.Subscription{}, rpc.ErrNotificationsUnsupported 92 } 93 94 rpcSub := notifier.CreateSubscription() 95 96 go func() { 97 statuses := make(chan interface{}) 98 sub := api.SubscribeSyncStatus(statuses) 99 100 for { 101 select { 102 case status := <-statuses: 103 notifier.Notify(rpcSub.ID, status) 104 case <-rpcSub.Err(): 105 sub.Unsubscribe() 106 return 107 case <-notifier.Closed(): 108 sub.Unsubscribe() 109 return 110 } 111 } 112 }() 113 114 return rpcSub, nil 115 } 116 117 // SyncingResult provides information about the current synchronisation status for this node. 118 type SyncingResult struct { 119 Syncing bool `json:"syncing"` 120 Status sberex.SyncProgress `json:"status"` 121 } 122 123 // uninstallSyncSubscriptionRequest uninstalles a syncing subscription in the API event loop. 124 type uninstallSyncSubscriptionRequest struct { 125 c chan interface{} 126 uninstalled chan interface{} 127 } 128 129 // SyncStatusSubscription represents a syncing subscription. 130 type SyncStatusSubscription struct { 131 api *PublicDownloaderAPI // register subscription in event loop of this api instance 132 c chan interface{} // channel where events are broadcasted to 133 unsubOnce sync.Once // make sure unsubscribe logic is executed once 134 } 135 136 // Unsubscribe uninstalls the subscription from the DownloadAPI event loop. 137 // The status channel that was passed to subscribeSyncStatus isn't used anymore 138 // after this method returns. 139 func (s *SyncStatusSubscription) Unsubscribe() { 140 s.unsubOnce.Do(func() { 141 req := uninstallSyncSubscriptionRequest{s.c, make(chan interface{})} 142 s.api.uninstallSyncSubscription <- &req 143 144 for { 145 select { 146 case <-s.c: 147 // drop new status events until uninstall confirmation 148 continue 149 case <-req.uninstalled: 150 return 151 } 152 } 153 }) 154 } 155 156 // SubscribeSyncStatus creates a subscription that will broadcast new synchronisation updates. 157 // The given channel must receive interface values, the result can either 158 func (api *PublicDownloaderAPI) SubscribeSyncStatus(status chan interface{}) *SyncStatusSubscription { 159 api.installSyncSubscription <- status 160 return &SyncStatusSubscription{api: api, c: status} 161 }