You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
notify/starnotify/define.go

110 lines
1.8 KiB
Go

package starnotify
import (
"b612.me/notify"
"errors"
"sync"
)
var (
cmu sync.RWMutex
smu sync.RWMutex
starClient map[string]notify.Client
starServer map[string]notify.Server
)
func init() {
starClient = make(map[string]notify.Client)
starServer = make(map[string]notify.Server)
}
func NewClient(key string) notify.Client {
client := notify.NewClient()
cmu.Lock()
4 years ago
starClient[key] = client
cmu.Unlock()
return client
4 years ago
}
func DeleteClient(key string) (err error) {
cmu.RLock()
client, ok := starClient[key]
cmu.RUnlock()
if !ok {
return errors.New("Not Exists Yet!")
}
if client.Status().Alive {
err = client.Stop()
}
client = nil
cmu.Lock()
delete(starClient, key)
cmu.Unlock()
return err
}
func NewServer(key string) notify.Server {
server := notify.NewServer()
smu.Lock()
starServer[key] = server
smu.Unlock()
return server
}
func DeleteServer(key string) error {
smu.RLock()
server, ok := starServer[key]
smu.RUnlock()
if !ok {
return errors.New("Not Exists Yet!")
}
if server.Status().Alive {
server.Stop()
}
server = nil
smu.Lock()
delete(starServer, key)
smu.Unlock()
return nil
}
func S(key string) notify.Server {
smu.RLock()
server, ok := starServer[key]
smu.RUnlock()
if !ok {
return nil
}
return server
}
func C(key string) notify.Client {
cmu.RLock()
client, ok := starClient[key]
cmu.RUnlock()
if !ok {
return nil
}
return client
}
func Server(key string) (notify.Server, error) {
smu.RLock()
server, ok := starServer[key]
smu.RUnlock()
if !ok {
return nil, errors.New("Not Exists Yet")
}
return server, nil
}
func Client(key string) (notify.Client, error) {
cmu.RLock()
client, ok := starClient[key]
cmu.RUnlock()
if !ok {
return nil, errors.New("Not Exists Yet")
}
return client, nil
}