forked from CortexFoundation/torrentfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs.go
215 lines (183 loc) · 5.19 KB
/
fs.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package torrentfs
import (
"context"
"errors"
"fmt"
"github.com/CortexFoundation/CortexTheseus/log"
"github.com/CortexFoundation/CortexTheseus/p2p"
"github.com/CortexFoundation/CortexTheseus/p2p/enode"
"github.com/CortexFoundation/CortexTheseus/rpc"
"sync"
)
// TorrentFS contains the torrent file system internals.
type TorrentFS struct {
protocol p2p.Protocol // Protocol description and parameters
config *Config
monitor *Monitor
peerMu sync.RWMutex // Mutex to sync the active peer set
peers map[string]*Peer // Set of currently active peers
}
func (t *TorrentFS) storage() *TorrentManager {
return t.monitor.dl
}
func (t *TorrentFS) chain() *ChainDB {
return t.monitor.fs
}
var inst *TorrentFS = nil
func GetStorage() CortexStorage {
return inst //GetTorrentInstance()
}
// New creates a new torrentfs instance with the given configuration.
func New(config *Config, commit string, cache, compress bool) (*TorrentFS, error) {
if inst != nil {
return inst, nil
}
monitor, moErr := NewMonitor(config, cache, compress)
if moErr != nil {
log.Error("Failed create monitor")
return nil, moErr
}
inst = &TorrentFS{
config: config,
monitor: monitor,
peers: make(map[string]*Peer),
}
inst.protocol = p2p.Protocol{
Name: ProtocolName,
Version: uint(ProtocolVersion),
Length: NumberOfMessageCodes,
Run: inst.HandlePeer,
NodeInfo: func() interface{} {
return map[string]interface{}{
"version": ProtocolVersion,
"status": map[string]interface{}{
"dht": !config.DisableDHT,
"listen": inst.LocalPort(),
"root": inst.chain().Root().Hex(),
"files": inst.Congress(),
"active": inst.Candidate(),
"leafs": len(inst.chain().Blocks()),
"number": monitor.currentNumber,
"maxMessageSize": inst.MaxMessageSize(),
},
}
},
PeerInfo: func(id enode.ID) interface{} {
inst.peerMu.Lock()
defer inst.peerMu.Unlock()
if p := inst.peers[fmt.Sprintf("%x", id[:8])]; p != nil {
return map[string]interface{}{
"version": p.version,
"listen": p.Info().Listen,
"root": p.Info().Root.Hex(),
"files": p.Info().Files,
"leafs": p.Info().Leafs,
}
}
return nil
},
}
return inst, nil
}
func (tfs *TorrentFS) MaxMessageSize() uint32 {
return DefaultMaxMessageSize
}
func (tfs *TorrentFS) HandlePeer(peer *p2p.Peer, rw p2p.MsgReadWriter) error {
tfsPeer := newPeer(fmt.Sprintf("%x", peer.ID().Bytes()[:8]), tfs, peer, rw)
tfs.peerMu.Lock()
tfs.peers[tfsPeer.id] = tfsPeer
tfs.peerMu.Unlock()
defer func() {
tfs.peerMu.Lock()
delete(tfs.peers, tfsPeer.id)
tfs.peerMu.Unlock()
}()
if err := tfsPeer.handshake(); err != nil {
return err
}
tfsPeer.start()
defer func() {
tfsPeer.stop()
}()
return tfs.runMessageLoop(tfsPeer, rw)
}
func (tfs *TorrentFS) runMessageLoop(p *Peer, rw p2p.MsgReadWriter) error {
for {
// fetch the next packet
packet, err := rw.ReadMsg()
if err != nil {
log.Debug("message loop", "peer", p.peer.ID(), "err", err)
return err
}
if packet.Size > tfs.MaxMessageSize() {
log.Warn("oversized message received", "peer", p.peer.ID())
packet.Discard()
return errors.New("oversized message received")
}
log.Debug("Nas package", "size", packet.Size)
switch packet.Code {
case statusCode:
var info *PeerInfo
if err := packet.Decode(&info); err != nil {
log.Warn("failed to decode peer state, peer will be disconnected", "peer", p.peer.ID(), "err", err)
packet.Discard()
return errors.New("invalid peer state")
}
p.peerInfo = info
case messagesCode:
//
default:
}
packet.Discard()
}
}
// Protocols implements the node.Service interface.
func (tfs *TorrentFS) Protocols() []p2p.Protocol { return []p2p.Protocol{tfs.protocol} }
// APIs implements the node.Service interface.
func (tfs *TorrentFS) APIs() []rpc.API {
return []rpc.API{
{
Namespace: ProtocolName,
Version: ProtocolVersionStr,
Service: NewPublicTorrentAPI(tfs),
Public: false,
},
}
}
func (tfs *TorrentFS) Version() uint {
return tfs.protocol.Version
}
// Start starts the data collection thread and the listening server of the dashboard.
// Implements the node.Service interface.
func (tfs *TorrentFS) Start(server *p2p.Server) error {
log.Info("Started nas v.1.0", "config", tfs)
if tfs == nil || tfs.monitor == nil {
return nil
}
return tfs.monitor.Start()
}
// Stop stops the data collection thread and the connection listener of the dashboard.
// Implements the node.Service interface.
func (tfs *TorrentFS) Stop() error {
if tfs == nil || tfs.monitor == nil {
return nil
}
// Wait until every goroutine terminates.
tfs.monitor.Stop()
return nil
}
func (fs *TorrentFS) Available(ctx context.Context, infohash string, rawSize int64) (bool, error) {
return fs.storage().Available(infohash, rawSize)
}
func (fs *TorrentFS) GetFile(ctx context.Context, infohash, subpath string) ([]byte, error) {
return fs.storage().GetFile(infohash, subpath)
}
func (fs *TorrentFS) LocalPort() int {
return fs.storage().LocalPort()
}
func (fs *TorrentFS) Congress() int {
return fs.storage().Congress()
}
func (fs *TorrentFS) Candidate() int {
return fs.storage().Candidate()
}