// 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 }
// 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() }