// Parses the information in swarm.info file and places each entry into a Peer // struct and appends that struct to the array of peers // @param string swarmPath - The path to the swarminfo file // @return error - An error can be produced when issues arise from trying to access // the swarm file or from an invalid swarm file type - otherwise error will be nil. func parseSwarminfo(swarmPath string) error { lynkName := getTLynkName(swarmPath) lynk := lynxutil.GetLynk(tLynks, lynkName) //fmt.Println(lynk) lynk.Peers = nil // Resets peers array swarmFile, err := os.Open(swarmPath) if err != nil { return err } else if !strings.Contains(swarmPath, "swarm.info") { return errors.New("Invalid File Type") } scanner := bufio.NewScanner(swarmFile) tempPeer := lynxutil.Peer{} // Scan each line for scanner.Scan() { line := strings.TrimSpace(scanner.Text()) // Trim helps with errors in \n split := strings.Split(line, ":::") tempPeer.IP = split[0] tempPeer.Port = split[1] lynk.Peers = append(lynk.Peers, tempPeer) } //fmt.Println(lynk.Peers) return swarmFile.Close() }
// JoinLynk - Function which will allow a user to join an existing link by way of its meta.info file // @param metaPath string - the path to the meta.info file which will be used to find the // information about the lynk func JoinLynk(metaPath string) error { metaFile, err := os.Open(metaPath) if err != nil { return err } lynkName := "" owner := "" scanner := bufio.NewScanner(metaFile) tempPeer := lynxutil.Peer{} // Scan each line for scanner.Scan() { line := strings.TrimSpace(scanner.Text()) // Trim helps with errors in \n split := strings.Split(line, ":::") if split[0] == "announce" { tempPeer.IP = split[metaValueIndex] } else if split[0] == "port" { tempPeer.Port = split[metaValueIndex] } else if split[0] == "lynkName" { lynkName = split[metaValueIndex] } else if split[0] == "owner" { owner = split[metaValueIndex] } else { return errors.New("Invalid Meta") } } createJoin(lynkName, metaPath) addLynk(lynkName, owner) return UpdateLynk(lynkName) // Gets all of the files for the lynk over the network }
// CreateSwarm - Creates a new swarm.info upon clicking of create button in gui // @param string name - the name of the lynk func CreateSwarm(name string) { p1 := lynxutil.Peer{IP: "", Port: "8080"} currentuser, err := user.Current() trackerDir := currentuser.HomeDir + "/Lynx/" + name + "/" + name + "_Tracker" os.Mkdir(trackerDir, 0755) _, err = os.Create(trackerDir + "/swarm.info") if err != nil { fmt.Println(err) } p1.IP = lynxutil.GetIP() addToSwarminfo(p1, trackerDir+"/swarm.info") lynxutil.FileCopy(currentuser.HomeDir+"/Lynx/"+name+"/meta.info", trackerDir+"/meta.info") }