File prio code added, API rewrite completed, some core features rewritten for clarity
This commit is contained in:
@@ -9,7 +9,7 @@ var (
|
||||
baseFile = `
|
||||
var authMessage = {
|
||||
MessageType: "authRequest",
|
||||
Payload: [ClientAuthString]
|
||||
Payload: {"ClientAuthString": ClientAuthString}
|
||||
}
|
||||
|
||||
var kickStart = {
|
||||
|
@@ -11,11 +11,8 @@ import (
|
||||
|
||||
//Message contains the JSON messages from the client, we first unmarshal to get the messagetype, then pass it on to each module
|
||||
type Message struct {
|
||||
MessageType string
|
||||
MessageDetail string `json:",omitempty"`
|
||||
MessageDetailTwo string `json:",omitempty"`
|
||||
MessageDetailThree string `json:",omitempty"`
|
||||
Payload []string
|
||||
MessageType string
|
||||
Payload interface{}
|
||||
}
|
||||
|
||||
//Next are the messages the server sends to the client
|
||||
@@ -56,48 +53,48 @@ type TorrentFileList struct {
|
||||
|
||||
//PeerFileList returns a slice of peers
|
||||
type PeerFileList struct {
|
||||
MessageType string `json:"MessageType"`
|
||||
TotalPeers int `json:"TotalPeers"`
|
||||
PeerList []torrent.Peer `json:"PeerList"`
|
||||
MessageType string
|
||||
TotalPeers int
|
||||
PeerList []torrent.Peer
|
||||
}
|
||||
|
||||
//TorrentFile describes a single file that a torrent client is downloading for a single torrent
|
||||
type TorrentFile struct {
|
||||
TorrentHashString string //Used to tie the file to a torrent //TODO not sure if neededs
|
||||
FileName string
|
||||
FilePath string
|
||||
FileSize string
|
||||
FilePercent string
|
||||
FilePriority string
|
||||
TorrentHashString string //Used to tie the file to a torrent //TODO not sure if needed
|
||||
FileName string //The name of the file
|
||||
FilePath string //The relative filepath to the file
|
||||
FileSize string //Humanized file size display
|
||||
FilePercent string //String value of percent of individual file percent done
|
||||
FilePriority string //Currently "High", "Normal", or "Cancel"
|
||||
}
|
||||
|
||||
//ClientDB struct contains the struct that is used to compose the torrentlist
|
||||
type ClientDB struct { //TODO maybe separate out the internal bits into another client struct
|
||||
TorrentHashString string `json:"TorrentHashString"` //Passed to client for displaying hash and is used to uniquly identify all torrents
|
||||
TorrentName string `json:"TorrentName"`
|
||||
DownloadedSize string `json:"DownloadedSize"` //how much the client has downloaded total
|
||||
Size string `json:"Size"` //total size of the torrent
|
||||
DownloadSpeed string `json:"DownloadSpeed"` //the dl speed of the torrent
|
||||
Status string `json:"Status"` //Passed to client for display
|
||||
PercentDone string `json:"PercentDone"` //Passed to client to show percent done
|
||||
ActivePeers string `json:"ActivePeers"` //passed to client
|
||||
UploadSpeed string `json:"UploadSpeed"` //passed to client to show Uploadspeed
|
||||
StoragePath string `json:"StoragePath"` //Passed to client (and stored in stormdb)
|
||||
TorrentHashString string //Passed to client for displaying hash and is used to uniquely identify all torrents
|
||||
TorrentName string //String of the name of the torrent
|
||||
DownloadedSize string //how much the client has downloaded total
|
||||
Size string //total size of the torrent
|
||||
DownloadSpeed string //the dl speed of the torrent
|
||||
Status string //Passed to client for display
|
||||
PercentDone string //Passed to client to show percent done
|
||||
ActivePeers string //passed to client
|
||||
UploadSpeed string //passed to client to show Uploadspeed
|
||||
StoragePath string //Passed to client (and stored in stormdb)
|
||||
DateAdded string //Passed to client (and stored in stormdb)
|
||||
ETA string `json:"ETA"` //Passed to client
|
||||
ETA string //Passed to client
|
||||
TorrentLabel string //Passed to client and stored in stormdb
|
||||
SourceType string `json:"SourceType"` //Stores whether the torrent came from a torrent file or a magnet link
|
||||
SourceType string //Stores whether the torrent came from a torrent file or a magnet link
|
||||
KnownSwarm []torrent.Peer //Passed to client for Peer Tab
|
||||
UploadRatio string //Passed to client, stores the string for uploadratio stored in stormdb
|
||||
TotalUploadedSize string //Humanized version of TotalUploadedBytes to pass to the client
|
||||
TotalUploadedBytes int64 //includes bytes that happened before reboot (from stormdb)
|
||||
TotalUploadedBytes int64 `json:"-"` //includes bytes that happened before reboot (from stormdb)
|
||||
downloadSpeedInt int64 //Internal used for calculating dl speed
|
||||
BytesCompleted int64 //Internal used for calculating the dl speed
|
||||
DataBytesWritten int64 //Internal used for calculating dl speed
|
||||
DataBytesRead int64 //Internal used for calculating dl speed
|
||||
UpdatedAt time.Time //Internal used for calculating speeds of upload and download
|
||||
TorrentHash metainfo.Hash //Used to create string for TorrentHashString... not sure why I have it... make that a TODO I guess
|
||||
NumberofFiles int
|
||||
NumberofPieces int
|
||||
MaxConnections int //Used to stop the torrent by limiting the max allowed connections
|
||||
BytesCompleted int64 `json:"-"` //Internal used for calculating the dl speed
|
||||
DataBytesWritten int64 `json:"-"` //Internal used for calculating dl speed
|
||||
DataBytesRead int64 `json:"-"` //Internal used for calculating dl speed
|
||||
UpdatedAt time.Time `json:"-"` //Internal used for calculating speeds of upload and download
|
||||
TorrentHash metainfo.Hash `json:"-"` //Used to create string for TorrentHashString... not sure why I have it... make that a TODO I guess
|
||||
NumberofFiles int //Number of files in the torrent
|
||||
NumberofPieces int //Total number of pieces in the torrent (Not currently used)
|
||||
MaxConnections int //Used to stop the torrent by limiting the max allowed connections
|
||||
}
|
||||
|
@@ -173,6 +173,7 @@ func StartTorrent(clientTorrent *torrent.Torrent, torrentLocalStorage Storage.To
|
||||
|
||||
}
|
||||
torrentLocalStorage.TorrentFilePriority = TorrentFilePriorityArray
|
||||
fmt.Println("TorrentUPloadLimit", torrentLocalStorage.TorrentUploadLimit)
|
||||
Storage.AddTorrentLocalStorage(torrentDbStorage, torrentLocalStorage) //writing all of the data to the database
|
||||
clientTorrent.DownloadAll() //starting the download
|
||||
CreateServerPushMessage(ServerPushMessage{MessageType: "serverPushMessage", MessageLevel: "success", Payload: "Torrent added!"}, Conn)
|
||||
@@ -214,11 +215,10 @@ func CreateRunningTorrentArray(tclient *torrent.Client, TorrentLocalArray []*Sto
|
||||
if err != nil {
|
||||
Logger.WithFields(logrus.Fields{"torrentFile": singleTorrent.Name(), "error": err}).Error("Unable to add infobytes to the torrent!")
|
||||
}
|
||||
//Logger.WithFields(logrus.Fields{"singleTorrent": singleTorrentFromStorage.TorrentName}).Info("Generating infohash")
|
||||
calculatedTotalSize := CalculateDownloadSize(singleTorrentFromStorage, singleTorrent)
|
||||
calculatedCompleteSize := CalculateCompleteSize(singleTorrentFromStorage, singleTorrent)
|
||||
calculatedCompletedSize := CalculateCompletedSize(singleTorrentFromStorage, singleTorrent)
|
||||
TempHash = singleTorrent.InfoHash()
|
||||
if (calculatedCompleteSize == singleTorrentFromStorage.TorrentSize) && (singleTorrentFromStorage.TorrentMoved == false) { //if we are done downloading and haven't moved torrent yet
|
||||
if (calculatedCompletedSize == singleTorrentFromStorage.TorrentSize) && (singleTorrentFromStorage.TorrentMoved == false) { //if we are done downloading and haven't moved torrent yet
|
||||
Logger.WithFields(logrus.Fields{"singleTorrent": singleTorrentFromStorage.TorrentName}).Info("Torrent Completed, moving...")
|
||||
MoveAndLeaveSymlink(config, singleTorrent.InfoHash().String(), db, false, "") //can take some time to move file so running this in another thread TODO make this a goroutine and skip this block if the routine is still running
|
||||
}
|
||||
@@ -229,12 +229,12 @@ func CreateRunningTorrentArray(tclient *torrent.Client, TorrentLocalArray []*Sto
|
||||
totalPeersString := fmt.Sprintf("%v", fullStruct.TotalPeers)
|
||||
fullClientDB.StoragePath = singleTorrentFromStorage.StoragePath
|
||||
|
||||
downloadedSizeHumanized := HumanizeBytes(float32(calculatedCompleteSize)) //convert size to GB if needed
|
||||
downloadedSizeHumanized := HumanizeBytes(float32(calculatedCompletedSize)) //convert size to GB if needed
|
||||
totalSizeHumanized := HumanizeBytes(float32(calculatedTotalSize))
|
||||
|
||||
fullClientDB.DownloadedSize = downloadedSizeHumanized
|
||||
fullClientDB.Size = totalSizeHumanized
|
||||
PercentDone := fmt.Sprintf("%.2f", float32(calculatedCompleteSize)/float32(calculatedTotalSize))
|
||||
PercentDone := fmt.Sprintf("%.2f", float32(calculatedCompletedSize)/float32(calculatedTotalSize))
|
||||
fullClientDB.TorrentHash = TempHash
|
||||
fullClientDB.PercentDone = PercentDone
|
||||
fullClientDB.DataBytesRead = fullStruct.ConnStats.BytesReadData //used for calculations not passed to client calculating up/down speed
|
||||
@@ -244,7 +244,7 @@ func CreateRunningTorrentArray(tclient *torrent.Client, TorrentLocalArray []*Sto
|
||||
fullClientDB.TorrentName = singleTorrentFromStorage.TorrentName
|
||||
fullClientDB.DateAdded = singleTorrentFromStorage.DateAdded
|
||||
fullClientDB.TorrentLabel = singleTorrentFromStorage.Label
|
||||
fullClientDB.BytesCompleted = calculatedCompleteSize
|
||||
fullClientDB.BytesCompleted = calculatedCompletedSize
|
||||
fullClientDB.NumberofFiles = len(singleTorrent.Files())
|
||||
|
||||
if len(PreviousTorrentArray) > 0 { //if we actually have a previous array //ranging over the previous torrent array to calculate the speed for each torrent
|
||||
@@ -256,18 +256,18 @@ func CreateRunningTorrentArray(tclient *torrent.Client, TorrentLocalArray []*Sto
|
||||
}
|
||||
}
|
||||
}
|
||||
CalculateTorrentETA(singleTorrentFromStorage.TorrentSize, calculatedCompleteSize, fullClientDB) //needs to be here since we need the speed calculated before we can estimate the eta.
|
||||
CalculateTorrentETA(singleTorrentFromStorage.TorrentSize, calculatedCompletedSize, fullClientDB) //needs to be here since we need the speed calculated before we can estimate the eta.
|
||||
|
||||
fullClientDB.TotalUploadedSize = HumanizeBytes(float32(fullClientDB.TotalUploadedBytes))
|
||||
fullClientDB.UploadRatio = CalculateUploadRatio(singleTorrent, fullClientDB) //calculate the upload ratio
|
||||
|
||||
CalculateTorrentStatus(singleTorrent, fullClientDB, config, singleTorrentFromStorage)
|
||||
CalculateTorrentStatus(singleTorrent, fullClientDB, config, singleTorrentFromStorage, calculatedCompletedSize, calculatedTotalSize)
|
||||
|
||||
tickUpdateStruct.UploadRatio = fullClientDB.UploadRatio
|
||||
tickUpdateStruct.UploadedBytes = fullClientDB.TotalUploadedBytes
|
||||
tickUpdateStruct.TorrentStatus = fullClientDB.Status
|
||||
tickUpdateStruct.Hash = fullClientDB.TorrentHashString //needed for index
|
||||
|
||||
fmt.Println("Status", tickUpdateStruct.TorrentStatus)
|
||||
Storage.UpdateStorageTick(db, tickUpdateStruct)
|
||||
RunningTorrentArray = append(RunningTorrentArray, *fullClientDB)
|
||||
|
||||
|
@@ -94,12 +94,11 @@ func CalculateDownloadSize(tFromStorage *Storage.TorrentLocal, activeTorrent *to
|
||||
return totalLength
|
||||
}
|
||||
|
||||
//CalculateCompleteSize will be used to calculate how much of the actual torrent we have completed minus the canceled files (even if they have been partially downloaded)
|
||||
func CalculateCompleteSize(tFromStorage *Storage.TorrentLocal, activeTorrent *torrent.Torrent) int64 {
|
||||
var downloadedLength int64
|
||||
//CalculateCompletedSize will be used to calculate how much of the actual torrent we have completed minus the canceled files (even if they have been partially downloaded)
|
||||
func CalculateCompletedSize(tFromStorage *Storage.TorrentLocal, activeTorrent *torrent.Torrent) int64 {
|
||||
var discardByteLength int64
|
||||
for _, storageFile := range tFromStorage.TorrentFilePriority {
|
||||
if storageFile.TorrentFilePriority != "Cancel" { //If the file is canceled
|
||||
if storageFile.TorrentFilePriority == "Cancel" { //If the file is canceled don't count it as downloaded
|
||||
for _, activeFile := range activeTorrent.Files() {
|
||||
if activeFile.DisplayPath() == storageFile.TorrentFilePath { //match the file from storage to active
|
||||
for _, piece := range activeFile.State() {
|
||||
@@ -111,7 +110,7 @@ func CalculateCompleteSize(tFromStorage *Storage.TorrentLocal, activeTorrent *to
|
||||
}
|
||||
}
|
||||
}
|
||||
downloadedLength = downloadedLength - discardByteLength
|
||||
downloadedLength := activeTorrent.BytesCompleted() - discardByteLength
|
||||
return downloadedLength
|
||||
}
|
||||
|
||||
@@ -141,22 +140,23 @@ func CalculateUploadRatio(t *torrent.Torrent, c *ClientDB) string {
|
||||
}
|
||||
|
||||
//CalculateTorrentStatus is used to determine what the STATUS column of the frontend will display ll2
|
||||
func CalculateTorrentStatus(t *torrent.Torrent, c *ClientDB, config FullClientSettings, tFromStorage *storage.TorrentLocal) {
|
||||
if (tFromStorage.TorrentStatus == "Stopped") || (float64(c.TotalUploadedBytes)/float64(t.BytesCompleted()) >= config.SeedRatioStop && tFromStorage.TorrentUploadLimit == true) { //If storage shows torrent stopped or if it is over the seeding ratio AND is under the global limit
|
||||
func CalculateTorrentStatus(t *torrent.Torrent, c *ClientDB, config FullClientSettings, tFromStorage *storage.TorrentLocal, bytesCompleted int64, totalSize int64) {
|
||||
if (tFromStorage.TorrentStatus == "Stopped") || (float64(c.TotalUploadedBytes)/float64(bytesCompleted) >= config.SeedRatioStop && tFromStorage.TorrentUploadLimit == true) { //If storage shows torrent stopped or if it is over the seeding ratio AND is under the global limit
|
||||
c.Status = "Stopped"
|
||||
c.MaxConnections = 0
|
||||
t.SetMaxEstablishedConns(0)
|
||||
} else { //Only has 2 states in storage, stopped or running, so we know it should be running, and the websocket request handled updating the database with connections and status
|
||||
bytesMissing := totalSize - bytesCompleted
|
||||
c.MaxConnections = 80
|
||||
t.SetMaxEstablishedConns(80) //TODO this should not be needed but apparently is needed
|
||||
t.DownloadAll() //ensure that we are setting the torrent to download
|
||||
if t.Seeding() && t.Stats().ActivePeers > 0 && t.BytesMissing() == 0 {
|
||||
if t.Seeding() && t.Stats().ActivePeers > 0 && bytesMissing == 0 {
|
||||
c.Status = "Seeding"
|
||||
} else if t.Stats().ActivePeers > 0 && t.BytesMissing() > 0 {
|
||||
} else if t.Stats().ActivePeers > 0 && bytesMissing > 0 {
|
||||
c.Status = "Downloading"
|
||||
} else if t.Stats().ActivePeers == 0 && t.BytesMissing() == 0 {
|
||||
} else if t.Stats().ActivePeers == 0 && bytesMissing == 0 {
|
||||
c.Status = "Completed"
|
||||
} else if t.Stats().ActivePeers == 0 && t.BytesMissing() > 0 {
|
||||
} else if t.Stats().ActivePeers == 0 && bytesMissing > 0 {
|
||||
c.Status = "Awaiting Peers"
|
||||
} else {
|
||||
c.Status = "Unknown"
|
||||
|
Reference in New Issue
Block a user