2024-04-16 12:23:00 +01:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
2024-04-19 23:59:26 +01:00
|
|
|
"encoding/json"
|
|
|
|
"time"
|
2024-04-16 12:23:00 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type PacketType int
|
|
|
|
|
|
|
|
const (
|
2024-04-20 17:16:52 +01:00
|
|
|
// 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
|
2024-04-23 11:12:18 +01:00
|
|
|
|
2024-04-28 22:02:13 +01:00
|
|
|
// Server tells the client that the message was successfully sent
|
|
|
|
FlagAnswerSendMsg
|
2024-04-23 11:12:18 +01:00
|
|
|
|
|
|
|
// Report an error
|
|
|
|
FlagReportError
|
2024-04-19 11:55:16 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2024-04-20 17:16:52 +01:00
|
|
|
GetUserCert struct {
|
2024-04-19 23:59:26 +01:00
|
|
|
UID string `json:"uid"`
|
|
|
|
}
|
|
|
|
|
2024-04-20 17:16:52 +01:00
|
|
|
GetUnreadMsgsInfo struct {
|
|
|
|
Page int `json:"page"`
|
|
|
|
PageSize int `json:"pageSize"`
|
2024-04-19 23:59:26 +01:00
|
|
|
}
|
|
|
|
|
2024-04-20 17:16:52 +01:00
|
|
|
GetMsg struct {
|
2024-04-19 23:59:26 +01:00
|
|
|
Num int `json:"num"`
|
|
|
|
}
|
|
|
|
|
2024-04-20 17:16:52 +01:00
|
|
|
SendMsg struct {
|
2024-04-19 23:59:26 +01:00
|
|
|
ToUID string `json:"to_uid"`
|
|
|
|
Subject []byte `json:"subject"`
|
|
|
|
Body []byte `json:"body"`
|
|
|
|
}
|
|
|
|
|
2024-04-20 17:16:52 +01:00
|
|
|
AnswerGetUserCert struct {
|
2024-04-19 23:59:26 +01:00
|
|
|
UID string `json:"uid"`
|
|
|
|
Certificate []byte `json:"certificate"`
|
|
|
|
}
|
|
|
|
|
2024-04-20 17:16:52 +01:00
|
|
|
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"`
|
|
|
|
}
|
2024-04-20 17:16:52 +01:00
|
|
|
|
|
|
|
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-23 11:12:18 +01:00
|
|
|
|
|
|
|
ReportError struct {
|
|
|
|
ErrorMessage string `json:"error"`
|
|
|
|
}
|
2024-04-16 12:23:00 +01:00
|
|
|
)
|
|
|
|
|
2024-04-17 15:44:38 +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"`
|
2024-04-17 15:44:38 +01:00
|
|
|
}
|
|
|
|
|
2024-04-20 17:16:52 +01:00
|
|
|
func NewPacket(fl PacketType, body PacketBody) Packet {
|
2024-04-19 23:59:26 +01:00
|
|
|
return Packet{
|
2024-04-20 17:16:52 +01:00
|
|
|
Flag: fl,
|
|
|
|
Body: body,
|
2024-04-19 23:59:26 +01:00
|
|
|
}
|
2024-04-20 17:16:52 +01:00
|
|
|
|
2024-04-17 15:44:38 +01:00
|
|
|
}
|
|
|
|
|
2024-04-20 17:16:52 +01:00
|
|
|
func NewGetUserCert(UID string) GetUserCert {
|
|
|
|
return GetUserCert{
|
|
|
|
UID: UID,
|
2024-04-19 23:59:26 +01:00
|
|
|
}
|
2024-04-17 15:44:38 +01:00
|
|
|
}
|
|
|
|
|
2024-04-20 17:16:52 +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
|
|
|
}
|
2024-04-17 15:44:38 +01:00
|
|
|
}
|
|
|
|
|
2024-04-20 17:16:52 +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
|
|
|
}
|
2024-04-17 15:44:38 +01:00
|
|
|
}
|
|
|
|
|
2024-04-20 17:16:52 +01:00
|
|
|
func NewAnswerGetUserCert(uid string, certificate []byte) AnswerGetUserCert {
|
|
|
|
return AnswerGetUserCert{
|
|
|
|
UID: uid,
|
|
|
|
Certificate: certificate,
|
2024-04-19 23:59:26 +01:00
|
|
|
}
|
|
|
|
}
|
2024-04-20 17:16:52 +01:00
|
|
|
|
|
|
|
func NewAnswerGetUnreadMsgsInfo(page int, numPages int, messagesInfo []MsgInfo) AnswerGetUnreadMsgsInfo {
|
2024-04-23 11:12:18 +01:00
|
|
|
return AnswerGetUnreadMsgsInfo{Page: page, NumPages: numPages, MessagesInfo: messagesInfo}
|
2024-04-20 17:16:52 +01:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-20 17:16:52 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-23 11:12:18 +01:00
|
|
|
func NewReportError(errorMessage string) ReportError {
|
|
|
|
return ReportError{
|
|
|
|
ErrorMessage: errorMessage,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-20 17:16:52 +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 {
|
2024-04-23 11:12:18 +01:00
|
|
|
return NewPacket(FlagAnswerGetUnreadMsgsInfo, NewAnswerGetUnreadMsgsInfo(page, numPages, messagesInfo))
|
2024-04-20 17:16:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2024-04-28 22:02:13 +01:00
|
|
|
func NewAnswerSendMsgPacket() Packet {
|
|
|
|
//This packet has no body
|
|
|
|
return NewPacket(FlagAnswerSendMsg, nil)
|
2024-04-23 11:12:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewReportErrorPacket(errorMessage string) Packet {
|
|
|
|
return NewPacket(FlagReportError, NewReportError(errorMessage))
|
|
|
|
}
|
|
|
|
|
2024-04-28 22:02:13 +01:00
|
|
|
func UnmarshalGetUserCert(data PacketBody) (GetUserCert, error) {
|
2024-04-19 23:59:26 +01:00
|
|
|
jsonData, err := json.Marshal(data)
|
|
|
|
if err != nil {
|
2024-04-28 22:02:13 +01:00
|
|
|
return GetUserCert{}, err
|
2024-04-19 23:59:26 +01:00
|
|
|
}
|
2024-04-20 17:16:52 +01:00
|
|
|
var packet GetUserCert
|
2024-04-19 23:59:26 +01:00
|
|
|
if err := json.Unmarshal(jsonData, &packet); err != nil {
|
2024-04-28 22:02:13 +01:00
|
|
|
return GetUserCert{}, err
|
2024-04-19 23:59:26 +01:00
|
|
|
}
|
2024-04-28 22:02:13 +01:00
|
|
|
return packet, nil
|
2024-04-19 23:59:26 +01:00
|
|
|
}
|
|
|
|
|
2024-04-28 22:02:13 +01:00
|
|
|
func UnmarshalGetUnreadMsgsInfo(data PacketBody) (GetUnreadMsgsInfo, error) {
|
2024-04-19 23:59:26 +01:00
|
|
|
jsonData, err := json.Marshal(data)
|
|
|
|
if err != nil {
|
2024-04-28 22:02:13 +01:00
|
|
|
return GetUnreadMsgsInfo{}, err
|
2024-04-19 23:59:26 +01:00
|
|
|
}
|
2024-04-20 17:16:52 +01:00
|
|
|
var packet GetUnreadMsgsInfo
|
2024-04-19 23:59:26 +01:00
|
|
|
if err := json.Unmarshal(jsonData, &packet); err != nil {
|
2024-04-28 22:02:13 +01:00
|
|
|
return GetUnreadMsgsInfo{}, err
|
2024-04-19 23:59:26 +01:00
|
|
|
}
|
2024-04-28 22:02:13 +01:00
|
|
|
return packet, nil
|
2024-04-19 23:59:26 +01:00
|
|
|
}
|
|
|
|
|
2024-04-28 22:02:13 +01:00
|
|
|
func UnmarshalGetMsg(data PacketBody) (GetMsg, error) {
|
2024-04-19 23:59:26 +01:00
|
|
|
jsonData, err := json.Marshal(data)
|
|
|
|
if err != nil {
|
2024-04-28 22:02:13 +01:00
|
|
|
return GetMsg{}, err
|
2024-04-19 23:59:26 +01:00
|
|
|
}
|
2024-04-20 17:16:52 +01:00
|
|
|
var packet GetMsg
|
2024-04-19 23:59:26 +01:00
|
|
|
if err := json.Unmarshal(jsonData, &packet); err != nil {
|
2024-04-28 22:02:13 +01:00
|
|
|
return GetMsg{}, err
|
2024-04-19 23:59:26 +01:00
|
|
|
}
|
2024-04-28 22:02:13 +01:00
|
|
|
return packet, nil
|
2024-04-19 23:59:26 +01:00
|
|
|
}
|
|
|
|
|
2024-04-28 22:02:13 +01:00
|
|
|
func UnmarshalSendMsg(data PacketBody) (SendMsg, error) {
|
2024-04-19 23:59:26 +01:00
|
|
|
jsonData, err := json.Marshal(data)
|
|
|
|
if err != nil {
|
2024-04-28 22:02:13 +01:00
|
|
|
return SendMsg{}, err
|
2024-04-19 23:59:26 +01:00
|
|
|
}
|
2024-04-20 17:16:52 +01:00
|
|
|
var packet SendMsg
|
2024-04-19 23:59:26 +01:00
|
|
|
if err := json.Unmarshal(jsonData, &packet); err != nil {
|
2024-04-28 22:02:13 +01:00
|
|
|
return SendMsg{}, err
|
2024-04-19 23:59:26 +01:00
|
|
|
}
|
2024-04-28 22:02:13 +01:00
|
|
|
return packet, nil
|
2024-04-19 23:59:26 +01:00
|
|
|
}
|
|
|
|
|
2024-04-28 22:02:13 +01:00
|
|
|
func UnmarshalAnswerGetUserCert(data PacketBody) (AnswerGetUserCert, error) {
|
2024-04-20 17:16:52 +01:00
|
|
|
jsonData, err := json.Marshal(data)
|
|
|
|
if err != nil {
|
2024-04-28 22:02:13 +01:00
|
|
|
return AnswerGetUserCert{}, err
|
2024-04-20 17:16:52 +01:00
|
|
|
}
|
|
|
|
var packet AnswerGetUserCert
|
|
|
|
if err := json.Unmarshal(jsonData, &packet); err != nil {
|
2024-04-28 22:02:13 +01:00
|
|
|
return AnswerGetUserCert{}, err
|
2024-04-20 17:16:52 +01:00
|
|
|
}
|
2024-04-28 22:02:13 +01:00
|
|
|
return packet, nil
|
2024-04-20 17:16:52 +01:00
|
|
|
}
|
2024-04-28 22:02:13 +01:00
|
|
|
func UnmarshalUnreadMsgInfo(data PacketBody) (MsgInfo, error) {
|
2024-04-19 23:59:26 +01:00
|
|
|
jsonData, err := json.Marshal(data)
|
|
|
|
if err != nil {
|
2024-04-28 22:02:13 +01:00
|
|
|
return MsgInfo{}, err
|
2024-04-19 23:59:26 +01:00
|
|
|
}
|
2024-04-20 17:16:52 +01:00
|
|
|
var packet MsgInfo
|
2024-04-19 23:59:26 +01:00
|
|
|
if err := json.Unmarshal(jsonData, &packet); err != nil {
|
2024-04-28 22:02:13 +01:00
|
|
|
return MsgInfo{}, err
|
2024-04-19 23:59:26 +01:00
|
|
|
}
|
2024-04-28 22:02:13 +01:00
|
|
|
return packet, nil
|
2024-04-19 23:59:26 +01:00
|
|
|
}
|
2024-04-20 17:16:52 +01:00
|
|
|
|
2024-04-28 22:02:13 +01:00
|
|
|
func UnmarshalAnswerGetUnreadMsgsInfo(data PacketBody) (AnswerGetUnreadMsgsInfo, error) {
|
2024-04-19 23:59:26 +01:00
|
|
|
jsonData, err := json.Marshal(data)
|
|
|
|
if err != nil {
|
2024-04-28 22:02:13 +01:00
|
|
|
return AnswerGetUnreadMsgsInfo{}, err
|
2024-04-19 23:59:26 +01:00
|
|
|
}
|
2024-04-20 17:16:52 +01:00
|
|
|
var packet AnswerGetUnreadMsgsInfo
|
2024-04-19 23:59:26 +01:00
|
|
|
if err := json.Unmarshal(jsonData, &packet); err != nil {
|
2024-04-28 22:02:13 +01:00
|
|
|
return AnswerGetUnreadMsgsInfo{}, err
|
2024-04-19 23:59:26 +01:00
|
|
|
}
|
2024-04-28 22:02:13 +01:00
|
|
|
return packet, nil
|
2024-04-16 12:23:00 +01:00
|
|
|
}
|
2024-04-17 15:44:38 +01:00
|
|
|
|
2024-04-28 22:02:13 +01:00
|
|
|
func UnmarshalAnswerGetMsg(data PacketBody) (AnswerGetMsg, error) {
|
2024-04-19 23:59:26 +01:00
|
|
|
jsonData, err := json.Marshal(data)
|
|
|
|
if err != nil {
|
2024-04-28 22:02:13 +01:00
|
|
|
return AnswerGetMsg{}, err
|
2024-04-19 23:59:26 +01:00
|
|
|
}
|
2024-04-20 17:16:52 +01:00
|
|
|
var packet AnswerGetMsg
|
2024-04-19 23:59:26 +01:00
|
|
|
if err := json.Unmarshal(jsonData, &packet); err != nil {
|
2024-04-28 22:02:13 +01:00
|
|
|
return AnswerGetMsg{}, err
|
2024-04-19 23:59:26 +01:00
|
|
|
}
|
2024-04-28 22:02:13 +01:00
|
|
|
return packet, nil
|
2024-04-17 15:44:38 +01:00
|
|
|
}
|
2024-04-23 11:12:18 +01:00
|
|
|
|
2024-04-28 22:02:13 +01:00
|
|
|
func UnmarshalReportError(data PacketBody) (ReportError, error) {
|
2024-04-23 11:12:18 +01:00
|
|
|
jsonData, err := json.Marshal(data)
|
|
|
|
if err != nil {
|
2024-04-28 22:02:13 +01:00
|
|
|
return ReportError{}, err
|
2024-04-23 11:12:18 +01:00
|
|
|
}
|
|
|
|
var packet ReportError
|
|
|
|
if err := json.Unmarshal(jsonData, &packet); err != nil {
|
2024-04-28 22:02:13 +01:00
|
|
|
return ReportError{}, err
|
2024-04-23 11:12:18 +01:00
|
|
|
}
|
2024-04-28 22:02:13 +01:00
|
|
|
return packet, nil
|
2024-04-23 11:12:18 +01:00
|
|
|
}
|