meowlib/client/matriochka.go

52 lines
2.0 KiB
Go
Raw Permalink Normal View History

package client
import (
"forge.redroom.link/yves/meowlib"
"github.com/google/uuid"
)
2023-11-08 22:01:44 +01:00
func ProcessForOutput(usermessage *meowlib.UserMessage, peer *Peer, servers *ServerList, trackingLookupKey string) ([]byte, error) {
lastIdx := len(servers.Servers) - 1
// LAST SERVER : Message delivery as usual
srv := &servers.Servers[lastIdx]
// Prepare cyphered + packed user message
packedMsg, err := peer.ProcessOutboundUserMessage(usermessage)
if err != nil {
return nil, err
}
// Creating Server message for transporting the user message
toServerMessage := srv.BuildToServerMessageFromUserMessage(packedMsg)
lastmsg, err := srv.ProcessOutboundMessage(toServerMessage)
if err != nil {
return nil, err
}
lastuuid := uuid.NewString()
// ALL PREVIOUS SERVERS
for i := lastIdx - 1; i >= 0; i-- {
srv = &servers.Servers[i]
var toServerMessage meowlib.ToServerMessage
toServerMessage.MatriochkaMessage.Data = lastmsg
toServerMessage.MatriochkaMessage.Next.Url = servers.Servers[i+1].ServerData.Url
toServerMessage.MatriochkaMessage.Next.PublicKey = servers.Servers[i+1].ServerData.PublicKey
toServerMessage.MatriochkaMessage.Next.Delay = int32(servers.Servers[i+1].AllowedDelay)
if trackingLookupKey != "" {
toServerMessage.MatriochkaMessage.Next.Uuid = lastuuid // change tracking uuid at each server
if i > 0 {
toServerMessage.MatriochkaMessage.Prev.Url = servers.Servers[i-1].ServerData.Url
toServerMessage.MatriochkaMessage.Prev.PublicKey = servers.Servers[i+1].ServerData.PublicKey
toServerMessage.MatriochkaMessage.Prev.Delay = int32(servers.Servers[i-1].AllowedDelay)
toServerMessage.MatriochkaMessage.Prev.Uuid = uuid.NewString()
lastuuid = toServerMessage.MatriochkaMessage.Prev.Uuid
} else { // for first message, no previous tracking but a lookupkey to stack the tracking messages
toServerMessage.MatriochkaMessage.LookupKey = trackingLookupKey
}
}
lastmsg, err = srv.ProcessOutboundMessage(&toServerMessage)
if err != nil {
return nil, err
}
}
return lastmsg, nil
}