ing
This commit is contained in:
parent
fe733fd0e1
commit
7f63063557
161
net/ping/ping.go
161
net/ping/ping.go
|
@ -7,96 +7,101 @@ import (
|
|||
)
|
||||
|
||||
type Option interface {
|
||||
Retry() int
|
||||
Interval() int
|
||||
Deadline() int
|
||||
GetRetry() int
|
||||
GetInterval() int
|
||||
GetDeadline() int
|
||||
Validate()
|
||||
}
|
||||
|
||||
type Response interface {
|
||||
TTL() int
|
||||
Time() float32
|
||||
GetTTL() int
|
||||
GetTime() float32
|
||||
GetError() string
|
||||
}
|
||||
|
||||
type Summary interface {
|
||||
SendCount() int
|
||||
ReceiveCount() int
|
||||
LossPercent() float32
|
||||
MinTime() float32
|
||||
MaxTime() float32
|
||||
AvgTime() float32
|
||||
GetSendCount() int
|
||||
GetReceiveCount() int
|
||||
GetLossPercent() float32
|
||||
GetMinTime() float32
|
||||
GetMaxTime() float32
|
||||
GetAvgTime() float32
|
||||
}
|
||||
|
||||
type Result interface {
|
||||
Responses() map[int]Response
|
||||
Summary() Summary
|
||||
GetResponses() map[int]Response
|
||||
GetSummary() Summary
|
||||
}
|
||||
|
||||
type PingOption struct {
|
||||
retry int
|
||||
interval int
|
||||
deadline int
|
||||
Retry int `json:"retry,omitempty"`
|
||||
Interval int `json:"interval,omitempty"`
|
||||
Deadline int `json:"deadline,omitempty"`
|
||||
}
|
||||
|
||||
func (o *PingOption) Retry() int {
|
||||
return o.retry
|
||||
func (o *PingOption) GetRetry() int {
|
||||
return o.Retry
|
||||
}
|
||||
func (o *PingOption) Interval() int {
|
||||
return o.interval
|
||||
func (o *PingOption) GetInterval() int {
|
||||
return o.Interval
|
||||
}
|
||||
func (o *PingOption) Deadline() int {
|
||||
return o.deadline
|
||||
func (o *PingOption) GetDeadline() int {
|
||||
return o.Deadline
|
||||
}
|
||||
func (o *PingOption) Validate() {
|
||||
if 0 >= o.retry {
|
||||
o.retry = 1
|
||||
if 0 >= o.Retry {
|
||||
o.Retry = 1
|
||||
}
|
||||
if 0 >= o.interval {
|
||||
o.interval = 1
|
||||
if 0 >= o.Interval {
|
||||
o.Interval = 1
|
||||
}
|
||||
if 0 >= o.deadline {
|
||||
o.deadline = 1
|
||||
if 0 >= o.Deadline {
|
||||
o.Deadline = 1
|
||||
}
|
||||
}
|
||||
|
||||
type PingResponse struct {
|
||||
ttl int
|
||||
time float32
|
||||
TTL int `json:"ttl,omitempty"`
|
||||
Time float32 `json:"time,omitempty"`
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
func (r *PingResponse) TTL() int {
|
||||
return r.ttl
|
||||
func (r *PingResponse) GetTTL() int {
|
||||
return r.TTL
|
||||
}
|
||||
func (r *PingResponse) Time() float32 {
|
||||
return r.time
|
||||
func (r *PingResponse) GetTime() float32 {
|
||||
return r.Time
|
||||
}
|
||||
func (r *PingResponse) GetError() string {
|
||||
return r.Error
|
||||
}
|
||||
|
||||
type PingSummary struct {
|
||||
sendCount int
|
||||
receiveCount int
|
||||
lossPercent float32
|
||||
minTime float32
|
||||
maxTime float32
|
||||
avgTime float32
|
||||
SendCount int
|
||||
ReceiveCount int
|
||||
LossPercent float32
|
||||
MinTime float32
|
||||
MaxTime float32
|
||||
AvgTime float32
|
||||
}
|
||||
|
||||
func (s *PingSummary) SendCount() int {
|
||||
return s.sendCount
|
||||
func (s *PingSummary) GetSendCount() int {
|
||||
return s.SendCount
|
||||
}
|
||||
func (s *PingSummary) ReceiveCount() int {
|
||||
return s.receiveCount
|
||||
func (s *PingSummary) GetReceiveCount() int {
|
||||
return s.ReceiveCount
|
||||
}
|
||||
func (s *PingSummary) LossPercent() float32 {
|
||||
return s.lossPercent
|
||||
func (s *PingSummary) GetLossPercent() float32 {
|
||||
return s.LossPercent
|
||||
}
|
||||
func (s *PingSummary) MinTime() float32 {
|
||||
return s.minTime
|
||||
func (s *PingSummary) GetMinTime() float32 {
|
||||
return s.MinTime
|
||||
}
|
||||
func (s *PingSummary) MaxTime() float32 {
|
||||
return s.maxTime
|
||||
func (s *PingSummary) GetMaxTime() float32 {
|
||||
return s.MaxTime
|
||||
}
|
||||
func (s *PingSummary) AvgTime() float32 {
|
||||
return s.avgTime
|
||||
func (s *PingSummary) GetAvgTime() float32 {
|
||||
return s.AvgTime
|
||||
}
|
||||
|
||||
type PingResult struct {
|
||||
|
@ -104,10 +109,10 @@ type PingResult struct {
|
|||
summary Summary
|
||||
}
|
||||
|
||||
func (r *PingResult) Responses() map[int]Response {
|
||||
func (r *PingResult) GetResponses() map[int]Response {
|
||||
return r.responses
|
||||
}
|
||||
func (r *PingResult) Summary() Summary {
|
||||
func (r *PingResult) GetSummary() Summary {
|
||||
return r.summary
|
||||
}
|
||||
|
||||
|
@ -145,21 +150,21 @@ LOOP:
|
|||
log.Print(err)
|
||||
continue LOOP
|
||||
}
|
||||
result.summary.(*PingSummary).minTime = float32(minTime)
|
||||
result.summary.(*PingSummary).MinTime = float32(minTime)
|
||||
|
||||
maxTime, err := strconv.ParseFloat(times[2], 32)
|
||||
if nil != err {
|
||||
log.Print(err)
|
||||
continue LOOP
|
||||
}
|
||||
result.summary.(*PingSummary).maxTime = float32(maxTime)
|
||||
result.summary.(*PingSummary).MaxTime = float32(maxTime)
|
||||
|
||||
avgTime, err := strconv.ParseFloat(times[1], 32)
|
||||
if nil != err {
|
||||
log.Print(err)
|
||||
continue LOOP
|
||||
}
|
||||
result.summary.(*PingSummary).avgTime = float32(avgTime)
|
||||
result.summary.(*PingSummary).AvgTime = float32(avgTime)
|
||||
|
||||
case 8:
|
||||
if "bytes" != fields[1] || "from" != fields[2] {
|
||||
|
@ -186,8 +191,8 @@ LOOP:
|
|||
}
|
||||
|
||||
result.responses[seq] = &PingResponse{
|
||||
ttl: ttl,
|
||||
time: float32(_time),
|
||||
TTL: ttl,
|
||||
Time: float32(_time),
|
||||
}
|
||||
|
||||
case 10:
|
||||
|
@ -196,21 +201,21 @@ LOOP:
|
|||
log.Print(err)
|
||||
continue LOOP
|
||||
}
|
||||
result.summary.(*PingSummary).sendCount = sendCount
|
||||
result.summary.(*PingSummary).SendCount = sendCount
|
||||
|
||||
receiveCount, err := strconv.Atoi(fields[3])
|
||||
if nil != err {
|
||||
log.Print(err)
|
||||
continue LOOP
|
||||
}
|
||||
result.summary.(*PingSummary).receiveCount = receiveCount
|
||||
result.summary.(*PingSummary).ReceiveCount = receiveCount
|
||||
|
||||
lossPercent, err := strconv.ParseFloat(strings.Replace(fields[5], "%", "", -1), 32)
|
||||
if nil != err {
|
||||
log.Print(err)
|
||||
continue LOOP
|
||||
}
|
||||
result.summary.(*PingSummary).lossPercent = float32(lossPercent)
|
||||
result.summary.(*PingSummary).LossPercent = float32(lossPercent)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -287,8 +292,8 @@ LOOP:
|
|||
}
|
||||
|
||||
result.responses[seq] = &PingResponse{
|
||||
ttl: ttl,
|
||||
time: float32(_time),
|
||||
TTL: ttl,
|
||||
Time: float32(_time),
|
||||
}
|
||||
seq = seq + 1
|
||||
case 9:
|
||||
|
@ -303,7 +308,7 @@ LOOP:
|
|||
log.Print(err)
|
||||
continue LOOP
|
||||
}
|
||||
result.summary.(*PingSummary).minTime = float32(minTime)
|
||||
result.summary.(*PingSummary).MinTime = float32(minTime)
|
||||
|
||||
maxTimes := strings.Replace(fields[5], "ms", "", -1)
|
||||
maxTimes = strings.Replace(maxTimes, ",", "", -1)
|
||||
|
@ -312,7 +317,7 @@ LOOP:
|
|||
log.Print(err)
|
||||
continue LOOP
|
||||
}
|
||||
result.summary.(*PingSummary).maxTime = float32(maxTime)
|
||||
result.summary.(*PingSummary).MaxTime = float32(maxTime)
|
||||
|
||||
avgTimes := strings.Replace(fields[8], "ms", "", -1)
|
||||
avgTime, err := strconv.ParseFloat(avgTimes, 32)
|
||||
|
@ -320,7 +325,7 @@ LOOP:
|
|||
log.Print(err)
|
||||
continue LOOP
|
||||
}
|
||||
result.summary.(*PingSummary).avgTime = float32(avgTime)
|
||||
result.summary.(*PingSummary).AvgTime = float32(avgTime)
|
||||
|
||||
case 12:
|
||||
if "Packets:" != fields[0] {
|
||||
|
@ -331,14 +336,14 @@ LOOP:
|
|||
log.Print(err)
|
||||
continue LOOP
|
||||
}
|
||||
result.summary.(*PingSummary).sendCount = sendCount
|
||||
result.summary.(*PingSummary).SendCount = sendCount
|
||||
|
||||
receiveCount, err := strconv.Atoi(strings.Replace(fields[6], ",", "", -1))
|
||||
if nil != err {
|
||||
log.Print(err)
|
||||
continue LOOP
|
||||
}
|
||||
result.summary.(*PingSummary).receiveCount = receiveCount
|
||||
result.summary.(*PingSummary).ReceiveCount = receiveCount
|
||||
|
||||
lossPercents := strings.Replace(fields[10], "(", "", -1)
|
||||
lossPercents = strings.Replace(lossPercents, "%", "", -1)
|
||||
|
@ -347,7 +352,7 @@ LOOP:
|
|||
log.Print(err)
|
||||
continue LOOP
|
||||
}
|
||||
result.summary.(*PingSummary).lossPercent = float32(lossPercent)
|
||||
result.summary.(*PingSummary).LossPercent = float32(lossPercent)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -388,21 +393,21 @@ LOOP:
|
|||
log.Print(err)
|
||||
continue LOOP
|
||||
}
|
||||
result.summary.(*PingSummary).minTime = float32(minTime)
|
||||
result.summary.(*PingSummary).MinTime = float32(minTime)
|
||||
|
||||
maxTime, err := strconv.ParseFloat(times[2], 32)
|
||||
if nil != err {
|
||||
log.Print(err)
|
||||
continue LOOP
|
||||
}
|
||||
result.summary.(*PingSummary).maxTime = float32(maxTime)
|
||||
result.summary.(*PingSummary).MaxTime = float32(maxTime)
|
||||
|
||||
avgTime, err := strconv.ParseFloat(times[1], 32)
|
||||
if nil != err {
|
||||
log.Print(err)
|
||||
continue LOOP
|
||||
}
|
||||
result.summary.(*PingSummary).avgTime = float32(avgTime)
|
||||
result.summary.(*PingSummary).AvgTime = float32(avgTime)
|
||||
|
||||
case 8:
|
||||
if "bytes" != fields[1] || "from" != fields[2] {
|
||||
|
@ -429,8 +434,8 @@ LOOP:
|
|||
}
|
||||
|
||||
result.responses[seq] = &PingResponse{
|
||||
ttl: ttl,
|
||||
time: float32(_time),
|
||||
TTL: ttl,
|
||||
Time: float32(_time),
|
||||
}
|
||||
|
||||
case 9:
|
||||
|
@ -439,21 +444,21 @@ LOOP:
|
|||
log.Print(err)
|
||||
continue LOOP
|
||||
}
|
||||
result.summary.(*PingSummary).sendCount = sendCount
|
||||
result.summary.(*PingSummary).SendCount = sendCount
|
||||
|
||||
receiveCount, err := strconv.Atoi(fields[3])
|
||||
if nil != err {
|
||||
log.Print(err)
|
||||
continue LOOP
|
||||
}
|
||||
result.summary.(*PingSummary).receiveCount = receiveCount
|
||||
result.summary.(*PingSummary).ReceiveCount = receiveCount
|
||||
|
||||
lossPercent, err := strconv.ParseFloat(strings.Replace(fields[6], "%", "", -1), 32)
|
||||
if nil != err {
|
||||
log.Print(err)
|
||||
continue LOOP
|
||||
}
|
||||
result.summary.(*PingSummary).lossPercent = float32(lossPercent)
|
||||
result.summary.(*PingSummary).LossPercent = float32(lossPercent)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -10,8 +10,8 @@ func Ping(destination string, option Option) (Result, error) {
|
|||
|
||||
params := make([]string, 0)
|
||||
params = append(params, destination)
|
||||
params = append(params, fmt.Sprintf("-c %d", option.Retry()))
|
||||
params = append(params, fmt.Sprintf("-i %d", option.Interval()))
|
||||
params = append(params, fmt.Sprintf("-c %d", option.GetRetry()))
|
||||
params = append(params, fmt.Sprintf("-i %d", option.GetInterval()))
|
||||
|
||||
pCmd := exec.Command("ping", params...)
|
||||
output, err := pCmd.CombinedOutput()
|
||||
|
|
|
@ -21,7 +21,7 @@ func TestPing(t *testing.T) {
|
|||
args: args{
|
||||
destination: "192.168.1.1",
|
||||
option: &PingOption{
|
||||
retry: 4,
|
||||
Retry: 4,
|
||||
},
|
||||
},
|
||||
},
|
||||
|
|
|
@ -11,8 +11,8 @@ func Ping(destination string, option Option) (Result, error) {
|
|||
|
||||
params := make([]string, 0)
|
||||
params = append(params, destination)
|
||||
params = append(params, fmt.Sprintf("-c %d", option.Retry()))
|
||||
params = append(params, fmt.Sprintf("-i %d", option.Interval()))
|
||||
params = append(params, fmt.Sprintf("-c %d", option.GetRetry()))
|
||||
params = append(params, fmt.Sprintf("-i %d", option.GetInterval()))
|
||||
|
||||
pCmd := exec.Command("ping", params...)
|
||||
log.Print(pCmd.Args)
|
||||
|
|
|
@ -18,7 +18,7 @@ func TestPing(t *testing.T) {
|
|||
args: args{
|
||||
destination: "192.168.1.1",
|
||||
option: &PingOption{
|
||||
retry: 4,
|
||||
Retry: 4,
|
||||
},
|
||||
},
|
||||
},
|
||||
|
|
|
@ -21,9 +21,9 @@ func TestPingOptions_Validate(t *testing.T) {
|
|||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
o := &PingOption{
|
||||
retry: tt.fields.Retry,
|
||||
interval: tt.fields.Interval,
|
||||
deadline: tt.fields.Deadline,
|
||||
Retry: tt.fields.Retry,
|
||||
Interval: tt.fields.Interval,
|
||||
Deadline: tt.fields.Deadline,
|
||||
}
|
||||
o.Validate()
|
||||
})
|
||||
|
|
|
@ -10,7 +10,7 @@ func Ping(destination string, option Option) (Result, error) {
|
|||
|
||||
params := make([]string, 0)
|
||||
params = append(params, "/C")
|
||||
params = append(params, fmt.Sprintf("chcp 437 && ping %s -n %d -w %d", destination, option.Retry(), option.Deadline()*1000))
|
||||
params = append(params, fmt.Sprintf("chcp 437 && ping %s -n %d -w %d", destination, option.GetRetry(), option.GetDeadline()*1000))
|
||||
|
||||
pCmd := exec.Command("cmd.exe", params...)
|
||||
output, err := pCmd.CombinedOutput()
|
||||
|
|
|
@ -18,7 +18,7 @@ func TestPing(t *testing.T) {
|
|||
args: args{
|
||||
destination: "192.168.1.1",
|
||||
option: &PingOption{
|
||||
retry: 4,
|
||||
Retry: 4,
|
||||
},
|
||||
},
|
||||
},
|
||||
|
|
Loading…
Reference in New Issue
Block a user