CSI-ES-2324/Projs/PD1/internal/protocol/protocol.go

273 lines
6.7 KiB
Go
Raw Normal View History

2024-04-16 12:23:00 +01:00
package protocol
import (
2024-04-19 23:59:26 +01:00
"encoding/json"
"fmt"
"time"
2024-04-16 12:23:00 +01:00
)
type PacketType int
const (
// 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 (
GetUserCert struct {
2024-04-19 23:59:26 +01:00
UID string `json:"uid"`
}
GetUnreadMsgsInfo struct {
Page int `json:"page"`
PageSize int `json:"pageSize"`
2024-04-19 23:59:26 +01:00
}
GetMsg struct {
2024-04-19 23:59:26 +01:00
Num int `json:"num"`
}
SendMsg struct {
2024-04-19 23:59:26 +01:00
ToUID string `json:"to_uid"`
Subject []byte `json:"subject"`
Body []byte `json:"body"`
}
AnswerGetUserCert struct {
2024-04-19 23:59:26 +01:00
UID string `json:"uid"`
Certificate []byte `json:"certificate"`
}
AnswerGetUnreadMsgsInfo struct {
Page int `json:"page"`
NumPages int `json:"num_pages"`
MessagesInfo []MsgInfo `json:"messages_info"`
}
MsgInfo struct {
2024-04-19 23:59:26 +01:00
Num int `json:"num"`
FromUID string `json:"from_uid"`
Subject []byte `json:"subject"`
Timestamp time.Time `json:"timestamp"`
}
AnswerGetMsg struct {
2024-04-19 23:59:26 +01:00
FromUID string `json:"from_uid"`
ToUID string `json:"to_uid"`
Subject []byte `json:"subject"`
Body []byte `json:"body"`
Timestamp time.Time `json:"timestamp"`
}
2024-04-16 12:23:00 +01:00
)
type PacketBody interface{}
2024-04-16 12:23:00 +01:00
type Packet struct {
2024-04-19 23:59:26 +01:00
Flag PacketType `json:"flag"`
Body PacketBody `json:"body"`
}
func NewPacket(fl PacketType, body PacketBody) Packet {
2024-04-19 23:59:26 +01:00
return Packet{
Flag: fl,
Body: body,
2024-04-19 23:59:26 +01:00
}
}
func NewGetUserCert(UID string) GetUserCert {
return GetUserCert{
UID: UID,
2024-04-19 23:59:26 +01:00
}
}
func NewGetUnreadMsgsInfo(page int, pageSize int) GetUnreadMsgsInfo {
return GetUnreadMsgsInfo{
Page: page,
PageSize: pageSize}
}
func NewGetMsg(num int) GetMsg {
return GetMsg{
Num: num,
2024-04-19 23:59:26 +01:00
}
}
func NewSendMsg(toUID string, subject []byte, body []byte) SendMsg {
return SendMsg{
ToUID: toUID,
Subject: subject,
Body: body,
2024-04-19 23:59:26 +01:00
}
}
func NewAnswerGetUserCert(uid string, certificate []byte) AnswerGetUserCert {
return AnswerGetUserCert{
UID: uid,
Certificate: certificate,
2024-04-19 23:59:26 +01:00
}
}
func NewAnswerGetUnreadMsgsInfo(page int, numPages int, messagesInfo []MsgInfo) AnswerGetUnreadMsgsInfo {
return AnswerGetUnreadMsgsInfo{Page:page,NumPages:numPages,MessagesInfo: messagesInfo}
}
func NewMsgInfo(num int, fromUID string, subject []byte, timestamp time.Time) MsgInfo {
return MsgInfo{
Num: num,
FromUID: fromUID,
Subject: subject,
Timestamp: timestamp,
2024-04-19 23:59:26 +01:00
}
}
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,
2024-04-19 23:59:26 +01:00
}
}
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 {
2024-04-19 23:59:26 +01:00
jsonData, err := json.Marshal(data)
if err != nil {
panic(fmt.Errorf("failed to marshal data: %v", err))
}
var packet GetUserCert
2024-04-19 23:59:26 +01:00
if err := json.Unmarshal(jsonData, &packet); err != nil {
panic(fmt.Errorf("failed to unmarshal into GetUserCert: %v", err))
2024-04-19 23:59:26 +01:00
}
return packet
}
func UnmarshalGetUnreadMsgsInfo(data PacketBody) GetUnreadMsgsInfo {
2024-04-19 23:59:26 +01:00
jsonData, err := json.Marshal(data)
if err != nil {
panic(fmt.Errorf("failed to marshal data: %v", err))
}
var packet GetUnreadMsgsInfo
2024-04-19 23:59:26 +01:00
if err := json.Unmarshal(jsonData, &packet); err != nil {
panic(fmt.Errorf("failed to unmarshal into GetUnreadMsgsInfo: %v", err))
2024-04-19 23:59:26 +01:00
}
return packet
}
func UnmarshalGetMsg(data PacketBody) GetMsg {
2024-04-19 23:59:26 +01:00
jsonData, err := json.Marshal(data)
if err != nil {
panic(fmt.Errorf("failed to marshal data: %v", err))
}
var packet GetMsg
2024-04-19 23:59:26 +01:00
if err := json.Unmarshal(jsonData, &packet); err != nil {
panic(fmt.Errorf("failed to unmarshal into GetMsg: %v", err))
2024-04-19 23:59:26 +01:00
}
return packet
}
func UnmarshalSendMsg(data PacketBody) SendMsg {
2024-04-19 23:59:26 +01:00
jsonData, err := json.Marshal(data)
if err != nil {
panic(fmt.Errorf("failed to marshal data: %v", err))
}
var packet SendMsg
2024-04-19 23:59:26 +01:00
if err := json.Unmarshal(jsonData, &packet); err != nil {
panic(fmt.Errorf("failed to unmarshal into SendMsg: %v", err))
2024-04-19 23:59:26 +01:00
}
return packet
}
func UnmarshalAnswerGetUserCert(data PacketBody) AnswerGetUserCert {
jsonData, err := json.Marshal(data)
if err != nil {
panic(fmt.Errorf("failed to marshal data: %v", err))
}
var packet AnswerGetUserCert
if err := json.Unmarshal(jsonData, &packet); err != nil {
panic(fmt.Errorf("failed to unmarshal into AnswerGetUserCert: %v", err))
}
return packet
}
func UnmarshalUnreadMsgInfo(data PacketBody) MsgInfo {
2024-04-19 23:59:26 +01:00
jsonData, err := json.Marshal(data)
if err != nil {
panic(fmt.Errorf("failed to marshal data: %v", err))
}
var packet MsgInfo
2024-04-19 23:59:26 +01:00
if err := json.Unmarshal(jsonData, &packet); err != nil {
panic(fmt.Errorf("failed to unmarshal into UnreadMsgInfo: %v", err))
2024-04-19 23:59:26 +01:00
}
return packet
}
func UnmarshalAnswerGetUnreadMsgsInfo(data PacketBody) AnswerGetUnreadMsgsInfo {
2024-04-19 23:59:26 +01:00
jsonData, err := json.Marshal(data)
if err != nil {
panic(fmt.Errorf("failed to marshal data: %v", err))
}
var packet AnswerGetUnreadMsgsInfo
2024-04-19 23:59:26 +01:00
if err := json.Unmarshal(jsonData, &packet); err != nil {
panic(fmt.Errorf("failed to unmarshal into AnswerGetUnreadMsgsInfo: %v", err))
2024-04-19 23:59:26 +01:00
}
return packet
2024-04-16 12:23:00 +01:00
}
func UnmarshalAnswerGetMsg(data PacketBody) AnswerGetMsg {
2024-04-19 23:59:26 +01:00
jsonData, err := json.Marshal(data)
if err != nil {
panic(fmt.Errorf("failed to marshal data: %v", err))
}
var packet AnswerGetMsg
2024-04-19 23:59:26 +01:00
if err := json.Unmarshal(jsonData, &packet); err != nil {
panic(fmt.Errorf("failed to unmarshal into AnswerGetMsg: %v", err))
2024-04-19 23:59:26 +01:00
}
return packet
}