65 Star 396 Fork 128

admpub/nging

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
notice.go 4.27 KB
一键复制 编辑 原始数据 按行查看 历史
admpub 提交于 2016-12-25 17:45 . improved & added license
/*
Copyright 2016 Wenhui Shen <www.webx.top>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package notice
import (
"encoding/json"
"encoding/xml"
"fmt"
"os"
"sync"
)
const (
Succeed = 1
Failed = 0
Unauthenticated = -1
Forbid = -2
)
type Message struct {
Type string `json:"type" xml:"type"`
Title string `json:"title" xml:"title"`
Status int `json:"status" xml:"status"`
Content interface{} `json:"content" xml:"content"`
}
type Notice struct {
Types map[string]bool
Message chan *Message `json:"-" xml:"-"`
}
func NewMessageWithValue(typ string, title string, content interface{}, status ...int) *Message {
st := Succeed
if len(status) > 0 {
st = status[0]
}
return &Message{
Type: typ,
Title: title,
Status: st,
Content: content,
}
}
func NewMessage() *Message {
return &Message{}
}
func NewNotice() *Notice {
return &Notice{
Types: map[string]bool{},
Message: make(chan *Message),
}
}
type OnlineUser struct {
Notice *Notice
Clients uint
}
func NewOnlineUser() *OnlineUser {
return &OnlineUser{
Notice: NewNotice(),
Clients: 1,
}
}
type userNotices struct {
Lock *sync.RWMutex
User map[string]*OnlineUser //key: user
}
func NewUserNotices() *userNotices {
return &userNotices{
Lock: &sync.RWMutex{},
User: map[string]*OnlineUser{},
}
}
func Stdout(message *Message) {
if message.Status == Succeed {
os.Stdout.WriteString(fmt.Sprint(message.Content))
} else {
os.Stderr.WriteString(fmt.Sprint(message.Content))
}
}
func (u *userNotices) Sendable(user string, types ...string) bool {
oUser, exists := u.User[user]
if !exists {
return false
}
for _, typ := range types {
if !oUser.Notice.Types[typ] {
return false
}
}
return true
}
func (u *userNotices) Send(user string, message *Message) {
u.Lock.Lock()
defer u.Lock.Unlock()
oUser, exists := u.User[user]
if !exists {
Stdout(message)
return
}
if !oUser.Notice.Types[message.Type] {
Stdout(message)
return
}
oUser.Notice.Message <- message
}
func (u *userNotices) Recv(user string) <-chan *Message {
//race...
//u.Lock.Lock()
//defer u.Lock.Unlock()
oUser, exists := u.User[user]
if !exists {
oUser = NewOnlineUser()
u.User[user] = oUser
}
return oUser.Notice.Message
}
func (u *userNotices) RecvJSON(user string) []byte {
message := <-u.Recv(user)
b, err := json.Marshal(message)
if err != nil {
return []byte(err.Error())
}
return b
}
func (u *userNotices) RecvXML(user string) []byte {
message := <-u.Recv(user)
b, err := xml.Marshal(message)
if err != nil {
return []byte(err.Error())
}
return b
}
func (u *userNotices) CloseClient(user string) bool {
u.Lock.Lock()
defer u.Lock.Unlock()
oUser, exists := u.User[user]
if !exists {
return true
}
oUser.Clients--
if oUser.Clients <= 0 {
delete(u.User, user)
return true
}
return false
}
func (u *userNotices) OpenClient(user string) {
u.Lock.Lock()
defer u.Lock.Unlock()
oUser, exists := u.User[user]
if !exists {
oUser = NewOnlineUser()
u.User[user] = oUser
}
oUser.Clients++
}
func (u *userNotices) CloseMessage(user string, types ...string) {
oUser, exists := u.User[user]
if !exists {
return
}
if len(types) > 0 {
for _, typ := range types {
_, ok := oUser.Notice.Types[typ]
if !ok {
continue
}
delete(oUser.Notice.Types, typ)
}
} else {
oUser.Notice.Types = map[string]bool{}
}
}
func (u *userNotices) OpenMessage(user string, types ...string) {
oUser, exists := u.User[user]
if !exists {
oUser = NewOnlineUser()
u.User[user] = oUser
}
if len(types) > 0 {
for _, typ := range types {
oUser.Notice.Types[typ] = true
}
} else {
for key := range oUser.Notice.Types {
oUser.Notice.Types[key] = true
}
}
}
func (u *userNotices) Clear() {
u.User = map[string]*OnlineUser{}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/admpub/nging.git
git@gitee.com:admpub/nging.git
admpub
nging
nging
v1.0.0

搜索帮助