Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
ACS
Public
Cloud
MAS
clonemap
Commits
15bd3bce
Commit
15bd3bce
authored
Jun 04, 2020
by
Stefan Dähling
Browse files
mqtt unsubscribe
parent
db048686
Pipeline
#290148
passed with stages
in 1 minute and 26 seconds
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
pkg/agency/agent.go
View file @
15bd3bce
...
@@ -108,26 +108,34 @@ func (agent *Agent) startAgent(task func(*Agent) error) (err error) {
...
@@ -108,26 +108,34 @@ func (agent *Agent) startAgent(task func(*Agent) error) (err error) {
// GetAgentID returns the agent ID
// GetAgentID returns the agent ID
func
(
agent
*
Agent
)
GetAgentID
()
(
ret
int
)
{
func
(
agent
*
Agent
)
GetAgentID
()
(
ret
int
)
{
agent
.
mutex
.
Lock
()
ret
=
agent
.
id
ret
=
agent
.
id
agent
.
mutex
.
Unlock
()
return
return
}
}
// GetAgentType returns the agent type and subtype
// GetAgentType returns the agent type and subtype
func
(
agent
*
Agent
)
GetAgentType
()
(
aType
string
,
aSubtype
string
)
{
func
(
agent
*
Agent
)
GetAgentType
()
(
aType
string
,
aSubtype
string
)
{
agent
.
mutex
.
Lock
()
aType
=
agent
.
aType
aType
=
agent
.
aType
aSubtype
=
agent
.
aSubtype
aSubtype
=
agent
.
aSubtype
agent
.
mutex
.
Unlock
()
return
return
}
}
// GetAgentName returns the agent name
// GetAgentName returns the agent name
func
(
agent
*
Agent
)
GetAgentName
()
(
ret
string
)
{
func
(
agent
*
Agent
)
GetAgentName
()
(
ret
string
)
{
agent
.
mutex
.
Lock
()
ret
=
agent
.
name
ret
=
agent
.
name
agent
.
mutex
.
Unlock
()
return
return
}
}
// GetCustomData returns custom data
// GetCustomData returns custom data
func
(
agent
*
Agent
)
GetCustomData
()
(
ret
string
)
{
func
(
agent
*
Agent
)
GetCustomData
()
(
ret
string
)
{
agent
.
mutex
.
Lock
()
ret
=
agent
.
custom
ret
=
agent
.
custom
agent
.
mutex
.
Unlock
()
return
return
}
}
...
...
pkg/agency/mqtt.go
View file @
15bd3bce
...
@@ -107,6 +107,19 @@ func (mq *MQTT) Subscribe(topic string, qos int) (err error) {
...
@@ -107,6 +107,19 @@ func (mq *MQTT) Subscribe(topic string, qos int) (err error) {
return
return
}
}
// Unsubscribe unsubscribes a topic
func
(
mq
*
MQTT
)
Unsubscribe
(
topic
string
)
(
err
error
)
{
mq
.
mutex
.
Lock
()
if
!
mq
.
active
{
mq
.
mutex
.
Unlock
()
err
=
errors
.
New
(
"mqtt not active"
)
return
}
mq
.
mutex
.
Unlock
()
err
=
mq
.
client
.
unsubscribe
(
mq
,
topic
)
return
}
// SendMessage sends a message
// SendMessage sends a message
func
(
mq
*
MQTT
)
SendMessage
(
msg
schemas
.
MQTTMessage
,
qos
int
)
(
err
error
)
{
func
(
mq
*
MQTT
)
SendMessage
(
msg
schemas
.
MQTTMessage
,
qos
int
)
(
err
error
)
{
mq
.
mutex
.
Lock
()
mq
.
mutex
.
Lock
()
...
@@ -297,47 +310,90 @@ func (cli *mqttClient) newIncomingMQTTMessage(client mqtt.Client, msg mqtt.Messa
...
@@ -297,47 +310,90 @@ func (cli *mqttClient) newIncomingMQTTMessage(client mqtt.Client, msg mqtt.Messa
// subscribe subscribes to specified topics
// subscribe subscribes to specified topics
func
(
cli
*
mqttClient
)
subscribe
(
mq
*
MQTT
,
topic
string
,
qos
int
)
(
err
error
)
{
func
(
cli
*
mqttClient
)
subscribe
(
mq
*
MQTT
,
topic
string
,
qos
int
)
(
err
error
)
{
if
cli
.
active
{
if
!
cli
.
active
{
cli
.
mutex
.
Lock
()
return
ag
,
ok
:=
cli
.
subscription
[
topic
]
}
cli
.
mutex
.
Unlock
()
cli
.
mutex
.
Lock
()
if
ok
{
ag
,
ok
:=
cli
.
subscription
[
topic
]
subscribed
:=
false
cli
.
mutex
.
Unlock
()
for
i
:=
range
ag
{
if
ok
{
if
ag
[
i
]
.
agentID
==
mq
.
agentID
{
subscribed
:=
false
subscribed
=
true
for
i
:=
range
ag
{
break
if
ag
[
i
]
.
agentID
==
mq
.
agentID
{
}
subscribed
=
true
}
break
if
!
subscribed
{
ag
=
append
(
ag
,
mq
)
cli
.
mutex
.
Lock
()
cli
.
subscription
[
topic
]
=
ag
cli
.
mutex
.
Unlock
()
}
}
else
{
cli
.
mutex
.
Lock
()
token
:=
cli
.
client
.
Subscribe
(
topic
,
byte
(
qos
),
nil
)
cli
.
mutex
.
Unlock
()
if
token
.
Wait
()
&&
token
.
Error
()
!=
nil
{
err
=
token
.
Error
()
return
}
}
ag
=
make
([]
*
MQTT
,
0
)
}
if
!
subscribed
{
ag
=
append
(
ag
,
mq
)
ag
=
append
(
ag
,
mq
)
cli
.
mutex
.
Lock
()
cli
.
mutex
.
Lock
()
cli
.
subscription
[
topic
]
=
ag
cli
.
subscription
[
topic
]
=
ag
cli
.
mutex
.
Unlock
()
cli
.
mutex
.
Unlock
()
}
}
// cli.mutex.Lock()
}
else
{
// numDel := len(cli.subscription) / 25
cli
.
mutex
.
Lock
()
// if numDel > cli.numDeliverer {
token
:=
cli
.
client
.
Subscribe
(
topic
,
byte
(
qos
),
nil
)
// for i := 0; i < numDel-cli.numDeliverer; i++ {
cli
.
mutex
.
Unlock
()
// go cli.deliverMsgs()
if
token
.
Wait
()
&&
token
.
Error
()
!=
nil
{
// }
err
=
token
.
Error
()
// cli.numDeliverer = numDel
return
// }
}
// cli.mutex.Unlock()
ag
=
make
([]
*
MQTT
,
0
)
ag
=
append
(
ag
,
mq
)
cli
.
mutex
.
Lock
()
cli
.
subscription
[
topic
]
=
ag
cli
.
mutex
.
Unlock
()
}
// cli.mutex.Lock()
// numDel := len(cli.subscription) / 25
// if numDel > cli.numDeliverer {
// for i := 0; i < numDel-cli.numDeliverer; i++ {
// go cli.deliverMsgs()
// }
// cli.numDeliverer = numDel
// }
// cli.mutex.Unlock()
return
}
// unsubscribe to a topic
func
(
cli
*
mqttClient
)
unsubscribe
(
mq
*
MQTT
,
topic
string
)
(
err
error
)
{
if
!
cli
.
active
{
return
}
cli
.
mutex
.
Lock
()
ag
,
ok
:=
cli
.
subscription
[
topic
]
cli
.
mutex
.
Unlock
()
if
!
ok
{
return
}
index
:=
-
1
for
i
:=
range
ag
{
if
mq
.
agentID
==
ag
[
i
]
.
agentID
{
index
=
i
break
}
}
if
index
==
-
1
{
return
}
if
index
==
0
&&
len
(
ag
)
==
1
{
// agent is the only one who has subscribed -> unsubscribe
delete
(
cli
.
subscription
,
topic
)
token
:=
cli
.
client
.
Unsubscribe
(
topic
)
if
token
.
Wait
()
&&
token
.
Error
()
!=
nil
{
err
=
token
.
Error
()
return
}
}
else
{
// remove agent from list of subscribed agents
ag
[
index
]
=
ag
[
len
(
ag
)
-
1
]
ag
[
len
(
ag
)
-
1
]
=
nil
ag
=
ag
[
:
len
(
ag
)
-
1
]
cli
.
mutex
.
Lock
()
cli
.
subscription
[
topic
]
=
ag
cli
.
mutex
.
Unlock
()
}
}
return
return
}
}
...
...
Write
Preview
Supports
Markdown
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