2022-10-22 14:41:48 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"forge.redroom.link/yves/meowlib"
|
|
|
|
"github.com/go-redis/redis"
|
2022-10-22 22:40:03 +02:00
|
|
|
"google.golang.org/protobuf/proto"
|
2022-10-22 14:41:48 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type RedisRouter struct {
|
|
|
|
Name string
|
|
|
|
ServerIdentity *Identity
|
|
|
|
Client *redis.Client
|
|
|
|
Context context.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRedisRouter(server *Identity, url string, password string, db int) *RedisRouter {
|
|
|
|
var r RedisRouter
|
|
|
|
r.ServerIdentity = server
|
|
|
|
r.Name = "Redis"
|
|
|
|
r.Client = redis.NewClient(&redis.Options{
|
|
|
|
Addr: url,
|
|
|
|
Password: password,
|
|
|
|
DB: db,
|
|
|
|
})
|
|
|
|
r.Context = context.Background()
|
|
|
|
return &r
|
|
|
|
}
|
|
|
|
|
2022-10-22 22:40:03 +02:00
|
|
|
func (r *RedisRouter) Route(msg *meowlib.ToServerMessage) (*meowlib.FromServerMessage, error) {
|
2022-10-22 14:41:48 +02:00
|
|
|
var from_server meowlib.FromServerMessage
|
|
|
|
if len(msg.Messages) > 0 { // user message
|
|
|
|
for _, usrmsg := range msg.Messages {
|
2022-10-22 22:40:03 +02:00
|
|
|
// serialize the message to store it as byte array into redis
|
2022-12-17 21:37:48 +01:00
|
|
|
out, err := proto.Marshal(usrmsg)
|
2022-10-22 22:40:03 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
r.Client.ZAdd(usrmsg.Destination, redis.Z{float64(time.Now().Unix()), out})
|
2022-10-22 14:41:48 +02:00
|
|
|
}
|
|
|
|
from_server.UuidAck = msg.Uuid
|
|
|
|
}
|
|
|
|
if len(msg.PullRequest) > 0 {
|
|
|
|
for _, rq := range msg.PullRequest {
|
2022-10-22 22:40:03 +02:00
|
|
|
msgcnt, err := r.Client.ZCount(rq.LookupKey, "-inf", "+inf").Result()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res, err := r.Client.ZPopMin(rq.LookupKey, msgcnt).Result()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, redismsg := range res {
|
2022-12-18 19:47:44 +01:00
|
|
|
//println(redismsg.Score)
|
2022-12-17 12:56:34 +01:00
|
|
|
val := redismsg.Member
|
2022-12-17 13:00:28 +01:00
|
|
|
test := val.(string)
|
2022-10-22 22:40:03 +02:00
|
|
|
var usrmsg meowlib.PackedUserMessage
|
2022-12-17 13:00:28 +01:00
|
|
|
err := proto.Unmarshal([]byte(test), &usrmsg)
|
2022-10-22 22:40:03 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// add server timestamp
|
|
|
|
usrmsg.ServerTimestamp = append(usrmsg.ServerTimestamp, int64(redismsg.Score))
|
|
|
|
|
|
|
|
from_server.Chat = append(from_server.Chat, &usrmsg)
|
|
|
|
}
|
|
|
|
|
2022-10-22 14:41:48 +02:00
|
|
|
}
|
2022-10-24 18:44:21 +02:00
|
|
|
}
|
|
|
|
if len(msg.KnownServers) > 0 {
|
|
|
|
|
2022-10-22 14:41:48 +02:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
|
|
|
|
case "s": // servers list
|
2022-10-22 22:40:03 +02:00
|
|
|
breakmsgs
|
2022-10-22 14:41:48 +02:00
|
|
|
|
|
|
|
case "m": // matriochka
|
|
|
|
break
|
|
|
|
case "b": // broadcast
|
|
|
|
break
|
|
|
|
case "a": // admin
|
|
|
|
break
|
|
|
|
}
|
|
|
|
*/
|
2022-10-22 22:40:03 +02:00
|
|
|
return &from_server, nil
|
2022-10-22 14:41:48 +02:00
|
|
|
}
|