71 lines
1.4 KiB
Go
71 lines
1.4 KiB
Go
package stat
|
|
|
|
import (
|
|
"container/list"
|
|
"fmt"
|
|
"github.com/TransX/log"
|
|
"sync"
|
|
)
|
|
|
|
type TunnelStatusManager struct {
|
|
mux sync.Mutex
|
|
tunnelList list.List
|
|
regChan chan interface{}
|
|
unregChan chan interface{}
|
|
}
|
|
|
|
func NewTunnelStatusManager() *TunnelStatusManager {
|
|
t := new(TunnelStatusManager)
|
|
t.regChan = make(chan interface{})
|
|
t.unregChan = make(chan interface{})
|
|
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{}) {
|
|
this.mux.Lock()
|
|
defer this.mux.Unlock()
|
|
this.tunnelList.PushBack(t)
|
|
}
|
|
|
|
func (this *TunnelStatusManager) unregister(t interface{}) {
|
|
this.mux.Lock()
|
|
defer this.mux.Unlock()
|
|
l := this.tunnelList
|
|
for e := l.Front(); e != nil; e = e.Next() {
|
|
if e == t {
|
|
l.Remove(e)
|
|
break
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
func (this *TunnelStatusManager) chanListener() {
|
|
for {
|
|
select {
|
|
case r := <-this.regChan:
|
|
this.register(r)
|
|
log.Debug("A tunnel registered")
|
|
log.Debug(this.QueryStatString())
|
|
case ur := <-this.unregChan:
|
|
this.unregister(ur)
|
|
log.Debug("A tunnel unregistered")
|
|
}
|
|
}
|
|
}
|
|
|
|
func (this *TunnelStatusManager) QueryStatString() string {
|
|
this.mux.Lock()
|
|
defer this.mux.Unlock()
|
|
return fmt.Sprintf("There %d tunnels running.\n", this.tunnelList.Len())
|
|
}
|