[PD1] small changes
This commit is contained in:
parent
568b6e6739
commit
2cafc3163c
10 changed files with 160 additions and 71 deletions
|
@ -29,6 +29,12 @@ const (
|
|||
|
||||
// Server sends requested message
|
||||
FlagAnswerGetMsg
|
||||
|
||||
// Server tells the client that the message was successfully sent
|
||||
FlagAnswerSendMsg
|
||||
|
||||
// Report an error
|
||||
FlagReportError
|
||||
)
|
||||
|
||||
type (
|
||||
|
@ -76,6 +82,10 @@ type (
|
|||
Body []byte `json:"body"`
|
||||
Timestamp time.Time `json:"timestamp"`
|
||||
}
|
||||
|
||||
ReportError struct {
|
||||
ErrorMessage string `json:"error"`
|
||||
}
|
||||
)
|
||||
|
||||
type PacketBody interface{}
|
||||
|
@ -127,7 +137,7 @@ func NewAnswerGetUserCert(uid string, certificate []byte) AnswerGetUserCert {
|
|||
}
|
||||
|
||||
func NewAnswerGetUnreadMsgsInfo(page int, numPages int, messagesInfo []MsgInfo) AnswerGetUnreadMsgsInfo {
|
||||
return AnswerGetUnreadMsgsInfo{Page:page,NumPages:numPages,MessagesInfo: messagesInfo}
|
||||
return AnswerGetUnreadMsgsInfo{Page: page, NumPages: numPages, MessagesInfo: messagesInfo}
|
||||
}
|
||||
func NewMsgInfo(num int, fromUID string, subject []byte, timestamp time.Time) MsgInfo {
|
||||
return MsgInfo{
|
||||
|
@ -148,6 +158,12 @@ func NewAnswerGetMsg(fromUID, toUID string, subject []byte, body []byte, timesta
|
|||
}
|
||||
}
|
||||
|
||||
func NewReportError(errorMessage string) ReportError {
|
||||
return ReportError{
|
||||
ErrorMessage: errorMessage,
|
||||
}
|
||||
}
|
||||
|
||||
func NewGetUserCertPacket(UID string) Packet {
|
||||
return NewPacket(FlagGetUserCert, NewGetUserCert(UID))
|
||||
}
|
||||
|
@ -169,13 +185,22 @@ func NewAnswerGetUserCertPacket(uid string, certificate []byte) Packet {
|
|||
}
|
||||
|
||||
func NewAnswerGetUnreadMsgsInfoPacket(page int, numPages int, messagesInfo []MsgInfo) Packet {
|
||||
return NewPacket(FlagAnswerGetUnreadMsgsInfo, NewAnswerGetUnreadMsgsInfo(page,numPages,messagesInfo))
|
||||
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 NewAnswerSendMsgPacket() Packet{
|
||||
//This packet has no body
|
||||
return NewPacket(FlagAnswerSendMsg,nil)
|
||||
}
|
||||
|
||||
func NewReportErrorPacket(errorMessage string) Packet {
|
||||
return NewPacket(FlagReportError, NewReportError(errorMessage))
|
||||
}
|
||||
|
||||
func UnmarshalGetUserCert(data PacketBody) GetUserCert {
|
||||
jsonData, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
|
@ -270,3 +295,15 @@ func UnmarshalAnswerGetMsg(data PacketBody) AnswerGetMsg {
|
|||
}
|
||||
return packet
|
||||
}
|
||||
|
||||
func UnmarshalReportError(data PacketBody) ReportError {
|
||||
jsonData, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("failed to marshal data: %v", err))
|
||||
}
|
||||
var packet ReportError
|
||||
if err := json.Unmarshal(jsonData, &packet); err != nil {
|
||||
panic(fmt.Errorf("failed to unmarshal into AnswerGetMsg: %v", err))
|
||||
}
|
||||
return packet
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue