Skip to content

Commit

Permalink
refactor ping: separate funcionality of cli client and sdk properly
Browse files Browse the repository at this point in the history
  • Loading branch information
halacs committed Jan 14, 2024
1 parent 29b8d0c commit fe5bc4e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 44 deletions.
19 changes: 16 additions & 3 deletions cli/cmd/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
55 changes: 15 additions & 40 deletions sdk/Client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion sdk/RealDevice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit fe5bc4e

Please sign in to comment.