コード例 #1
0
ファイル: udtserver.go プロジェクト: kambeena/udtgo
func handleRequest(socket *udtgo.Socket, uploadDir string) {

	defer udtgo.Close(socket)
	//receive message
	request, err := receiveRequest(socket)

	if err != nil {
		fmt.Printf("Unable to get request %s", err)
		return
	}

	//ummarshall request
	reqObject := make(map[string]interface{})
	err = json.Unmarshal([]byte(request), &reqObject)

	if err != nil {
		fmt.Printf("Unable to Unmarshal request %s", err)
		return
	}

	fileName := reqObject["fileName"].(string)

	if uploadDir != "" {
		fileName = fmt.Sprintf("%s%s", uploadDir, fileName)
	}

	base := path.Dir(fileName)

	err = os.MkdirAll(base, os.ModePerm)

	if err != nil {
		fmt.Printf("Unable to create dir path %s", err)
		return
	}

	fileSize := int64(reqObject["fileSize"].(float64))
	fmt.Printf("Received request fileName %s fileSize %v \n", fileName, fileSize)

	var offset int64 = 0

	n, err := udtgo.Recvfile(socket, fileName, &offset, fileSize)

	if err != nil {
		fmt.Printf("Unable to receive file %s %d", err, n)
		return
	}

	fi, err := os.Lstat(fileName)
	if err != nil {
		fmt.Printf("Unable read file %s", fileName)
		return
	}

	fmt.Printf("Successfully recived file %s \n", fi.Name())
}
コード例 #2
0
ファイル: udtserver.go プロジェクト: kambeena/udtgo
func main() {

	//start UDT system
	udtgo.Startup()

	portno := 9000
	network := "ip4"
	isStream := true
	uploadDir := ""

	if len(os.Args) >= 2 {
		portno, _ = strconv.Atoi(os.Args[1])
	}

	if len(os.Args) >= 3 {
		uploadDir = os.Args[2]
	}

	s, err := startServer(portno, network, isStream)
	if err != nil {
		fmt.Printf("Unable to start server at %d %s", portno)
		log.Fatal(err)
	}

	defer udtgo.Close(s)

	fmt.Printf("Started Server at port %d \n", portno)

	for {
		ns, err := udtgo.Accept(s)
		if err != nil {
			fmt.Printf("Unable to accept request on socket")
			continue
		}

		go handleRequest(ns, uploadDir)
	}

}
コード例 #3
0
ファイル: udtclient.go プロジェクト: kambeena/udtgo
func main() {

	portno := 9000
	network := "ip4"
	isStream := true
	host := "localhost"
	fileName := "myfile"

	if len(os.Args) <= 1 {
		fmt.Println("Please provide filname to be uploaded.usage : filename port server")
		return
	}

	if len(os.Args) >= 2 {
		fileName = os.Args[1]
	}

	if len(os.Args) >= 3 {
		portno, _ = strconv.Atoi(os.Args[2])
	}

	if len(os.Args) >= 4 {
		host = os.Args[3]
	}

	fmt.Printf("staring client to %s %d \n", host, portno)

	s, err := startClient(network, host, portno, isStream)

	if err != nil {
		fmt.Printf("Unable to start client %s %d error:%s", host, portno, err)
		return
	}
	defer udtgo.Close(s)

	fi, err := os.Lstat(fileName)
	if err != nil {
		fmt.Printf("Unable read file %s error:%s", fileName, err)
		return
	}

	reqContent := map[string]interface{}{
		"fileName": fileName + "_copy",
		"fileSize": fi.Size(),
	}

	msg, err := json.Marshal(reqContent)
	if err != nil {
		fmt.Println("Error encoding JSON")
		return
	}

	n, err := udtgo.Send(s, &msg[0], len(msg))

	if err != nil {
		fmt.Printf("Unable to send request %s %d", err, n)
		return
	}

	fmt.Printf("Request sent %s \n", string(msg))

	//send file
	var offset int64 = 0

	datasent, err := udtgo.Sendfile(s, fileName, &offset, fi.Size())

	if err != nil {
		fmt.Printf("Unable to send file %s %d %s", err, datasent, err)
	}

}