[PD1] Cleaned up protocol,database and receiving multiple messageInfos

This commit is contained in:
Afonso Franco 2024-04-20 17:16:52 +01:00
parent 4c141bbc6e
commit f3cf9cfc40
Signed by: afonso
SSH key fingerprint: SHA256:aiLbdlPwXKJS5wMnghdtod0SPy8imZjlVvCyUX9DJNk
6 changed files with 347 additions and 231 deletions

View file

@ -9,46 +9,67 @@ import (
type PacketType int
const (
ReqUserCertPkt PacketType = iota
ReqMsgsQueue
ReqMsgPkt
SubmitMsgPkt
SendUserCertPkt
ServerMsgInfoPkt
ServerMsgPkt
// Client requests user certificate
FlagGetUserCert PacketType = iota
// Client requests unread message info
FlagGetUnreadMsgsInfo
// Client requests a message from the queue
FlagGetMsg
// Client sends a message
FlagSendMsg
// Server sends user certificate
FlagAnswerGetUserCert
// Server sends list of unread messages
FlagAnswerGetUnreadMsgsInfo
// Server sends requested message
FlagAnswerGetMsg
)
type (
RequestUserCertPacket struct {
GetUserCert struct {
UID string `json:"uid"`
}
RequestMsgsQueuePacket struct {
GetUnreadMsgsInfo struct {
Page int `json:"page"`
PageSize int `json:"pageSize"`
}
RequestMsgPacket struct {
GetMsg struct {
Num int `json:"num"`
}
SubmitMessagePacket struct {
SendMsg struct {
ToUID string `json:"to_uid"`
Subject []byte `json:"subject"`
Body []byte `json:"body"`
}
SendUserCertPacket struct {
AnswerGetUserCert struct {
UID string `json:"uid"`
Certificate []byte `json:"certificate"`
}
ServerMessageInfoPacket struct {
AnswerGetUnreadMsgsInfo struct {
Page int `json:"page"`
NumPages int `json:"num_pages"`
MessagesInfo []MsgInfo `json:"messages_info"`
}
MsgInfo struct {
Num int `json:"num"`
FromUID string `json:"from_uid"`
Subject []byte `json:"subject"`
Timestamp time.Time `json:"timestamp"`
Last bool `json:"last"`
}
ServerMessagePacket struct {
AnswerGetMsg struct {
FromUID string `json:"from_uid"`
ToUID string `json:"to_uid"`
Subject []byte `json:"subject"`
@ -64,156 +85,188 @@ type Packet struct {
Body PacketBody `json:"body"`
}
func NewRequestUserCertPacket(UID string) Packet {
func NewPacket(fl PacketType, body PacketBody) Packet {
return Packet{
Flag: ReqUserCertPkt,
Body: RequestUserCertPacket{
UID: UID,
},
Flag: fl,
Body: body,
}
}
func NewGetUserCert(UID string) GetUserCert {
return GetUserCert{
UID: UID,
}
}
func NewRequestUnreadMsgsQueuePacket() Packet {
return Packet{
Flag: ReqMsgsQueue,
Body: RequestMsgsQueuePacket{},
func NewGetUnreadMsgsInfo(page int, pageSize int) GetUnreadMsgsInfo {
return GetUnreadMsgsInfo{
Page: page,
PageSize: pageSize}
}
func NewGetMsg(num int) GetMsg {
return GetMsg{
Num: num,
}
}
func NewRequestMsgPacket(num int) Packet {
return Packet{
Flag: ReqMsgPkt,
Body: RequestMsgPacket{
Num: num,
},
func NewSendMsg(toUID string, subject []byte, body []byte) SendMsg {
return SendMsg{
ToUID: toUID,
Subject: subject,
Body: body,
}
}
func NewSubmitMessagePacket(toUID string, subject []byte, body []byte) Packet {
return Packet{
Flag: SubmitMsgPkt,
Body: SubmitMessagePacket{
ToUID: toUID,
Subject: subject,
Body: body,
},
func NewAnswerGetUserCert(uid string, certificate []byte) AnswerGetUserCert {
return AnswerGetUserCert{
UID: uid,
Certificate: certificate,
}
}
func NewSendUserCertPacket(uid string, certificate []byte) Packet {
return Packet{
Flag: SendUserCertPkt,
Body: SendUserCertPacket{
UID: uid,
Certificate: certificate,
},
}
func NewAnswerGetUnreadMsgsInfo(page int, numPages int, messagesInfo []MsgInfo) AnswerGetUnreadMsgsInfo {
return AnswerGetUnreadMsgsInfo{Page:page,NumPages:numPages,MessagesInfo: messagesInfo}
}
func NewServerMessageInfoPacket(num int, fromUID string, subject []byte, timestamp time.Time, last bool) Packet {
return Packet{
Flag: ServerMsgInfoPkt,
Body: ServerMessageInfoPacket{
Num: num,
FromUID: fromUID,
Subject: subject,
Timestamp: timestamp,
Last: last,
},
func NewMsgInfo(num int, fromUID string, subject []byte, timestamp time.Time) MsgInfo {
return MsgInfo{
Num: num,
FromUID: fromUID,
Subject: subject,
Timestamp: timestamp,
}
}
func NewServerMessagePacket(fromUID, toUID string, subject []byte, body []byte, timestamp time.Time, last bool) Packet {
return Packet{
Flag: ServerMsgPkt,
Body: ServerMessagePacket{
FromUID: fromUID,
ToUID: toUID,
Subject: subject,
Body: body,
Timestamp: timestamp,
},
func NewAnswerGetMsg(fromUID, toUID string, subject []byte, body []byte, timestamp time.Time, last bool) AnswerGetMsg {
return AnswerGetMsg{
FromUID: fromUID,
ToUID: toUID,
Subject: subject,
Body: body,
Timestamp: timestamp,
}
}
func UnmarshalRequestUserCertPacket(data PacketBody) RequestUserCertPacket {
func NewGetUserCertPacket(UID string) Packet {
return NewPacket(FlagGetUserCert, NewGetUserCert(UID))
}
func NewGetUnreadMsgsInfoPacket(page int, pageSize int) Packet {
return NewPacket(FlagGetUnreadMsgsInfo, NewGetUnreadMsgsInfo(page, pageSize))
}
func NewGetMsgPacket(num int) Packet {
return NewPacket(FlagGetMsg, NewGetMsg(num))
}
func NewSendMsgPacket(toUID string, subject []byte, body []byte) Packet {
return NewPacket(FlagSendMsg, NewSendMsg(toUID, subject, body))
}
func NewAnswerGetUserCertPacket(uid string, certificate []byte) Packet {
return NewPacket(FlagAnswerGetUserCert, NewAnswerGetUserCert(uid, certificate))
}
func NewAnswerGetUnreadMsgsInfoPacket(page int, numPages int, messagesInfo []MsgInfo) Packet {
return NewPacket(FlagAnswerGetUnreadMsgsInfo, NewAnswerGetUnreadMsgsInfo(page,numPages,messagesInfo))
}
func NewAnswerGetMsgPacket(fromUID, toUID string, subject []byte, body []byte, timestamp time.Time, last bool) Packet {
return NewPacket(FlagAnswerGetMsg, NewAnswerGetMsg(fromUID, toUID, subject, body, timestamp, last))
}
func UnmarshalGetUserCert(data PacketBody) GetUserCert {
jsonData, err := json.Marshal(data)
if err != nil {
panic(fmt.Errorf("failed to marshal data: %v", err))
}
var packet RequestUserCertPacket
var packet GetUserCert
if err := json.Unmarshal(jsonData, &packet); err != nil {
panic(fmt.Errorf("failed to unmarshal into RequestUserCertPacket: %v", err))
panic(fmt.Errorf("failed to unmarshal into GetUserCert: %v", err))
}
return packet
}
func UnmarshalRequestMsgsQueuePacket(data PacketBody) RequestMsgsQueuePacket {
func UnmarshalGetUnreadMsgsInfo(data PacketBody) GetUnreadMsgsInfo {
jsonData, err := json.Marshal(data)
if err != nil {
panic(fmt.Errorf("failed to marshal data: %v", err))
}
var packet RequestMsgsQueuePacket
var packet GetUnreadMsgsInfo
if err := json.Unmarshal(jsonData, &packet); err != nil {
panic(fmt.Errorf("failed to unmarshal into RequestMsgsQueuePacket: %v", err))
panic(fmt.Errorf("failed to unmarshal into GetUnreadMsgsInfo: %v", err))
}
return packet
}
func UnmarshalRequestMsgPacket(data PacketBody) RequestMsgPacket {
func UnmarshalGetMsg(data PacketBody) GetMsg {
jsonData, err := json.Marshal(data)
if err != nil {
panic(fmt.Errorf("failed to marshal data: %v", err))
}
var packet RequestMsgPacket
var packet GetMsg
if err := json.Unmarshal(jsonData, &packet); err != nil {
panic(fmt.Errorf("failed to unmarshal into RequestMsgPacket: %v", err))
panic(fmt.Errorf("failed to unmarshal into GetMsg: %v", err))
}
return packet
}
func UnmarshalSubmitMessagePacket(data PacketBody) SubmitMessagePacket {
func UnmarshalSendMsg(data PacketBody) SendMsg {
jsonData, err := json.Marshal(data)
if err != nil {
panic(fmt.Errorf("failed to marshal data: %v", err))
}
var packet SubmitMessagePacket
var packet SendMsg
if err := json.Unmarshal(jsonData, &packet); err != nil {
panic(fmt.Errorf("failed to unmarshal into SubmitMessagePacket: %v", err))
panic(fmt.Errorf("failed to unmarshal into SendMsg: %v", err))
}
return packet
}
func UnmarshalSendUserCertPacket(data PacketBody) SendUserCertPacket {
func UnmarshalAnswerGetUserCert(data PacketBody) AnswerGetUserCert {
jsonData, err := json.Marshal(data)
if err != nil {
panic(fmt.Errorf("failed to marshal data: %v", err))
}
var packet SendUserCertPacket
var packet AnswerGetUserCert
if err := json.Unmarshal(jsonData, &packet); err != nil {
panic(fmt.Errorf("failed to unmarshal into SendUserCertPacket: %v", err))
panic(fmt.Errorf("failed to unmarshal into AnswerGetUserCert: %v", err))
}
return packet
}
func UnmarshalServerMessageInfoPacket(data PacketBody) ServerMessageInfoPacket {
func UnmarshalUnreadMsgInfo(data PacketBody) MsgInfo {
jsonData, err := json.Marshal(data)
if err != nil {
panic(fmt.Errorf("failed to marshal data: %v", err))
}
var packet ServerMessageInfoPacket
var packet MsgInfo
if err := json.Unmarshal(jsonData, &packet); err != nil {
panic(fmt.Errorf("failed to unmarshal into ServerMessageInfoPacket: %v", err))
panic(fmt.Errorf("failed to unmarshal into UnreadMsgInfo: %v", err))
}
return packet
}
func UnmarshalServerMessagePacket(data PacketBody) ServerMessagePacket {
func UnmarshalAnswerGetUnreadMsgsInfo(data PacketBody) AnswerGetUnreadMsgsInfo {
jsonData, err := json.Marshal(data)
if err != nil {
panic(fmt.Errorf("failed to marshal data: %v", err))
}
var packet ServerMessagePacket
var packet AnswerGetUnreadMsgsInfo
if err := json.Unmarshal(jsonData, &packet); err != nil {
panic(fmt.Errorf("failed to unmarshal into ServerMessagePacket: %v", err))
panic(fmt.Errorf("failed to unmarshal into AnswerGetUnreadMsgsInfo: %v", err))
}
return packet
}
func UnmarshalAnswerGetMsg(data PacketBody) AnswerGetMsg {
jsonData, err := json.Marshal(data)
if err != nil {
panic(fmt.Errorf("failed to marshal data: %v", err))
}
var packet AnswerGetMsg
if err := json.Unmarshal(jsonData, &packet); err != nil {
panic(fmt.Errorf("failed to unmarshal into AnswerGetMsg: %v", err))
}
return packet
}