sum7
/
yaja
Archived
1
0
Fork 0
This repository has been archived on 2020-09-27. You can view files and clone it, but cannot push or open issues or pull requests.
yaja/daemon/tester/tester.go

178 lines
3.8 KiB
Go
Raw Normal View History

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"
)
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
2018-02-13 20:05:18 +01:00
LoggingClients *log.Logger `json:"-"`
LoggingBots log.Level `json:"-"`
Admins []*messages.JID `json:"-"`
2018-02-10 13:34:42 +01:00
}
func NewTester() *Tester {
return &Tester{
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
status := NewStatus(mainClient, &Account{
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)
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 {
s.Disconnect("yaja tester stopped")
2018-02-10 13:34:42 +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 {
status = NewStatus(t.mainClient, acc)
t.Status[acc.JID.Bare()] = status
2018-02-10 13:34:42 +01:00
} else if status.JID == nil {
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
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)
}
}
2018-02-13 20:05:18 +01:00
func (t *Tester) UpdateConnectionStatus(from, to *messages.JID, recvmsg string) {
2018-02-10 13:34:42 +01:00
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 {
acc, ok := t.Accounts[ownJID]
2018-02-10 13:34:42 +01:00
if ok {
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")
}