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