Skip to content

Commit

Permalink
fix: [filter] fix #16
Browse files Browse the repository at this point in the history
  • Loading branch information
gallypette committed Jul 1, 2022
1 parent 70c6959 commit 06f6cc3
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 29 deletions.
51 changes: 33 additions & 18 deletions hashlookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ type (
}

HashlookupBloom struct {
hgui *hgui
tabPtr *container.TabItem
B *bloom.BloomFilter
path string
hgui *hgui
B *bloom.BloomFilter
path string
// if the download completed
Complete bool
// if the download was cancelled
Expand Down Expand Up @@ -108,7 +107,7 @@ func (c *Client) LookupSHA1(sha1 string) (resp *gabs.Container, err error) {
func NewHashlookupBloom(path string, h *hgui) *HashlookupBloom {
tmpBloom := bloom.BloomFilter{}
counter := &WriteCounter{}
tmpChan := make(chan struct{})
tmpChan := make(chan struct{}, 1)
return &HashlookupBloom{
B: &tmpBloom,
path: path,
Expand All @@ -123,6 +122,8 @@ func NewHashlookupBloom(path string, h *hgui) *HashlookupBloom {
// DownloadFilterToFilter downloads the bloom filter
// and load it without touching the disk
func (h *HashlookupBloom) DownloadFilterToFilter() error {
h.Complete = false
h.Ready = false
var err error
resp, err := http.Get(_bloomFilterDefaultUrl)
defer resp.Body.Close()
Expand All @@ -132,6 +133,10 @@ func (h *HashlookupBloom) DownloadFilterToFilter() error {
return err
}
ch := make(chan struct{})
// make sure the cancelChan is empty before starting
for len(h.cancelDownload) > 0 {
<-h.cancelDownload
}
go func() {
select {
case <-h.cancelDownload:
Expand All @@ -149,18 +154,20 @@ func (h *HashlookupBloom) DownloadFilterToFilter() error {
fmt.Println("Issue when reading from the remote %s: %s", _bloomFilterDefaultUrl, err)
} else {
h.Complete = true
h.GetFilterDetails()
h.Ready = true
h.StopBar()
h.hgui.setOffline()
}
ch <- struct{}{}
h.GetFilterDetails()
h.Ready = true
h.StopBar()
h.hgui.setOffline()
return nil
}

// DownloadFilterToFile downloads the bloom filter into
// the file at path. Beware this is blocking and takes time
func (h *HashlookupBloom) DownloadFilterToFile() error {
h.Complete = false
h.Ready = false
var err error
out, err := os.Create(h.path)
if err != nil {
Expand All @@ -169,6 +176,10 @@ func (h *HashlookupBloom) DownloadFilterToFile() error {
defer out.Close()
resp, err := http.Get(_bloomFilterDefaultUrl)
ch := make(chan struct{})
// make sure the cancelChan is empty before starting the routine
for len(h.cancelDownload) > 0 {
<-h.cancelDownload
}
go func() {
select {
case <-h.cancelDownload:
Expand Down Expand Up @@ -197,15 +208,18 @@ func (h *HashlookupBloom) DownloadFilterToFile() error {
// LoadFilterFromFile loads the bloom filter from the file
// located at path. Beware this is blocking and takes time
func (h *HashlookupBloom) LoadFilterFromFile() error {
var err error
h.B, err = bloom.LoadFilter(h.path, _bloomFilterGzip)
h.GetFilterDetails()
if err != nil {
log.Fatal(err)
if !h.Cancelled {
h.Ready = false
var err error
h.B, err = bloom.LoadFilter(h.path, _bloomFilterGzip)
h.GetFilterDetails()
if err != nil {
log.Fatal(err)
}
h.Ready = true
h.StopBar()
h.hgui.setOffline()
}
h.Ready = true
h.StopBar()
h.hgui.setOffline()
return nil
}

Expand Down Expand Up @@ -242,7 +256,8 @@ func (h *HashlookupBloom) Content() fyne.CanvasObject {
infinite := widget.NewProgressBarInfinite()
cancelBtn := widget.NewButtonWithIcon("Cancel", theme.CancelIcon(), func() {
h.cancelDownload <- struct{}{}
h.hgui.resultsTabs.Remove(h.tabPtr)
h.hgui.resultsTabs.Remove(h.hgui.bfTab.tab)
h.hgui.bfTab.isOpened = false
return
})
bottomHboxContainer := container.NewBorder(nil, nil, nil, cancelBtn, infinite)
Expand Down
51 changes: 42 additions & 9 deletions hgui.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ type hashlookupTab struct {
uri fyne.URI
}

// bloofilterTab holds the hashlookup filter and its tab-related data
type bloomfilterTab struct {
tab *container.TabItem
isOpened bool
}

type hgui struct {
win fyne.Window
app *fyne.App
Expand All @@ -31,6 +37,7 @@ type hgui struct {
toolBar *widget.Toolbar
// The Bloom filter is tied to the application
Filter *HashlookupBloom
bfTab *bloomfilterTab
}

func (h *hgui) OpenHashlooker(u fyne.URI) {
Expand Down Expand Up @@ -75,6 +82,39 @@ func (h *hgui) OpenHashlooker(u fyne.URI) {
// a special tab that presents the filter's details
// as well as its download progress
func (h *hgui) OpenBloomFilter(operation string) {
// TODO write a newBloomFilterTab function
if h.Filter == nil {
fmt.Println("filter is nil")
h.Filter = &HashlookupBloom{}
}
// There was no tab struct so let's create one
if h.bfTab == nil {
fmt.Println("bfTab is nil")
h.bfTab = &bloomfilterTab{nil, false}
}

// It opened, it already exist
if h.bfTab.isOpened {
fmt.Println("bfTab is isOpened")
// need to cancel what was happening
h.Filter.cancelDownload <- struct{}{}
// reselect the tab if already opened
for _, item := range h.resultsTabs.Items {
if item == h.bfTab.tab {
h.resultsTabs.Select(h.bfTab.tab)
h.bfTab.isOpened = true
}
}
// create fresh content
h.bfTab.tab.Content = h.Filter.Content()
// If not opened yet, we open it
} else {
h.bfTab.tab = container.NewTabItemWithIcon("Bloom filter", theme.InfoIcon(), h.Filter.Content())
h.resultsTabs.Append(h.bfTab.tab)
h.resultsTabs.Select(h.bfTab.tab)
h.bfTab.isOpened = true
}

switch operation {
case "download":
// Closing the tab won't kill it
Expand All @@ -85,6 +125,8 @@ func (h *hgui) OpenBloomFilter(operation string) {
time.Sleep(time.Second * 1)
}
if h.Filter.Cancelled {
// Reset the value in case of object reuse
h.Filter.Cancelled = false
return
}
if h.Filter.Complete {
Expand All @@ -97,13 +139,4 @@ func (h *hgui) OpenBloomFilter(operation string) {
case "remote":
go h.Filter.DownloadFilterToFilter()
}

newTab := container.NewTabItemWithIcon("Bloom filter", theme.InfoIcon(), h.Filter.Content())
h.Filter.tabPtr = newTab
h.resultsTabs.Append(newTab)
h.resultsTabs.Select(newTab)
}

func (d *hgui) doStuff(u fyne.URI) {
fmt.Println("I do stuff.")
}
3 changes: 1 addition & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ func main() {
w := a.NewWindow("Hashlookup-gui")
w.Resize(fyne.NewSize(1024, 768))

tmpBloom := HashlookupBloom{}
hgui := &hgui{win: w, offlineMode: false, Filter: &tmpBloom, app: &a}
hgui := &hgui{win: w, offlineMode: false, app: &a}
if len(os.Args) > 1 {
path, _ := filepath.Abs(os.Args[1])
root := storage.NewFileURI(path)
Expand Down

0 comments on commit 06f6cc3

Please sign in to comment.