Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Open sidebar
leap
vpnweb
Commits
3de10718
Commit
3de10718
authored
Feb 11, 2020
by
Kali Kaneko
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[feat] sip client heartbeat
parent
27339fc8
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
45 additions
and
14 deletions
+45
-14
pkg/auth/sip2/auth.go
pkg/auth/sip2/auth.go
+2
-2
pkg/auth/sip2/client.go
pkg/auth/sip2/client.go
+43
-12
No files found.
pkg/auth/sip2/auth.go
View file @
3de10718
...
...
@@ -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
.
doConnect
AndReact
()
if
err
!=
nil
{
return
sip
,
err
}
...
...
pkg/auth/sip2/client.go
View file @
3de10718
...
...
@@ -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
)
doConnect
AndReact
()
(
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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment