mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-10-30 18:23:35 +08:00
96 lines
1.8 KiB
Go
96 lines
1.8 KiB
Go
package monitor
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
"sync"
|
|
"time"
|
|
|
|
"yunion.io/x/log"
|
|
)
|
|
|
|
type StringCallback func(string)
|
|
|
|
type Monitor interface {
|
|
Connect(host string, port int) error
|
|
Dicconnect()
|
|
IsConnected() bool
|
|
|
|
// The callback function will be called in another goroutine
|
|
SimpleCommand(cmd string, callback StringCallback)
|
|
QueryStatus(callback StringCallback)
|
|
GetVersion(callback StringCallback)
|
|
}
|
|
|
|
type MonitorErrorFunc func(error)
|
|
type MonitorSuccFunc func()
|
|
|
|
type SBaseMonitor struct {
|
|
OnMonitorDisConnect MonitorErrorFunc
|
|
OnMonitorConnected MonitorSuccFunc
|
|
OnMonitorTimeout MonitorErrorFunc
|
|
|
|
QemuVersion string
|
|
connected bool
|
|
rwc net.Conn
|
|
|
|
mutex *sync.Mutex
|
|
writing bool
|
|
reading bool
|
|
}
|
|
|
|
func NewBaseMonitor(OnMonitorConnected MonitorSuccFunc, OnMonitorDisConnect, OnMonitorTimeout MonitorErrorFunc) *SBaseMonitor {
|
|
return &SBaseMonitor{
|
|
OnMonitorConnected: OnMonitorConnected,
|
|
OnMonitorDisConnect: OnMonitorDisConnect,
|
|
OnMonitorTimeout: OnMonitorTimeout,
|
|
mutex: &sync.Mutex{},
|
|
}
|
|
}
|
|
|
|
func (m *SBaseMonitor) Connect(host string, port int) error {
|
|
address := fmt.Sprintf("%s:%d", host, port)
|
|
conn, err := net.Dial("tcp", address)
|
|
if err != nil {
|
|
log.Errorln("Connect monitor error:%s", err)
|
|
return err
|
|
}
|
|
// Setup reader timeout
|
|
conn.SetReadDeadline(time.Now().Add(90 * time.Second))
|
|
m.rwc = conn
|
|
return nil
|
|
}
|
|
|
|
func (m *SBaseMonitor) Disconnect() {
|
|
if m.connected {
|
|
m.connected = false
|
|
m.rwc.Close()
|
|
}
|
|
}
|
|
|
|
func (m *SBaseMonitor) IsConnected() bool {
|
|
return m.connected
|
|
}
|
|
|
|
func (m *SBaseMonitor) checkReading() bool {
|
|
m.mutex.Lock()
|
|
defer m.mutex.Unlock()
|
|
if m.reading {
|
|
return false
|
|
} else {
|
|
m.reading = true
|
|
}
|
|
return true
|
|
}
|
|
|
|
func (m *SBaseMonitor) checkWriting() bool {
|
|
m.mutex.Lock()
|
|
defer m.mutex.Unlock()
|
|
if m.writing {
|
|
return false
|
|
} else {
|
|
m.writing = true
|
|
}
|
|
return true
|
|
}
|