41 lines
850 B
Go
41 lines
850 B
Go
package protocol
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
)
|
|
|
|
type ControlMessage struct {
|
|
Type MessageType `json:"type"`
|
|
Topic Topic `json:"topic,omitempty"`
|
|
Topics []Topic `json:"topics,omitempty"`
|
|
}
|
|
|
|
type BroadcastMessage struct {
|
|
Type MessageType `json:"type"`
|
|
Topic Topic `json:"topic"`
|
|
Payload json.RawMessage `json:"payload"`
|
|
Timestamp int64 `json:"timestamp"`
|
|
}
|
|
|
|
func (m ControlMessage) Validate() error {
|
|
switch m.Type {
|
|
case MsgInit:
|
|
if len(m.Topics) == 0 {
|
|
return errors.New("init requires topics")
|
|
}
|
|
case MsgSubscribe:
|
|
if m.Topic == "" {
|
|
return errors.New("subscribe requires topic")
|
|
}
|
|
default:
|
|
return errors.New("unknown message type")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
var (
|
|
ErrInvalidMessage = errors.New("invalid message")
|
|
ErrPolicyViolation = errors.New("policy violation")
|
|
)
|