summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkali kaneko (leap communications) <kali@leap.se>2020-02-11 18:33:35 +0100
committerkali kaneko (leap communications) <kali@leap.se>2020-02-11 20:32:30 +0100
commit3de1071816e4314cd6f8d465b95bbb57038c5a2d (patch)
tree85dc975d0999ffe155d3aaa4c5cc7b5f7693568d
parent27339fc8d15198f22aa853a0237e2737c06bd442 (diff)
[feat] sip client heartbeat
-rw-r--r--pkg/auth/sip2/auth.go4
-rw-r--r--pkg/auth/sip2/client.go55
2 files changed, 45 insertions, 14 deletions
diff --git a/pkg/auth/sip2/auth.go b/pkg/auth/sip2/auth.go
index 0fe596b..72b94cd 100644
--- a/pkg/auth/sip2/auth.go
+++ b/pkg/auth/sip2/auth.go
@@ -67,12 +67,12 @@ func initializeSipConnection(skipConnect bool) (sipClient, error) {
sip := newClient(host, port, loc)
if skipConnect {
- // mainly for testing purposes at the moment
+ // for testing purposes
return sip, nil
}
sip.setCredentials(user, pass)
- _, err := sip.doConnect()
+ _, err := sip.doConnectAndReact()
if err != nil {
return sip, err
}
diff --git a/pkg/auth/sip2/client.go b/pkg/auth/sip2/client.go
index 9686e4b..d52fe2a 100644
--- a/pkg/auth/sip2/client.go
+++ b/pkg/auth/sip2/client.go
@@ -28,17 +28,20 @@ const (
Label string = "sip2"
loginRequestTemplate string = "9300CN%s|CO%s|CP%s|"
statusRequestTemplate string = "23000%s %sAO%s|AA%s|AD%s|"
+ scStatusRequest string = "9901002.00"
+ heartBeatSeconds = 240
)
type sipClient struct {
- host string
- port string
- location string
- user string
- pass string
- conn gote.Connection
- reqQueue chan request
- parser *Parser
+ host string
+ port string
+ location string
+ user string
+ pass string
+ conn gote.Connection
+ heartBeatDone chan bool
+ reqQueue chan request
+ parser *Parser
}
type request struct {
@@ -54,7 +57,7 @@ type response struct {
func newClient(host, port, location string) sipClient {
reqQ := make(chan request)
parser := getParser()
- c := sipClient{host, port, location, "", "", nil, reqQ, parser}
+ c := sipClient{host, port, location, "", "", nil, nil, reqQ, parser}
return c
}
@@ -68,6 +71,30 @@ func (c *sipClient) startDispatcher() {
}()
}
+func (c *sipClient) startHeartBeat() {
+ ticker := time.NewTicker(heartBeatSeconds * time.Second)
+ c.heartBeatDone = make(chan bool)
+ go func() {
+ for {
+ select {
+ case <-c.heartBeatDone:
+ log.Println("Stopping heartbeat")
+ return
+ case <-ticker.C:
+ resp, err := c.sendRequest(scStatusRequest)
+ /* TODO
+ for now we are only interested in letting the server
+ know that we are alive.
+ but we could parse the response fields */
+ if err != nil {
+ log.Println(">> status error:", err)
+ }
+ log.Println(">> sip status:", resp)
+ }
+ }
+ }()
+}
+
func (c *sipClient) sendRequest(msg string) (string, error) {
respChan := make(chan response)
c.reqQueue <- request{msg, respChan}
@@ -84,7 +111,7 @@ func (c *sipClient) handleRequest(msg string) (string, error) {
return resp, err
}
-func (c *sipClient) doConnect() (bool, error) {
+func (c *sipClient) doConnectAndReact() (bool, error) {
_, err := c.connect()
if err != nil {
return false, err
@@ -94,6 +121,7 @@ func (c *sipClient) doConnect() (bool, error) {
return false, err
}
c.startDispatcher()
+ c.startHeartBeat()
return true, nil
}
@@ -103,8 +131,6 @@ func (c *sipClient) setCredentials(user, pass string) {
c.pass = pass
}
-/* TODO heartbeat function -------------------------------------------------- */
-
func (c *sipClient) connect() (bool, error) {
conn, err := gote.DialTimeout("tcp", c.host+":"+c.port, time.Second*2)
if nil != err {
@@ -146,6 +172,11 @@ func (c *sipClient) login() (bool, error) {
return false, nil
}
+func (c *sipClient) close() {
+ c.heartBeatDone <- true
+ c.conn.Close()
+}
+
func (c *sipClient) parseResponse(txt string) (*message, error) {
msg, err := c.parser.parseMessage(txt)
return msg, err