2018-02-10 13:34:42 +01:00
|
|
|
package tester
|
|
|
|
|
|
|
|
import (
|
2018-02-11 11:15:11 +01:00
|
|
|
"sync"
|
2018-02-11 09:07:07 +01:00
|
|
|
"time"
|
|
|
|
|
2018-02-10 13:34:42 +01:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
"dev.sum7.eu/genofire/yaja/client"
|
|
|
|
"dev.sum7.eu/genofire/yaja/messages"
|
|
|
|
"dev.sum7.eu/genofire/yaja/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Tester struct {
|
2018-02-11 22:03:58 +01:00
|
|
|
mainClient *client.Client
|
|
|
|
Timeout time.Duration `json:"-"`
|
|
|
|
Accounts map[string]*Account `json:"accounts"`
|
|
|
|
Status map[string]*Status `json:"-"`
|
|
|
|
mux sync.Mutex
|
|
|
|
LoggingClients *log.Logger `json:"-"`
|
|
|
|
LoggingBots log.Level `json:"-"`
|
|
|
|
Admins []*model.JID `json:"-"`
|
2018-02-10 13:34:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewTester() *Tester {
|
|
|
|
return &Tester{
|
2018-02-10 16:45:01 +01:00
|
|
|
Accounts: make(map[string]*Account),
|
2018-02-10 13:34:42 +01:00
|
|
|
Status: make(map[string]*Status),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Tester) Start(mainClient *client.Client, password string) {
|
|
|
|
|
|
|
|
t.mainClient = mainClient
|
|
|
|
|
2018-02-11 23:28:21 +01:00
|
|
|
status := NewStatus(mainClient, &Account{
|
2018-02-10 16:45:01 +01:00
|
|
|
JID: mainClient.JID,
|
|
|
|
Password: password,
|
|
|
|
})
|
2018-02-10 13:34:42 +01:00
|
|
|
status.client = mainClient
|
|
|
|
status.Login = true
|
2018-02-11 11:15:11 +01:00
|
|
|
status.Update(t.Timeout)
|
2018-02-10 13:34:42 +01:00
|
|
|
|
2018-02-11 11:15:11 +01:00
|
|
|
t.mux.Lock()
|
|
|
|
defer t.mux.Unlock()
|
|
|
|
|
|
|
|
t.Status[mainClient.JID.Bare()] = status
|
2018-02-10 13:34:42 +01:00
|
|
|
go t.StartBot(status)
|
|
|
|
|
2018-02-10 16:45:01 +01:00
|
|
|
for _, acc := range t.Accounts {
|
|
|
|
t.Connect(acc)
|
2018-02-10 13:34:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
func (t *Tester) Close() {
|
|
|
|
for _, s := range t.Status {
|
2018-02-11 23:28:21 +01:00
|
|
|
s.Disconnect("yaja tester stopped")
|
2018-02-10 13:34:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-10 16:45:01 +01:00
|
|
|
func (t *Tester) Connect(acc *Account) {
|
|
|
|
logCTX := log.WithField("jid", acc.JID.Full())
|
|
|
|
status, ok := t.Status[acc.JID.Bare()]
|
2018-02-10 13:34:42 +01:00
|
|
|
if !ok {
|
2018-02-11 23:28:21 +01:00
|
|
|
status = NewStatus(t.mainClient, acc)
|
2018-02-10 16:45:01 +01:00
|
|
|
t.Status[acc.JID.Bare()] = status
|
2018-02-10 13:34:42 +01:00
|
|
|
} else if status.JID == nil {
|
2018-02-10 16:45:01 +01:00
|
|
|
status.JID = acc.JID
|
2018-02-10 13:34:42 +01:00
|
|
|
}
|
|
|
|
if status.Login {
|
|
|
|
logCTX.Warn("is already loggedin")
|
|
|
|
return
|
|
|
|
}
|
2018-02-11 22:03:58 +01:00
|
|
|
c := &client.Client{
|
|
|
|
Timeout: t.Timeout,
|
|
|
|
JID: acc.JID,
|
|
|
|
Logging: t.LoggingClients,
|
|
|
|
}
|
|
|
|
err := c.Connect(acc.Password)
|
2018-02-10 13:34:42 +01:00
|
|
|
if err != nil {
|
|
|
|
logCTX.Warnf("could not connect client: %s", err)
|
|
|
|
} else {
|
|
|
|
logCTX.Info("client connected")
|
|
|
|
status.Login = true
|
|
|
|
status.client = c
|
2018-02-10 16:45:01 +01:00
|
|
|
status.account.JID = c.JID
|
|
|
|
status.JID = c.JID
|
2018-02-11 11:15:11 +01:00
|
|
|
status.Update(t.Timeout)
|
2018-02-10 13:34:42 +01:00
|
|
|
go t.StartBot(status)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Tester) UpdateConnectionStatus(from, to *model.JID, recvmsg string) {
|
|
|
|
logCTX := log.WithFields(log.Fields{
|
2018-02-11 11:15:11 +01:00
|
|
|
"jid": to.Full(),
|
|
|
|
"from": from.Full(),
|
|
|
|
"msg-recv": recvmsg,
|
2018-02-10 13:34:42 +01:00
|
|
|
})
|
|
|
|
|
2018-02-11 11:15:11 +01:00
|
|
|
t.mux.Lock()
|
|
|
|
defer t.mux.Unlock()
|
|
|
|
|
2018-02-10 13:34:42 +01:00
|
|
|
status, ok := t.Status[from.Bare()]
|
|
|
|
if !ok {
|
2018-02-11 11:15:11 +01:00
|
|
|
logCTX.Warn("recv msg without receiver")
|
2018-02-10 13:34:42 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
msg, ok := status.MessageForConnection[to.Bare()]
|
2018-02-11 11:15:11 +01:00
|
|
|
logCTX = logCTX.WithField("msg-send", msg)
|
|
|
|
if !ok || msg != recvmsg || msg == "" || recvmsg == "" {
|
2018-02-10 13:34:42 +01:00
|
|
|
logCTX.Warn("recv wrong msg")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
delete(status.MessageForConnection, to.Bare())
|
|
|
|
status.Connections[to.Bare()] = true
|
|
|
|
logCTX.Info("recv msg")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Tester) CheckStatus() {
|
|
|
|
send := 0
|
|
|
|
online := 0
|
|
|
|
connection := 0
|
2018-02-11 11:15:11 +01:00
|
|
|
|
|
|
|
t.mux.Lock()
|
|
|
|
defer t.mux.Unlock()
|
|
|
|
|
2018-02-10 13:34:42 +01:00
|
|
|
for ownJID, own := range t.Status {
|
|
|
|
logCTX := log.WithField("jid", ownJID)
|
|
|
|
if !own.Login {
|
2018-02-10 16:45:01 +01:00
|
|
|
acc, ok := t.Accounts[ownJID]
|
2018-02-10 13:34:42 +01:00
|
|
|
if ok {
|
2018-02-10 16:45:01 +01:00
|
|
|
t.Connect(acc)
|
2018-02-10 13:34:42 +01:00
|
|
|
}
|
|
|
|
if !own.Login {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
online++
|
|
|
|
for jid, s := range t.Status {
|
|
|
|
logCTXTo := logCTX.WithField("to", jid)
|
|
|
|
if jid == ownJID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
connection++
|
|
|
|
if own.MessageForConnection == nil {
|
|
|
|
own.MessageForConnection = make(map[string]string)
|
|
|
|
}
|
|
|
|
msg, ok := own.MessageForConnection[jid]
|
|
|
|
if ok {
|
2018-02-11 11:15:11 +01:00
|
|
|
logCTXTo = logCTXTo.WithField("msg-old", msg)
|
2018-02-10 13:34:42 +01:00
|
|
|
own.Connections[jid] = false
|
|
|
|
if ok, exists := own.Connections[jid]; !exists || ok {
|
2018-02-11 22:03:58 +01:00
|
|
|
logCTXTo.Info("could not recv msg")
|
2018-02-10 13:34:42 +01:00
|
|
|
} else {
|
|
|
|
logCTXTo.Debug("could not recv msg")
|
|
|
|
}
|
|
|
|
}
|
2018-02-11 19:35:32 +01:00
|
|
|
msg = messages.CreateCookieString()
|
2018-02-11 11:15:11 +01:00
|
|
|
logCTXTo = logCTXTo.WithField("msg-send", msg)
|
2018-02-10 13:34:42 +01:00
|
|
|
|
|
|
|
own.client.Send(&messages.MessageClient{
|
|
|
|
Body: "checkmsg " + msg,
|
2018-02-11 19:35:32 +01:00
|
|
|
Type: messages.MessageTypeChat,
|
2018-02-10 13:34:42 +01:00
|
|
|
To: s.JID,
|
|
|
|
})
|
2018-02-11 11:15:11 +01:00
|
|
|
own.MessageForConnection[s.JID.Bare()] = msg
|
2018-02-11 22:03:58 +01:00
|
|
|
logCTXTo.Debug("test send")
|
2018-02-10 13:34:42 +01:00
|
|
|
send++
|
|
|
|
}
|
|
|
|
}
|
2018-02-11 11:15:11 +01:00
|
|
|
|
2018-02-10 13:34:42 +01:00
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"online": online,
|
|
|
|
"connection": connection,
|
|
|
|
"send": send,
|
|
|
|
"all": len(t.Status),
|
|
|
|
}).Info("checked complete")
|
|
|
|
}
|