meowlib/server/router.go

62 lines
1.3 KiB
Go

package server
import (
"context"
"time"
"forge.redroom.link/yves/meowlib"
"github.com/go-redis/redis"
)
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
}
func (r *RedisRouter) Route(msg *meowlib.ToServerMessage) *meowlib.FromServerMessage {
var from_server meowlib.FromServerMessage
if len(msg.Messages) > 0 { // user message
for _, usrmsg := range msg.Messages {
r.Client.ZAdd(usrmsg.Destination, redis.Z{float64(time.Now().Unix()), usrmsg.Payload})
}
from_server.UuidAck = msg.Uuid
}
if len(msg.PullRequest) > 0 {
for _, rq := range msg.PullRequest {
msgcnt := r.Client.ZCount(rq.LookupKey, "-inf", "+inf")
r.Client.ZPopMin(rq.LookupKey, msgcnt.Val())
}
}
/*
case "q": // query messages
break
case "s": // servers list
break
case "m": // matriochka
break
case "b": // broadcast
break
case "a": // admin
break
}
*/
return &from_server
}