-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnsconnector.go
110 lines (83 loc) · 1.88 KB
/
nsconnector.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
package tsuki
import (
"fmt"
"log"
"net/http"
"strings"
)
const NSPORT = ":7071"
type NSConnector interface {
ReceivedChunk(id string)
SetNSAddr(addr string)
GetNSAddr() string
IsNS(addr string) bool
Poller
}
type HTTPNSConnector struct {
Addr string
httpAddr string
ip string
}
func (c *HTTPNSConnector) ReceivedChunk(id string) {
url := fmt.Sprintf("%s/confirm/receivedChunk?chunkID=%s", c.httpAddr, id)
log.Printf("ReceivedChunk: %s", url)
go http.Get(url)
}
func (c *HTTPNSConnector) GetNSAddr() string {
return c.Addr
}
func (c *HTTPNSConnector) SetNSAddr(addr string) {
colon := strings.IndexRune(addr, ':')
if colon == -1 {
colon = len(addr)
}
c.ip = addr[:colon]
c.Addr = c.ip + NSPORT
c.httpAddr = "http://" + c.Addr
log.Printf("SetNSAddr: %#v", c)
}
func (c *HTTPNSConnector) IsNS(addr string) bool {
if c.ip == "" {
return true
}
colon := strings.IndexRune(addr, ':')
if colon == -1 {
colon = len(addr)
}
ip := addr[:colon]
return c.ip == ip
}
func (c *HTTPNSConnector) Poll() {
_, err := http.Get(c.httpAddr + "/pulse")
if err != nil {
log.Printf("warning: couldn't send hertbeat to %s", c.httpAddr + "/pulse")
}
}
type SpyNSConnector struct {
receivedChunks []string
Addr string
PulseCount int
}
func (c *SpyNSConnector) ReceivedChunk(id string) {
c.receivedChunks = append(c.receivedChunks, id)
}
func (c *SpyNSConnector) Reset() {
c.receivedChunks = nil
}
func (c *SpyNSConnector) GetNSAddr() string {
return c.Addr
}
func (c *SpyNSConnector) SetNSAddr(addr string) {
c.Addr = addr
}
func (c *SpyNSConnector) IsNS(addr string) bool {
if c.Addr == "" {
return true
}
return c.Addr == addr
}
func (c *SpyNSConnector) Poll() {
if c.Addr != "" {
c.PulseCount++
}
}