/
connection.go
132 lines (115 loc) · 4.28 KB
/
connection.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package client
import (
"sync"
"time"
"github.com/rs/zerolog"
"github.com/sushshring/torrxfer/pkg/net"
)
// ConnectionNotificationType is an iota for all types of connection notifications
type ConnectionNotificationType uint8
const (
// ConnectionNotificationTypeConnected Connected
ConnectionNotificationTypeConnected ConnectionNotificationType = iota
// ConnectionNotificationTypeDisconnected Disconnected
ConnectionNotificationTypeDisconnected
// ConnectionNotificationTypeFilesUpdated Bytes transferred for file
ConnectionNotificationTypeFilesUpdated
// ConnectionNotificationTypeCompleted File transfer completed
ConnectionNotificationTypeCompleted
// ConnectionNotificationTypeQueryError Query error from server
ConnectionNotificationTypeQueryError
// ConnectionNotificationTypeTransferError Transfer error from server
ConnectionNotificationTypeTransferError
// ConnectionNotificationTypeFatalError Fatal transfer error
ConnectionNotificationTypeFatalError
)
// ConnectionNotificationStrings String representation of ConnectionNotificationType iota
var ConnectionNotificationStrings = map[ConnectionNotificationType]string{
ConnectionNotificationTypeConnected: "Connected",
ConnectionNotificationTypeDisconnected: "Disconnected",
ConnectionNotificationTypeFilesUpdated: "File Updated",
ConnectionNotificationTypeQueryError: "Query Error",
ConnectionNotificationTypeTransferError: "Transfer Error",
ConnectionNotificationTypeCompleted: "Completed",
}
// ServerNotification is a struct that contains details about a notification from a server transfer action
type ServerNotification struct {
NotificationType ConnectionNotificationType
Error error
Connection *ServerConnection
SentFile *File
LastSentSize uint64
}
// ServerConnection contains all active data about a connection with a Torrxfer server
type ServerConnection struct {
index uint16
address string
port uint32
connectionTime time.Time
bytesTransferred uint64
fileTransferStatus map[*File]uint64
filesTransferred map[string]*File
rpcConnection net.TorrxferServerConnection
sync.RWMutex
}
func newServerConnection(index uint16, address string, port uint32, rpcConnection net.TorrxferServerConnection) *ServerConnection {
serverConnection := &ServerConnection{
index: index,
address: address,
port: port,
connectionTime: time.Now(),
bytesTransferred: 0,
fileTransferStatus: map[*File]uint64{},
filesTransferred: map[string]*File{},
rpcConnection: rpcConnection,
}
return serverConnection
}
// GetIndex returns the index of the server for the client
func (s *ServerConnection) GetIndex() (index uint16) {
index = s.index
return
}
// GetAddress returns the address of the server
func (s *ServerConnection) GetAddress() (address string) {
address = s.address
return
}
// GetPort returns the port of the server
func (s *ServerConnection) GetPort() (port uint32) {
port = s.port
return
}
// GetConnectionTime returns the time when the server was connected
func (s *ServerConnection) GetConnectionTime() (connectionTime time.Time) {
connectionTime = s.connectionTime
return
}
// GetBytesTransferred returns the total bytes transferred to the server in this session
func (s *ServerConnection) GetBytesTransferred() (bytesTransferred uint64) {
s.RLock()
defer s.RUnlock()
bytesTransferred = s.bytesTransferred
return
}
// GetFileSizeOnServer returns the current number of bytes transferred for this file in this session
func (s *ServerConnection) GetFileSizeOnServer(filename string) (fileSize uint64) {
s.RLock()
defer s.RUnlock()
fileSize = s.fileTransferStatus[s.filesTransferred[filename]]
return
}
// GetFilesTransferred returns the files transferred to the server in this session
func (s *ServerConnection) GetFilesTransferred() (files []*File) {
s.RLock()
defer s.RUnlock()
files = make([]*File, len(s.filesTransferred))
for _, v := range s.filesTransferred {
files = append(files, v)
}
return
}
// MarshalZerologObject implements the zerolog Object Marshaller for easy connection detail logging
func (s *ServerConnection) MarshalZerologObject(e *zerolog.Event) {
e.Str("Address", s.GetAddress()).Uint32("Port", s.GetPort()).Uint64("Bytes transferred", s.GetBytesTransferred())
}