[PD1] Fixed stuff. Unmarshal still returns map[string]interface{}, need to fix

This commit is contained in:
Afonso Franco 2024-04-19 11:55:16 +01:00
parent c131aa2aea
commit 39a0e5c01f
Signed by: afonso
SSH key fingerprint: SHA256:aiLbdlPwXKJS5wMnghdtod0SPy8imZjlVvCyUX9DJNk
6 changed files with 127 additions and 96 deletions

View file

@ -1,116 +1,114 @@
package protocol
import (
"time"
"time"
)
type PacketType int
const (
ReqUserCertPkt PacketType = iota
ReqAllMsgPkt
ReqMsgPkt
SubmitMsgPkt
SendUserCertPkt
ServerMsgPkt
ReqUserCertPkt PacketType = iota
ReqAllMsgPkt
ReqMsgPkt
SubmitMsgPkt
SendUserCertPkt
ServerMsgPkt
)
// Define interfaces for packet bodies
type (
RequestUserCertPacket struct {
UID string `json:"uid"`
}
RequestAllMsgPacket struct {
FromUID string `json:"from_uid"`
}
RequestMsgPacket struct {
Num uint16 `json:"num"`
}
SubmitMessagePacket struct {
ToUID string `json:"to_uid"`
Content []byte `json:"content"`
}
SendUserCertPacket struct {
UID string `json:"uid"`
Key []byte `json:"key"`
}
ServerMessagePacket struct {
FromUID string `json:"from_uid"`
ToUID string `json:"to_uid"`
Content []byte `json:"content"`
Timestamp time.Time `json:"timestamp"`
}
)
type PacketBody interface{}
type Packet struct {
Flag PacketType
Body PacketBody
}
// Client --> Server: Ask for a user's certificate
type RequestUserCertPacket struct {
UID string
Flag PacketType `json:"flag"`
Body PacketBody `json:"body"`
}
func NewRequestUserCertPacket(UID string) Packet {
return Packet{
Flag: ReqUserCertPkt,
Body: RequestUserCertPacket{
UID: UID,
},
}
}
// Client --> Server: Ask for all the client's messages in the queue
type RequestAllMsgPacket struct {
FromUID string
return Packet{
Flag: ReqUserCertPkt,
Body: RequestUserCertPacket{
UID: UID,
},
}
}
func NewRequestAllMsgPacket(fromUID string) Packet {
return Packet{
Flag: ReqAllMsgPkt,
Body: RequestAllMsgPacket{
FromUID: fromUID,
},
}
}
// Client --> Server: Ask for a specific message in the queue
type RequestMsgPacket struct {
Num uint16
return Packet{
Flag: ReqAllMsgPkt,
Body: RequestAllMsgPacket{
FromUID: fromUID,
},
}
}
func NewRequestMsgPacket(num uint16) Packet {
return Packet{
Flag: ReqMsgPkt,
Body: RequestMsgPacket{
Num: num,
},
}
}
// Client --> Server: Send message from client to server
type SubmitMessagePacket struct {
ToUID string
Content []byte
return Packet{
Flag: ReqMsgPkt,
Body: RequestMsgPacket{
Num: num,
},
}
}
func NewSubmitMessagePacket(toUID string, content []byte) Packet {
return Packet{
Flag: SubmitMsgPkt,
Body: SubmitMessagePacket{
ToUID: toUID,
Content: content},
}
}
// Server --> Client: Send the client the requested public key
type SendUserCertPacket struct {
UID string
Key []byte
return Packet{
Flag: SubmitMsgPkt,
Body: SubmitMessagePacket{
ToUID: toUID,
Content: content,
},
}
}
func NewSendUserCertPacket(uid string, key []byte) Packet {
return Packet{
Flag: SendUserCertPkt,
Body: SendUserCertPacket{
UID: uid,
Key: key,
},
}
}
// Server --> Client: Send the client a message
type ServerMessagePacket struct {
FromUID string
ToUID string
Content []byte
Timestamp time.Time
return Packet{
Flag: SendUserCertPkt,
Body: SendUserCertPacket{
UID: uid,
Key: key,
},
}
}
func NewServerMessagePacket(fromUID, toUID string, content []byte, timestamp time.Time) Packet {
return Packet{
Flag: ServerMsgPkt,
Body: ServerMessagePacket{
FromUID: fromUID,
ToUID: toUID,
Content: content,
Timestamp: timestamp,
},
}
return Packet{
Flag: ServerMsgPkt,
Body: ServerMessagePacket{
FromUID: fromUID,
ToUID: toUID,
Content: content,
Timestamp: timestamp,
},
}
}