From 8faca5dd5291bc5fea51e5084b1f29a1b5a22ee6 Mon Sep 17 00:00:00 2001 From: "dmy@lab" Date: Wed, 13 Jan 2016 22:17:12 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=9F=E8=AE=A1=E4=B8=AD=E6=B2=A1=E6=9C=89?= =?UTF-8?q?=E7=94=A8List=E7=9A=84=E6=8C=87=E9=92=88=EF=BC=8C=E9=80=A0?= =?UTF-8?q?=E6=88=90=E4=BA=86=E9=97=AE=E9=A2=98=E3=80=82=E5=BA=94=E8=AF=A5?= =?UTF-8?q?=E6=98=AF=E4=B8=8E=E7=BB=93=E6=9E=84=E6=8B=B7=E8=B4=9D=E6=9C=89?= =?UTF-8?q?=E5=85=B3=E3=80=82=E5=B7=B2=E8=A7=A3=E5=86=B3=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- stat/tunnelstatus.go | 84 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 67 insertions(+), 17 deletions(-) diff --git a/stat/tunnelstatus.go b/stat/tunnelstatus.go index 2eb12a5..384de1b 100644 --- a/stat/tunnelstatus.go +++ b/stat/tunnelstatus.go @@ -8,9 +8,11 @@ import ( "sync" ) +var mux sync.Mutex + type TunnelStatusManager struct { - mux sync.Mutex - tunnelList list.List + // mux sync.Mutex + tunnelList *list.List regChan chan interface{} unregChan chan interface{} } @@ -19,6 +21,7 @@ func NewTunnelStatusManager() *TunnelStatusManager { t := new(TunnelStatusManager) t.regChan = make(chan interface{}) t.unregChan = make(chan interface{}) + t.tunnelList = new(list.List) go t.chanListener() return t } @@ -32,33 +35,75 @@ func (this *TunnelStatusManager) GetUnregChan() chan interface{} { } func (this *TunnelStatusManager) register(t interface{}) { - this.mux.Lock() - defer this.mux.Unlock() + // this.mux.Lock() + // defer this.mux.Unlock() + mux.Lock() + defer mux.Unlock() this.tunnelList.PushBack(t) + + l := this.tunnelList + // c := 0 + // for e := l.Front(); e != nil; e = e.Next() { + // c++ + // if c > l.Len() { + // log.Error("out of bound of list") + // } + // if e.Value == nil { + // log.Debug("a nil len of list %d c %d", l.Len(), c) + // back := l.Back() + // if back == nil { + // log.Debug("back is nill") + // } + // } + + // } + m := t.(*model.Tunnel) + log.Info("tunnel %s registerd len %d", m.GetID(), l.Len()) + } func (this *TunnelStatusManager) unregister(t interface{}) { - this.mux.Lock() - defer this.mux.Unlock() + // this.mux.Lock() + // defer this.mux.Unlock() + mux.Lock() + defer mux.Unlock() + l := this.tunnelList log.Debug("%d tunnels before remove.", this.tunnelList.Len()) - n := 0 + // n := 0 nl := new(list.List) + // tn := "" for e := l.Front(); e != nil; e = e.Next() { if e.Value != t { // log.Debug("Found") // l.Remove(e) // break - n++ + // n++ nl.PushBack(e.Value) + // en := 0 + // for ee := nl.Front(); ee != nil; ee = ee.Next() { + // en++ + + // } + // if en > nl.Len() { + // log.Error("en > ee") + // } + + // v := e.Value + // if v == nil { + // continue + // } + // m_ := v.(*model.Tunnel) + // tn = tn + m_.GetID() } } - if n == this.tunnelList.Len() { - m := t.(*model.Tunnel) - log.Debug("check tunntel %s", m.GetID()) - } - this.tunnelList = *nl + // log.Debug("display tunnels %s", tn) + // if n == this.tunnelList.Len() { + // m := t.(*model.Tunnel) + // log.Debug("check tunntel %s", m.GetID()) + // } + this.tunnelList = nl log.Debug("%d tunnels after remove.", this.tunnelList.Len()) } @@ -68,17 +113,22 @@ func (this *TunnelStatusManager) chanListener() { select { case r := <-this.regChan: this.register(r) - log.Debug("A tunnel registered") + m := r.(*model.Tunnel) + log.Debug("A tunnel registered id %s", m.GetID()) log.Debug(this.QueryStatString()) case ur := <-this.unregChan: + this.unregister(ur) - log.Debug("A tunnel unregistered") + m := ur.(*model.Tunnel) + log.Debug("A tunnel unregistered id %s", m.GetID()) } } } func (this *TunnelStatusManager) QueryStatString() string { - this.mux.Lock() - defer this.mux.Unlock() + // this.mux.Lock() + // defer this.mux.Unlock() + mux.Lock() + defer mux.Unlock() return fmt.Sprintf("There %d tunnels running.\n", this.tunnelList.Len()) }