-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileTransferMiddleware.go
More file actions
150 lines (131 loc) · 3.69 KB
/
fileTransferMiddleware.go
File metadata and controls
150 lines (131 loc) · 3.69 KB
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
package p2p
import (
"github.com/symphonyprotocol/p2p/models"
"github.com/symphonyprotocol/p2p/tcp"
"github.com/symphonyprotocol/log"
"math/rand"
"time"
"math/big"
"crypto/sha256"
"strings"
"fmt"
)
var fSyncLogger = log.GetLogger("example - fileSyncLogger")
type FileDiagram struct {
models.TCPDiagram
Bytes []byte
FileHash string
}
type FileTransferMiddleware struct {
bytesSent *big.Int
bytesReceived *big.Int
succeeded *big.Int
failed *big.Int
}
func NewFileTransferMiddleware() *FileTransferMiddleware {
return &FileTransferMiddleware{
bytesSent: big.NewInt(0),
bytesReceived: big.NewInt(0),
succeeded: big.NewInt(0),
failed: big.NewInt(0),
}
}
func (d *FileTransferMiddleware) Handle(ctx *tcp.P2PContext) {
diag := ctx.Params().GetDiagram()
if (diag.GetDType() == "/file_sync") {
var fileDiag FileDiagram
err := ctx.GetDiagram(&fileDiag)
if err == nil {
fSyncLogger.Info("Good, file sync diag received, will check the sha256 sum")
d.bytesReceived.Add(d.bytesReceived, big.NewInt(int64(len(fileDiag.Bytes))))
h := sha256.New()
h.Write(fileDiag.Bytes)
hash := fmt.Sprintf("%x", h.Sum(nil))
fSyncLogger.Debug("comparing hash we calculated: %v with the hash we got: %v", hash, fileDiag.FileHash)
if strings.Compare(hash, fileDiag.FileHash) == 0 {
fSyncLogger.Info("Good, hashes are the same")
d.succeeded.Add(d.succeeded, big.NewInt(1))
} else {
fSyncLogger.Error("Boom, file transfer got damaged in the middle.")
d.failed.Add(d.failed, big.NewInt(1))
}
} else {
fSyncLogger.Error("Boom, what we got is not a 10M file sync diagram?")
}
}
ctx.Next()
}
func (d *FileTransferMiddleware) Start(ctx *tcp.P2PContext) {
rand.Seed(time.Now().Unix())
// BlockHeight = big.NewInt(rand.Int63n(50))
// go func() {
// // randomly increase the block height
// for {
// time.Sleep(time.Duration(rand.Intn(50000)) * time.Millisecond + 50)
// r := rand.Int63n(50)
// added := big.NewInt(0)
// added.Add(BlockHeight, big.NewInt(r))
// syncLogger.Debug("My Block Height increased %v -> %v", BlockHeight, added)
// BlockHeight = added
// }
// }()
go func() {
for {
time.Sleep(300 * time.Second)
// force sync, not for real case, in real case, only restart the node will do the sync, or the node will be informed if there is news.
tDiag := models.NewTCPDiagram()
tDiag.DType = "/file_sync"
tDiag.NodeID = ctx.LocalNode().GetID()
bytes := d.randomSizeBytes()
h := sha256.New()
h.Write(bytes)
hash := h.Sum(nil)
d.bytesSent.Add(d.bytesSent, big.NewInt(int64(len(bytes))))
ctx.Broadcast(&FileDiagram{
TCPDiagram: *tDiag,
Bytes: bytes,
FileHash: fmt.Sprintf("%x", hash),
})
}
}()
}
func (d *FileTransferMiddleware) AcceptConnection(*tcp.TCPConnection) {
}
func (d *FileTransferMiddleware) DropConnection(*tcp.TCPConnection) {
}
func (b *FileTransferMiddleware) DashboardData() interface{} {
return [][]string{
[]string{
"Bytes Sent:", b.bytesSent.String(),
},
[]string{
"Bytes Recieved:", b.bytesReceived.String(),
},
[]string{
"Succeeded Transfer:", b.succeeded.String(),
},
[]string{
"Failed Transfer:", b.failed.String(),
},
}
}
func (b *FileTransferMiddleware) DashboardType() string {
return "table"
}
func (b *FileTransferMiddleware) DashboardTitle() string {
return "Middleware - File Transfer (Big []byte)"
}
func (b *FileTransferMiddleware) DashboardTableHasColumnTitles() bool {
return false
}
func (b *FileTransferMiddleware) Name() string {
return "Dashboard"
}
func (b *FileTransferMiddleware) randomSizeBytes() []byte {
size := 5000000 + rand.Intn(500000)
res := make([]byte, size, size)
for n, _ := range res {
res[n] = byte(rand.Intn(255))
}
return res
}