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 573a095
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 47 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ Use "halsecur [command] --help" for more information about a command.
### Ping
```bash
$ ./halsecur ping --host 192.168.3.232 --mac 54:10:EC:85:28:BB --count 3 --delay 1000
INFO[2024-01-13T22:36:49+01:00] Response received in 65 ms
INFO[2024-01-13T22:36:50+01:00] Response received in 64 ms
INFO[2024-01-13T22:36:51+01:00] Response received in 62 ms
INFO[2024-01-14T10:18:54+01:00] Response 1 of 3 received in 73 ms
INFO[2024-01-14T10:18:55+01:00] Response 2 of 3 received in 75 ms
INFO[2024-01-14T10:18:56+01:00] Response 3 of 3 received in 76 ms
```

### Get device name
Expand Down
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 573a095

Please sign in to comment.