Fix mistake with generating new message ID instead of reusing existing ID

This commit is contained in:
ChronosX88 2020-02-10 14:56:13 +04:00
parent ff10a18f3b
commit 7e534680c7
3 changed files with 10 additions and 7 deletions

View File

@ -1,7 +1,5 @@
package models package models
import "github.com/google/uuid"
// BaseMessage is a basic message model, basis of the whole protocol. It is used for a very easy protocol extension process. // BaseMessage is a basic message model, basis of the whole protocol. It is used for a very easy protocol extension process.
type BaseMessage struct { type BaseMessage struct {
ID string `json:"id"` ID string `json:"id"`
@ -13,11 +11,9 @@ type BaseMessage struct {
Payload map[string]interface{} `json:"payload"` Payload map[string]interface{} `json:"payload"`
} }
func NewBaseMessage(messageType string, from string, to string, ok bool, payload map[string]interface{}) BaseMessage { func NewBaseMessage(id, messageType, from, to string, ok bool, payload map[string]interface{}) BaseMessage {
uuid, _ := uuid.NewRandom()
uuidStr := uuid.String()
return BaseMessage{ return BaseMessage{
ID: uuidStr, ID: id,
MessageType: messageType, MessageType: messageType,
From: from, From: from,
To: to, To: to,

View File

@ -53,7 +53,14 @@ func (r *Router) RouteMessage(origin *OriginC2S, message models.BaseMessage) {
go v.HandlerFunc(origin, message) go v.HandlerFunc(origin, message)
} }
} else { } else {
protocolError := models.ProtocolError{
ErrCode: "unhandled",
ErrText: "Server doesn't implement message type " + message.MessageType,
ErrPayload: make(map[string]interface{}),
}
errMsg := models.NewBaseMessage(message.ID, message.MessageType, serverDomain, message.From, false, StructToMap(protocolError))
logger.Infof("Drop message with type %s because server hasn't proper handlers", message.MessageType) logger.Infof("Drop message with type %s because server hasn't proper handlers", message.MessageType)
origin.Send(errMsg)
} }
} }

View File

@ -45,6 +45,6 @@ func PrepareMessageUnauthorized(msg models.BaseMessage) models.BaseMessage {
ErrText: "Unauthorized access", ErrText: "Unauthorized access",
ErrPayload: make(map[string]interface{}), ErrPayload: make(map[string]interface{}),
} }
errMsg := models.NewBaseMessage(msg.MessageType, serverDomain, msg.From, false, StructToMap(protocolError)) errMsg := models.NewBaseMessage(msg.ID, msg.MessageType, serverDomain, msg.From, false, StructToMap(protocolError))
return errMsg return errMsg
} }