forked from PoC-Consortium/aggregator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
miner.go
74 lines (65 loc) · 1.62 KB
/
miner.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
package main
import (
"crypto/md5"
"encoding/hex"
"log"
"strconv"
"sync"
cache "github.com/patrickmn/go-cache"
)
// ClientData stores all miner info
type clientData struct {
Id clientID `json:"id"`
Capacity int64 `json:"capacity"`
sync.Mutex
}
type clientID struct {
IP string `json:"ip"`
Port string `json:"port"`
MinerName string `json:"minerName"`
sync.Mutex
}
var clients *cache.Cache
// UpdateClient refreshed Miner data
func UpdateClient(ip string, port string, minerName string, capacity int64) {
cid := clientID{IP: ip, Port: port, MinerName: minerName}
cd := clientData{Id: cid, Capacity: capacity}
key := hash(&cid)
clients.SetDefault(key, &cd)
}
func hash(cd *clientID) string {
cd.Lock()
req, _ := jsonx.MarshalToString(cd)
cd.Unlock()
hash := md5.Sum([]byte(req))
hashString := hex.EncodeToString(hash[:])
return hashString
}
// DisplayMiners shows all miners
func DisplayMiners() {
var count = 0
if clients.ItemCount() == 0 {
return
}
miners := clients.Items()
for key, value := range miners {
miner := value.Object.(*clientData)
miner.Lock()
log.Println("Miner:", key, miner.Id.IP, miner.Id.MinerName, strconv.FormatFloat(float64(miner.Capacity)/1024.0, 'f', 5, 64), "TiB")
miner.Unlock()
count++
}
log.Println("Total Capacity:", strconv.FormatFloat(float64(TotalCapacity())/1024.0, 'f', 5, 64), "TiB")
}
// TotalCapacity outputs total capacity
func TotalCapacity() int64 {
var capa int64
miners := clients.Items()
for _, value := range miners {
miner := value.Object.(*clientData)
miner.Lock()
defer miner.Unlock()
capa += miner.Capacity
}
return capa
}