Exemplo n.º 1
0
//Init is called initlally when requested retrieving..
func (h *RetrieveHandler) Init(msg []byte) ([]byte, storjtelehash.HandleMessage, error) {
	h.status = &RetrieveAccepting
	logging.Println(h.status.Desc)

	recv := struct {
		FileHash string
		UtpIP    string
		UtpPort  int
	}{}
	var err error
	err = json.Unmarshal(msg, &recv)
	if err != nil {
		return nil, nil, err
	}
	h.fileHash, err = hex.DecodeString(recv.FileHash)
	if err != nil {
		return nil, nil, err
	}
	RetrievingFiles.SetStatus(h.fileHash, h)
	h.Utp, err = storjutp.NewStorjUtp(0)
	if err != nil {
		return nil, nil, err
	}
	h.fileInfo, err = h.Utp.SendFile(recv.UtpIP, recv.UtpPort, DownloadDir+"/"+recv.FileHash, h.utpHandler)
	if err != nil {
		return nil, nil, err
	}
	h.StartMeasuring(h.fileInfo)
	return nil, nil, nil
}
Exemplo n.º 2
0
//Init is called initlally when requesting retrieving..
func (h *RetrieveHandler) Init(msg []byte) ([]byte, storjtelehash.HandleMessage, error) {
	var err error
	h.status = &RetriveRequesting
	logging.Println(h.status.Desc)
	h.Utp, err = storjutp.NewStorjUtp(0)
	if err != nil {
		return nil, nil, err
	}
	logging.Println("registered hash ", hex.EncodeToString(h.fileHash), "size", int64(h.fileSize))
	h.fileInfo, err = h.Utp.RegisterFile(h.fileHash, DownloadDir+"/", int64(h.fileSize), h.utpHandler)
	if err != nil {
		return nil, nil, err
	}
	h.StartMeasuring(h.fileInfo)
	RetrievingFiles.SetStatus(h.fileHash, h)
	rpacket := struct {
		FileHash string
		UtpIP    string
		UtpPort  int
	}{
		hex.EncodeToString(h.fileHash),
		telehash.GlobalIP,
		h.Utp.GetPort(),
	}
	res, err := json.Marshal(rpacket)
	return res, nil, err
}
Exemplo n.º 3
0
//SendFile is called after init(), when sending files.
func (h *UploadHandler) SendFile(msg []byte) ([]byte, storjtelehash.HandleMessage, error) {
	h.status = &Sending
	logging.Println(h.status.Desc)
	recv := struct {
		UtpIP            string
		UtpPort          int
		TelehashLocation string
	}{}
	var err error
	err = json.Unmarshal(msg, &recv)
	if err != nil {
		return nil, nil, err
	}
	h.destination = recv.TelehashLocation
	h.Utp, err = storjutp.NewStorjUtp(0)
	if err != nil {
		return nil, nil, err
	}
	h.fileInfo, err = h.Utp.SendFile(recv.UtpIP, recv.UtpPort, h.file, h.utpHandler)
	if err != nil {
		return nil, nil, err
	}
	h.tagInfo, err = h.Utp.SendFile(recv.UtpIP, recv.UtpPort, h.tag, h.utpHandler)
	h.StartMeasuring(h.fileInfo, h.tagInfo)

	callHeartbeat(h.fileHash, h.destination, h.hb)

	return nil, nil, err

}
Exemplo n.º 4
0
//Init is called initlally when reqeusted farming.
func (h *UploadHandler) Init(msg []byte) ([]byte, storjtelehash.HandleMessage, error) {
	h.status = &Accepting
	logging.Println(h.status.Desc)

	recv := struct {
		FileHash string
		TagHash  string
		FileSize float64
		TagSize  float64
	}{}
	var err error
	err = json.Unmarshal(msg, &recv)
	if err != nil {
		return nil, nil, err
	}
	h.fileHash, err = hex.DecodeString(recv.FileHash)
	if err != nil {
		return nil, nil, err
	}
	StoredFiles.SetStatus(h.fileHash, h)
	h.tagHash, err = hex.DecodeString(recv.TagHash)
	if err != nil {
		return nil, nil, err
	}
	h.Utp, err = storjutp.NewStorjUtp(0)
	if err != nil {
		return nil, nil, err
	}
	logging.Println("registered hash ", hex.EncodeToString(h.fileHash), "size", int64(recv.FileSize))
	h.fileInfo, err = h.Utp.RegisterFile(h.fileHash, DownloadDir, int64(recv.FileSize), h.utpHandler)
	if err != nil {
		return nil, nil, err
	}
	logging.Println("registered hash ", hex.EncodeToString(h.tagHash), "size", int64(recv.TagSize))
	h.tagInfo, err = h.Utp.RegisterFile(h.tagHash, DownloadDir, int64(recv.TagSize), h.utpHandler)
	if err != nil {
		return nil, nil, err
	}
	loc, err := telehash.GetMyLocation()
	if err != nil {
		return nil, nil, err
	}
	h.StartMeasuring(h.fileInfo, h.tagInfo)
	rpacket := struct {
		UtpIP            string
		UtpPort          int
		TelehashLocation string
	}{
		telehash.GlobalIP,
		h.Utp.GetPort(),
		loc,
	}
	res, err := json.Marshal(&rpacket)
	return res, nil, err
}