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

157 lines
3.4 KiB
Go
Raw Normal View History

2018-02-10 13:34:42 +01:00
package tester
import (
log "github.com/sirupsen/logrus"
"dev.sum7.eu/genofire/yaja/client"
"dev.sum7.eu/genofire/yaja/messages"
"dev.sum7.eu/genofire/yaja/model"
"dev.sum7.eu/genofire/yaja/server/utils"
)
type Tester struct {
mainClient *client.Client
Accounts map[string]*Account `json:"accounts"`
Status map[string]*Status `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(&Account{
JID: mainClient.JID,
Password: password,
})
2018-02-10 13:34:42 +01:00
status.client = mainClient
status.Login = true
status.Update()
t.Status[mainClient.JID.Domain] = 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.Login = false
s.client.Close()
}
}
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(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
}
c, err := client.NewClient(acc.JID, 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-10 13:34:42 +01:00
status.Update()
go t.StartBot(status)
}
}
func (t *Tester) UpdateConnectionStatus(from, to *model.JID, recvmsg string) {
logCTX := log.WithFields(log.Fields{
"jid": to.Full(),
"from": from.Full(),
"recvmsg": recvmsg,
})
status, ok := t.Status[from.Bare()]
if !ok {
logCTX.Warn("recv wrong msg")
return
}
msg, ok := status.MessageForConnection[to.Bare()]
logCTX = logCTX.WithField("msg", msg)
if !ok || msg != recvmsg {
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
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 {
logCTXTo = logCTXTo.WithField("old-msg", msg)
own.Connections[jid] = false
if ok, exists := own.Connections[jid]; !exists || ok {
logCTXTo.Warn("could not recv msg")
} else {
logCTXTo.Debug("could not recv msg")
}
}
msg = utils.CreateCookieString()
logCTXTo = logCTXTo.WithField("msg", msg)
own.client.Send(&messages.MessageClient{
Body: "checkmsg " + msg,
Type: messages.ChatTypeChat,
To: s.JID,
})
own.MessageForConnection[jid] = msg
logCTXTo.Info("test send")
send++
}
}
log.WithFields(log.Fields{
"online": online,
"connection": connection,
"send": send,
"all": len(t.Status),
}).Info("checked complete")
}