Add cleaning for links

This commit is contained in:
qowevisa 2024-06-08 23:18:41 +03:00
parent 718362a2d7
commit c37430c371
2 changed files with 36 additions and 2 deletions

View File

@ -12,8 +12,13 @@ type UserLink struct {
UserID uint16 UserID uint16
} }
type LinkArray struct {
Array []string
}
type LinkCenter struct { type LinkCenter struct {
Links map[string]*UserLink Links map[string]*UserLink
SavedLinks map[uint16]*LinkArray
Mu sync.Mutex Mu sync.Mutex
} }
@ -35,7 +40,18 @@ func (l *LinkCenter) AddLink(id uint16, link com.Link) error {
LeftNum: link.UseCount, LeftNum: link.UseCount,
UserID: id, UserID: id,
} }
val, found := l.SavedLinks[id]
if !found {
var tmpAr []string
tmpAr = append(tmpAr, string(link.Data))
l.SavedLinks[id] = &LinkArray{
Array: tmpAr,
}
} else {
val.Array = append(val.Array, string(link.Data))
}
log.Printf("Added link by %s\n", string(link.Data)) log.Printf("Added link by %s\n", string(link.Data))
log.Printf("SavedLinks[%d] is now %v\n", id, l.SavedLinks[id])
l.Mu.Unlock() l.Mu.Unlock()
return nil return nil
} }
@ -58,9 +74,26 @@ func (l *LinkCenter) DeleteLink(data []byte) error {
} }
func (l *LinkCenter) GetLink(data []byte) (*UserLink, error) { func (l *LinkCenter) GetLink(data []byte) (*UserLink, error) {
l.Mu.Lock()
l.Mu.Unlock()
val, found := l.Links[string(data)] val, found := l.Links[string(data)]
if !found { if !found {
return nil, ERROR_DONT_HAVE return nil, ERROR_DONT_HAVE
} }
return val, nil return val, nil
} }
func (l *LinkCenter) CleanAfterLeave(id uint16) {
log.Printf("Cleaning after id=%d left;\n", id)
ar, found := l.SavedLinks[id]
if !found {
log.Printf("Cleaning: Id=%d not found;\n", id)
return
}
l.Mu.Lock()
for _, link := range ar.Array {
log.Printf("Cleaning: Id=%d deleting %s;\n", id, link)
delete(l.Links, link)
}
l.Mu.Unlock()
}

View File

@ -163,5 +163,6 @@ func handleClient(conn net.Conn) {
log.Println("server: conn: closed") log.Println("server: conn: closed")
if isRegistered { if isRegistered {
userCenter.DeleteIfHaveOne(registeredID) userCenter.DeleteIfHaveOne(registeredID)
linkCenter.CleanAfterLeave(registeredID)
} }
} }