2015-11-20 14:19:10 +08:00
|
|
|
package stat
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"container/list"
|
|
|
|
|
"fmt"
|
|
|
|
|
"github.com/TransX/log"
|
2016-01-11 21:29:48 +08:00
|
|
|
"github.com/TransX/model"
|
2015-11-20 14:19:10 +08:00
|
|
|
"sync"
|
|
|
|
|
)
|
|
|
|
|
|
2016-01-13 22:17:12 +08:00
|
|
|
var mux sync.Mutex
|
|
|
|
|
|
2015-11-20 14:19:10 +08:00
|
|
|
type TunnelStatusManager struct {
|
2016-01-13 22:17:12 +08:00
|
|
|
// mux sync.Mutex
|
|
|
|
|
tunnelList *list.List
|
2015-11-20 14:19:10 +08:00
|
|
|
regChan chan interface{}
|
|
|
|
|
unregChan chan interface{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewTunnelStatusManager() *TunnelStatusManager {
|
|
|
|
|
t := new(TunnelStatusManager)
|
|
|
|
|
t.regChan = make(chan interface{})
|
|
|
|
|
t.unregChan = make(chan interface{})
|
2016-01-13 22:17:12 +08:00
|
|
|
t.tunnelList = new(list.List)
|
2015-11-20 14:19:10 +08:00
|
|
|
go t.chanListener()
|
|
|
|
|
return t
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *TunnelStatusManager) GetRegChan() chan interface{} {
|
|
|
|
|
return this.regChan
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *TunnelStatusManager) GetUnregChan() chan interface{} {
|
|
|
|
|
return this.unregChan
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *TunnelStatusManager) register(t interface{}) {
|
2016-01-13 22:17:12 +08:00
|
|
|
// this.mux.Lock()
|
|
|
|
|
// defer this.mux.Unlock()
|
|
|
|
|
mux.Lock()
|
|
|
|
|
defer mux.Unlock()
|
2015-11-20 14:19:10 +08:00
|
|
|
this.tunnelList.PushBack(t)
|
2016-01-13 22:17:12 +08:00
|
|
|
|
|
|
|
|
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())
|
|
|
|
|
|
2015-11-20 14:19:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *TunnelStatusManager) unregister(t interface{}) {
|
2016-01-13 22:17:12 +08:00
|
|
|
// this.mux.Lock()
|
|
|
|
|
// defer this.mux.Unlock()
|
|
|
|
|
mux.Lock()
|
|
|
|
|
defer mux.Unlock()
|
|
|
|
|
|
2015-11-20 14:19:10 +08:00
|
|
|
l := this.tunnelList
|
2016-01-11 21:29:48 +08:00
|
|
|
log.Debug("%d tunnels before remove.", this.tunnelList.Len())
|
2016-01-13 22:17:12 +08:00
|
|
|
// n := 0
|
2016-01-11 21:29:48 +08:00
|
|
|
nl := new(list.List)
|
2016-01-13 22:17:12 +08:00
|
|
|
// tn := ""
|
2015-11-20 14:19:10 +08:00
|
|
|
for e := l.Front(); e != nil; e = e.Next() {
|
2016-01-11 21:29:48 +08:00
|
|
|
if e.Value != t {
|
|
|
|
|
// log.Debug("Found")
|
|
|
|
|
// l.Remove(e)
|
|
|
|
|
// break
|
2016-01-13 22:17:12 +08:00
|
|
|
// n++
|
2016-01-11 21:29:48 +08:00
|
|
|
nl.PushBack(e.Value)
|
2016-01-13 22:17:12 +08:00
|
|
|
// 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()
|
2015-11-20 14:19:10 +08:00
|
|
|
}
|
2016-01-11 21:29:48 +08:00
|
|
|
|
|
|
|
|
}
|
2016-01-13 22:17:12 +08:00
|
|
|
// 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
|
2016-01-11 21:29:48 +08:00
|
|
|
log.Debug("%d tunnels after remove.", this.tunnelList.Len())
|
2015-11-20 14:19:10 +08:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *TunnelStatusManager) chanListener() {
|
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case r := <-this.regChan:
|
|
|
|
|
this.register(r)
|
2016-01-13 22:17:12 +08:00
|
|
|
m := r.(*model.Tunnel)
|
|
|
|
|
log.Debug("A tunnel registered id %s", m.GetID())
|
2015-11-20 17:31:32 +08:00
|
|
|
log.Debug(this.QueryStatString())
|
2015-11-20 14:19:10 +08:00
|
|
|
case ur := <-this.unregChan:
|
2016-01-13 22:17:12 +08:00
|
|
|
|
2015-11-20 14:19:10 +08:00
|
|
|
this.unregister(ur)
|
2016-01-13 22:17:12 +08:00
|
|
|
m := ur.(*model.Tunnel)
|
|
|
|
|
log.Debug("A tunnel unregistered id %s", m.GetID())
|
2015-11-20 14:19:10 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *TunnelStatusManager) QueryStatString() string {
|
2016-01-13 22:17:12 +08:00
|
|
|
// this.mux.Lock()
|
|
|
|
|
// defer this.mux.Unlock()
|
|
|
|
|
mux.Lock()
|
|
|
|
|
defer mux.Unlock()
|
2015-11-20 14:19:10 +08:00
|
|
|
return fmt.Sprintf("There %d tunnels running.\n", this.tunnelList.Len())
|
|
|
|
|
}
|