75 lines
1.5 KiB
Go
75 lines
1.5 KiB
Go
package context
|
|
|
|
import (
|
|
"sync"
|
|
|
|
mqtt_server "github.com/mochi-mqtt/server/v2"
|
|
|
|
"ac/internal/mqtt/message"
|
|
"ac/pkg/factory"
|
|
)
|
|
|
|
var acContext ACContext
|
|
|
|
func init() {
|
|
}
|
|
|
|
type ACContext struct {
|
|
ApPool sync.Map // map[mqtt_server.Client]*Ap
|
|
CapwapAddr string
|
|
}
|
|
|
|
type Ap struct {
|
|
Client *mqtt_server.Client
|
|
LastEcho *message.Echo
|
|
}
|
|
|
|
func InitAcContext(context *ACContext) {
|
|
config := factory.AcConfig
|
|
context.CapwapAddr = config.CapwapAddr
|
|
}
|
|
|
|
func (context *ACContext) NewAp(client *mqtt_server.Client) *Ap {
|
|
ap := Ap{Client: client}
|
|
context.ApPool.Store(client, &ap)
|
|
return &ap
|
|
}
|
|
|
|
// use mqtt_server.Client to find AP context, return *Ap and ok bit
|
|
func (context *ACContext) ApFindByClient(client *mqtt_server.Client) (*Ap, bool) {
|
|
if value, ok := context.ApPool.Load(client); ok {
|
|
return value.(*Ap), ok
|
|
}
|
|
return nil, false
|
|
}
|
|
|
|
// use clientId to find AP context, return *Ap and ok bit
|
|
func (context *ACContext) ApFindByClientId(clientId string) (ap *Ap, ok bool) {
|
|
context.ApPool.Range(func(key, value interface{}) bool {
|
|
candidate := value.(*Ap)
|
|
if ok = (candidate.Client.ID == clientId); ok {
|
|
ap = candidate
|
|
return false
|
|
}
|
|
return true
|
|
})
|
|
return
|
|
}
|
|
|
|
func (context *ACContext) DeleteAp(client *mqtt_server.Client) {
|
|
context.ApPool.Delete(client)
|
|
}
|
|
|
|
// Reset Ac Context
|
|
func (context *ACContext) Reset() {
|
|
context.ApPool.Range(func(key, value interface{}) bool {
|
|
context.ApPool.Delete(key)
|
|
return true
|
|
})
|
|
}
|
|
|
|
// Create new AC context
|
|
func GetSelf() *ACContext {
|
|
return &acContext
|
|
}
|