Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add WAMP control messages #6

Merged
merged 4 commits into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions messages/cancel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package messages

import "fmt"

const MessageTypeCancel = 49
const MessageNameCancel = "CANCEL"

var cancelValidationSpec = ValidationSpec{ //nolint:gochecknoglobals
MinLength: 3,
MaxLength: 3,
Message: MessageNameCancel,
Spec: Spec{
1: ValidateRequestID,
2: ValidateOptions,
},
}

type Cancel interface {
Message

RequestID() int64
Options() map[string]any
}

type cancel struct {
requestID int64
options map[string]any
}

func NewEmptyCancel() Cancel {
return &cancel{}
}

func NewCancel(requestID int64, options map[string]any) Cancel {
return &cancel{
requestID: requestID,
options: options,
}
}

func (c *cancel) RequestID() int64 {
return c.requestID
}

func (c *cancel) Options() map[string]any {
return c.options
}

func (c *cancel) Type() int {
return MessageTypeCancel
}

func (c *cancel) Parse(wampMsg []any) error {
fields, err := ValidateMessage(wampMsg, cancelValidationSpec)
if err != nil {
return fmt.Errorf("cancel: failed to validate message %s: %w", MessageNameCancel, err)
}

c.requestID = fields.RequestID
c.options = fields.Options

return nil
}

func (c *cancel) Marshal() []any {
return []any{MessageTypeCancel, c.requestID, c.options}
}
116 changes: 116 additions & 0 deletions messages/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package messages

import "fmt"

const MessageTypeError = 8
const MessageNameError = "ERROR"

var errorValidationSpec = ValidationSpec{ //nolint:gochecknoglobals
MinLength: 5,
MaxLength: 7,
Message: MessageNameError,
Spec: Spec{
1: ValidateMessageType,
2: ValidateRequestID,
3: ValidateDetails,
4: ValidateURI,
5: ValidateArgs,
6: ValidateKwArgs,
},
}

type Error interface {
Message

MessageType() int64
RequestID() int64
Details() map[string]any
URI() string
Args() []any
KwArgs() map[string]any
}

type err struct {
messageType int64
requestID int64
details map[string]any
uri string
args []any
kwArgs map[string]any
}

func NewEmptyError() Error {
return &err{}
}

func NewError(messageType, requestID int64, uri string, args []any, kwArgs map[string]any) Error {
return &err{
messageType: messageType,
requestID: requestID,
uri: uri,
args: args,
kwArgs: kwArgs,
}
}

func (e *err) MessageType() int64 {
return e.messageType
}

func (e *err) RequestID() int64 {
return e.requestID
}

func (e *err) Details() map[string]any {
return e.details
}

func (e *err) URI() string {
return e.uri
}

func (e *err) Args() []any {
return e.args
}

func (e *err) KwArgs() map[string]any {
return e.kwArgs
}

func (e *err) Type() int {
return MessageTypeError
}

func (e *err) Parse(wampMsg []any) error {
fields, err := ValidateMessage(wampMsg, errorValidationSpec)
if err != nil {
return fmt.Errorf("error: failed to validate message %s: %w", MessageNameError, err)
}

e.messageType = fields.MessageType
e.requestID = fields.RequestID
e.details = fields.Details
e.uri = fields.URI
e.args = fields.Args
e.kwArgs = fields.KwArgs

return nil
}

func (e *err) Marshal() []any {
result := []any{MessageTypeError, e.messageType, e.requestID, e.uri}

if e.args != nil {
result = append(result, e.args)
}

if e.kwArgs != nil {
if e.args == nil {
result = append(result, []any{})
}

result = append(result, e.kwArgs)
}

return result
}
67 changes: 67 additions & 0 deletions messages/goodbye.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package messages

import "fmt"

const MessageTypeGoodbye = 6
const MessageNameGoodbye = "GOODBYE"

var goodByeValidationSpec = ValidationSpec{ //nolint:gochecknoglobals
MinLength: 3,
MaxLength: 3,
Message: MessageNameGoodbye,
Spec: Spec{
1: ValidateDetails,
2: ValidateReason,
},
}

type GoodBye interface {
Message

Details() map[string]any
Reason() string
}

type goodBye struct {
details map[string]any
reason string
}

func NewEmptyGoodBye() GoodBye {
return &goodBye{}
}

func NewGoodBye(reason string, details map[string]any) GoodBye {
return &goodBye{
reason: reason,
details: details,
}
}

func (g *goodBye) Reason() string {
return g.reason
}

func (g *goodBye) Details() map[string]any {
return g.details
}

func (g *goodBye) Type() int {
return MessageTypeGoodbye
}

func (g *goodBye) Parse(wampMsg []any) error {
fields, err := ValidateMessage(wampMsg, goodByeValidationSpec)
if err != nil {
return fmt.Errorf("goodbye: failed to validate message %s: %w", MessageNameGoodbye, err)
}

g.details = fields.Details
g.reason = fields.Reason

return nil
}

func (g *goodBye) Marshal() []any {
return []any{MessageTypeGoodbye, g.details, g.reason}
}
67 changes: 67 additions & 0 deletions messages/interrupt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package messages

import "fmt"

const MessageTypeInterrupt = 69
const MessageNameInterrupt = "INTERRUPT"

var interruptValidationSpec = ValidationSpec{ //nolint:gochecknoglobals
MinLength: 3,
MaxLength: 3,
Message: MessageNameInterrupt,
Spec: Spec{
1: ValidateRequestID,
2: ValidateOptions,
},
}

type Interrupt interface {
Message

RequestID() int64
Options() map[string]any
}

type interrupt struct {
requestID int64
options map[string]any
}

func NewEmptyInterrupt() Interrupt {
return &interrupt{}
}

func NewInterrupt(requestID int64, options map[string]any) Interrupt {
return &interrupt{
requestID: requestID,
options: options,
}
}

func (c *interrupt) RequestID() int64 {
return c.requestID
}

func (c *interrupt) Options() map[string]any {
return c.options
}

func (c *interrupt) Type() int {
return MessageTypeInterrupt
}

func (c *interrupt) Parse(wampMsg []any) error {
fields, err := ValidateMessage(wampMsg, interruptValidationSpec)
if err != nil {
return fmt.Errorf("interrupt: failed to validate message %s: %w", MessageNameInterrupt, err)
}

c.requestID = fields.RequestID
c.options = fields.Options

return nil
}

func (c *interrupt) Marshal() []any {
return []any{MessageTypeInterrupt, c.requestID, c.options}
}
12 changes: 11 additions & 1 deletion messages/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type Fields struct {
AuthExtra map[string]any
Roles map[string]any

MessageType int
MessageType int64
Signature string
Reason string
Topic string
Expand Down Expand Up @@ -122,6 +122,16 @@ func ValidateSessionID(wampMsg []any, index int, fields *Fields) error {
return nil
}

func ValidateMessageType(wampMsg []any, index int, fields *Fields) error {
data, err := validateID(wampMsg, index)
if err != nil {
return err
}

fields.MessageType = data
return nil
}

func ValidateRequestID(wampMsg []any, index int, fields *Fields) error {
data, err := validateID(wampMsg, index)
if err != nil {
Expand Down
Loading