-
Notifications
You must be signed in to change notification settings - Fork 0
/
preparation.go
118 lines (103 loc) · 2.8 KB
/
preparation.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
package pinger4
import (
"context"
"errors"
"io"
"net"
"sync"
"github.com/umenosuke/labelinglog"
)
// AddTarget is AddTarget
func (thisPinger *Pinger) AddTarget(ipAddress string, comment string) error {
thisPinger.isStarted.Lock()
defer thisPinger.isStarted.Unlock()
if thisPinger.isStarted.flg {
msg := "Pinger has already started"
thisPinger.logger.Log(labelinglog.FlgWarn, msg)
return errors.New(msg)
}
binIPAddress := net.ParseIP(ipAddress)
if binIPAddress == nil {
resolveIPAddress, err := net.ResolveIPAddr("ip4", ipAddress)
if err != nil {
msg := "parseIP fail : " + err.Error()
thisPinger.logger.Log(labelinglog.FlgWarn, msg)
return errors.New(msg)
}
binIPAddress = resolveIPAddress.IP
}
if binIPAddress == nil {
msg := "parseIP fail : " + ipAddress
thisPinger.logger.Log(labelinglog.FlgWarn, msg)
return errors.New(msg)
}
targetID := NetIP2BinIPv4Address(binIPAddress)
if _, ok := thisPinger.targets[targetID]; !ok {
thisPinger.targets[targetID] = icmpTarget{
id: targetID,
ipAddress: ipAddress,
comment: comment,
binIPAddress: binIPAddress,
netIPAddr: &net.IPAddr{IP: binIPAddress},
}
thisPinger.targetsOrder = append(thisPinger.targetsOrder, targetID)
list := make([]int64, thisPinger.config.StatisticsCountsNum)
for i := range list {
list[i] = 1
}
thisPinger.statisticsData.targets[targetID] = &sData{
Res: list,
Index: 0,
}
var cancelFuncs [responseListNum]context.CancelFunc
for i := range cancelFuncs {
cancelFuncs[i] = func() {}
}
thisPinger.timeouterList[targetID] = &struct {
sync.Mutex
cancelFunc [responseListNum]context.CancelFunc
}{
cancelFunc: cancelFuncs,
}
var req [responseListNum]struct {
isReceived bool
}
thisPinger.reqList[targetID] = &struct {
sync.Mutex
req [responseListNum]struct {
isReceived bool
}
}{
req: req,
}
} else {
msg := "add skip already added : " + ipAddress
thisPinger.logger.Log(labelinglog.FlgWarn, msg)
return errors.New(msg)
}
return nil
}
//SetLogEnableLevel a
func (thisPinger *Pinger) SetLogEnableLevel(targetLevelFlgs labelinglog.LogLevel) error {
thisPinger.isStarted.Lock()
defer thisPinger.isStarted.Unlock()
if thisPinger.isStarted.flg {
msg := "Pinger has already started"
thisPinger.logger.Log(labelinglog.FlgWarn, msg)
return errors.New(msg)
}
thisPinger.logger.SetEnableLevel(targetLevelFlgs)
return nil
}
//SetLogWriter a
func (thisPinger *Pinger) SetLogWriter(targetLevelFlgs labelinglog.LogLevel, writer io.Writer) error {
thisPinger.isStarted.Lock()
defer thisPinger.isStarted.Unlock()
if thisPinger.isStarted.flg {
msg := "Pinger has already started"
thisPinger.logger.Log(labelinglog.FlgWarn, msg)
return errors.New(msg)
}
thisPinger.logger.SetIoWriter(targetLevelFlgs, writer)
return nil
}