-
Notifications
You must be signed in to change notification settings - Fork 1
/
vishandler.go
165 lines (141 loc) · 3.94 KB
/
vishandler.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
package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
log "github.com/sirupsen/logrus"
)
func init() {
// Log as JSON instead of the default ASCII formatter.
log.SetFormatter(&log.TextFormatter{ForceColors: true})
// Output to stdout instead of the default stderr
// Can be any io.Writer, see below for File example
log.SetOutput(os.Stdout)
// Only log the warning severity or above.
//log.SetLevel(log.WarnLevel)
}
type VisNode struct {
Id string `json:"id"`
Parents []interface{} `json:"parents"`
Info VisNodeInfo `json:"info"`
}
type VisNodeInfo struct {
IP string `json:"IP"`
ISP string `json:"ISP"`
Country string `json:"country"`
Province string `json:"province"`
City string `json:"city"`
UploadBW int64 `json:"ul_bw"`
}
func (this *VisClient) handle(message []byte) {
log.Printf("[VisClient.handle] %s", string(message))
action := struct {
Action string `json:"action"`
}{}
if err := json.Unmarshal(message, &action); err != nil {
//logrus.Errorf("[Client.handle] json.Unmarshal %s", err.Error())
log.Printf("json.Unmarshal %s", err.Error())
return
}
this.CreateHandler(action.Action, message).Handle()
}
func (this *VisClient) CreateHandler(action string, payload []byte) Handler {
//log.WithFields(log.Fields{
// "animal": "walrus",
//}).Warn("A walrus appears")
switch action {
case "get_topology":
return &GETTOPOHandler{this}
case "get_stats":
return &GETStatsHandler{this}
}
return &ExceptionHandler{message: fmt.Sprintf("visclient unregnized action %s", action)}
}
type GETTOPOHandler struct {
visclient *VisClient
}
func (this *GETTOPOHandler) Handle() {
nodes := make([]VisNode, 0)
allnode := 0
p2pnode := 0
this.visclient.hub.clients.Range(func(key, value interface{}) bool {
client := value.(*Client)
log.Printf("[GETTOPOHandler] v.id %s", client.PeerId)
node := VisNode{
Id: client.PeerId,
Parents: make([]interface{}, 0),
Info: VisNodeInfo{
IP: client.conn.RemoteAddr().String(),
ISP: client.IpInfo.ISP,
Country: client.IpInfo.Country,
Province: client.IpInfo.Province,
City: client.IpInfo.City,
UploadBW: client.UploadBW,
},
}
allnode = allnode + 1
if client.isP2P {
p2pnode = p2pnode + 1
}
for _, parent := range client.treeNode.parents {
//log.Warn(client.streamMap[parent.id])
node.Parents = append(node.Parents, map[string]interface{}{
"id": parent.id,
"substreams": client.streamMap[parent.id],
})
}
nodes = append(nodes, node)
return true
})
p2pratio := 100 * float64(p2pnode) / float64(allnode)
log.Printf("--p2p ratio-----%d,%d,%f %%", allnode, p2pnode, p2pratio)
resp := map[string]interface{}{
"action": "topology",
"nodes": nodes,
"totalstreams": this.visclient.hub.P2pConfig.Live.Substreams,
"p2pratio": p2pratio,
}
this.visclient.conn.WriteJSON(resp)
}
type GETStatsHandler struct {
visclient *VisClient
}
func (this *GETStatsHandler) Handle() {
resp := map[string]interface{}{
"action": "statistics",
"result": map[string]interface{}{
"source": this.visclient.hub.Stats.CDN,
"p2p": this.visclient.hub.Stats.P2p,
},
}
this.visclient.conn.WriteJSON(resp)
}
func serveVisHttp(hub *Hub, w http.ResponseWriter, r *http.Request) {
nodes := make([]VisNode, 0)
hub.clients.Range(func(key, value interface{}) bool {
client := value.(*Client)
log.Printf("[GETTOPOHandler] v.id %s", client.PeerId)
node := VisNode{
Id: client.PeerId,
Parents: make([]interface{}, 0),
Info: VisNodeInfo{
ISP: client.IpInfo.ISP,
Country: client.IpInfo.Country,
Province: client.IpInfo.Province,
City: client.IpInfo.City,
},
}
for _, parent := range client.treeNode.parents {
node.Parents = append(node.Parents, parent.id)
}
nodes = append(nodes, node)
return true
})
resp := map[string]interface{}{
"action": "topology",
"nodes": nodes,
}
b, _ := json.Marshal(resp)
w.Write(b)
}