func Initialize(newAddressList *addresslist.SafeIPList, newSandwichSettings *settings.Settings, newShutdown func(fileindex.FileManifest)) { addressList = newAddressList sandwichSettings = newSandwichSettings shutdown = newShutdown go downloadThread() go downloadThread() go downloadThread() go downloadThread() DownloadQueue = make(chan *IPFilePair, 1000) file, err := os.Open(util.ConfPath("manifest-cache.json")) if err != nil && os.IsNotExist(err) { fileManifest = client.BuildFileManifest() } else if err != nil { log.Println(err) fileManifest = client.BuildFileManifest() } else if xml, err := ioutil.ReadAll(file); err != nil { log.Println(err) fileManifest = client.BuildFileManifest() file.Close() } else if fileManifest, err = fileindex.UnmarshalManifest(xml); err != nil { fileManifest = client.BuildFileManifest() } else { fileManifest = client.CleanManifest(fileManifest) file.Close() } go InitializeFancyStuff() if !sandwichSettings.DontOpenBrowserOnStart { webbrowser.Open("http://localhost:" + sandwichSettings.LocalServerPort) } }
func ManifestMap() IPFilePairs { fileManifest = client.CleanManifest(fileManifest) log.Println("FileManifest was updated.") fileList := make(IPFilePairs, 0, 100) for ipString, tempFileList := range fileManifest { ip := net.ParseIP(ipString) port := util.GetPort(ip) for _, fileItem := range tempFileList.List { fileList = append(fileList, &IPFilePair{NetIP(ip), port, fileItem.FileName}) } } return fileList }
func peerHandler(w http.ResponseWriter, r *http.Request) { peer_ip := r.FormValue("peer") path := r.FormValue("path") if peerCacheIP != peer_ip { if timeOut == nil || !timeOut.Stop() { fileManifest = client.CleanManifest(fileManifest) } x := fileManifest[peer_ip] if x != nil { peerCache = makeFolders(x.List) peerCacheIP = peer_ip } else { fmt.Fprintf(w, "[]") return } timeOut = time.AfterFunc(time.Minute, func() {}) } step, _ := strconv.Atoi(r.FormValue("step")) start, _ := strconv.Atoi(r.FormValue("start")) files := peerCache[path] var end int if step != 0 { end = start + step if len(files) < end { end = len(files) } } else { end = len(files) } if start > len(files) { fmt.Fprintf(w, "") } else { json_res, _ := json.Marshal(files[start:end]) w.Write(json_res) } }