Exemple #1
0
/*------------------------------------------------------------------------
 * int Command_dir(Command_t *Command, session_t *session)
 *
 * Tries to request a list of server shared files and their sizes.
 * Returns 0 on a successful transfer and nonzero on an error condition.
 * Allocates and fills out session->fileslist struct, the caller needs to
 * free it after use.
 *------------------------------------------------------------------------*/
func CommandDir(session *Session) error {

	if session == nil || session.connection == nil {
		fmt.Fprintln(os.Stderr, "Not connected to a Tsunami server")
		return errors.New("Not connected to a Tsunami server")
	}
	data := []byte(fmt.Sprintf("%v\n", tsunami.TS_DIRLIST_HACK_CMD))
	session.connection.Write(data)
	result := make([]byte, 1)
	_, err := session.connection.Read(result)
	if err != nil {
		fmt.Fprintln(os.Stderr, "Could not read response to directory request")
		return err
	}
	if result[0] == 8 {
		fmt.Fprintln(os.Stderr, "Server does no support listing of shared files")
		return errors.New("Server does no support listing of shared files")
	}

	str, err := tsunami.ReadLine(session.connection, 2048)
	if err != nil {
		return err
	}
	var numOfFile int64
	str = fmt.Sprint(string(result), str)
	if str != "" {
		numOfFile, err = strconv.ParseInt(str, 10, 32)
		if err != nil {
			return err
		}
	}

	fmt.Fprintln(os.Stderr, "Remote file list:")
	for i := 0; i < int(numOfFile); i++ {
		str, _ = tsunami.ReadLine(session.connection, 2048)
		fmt.Fprintf(os.Stderr, "%v) %v\t", i+1, str)
		str, _ = tsunami.ReadLine(session.connection, 2048)
		fmt.Fprintf(os.Stderr, "%v bytes\n", str)
	}
	fmt.Fprintln(os.Stderr, "")
	session.connection.Write([]byte{'0'})
	return nil
}
func (session *Session) multiFileRequest() ([]string, error) {
	fmt.Println("Requesting all available files")
	/* Send request and try to calculate the RTT from client to server */
	t1 := time.Now()
	_, err := session.connection.Write([]byte("*\n"))
	if err != nil {
		return nil, err
	}
	b10 := make([]byte, 10)
	l, err := session.connection.Read(b10)
	if err != nil {
		return nil, err
	}
	// filearray_size := string(b10[:l])
	t2 := time.Now()
	fmt.Println("elapsed", t1, t2)
	l, err = session.connection.Read(b10)
	if err != nil {
		return nil, err
	}
	file_count := string(b10[:l])
	_, err = session.connection.Write([]byte("got size"))
	if err != nil {
		fmt.Fprintln(os.Stderr, "Could not request file")
		return nil, err
	}

	/* See if the request was successful */
	if l < 1 {
		err = errors.New("Could not read response to file request")
		fmt.Fprintln(os.Stderr, err)
		return nil, err
	}

	/* Calculate and convert RTT to u_sec, with +10% margin */
	// d := t2.Sub(t1).Nanoseconds()
	// wait_u_sec := (d + d/10) / 1000

	// f_arrsize, _ := strconv.ParseUint(filearray_size, 10, 64)
	f_total, _ := strconv.ParseUint(file_count, 10, 64)
	if f_total <= 0 {
		/* get the \x008 failure signal */
		dummy := make([]byte, 1)
		session.connection.Read(dummy)
		err = errors.New("Server advertised no files to get")
		fmt.Fprintln(os.Stderr, err)
		return nil, err
	}
	fmt.Printf("\nServer is sharing %v files\n", f_total)

	/* Read the file list */
	file_names := make([]string, f_total)

	fmt.Printf("Multi-GET of %v files:\n", f_total)
	for i := 0; i < int(f_total); i++ {
		tmpname, err := tsunami.ReadLine(session.connection, 1024)
		if err != nil {
			return nil, err
		}
		file_names[i] = tmpname
		fmt.Print(tmpname)
	}
	session.connection.Write([]byte("got list"))
	fmt.Println("")
	return file_names, nil
}