diff --git a/cli/cmd/ping.go b/cli/cmd/ping.go index 550ad19..31e2cbc 100644 --- a/cli/cmd/ping.go +++ b/cli/cmd/ping.go @@ -71,9 +71,22 @@ func ping(localMac [6]byte, mac [6]byte, host string, port int, count int, delay } }() - err = client.Ping(count, delay) - if err != nil { - return err + received := 0 + for i := 0; i < count; i++ { + sentTimestamp, receivedTimestamp, err := client.Ping() + + if err != nil { + log.Errorf("%v", err) + continue + } + + received = received + 1 + rtt := receivedTimestamp - sentTimestamp + log.Infof("Response %d of %d received in %d ms", received, count, rtt) + + if i < count { + time.Sleep(delay) + } } return nil diff --git a/sdk/Client.go b/sdk/Client.go index 2924e09..a0e7815 100644 --- a/sdk/Client.go +++ b/sdk/Client.go @@ -147,54 +147,29 @@ func (c *Client) IsOpened() bool { return c.connection != nil } -func (c *Client) Ping(count int, delay time.Duration) error { - received := 0 +func (c *Client) Ping() (int64, int64, error) { + requestTc := c.getTransmissionContainer(COMMANDID_PING, payload.EmptyPayload()) + c.log.Debugf("requestTC: %v", requestTc) - for i := 0; i < count; i++ { - requestTc := c.getTransmissionContainer(COMMANDID_PING, payload.EmptyPayload()) - c.log.Debugf("requestTC: %v", requestTc) + sendTimestamp := time.Now().UnixMilli() + responseTc, err := c.transmitCommandWithResponse(requestTc) + receivedTimestamp := time.Now().UnixMilli() - sendTimestamp := time.Now().UnixMilli() - responseTc, err := c.transmitCommandWithResponse(requestTc) - receivedTimestamp := time.Now().UnixMilli() + c.log.Debugf("responseTC: %v", responseTc) - c.log.Debugf("responseTC: %v", responseTc) - - if err != nil { - //return fmt.Errorf("%v", err) - c.log.Errorf("%v", err) - continue - } - - if responseTc == nil { - //return fmt.Errorf("unexpected nil responseTc value") - c.log.Errorf("unexpected nil responseTc value") - continue - } - - if !responseTc.isResponseFor(requestTc) { - //return fmt.Errorf("received unexpected packet", responseTc) - c.log.Errorf("received unexpected packet") - continue - } - - received = received + 1 - c.log.Debugf("received %d packet(s)", received) - - rtt := receivedTimestamp - sendTimestamp - c.log.Infof("Response received in %d ms", rtt) + if err != nil { + return sendTimestamp, receivedTimestamp, fmt.Errorf("%v", err) + } - if i < count { - time.Sleep(delay) - c.log.Debugf("-------------------") - } + if responseTc == nil { + return sendTimestamp, receivedTimestamp, fmt.Errorf("unexpected nil responseTc value") } - if count != received { - return fmt.Errorf("lost packets. Sent: %d, Received: %d, Ration %f", count, received, (float32(received)/float32(count))*100.0) + if !responseTc.isResponseFor(requestTc) { + return sendTimestamp, receivedTimestamp, fmt.Errorf("received unexpected packet. %v", responseTc) } - return nil + return sendTimestamp, receivedTimestamp, nil } func (c *Client) GetMac() ([6]byte, error) { diff --git a/sdk/RealDevice_test.go b/sdk/RealDevice_test.go index 272bbeb..89fa884 100644 --- a/sdk/RealDevice_test.go +++ b/sdk/RealDevice_test.go @@ -202,11 +202,13 @@ func TestPingOnRealGateway(t *testing.T) { } }() - err = client.Ping(5) + sendTimestamp, receivedTimestamp, err = client.Ping() if err != nil { t.Logf("%v", err) t.Fail() } + + t.Logf("sendTimestamp: %d, receivedTimestamp: %d", sendTimestamp, receivedTimestamp) } func TestLoginOnRealGateway(t *testing.T) {