started adding the back to front api via websocket and json

This commit is contained in:
2017-12-24 10:42:12 -05:00
parent 2e1eb8e4e1
commit 8a5f7eaa09
37 changed files with 676 additions and 666 deletions

82
engine/clientStructs.go Normal file
View File

@@ -0,0 +1,82 @@
package engine
import (
"encoding/json"
"time"
"github.com/anacrolix/torrent"
"github.com/anacrolix/torrent/metainfo"
)
//All the message types are first, first the server handling messages from the client
//Message contains the JSON messages from the client, we first unmarshal to get the messagetype, then each module unmarshalls the actual message once we know the type
type Message struct {
MessageType string
Payload json.RawMessage
}
//GenericPayload is for any request to the server that only requires the TorrentHashString for matching (which is a lot of requests)
type GenericPayload struct {
TorrentHashString string
}
//MagnetMessage contains the magnet link entered by the user under ADD MAGNET link on the top toolbar
type MagnetMessage struct {
MagnetLink string `json:MagnetLink`
}
//TorrentCommandMessage contains a slice of strings that has the list of torrents to be acted on.
type TorrentCommandMessage struct {
TorrentHashStrings []string
}
//Next are the messages the server sends to the client
//TorrentList struct contains the torrent list that is sent to the client
type TorrentList struct { //helps create the JSON structure that react expects to recieve
MessageType string `json:"MessageType"`
Totaltorrents int `json:"total"`
ClientDBstruct []ClientDB `json:"data"`
}
//TorrentFileList supplies a list of files attached to a single torrent along with some additional information
type TorrentFileList struct {
MessageType string
TotalFiles int `json:"total"`
FileList []torrent.File `json:"fileList"`
}
//PeerFileList returns a slice of peers
type PeerFileList struct {
MessageType string
TotalPeers int
PeerList []torrent.Peer
}
//ClientDB struct contains the struct that is used to compose the torrentlist
type ClientDB struct {
TorrentName string `json:"TorrentName"`
DownloadedSize string `json:"DownloadedSize"`
Size string `json:"Size"`
DownloadSpeed string `json:"DownloadSpeed"`
downloadSpeedInt int64
UploadSpeed string `json:"UploadSpeed"`
//UploadSpeedInt int64
DataBytesWritten int64
DataBytesRead int64
ActivePeers string `json:"ActivePeers"`
TorrentHashString string `json:"TorrentHashString"`
PercentDone string `json:"PercentDone"`
TorrentHash metainfo.Hash
StoragePath string `json:"StorageLocation"`
DateAdded string
KnownSwarm []torrent.Peer
Status string `json:"Status"`
BytesCompleted int64
UpdatedAt time.Time
AddedAt string
ETA string `json:"ETA"`
Label string
SourceLocation string
}

View File

@@ -1,104 +1,185 @@
package engine //main file for all the calculations and data gathering needed for creating the running torrent array
package engine //main file for all the calculations and data gathering needed for creating the running torrent arrays
import (
"fmt"
"os"
"time"
"github.com/anacrolix/torrent"
Main "github.com/deranjer/goTorrent"
"github.com/anacrolix/torrent/metainfo"
"github.com/boltdb/bolt"
Storage "github.com/deranjer/goTorrent/storage"
)
func secondsToMinutes(inSeconds int64) string {
minutes := inSeconds / 60
seconds := inSeconds % 60
minutesString := fmt.Sprintf("%d", minutes)
secondsString := fmt.Sprintf("%d", seconds)
str := minutesString + " Min/ " + secondsString + " Sec"
return str
func timeOutInfo(clientTorrent *torrent.Torrent, seconds time.Duration) (deleted bool) { //forcing a timeout of the torrent if it doesn't load
timeout := make(chan bool, 1) //creating a timeout channel for our gotinfo
go func() {
time.Sleep(seconds * time.Second)
timeout <- true
}()
select {
case <-clientTorrent.GotInfo(): //attempting to retrieve info for torrent
fmt.Println("Recieved torrent info for...", clientTorrent.Name())
clientTorrent.DownloadAll()
return false
case <-timeout: // getting info for torrent has timed out so purging the torrent
fmt.Println("Dropping Torrent")
clientTorrent.Drop()
return true
}
}
func convertSizetoGB(t float32, d float32) (tDelta string, dDelta string) { //converting sizes to MB or GB as needed and adding string
if t > 1024 && d > 1024 {
t := fmt.Sprintf("%.2f", t/1024)
t = t + " GB"
d := fmt.Sprintf("%.2f", d/1024)
d = d + " GB"
return t, d
} else if d > 1024 || t > 1024 {
if d > 1024 {
d := fmt.Sprintf("%.2f", d/1024)
d = d + " GB"
t := fmt.Sprintf("%.2f", t)
t = t + " MB"
return t, d
//StartTorrent creates the storage.db entry and starts the torrent and adds to the running torrent array
func StartTorrent(clientTorrent *torrent.Torrent, torrentLocalStorage *Storage.TorrentLocal, torrentDbStorage *bolt.DB, dataDir string, torrentFile string, torrentFileName string) {
timeOutInfo(clientTorrent, 45) //seeing if adding the torrrent times out (giving 45 seconds)
var TempHash metainfo.Hash
TempHash = clientTorrent.InfoHash()
fmt.Println(clientTorrent.Info().Source)
torrentLocalStorage.Hash = TempHash.String() // we will store the infohash to add it back later on client restart (if needed)
torrentLocalStorage.DateAdded = time.Now().Format("Jan _2 2006")
torrentLocalStorage.StoragePath = dataDir //TODO check full path information for torrent storage
torrentLocalStorage.TorrentName = clientTorrent.Name()
torrentLocalStorage.TorrentStatus = "downloading" //by default start all the torrents as downloading.
torrentLocalStorage.TorrentType = torrentFile //either "file" or "magnet" maybe more in the future
if torrentFile == "file" {
torrentLocalStorage.TorrentFileName = torrentFileName
} else {
torrentLocalStorage.TorrentFileName = ""
}
fmt.Printf("%+v\n", torrentLocalStorage)
Storage.AddTorrentLocalStorage(torrentDbStorage, torrentLocalStorage) //writing all of the data to the database
clientTorrent.DownloadAll() //starting the download
}
//CreateRunningTorrentArray creates the entire torrent list to pass to client
func CreateRunningTorrentArray(tclient *torrent.Client, TorrentLocalArray []*Storage.TorrentLocal, PreviousTorrentArray []ClientDB, config FullClientSettings, db *bolt.DB) (RunningTorrentArray []ClientDB) {
for _, element := range TorrentLocalArray { //re-adding all the torrents we had stored from last shutdown or just added via file or magnet link
var singleTorrent *torrent.Torrent
if element.TorrentType == "file" { //if it is a file pull it from the uploaded torrent folder
//fmt.Println("Filename", element.TorrentFileName)
if _, err := os.Stat(element.TorrentFileName); err == nil { //if we CAN find the torrent, add it
//fmt.Println("Adding file name...", element.TorrentFileName)
singleTorrent, _ = tclient.AddTorrentFromFile(element.TorrentFileName)
} else { //if we cant find the torrent delete it
fmt.Println("File Error", err)
Storage.DelTorrentLocalStorage(db, element)
continue
}
} else {
elementMagnet := "magnet:?xt=urn:btih:" + element.Hash //For magnet links just need to prepend the magnet part to the hash to readd
singleTorrent, _ = tclient.AddMagnet(elementMagnet)
}
d := fmt.Sprintf("%.2f", d)
d = d + " MB"
t := fmt.Sprintf("%.2f", t/1024)
t = t + " GB"
return t, d
} else {
d := fmt.Sprintf("%.2f", d)
t := fmt.Sprintf("%.2f", t)
t = t + " MB"
d = d + " MB"
return t, d
timeOut := timeOutInfo(singleTorrent, 45)
if timeOut == true { // if we did timeout then drop the torrent from the boltdb database
Storage.DelTorrentLocalStorage(db, element) //purging torrent from the local database
}
fullClientDB := new(ClientDB)
fullStruct := singleTorrent.Stats()
//ranging over the previous torrent array to calculate the speed for each torrent
if len(PreviousTorrentArray) > 0 { //if we actually have a previous array
for _, previousElement := range PreviousTorrentArray {
TempHash := singleTorrent.InfoHash()
if previousElement.TorrentHashString == TempHash.AsString() { //matching previous to new
CalculateTorrentSpeed(singleTorrent, fullClientDB, previousElement)
}
}
}
activePeersString := fmt.Sprintf("%v", fullStruct.ActivePeers) //converting to strings
totalPeersString := fmt.Sprintf("%v", fullStruct.TotalPeers)
bytesCompletedMB := float32(singleTorrent.BytesCompleted() / 1024 / 1024)
totalSizeMB := float32(singleTorrent.Length() / 1024 / 1024)
//downloadSizeString := fmt.Sprintf("%d", bytesCompletedMB)
tSize, dSize := ConvertSizetoGB(totalSizeMB, bytesCompletedMB) //convert size to GB if needed
var TempHash metainfo.Hash
TempHash = singleTorrent.InfoHash()
fullClientDB.DownloadedSize = dSize
fullClientDB.Size = tSize
PercentDone := fmt.Sprintf("%.2f", bytesCompletedMB/totalSizeMB)
fullClientDB.TorrentHash = TempHash
fullClientDB.PercentDone = PercentDone
fullClientDB.DataBytesRead = fullStruct.ConnStats.DataBytesRead
fullClientDB.DataBytesWritten = fullStruct.ConnStats.DataBytesWritten
fullClientDB.ActivePeers = activePeersString + " / (" + totalPeersString + ")"
fullClientDB.TorrentHashString = TempHash.AsString()
fullClientDB.StoragePath = element.StoragePath
fullClientDB.TorrentName = element.TorrentName
fullClientDB.DateAdded = element.DateAdded
fullClientDB.BytesCompleted = singleTorrent.BytesCompleted()
CalculateTorrentETA(singleTorrent, fullClientDB) //calculating the ETA for the torrent
//fmt.Println("Download Speed: ", fullClientDB.DownloadSpeed)
//fmt.Println("Percent Done: ", fullClientDB.PercentDone)
//tclient.WriteStatus(os.Stdout)
CalculateTorrentStatus(singleTorrent, fullClientDB) //calculate the status of the torrent, ie downloading seeding etc
RunningTorrentArray = append(RunningTorrentArray, *fullClientDB)
}
fmt.Println("RunningTorrentArrayCreated...")
return RunningTorrentArray
}
func CalculateTorrentSpeed(t *torrent.Torrent, c *Main.ClientDB, oc Main.ClientDB) {
now := time.Now()
bytes := t.BytesCompleted()
bytesUpload := t.Stats().DataBytesWritten
dt := float32(now.Sub(oc.UpdatedAt)) // get the delta time length between now and last updated
db := float32(bytes - oc.BytesCompleted) //getting the delta bytes
rate := db * (float32(time.Second) / dt) // converting into seconds
dbU := float32(bytesUpload - oc.DataBytesWritten)
fmt.Println("BytesWritten", bytesUpload)
fmt.Println("WireBytes", t.Stats().DataBytesWritten)
fmt.Println("ChunksWritten", t.Stats().ChunksWritten)
rateUpload := dbU * (float32(time.Second) / dt)
if rate >= 0 {
rate = rate / 1024 / 1024 //creating integer to calculate ETA
c.DownloadSpeed = fmt.Sprintf("%.2f", rate)
c.DownloadSpeed = c.DownloadSpeed + " MB/s"
c.downloadSpeedInt = int64(rate)
//CreateFileListArray creates a file list for a single torrent that is selected and sent to the server
func CreateFileListArray(tclient *torrent.Client, selectedHash string) TorrentFileList {
runningTorrents := tclient.Torrents() //don't need running torrent array since we aren't adding or deleting from storage
TorrentFileListSelected := TorrentFileList{}
for _, singleTorrent := range runningTorrents {
tempHash := singleTorrent.InfoHash()
if tempHash.AsString() == selectedHash { // if our selection hash equals our torrent hash
TorrentFileListSelected.FileList = singleTorrent.Files()
TorrentFileListSelected.MessageType = "torrentFileList"
TorrentFileListSelected.TotalFiles = len(singleTorrent.Files())
return TorrentFileListSelected
}
}
if rateUpload >= 0 {
rateUpload = rateUpload / 1024 / 1024
c.UploadSpeed = fmt.Sprintf("%.2f", rateUpload)
c.UploadSpeed = c.UploadSpeed + " MB/s"
//c.UploadSpeedInt = int64(rateUpload)
}
//c.DownloadSpeed = fmt.Sprintf("%.2f", rate) //setting zero for download speed
//c.DownloadSpeed = c.DownloadSpeed + " MB/s"
c.UpdatedAt = now
return TorrentFileListSelected
}
func calculateTorrentETA(t *torrent.Torrent, c *Main.ClientDB) {
missingBytes := t.Length() - t.BytesCompleted()
missingMB := missingBytes / 1024 / 1024
if missingMB == 0 {
c.ETA = "Done"
} else if c.downloadSpeedInt == 0 {
c.ETA = "N/A"
} else {
ETASeconds := missingMB / c.downloadSpeedInt
str := secondsToMinutes(ETASeconds) //converting seconds to minutes + seconds
c.ETA = str
//CreatePeerListArray create a list of peers for the torrent and displays them
func CreatePeerListArray(tclient *torrent.Client, selectedHash string) PeerFileList {
runningTorrents := tclient.Torrents()
TorrentPeerList := PeerFileList{}
for _, singleTorrent := range runningTorrents {
tempHash := singleTorrent.InfoHash()
if tempHash.AsString() == selectedHash {
TorrentPeerList.MessageType = "torrentPeerList"
TorrentPeerList.TotalPeers = len(TorrentPeerList.PeerList)
TorrentPeerList.PeerList = singleTorrent.KnownSwarm()
return TorrentPeerList
break //only looking for one result
}
}
return TorrentPeerList
}
func calculateTorrentStatus(t *torrent.Torrent, c *Main.ClientDB) {
if t.Seeding() && t.Stats().ActivePeers > 0 && t.BytesMissing() == 0 {
c.Status = "Seeding"
} else if t.Stats().ActivePeers > 0 && t.BytesMissing() > 0 {
c.Status = "Downloading"
} else if t.Stats().ActivePeers == 0 && t.BytesMissing() == 0 {
c.Status = "Completed"
} else if t.Stats().ActivePeers == 0 && t.BytesMissing() > 0 {
c.Status = "Awaiting Peers"
} else {
c.Status = "Unknown"
//CreateTorrentDetailJSON creates the json response for a request for more torrent information
func CreateTorrentDetailJSON(tclient *torrent.Client, selectedHash string, torrentStorage *bolt.DB) ClientDB {
localTorrentInfo := Storage.FetchTorrentFromStorage(torrentStorage, []byte(selectedHash))
runningTorrents := tclient.Torrents()
TorrentDetailStruct := ClientDB{}
for _, singleTorrent := range runningTorrents { //ranging through the running torrents to find the one we are looking for
tempHash := singleTorrent.InfoHash()
if tempHash.AsString() == selectedHash {
fmt.Println("CreateTorrentDetail", localTorrentInfo)
return TorrentDetailStruct
break //only looking for one result
}
}
return TorrentDetailStruct
}

105
engine/engineMaths.go Normal file
View File

@@ -0,0 +1,105 @@
package engine
import (
"fmt"
"time"
"github.com/anacrolix/torrent"
)
func secondsToMinutes(inSeconds int64) string {
minutes := inSeconds / 60
seconds := inSeconds % 60
minutesString := fmt.Sprintf("%d", minutes)
secondsString := fmt.Sprintf("%d", seconds)
str := minutesString + " Min/ " + secondsString + " Sec"
return str
}
//ConvertSizetoGB changes the sizes
func ConvertSizetoGB(t float32, d float32) (tDelta string, dDelta string) { //converting sizes to MB or GB as needed and adding string
if t > 1024 && d > 1024 {
t := fmt.Sprintf("%.2f", t/1024)
t = t + " GB"
d := fmt.Sprintf("%.2f", d/1024)
d = d + " GB"
return t, d
} else if d > 1024 || t > 1024 {
if d > 1024 {
d := fmt.Sprintf("%.2f", d/1024)
d = d + " GB"
t := fmt.Sprintf("%.2f", t)
t = t + " MB"
return t, d
}
d := fmt.Sprintf("%.2f", d)
d = d + " MB"
t := fmt.Sprintf("%.2f", t/1024)
t = t + " GB"
return t, d
} else {
d := fmt.Sprintf("%.2f", d)
t := fmt.Sprintf("%.2f", t)
t = t + " MB"
d = d + " MB"
return t, d
}
}
//CalculateTorrentSpeed is used to calculate the torrent upload and download speed over time
func CalculateTorrentSpeed(t *torrent.Torrent, c *ClientDB, oc ClientDB) {
now := time.Now()
bytes := t.BytesCompleted()
bytesUpload := t.Stats().DataBytesWritten
dt := float32(now.Sub(oc.UpdatedAt)) // get the delta time length between now and last updated
db := float32(bytes - oc.BytesCompleted) //getting the delta bytes
rate := db * (float32(time.Second) / dt) // converting into seconds
dbU := float32(bytesUpload - oc.DataBytesWritten)
//fmt.Println("BytesWritten", bytesUpload)
//fmt.Println("WireBytes", t.Stats().DataBytesWritten)
//fmt.Println("ChunksWritten", t.Stats().ChunksWritten)
rateUpload := dbU * (float32(time.Second) / dt)
if rate >= 0 {
rate = rate / 1024 / 1024 //creating integer to calculate ETA
c.DownloadSpeed = fmt.Sprintf("%.2f", rate)
c.DownloadSpeed = c.DownloadSpeed + " MB/s"
c.downloadSpeedInt = int64(rate)
}
if rateUpload >= 0 {
rateUpload = rateUpload / 1024 / 1024
c.UploadSpeed = fmt.Sprintf("%.2f", rateUpload)
c.UploadSpeed = c.UploadSpeed + " MB/s"
}
c.UpdatedAt = now
}
//CalculateTorrentETA is used to estimate the remaining dl time of the torrent based on the speed that the MB are being downloaded
func CalculateTorrentETA(t *torrent.Torrent, c *ClientDB) {
missingBytes := t.Length() - t.BytesCompleted()
missingMB := missingBytes / 1024 / 1024
if missingMB == 0 {
c.ETA = "Done"
} else if c.downloadSpeedInt == 0 {
c.ETA = "N/A"
} else {
ETASeconds := missingMB / c.downloadSpeedInt
str := secondsToMinutes(ETASeconds) //converting seconds to minutes + seconds
c.ETA = str
}
}
//CalculateTorrentStatus is used to determine what the STATUS column of the frontend will display ll2
func CalculateTorrentStatus(t *torrent.Torrent, c *ClientDB) {
if t.Seeding() && t.Stats().ActivePeers > 0 && t.BytesMissing() == 0 {
c.Status = "Seeding"
} else if t.Stats().ActivePeers > 0 && t.BytesMissing() > 0 {
c.Status = "Downloading"
} else if t.Stats().ActivePeers == 0 && t.BytesMissing() == 0 {
c.Status = "Completed"
} else if t.Stats().ActivePeers == 0 && t.BytesMissing() > 0 {
c.Status = "Awaiting Peers"
} else {
c.Status = "Unknown"
}
}

32
engine/settings.go Normal file
View File

@@ -0,0 +1,32 @@
package engine //Settings.go contains all of the program settings
import (
"github.com/anacrolix/dht"
"github.com/anacrolix/torrent"
)
//FullCLientSettings struct is a struct that can be read into anacrolix/torrent to setup a torrent client
type FullClientSettings struct {
Version int
torrent.Config
TFileUploadFolder string
}
//FullClientSettingsNew creates a new torrent client config TODO read from a TOML file
func FullClientSettingsNew() FullClientSettings {
//Config := fullClientSettings //generate a new struct
var Config FullClientSettings
Config.Version = 1.0
Config.DataDir = "downloads" //the full OR relative path of the default download directory for torrents
Config.TFileUploadFolder = "uploadedTorrents"
Config.Seed = true
Config.DHTConfig = dht.ServerConfig{
StartingNodes: dht.GlobalBootstrapAddrs,
}
return Config
}