Skip to content

Commit

Permalink
Merge pull request #5 from xconnio/reg-msg
Browse files Browse the repository at this point in the history
Add registration related messages
  • Loading branch information
om26er authored May 25, 2024
2 parents f5d660b + 09bdf8d commit 4d3b222
Show file tree
Hide file tree
Showing 5 changed files with 308 additions and 0 deletions.
76 changes: 76 additions & 0 deletions messages/register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package messages

import "fmt"

const MessageTypeRegister = 64
const MessageNameRegister = "REGISTER"

var registerValidationSpec = ValidationSpec{ //nolint:gochecknoglobals
MinLength: 4,
MaxLength: 4,
Message: MessageNameRegister,
Spec: Spec{
1: ValidateRequestID,
2: ValidateOptions,
3: ValidateURI,
},
}

type Register interface {
Message

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

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

func NewEmptyRegister() Register {
return &register{}
}

func NewRegister(requestID int64, options map[string]any, uri string) Register {
return &register{
requestID: requestID,
options: options,
procedure: uri,
}
}

func (r *register) Type() int {
return MessageTypeRegister
}

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

r.requestID = fields.SessionID
r.options = fields.Options
r.procedure = fields.URI

return nil
}

func (r *register) Marshal() []any {
return []any{MessageTypeRegister, r.requestID, r.options, r.procedure}
}

func (r *register) RequestID() int64 {
return r.requestID
}

func (r *register) Options() map[string]any {
return r.options
}

func (r *register) Procedure() string {
return r.procedure
}
67 changes: 67 additions & 0 deletions messages/registered.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package messages

import "fmt"

const MessageTypeRegistered = 65
const MessageNameRegistered = "REGISTERED"

var registeredValidationSpec = ValidationSpec{ //nolint:gochecknoglobals
MinLength: 3,
MaxLength: 3,
Message: MessageNameRegistered,
Spec: Spec{
1: ValidateRequestID,
2: ValidateRegistrationID,
},
}

type Registered interface {
Message

RequestID() int64
RegistrationID() int64
}

type registered struct {
requestID int64
registrationID int64
}

func NewEmptyRegistered() Registered {
return &registered{}
}

func NewRegistered(requestID, registrationID int64) Registered {
return &registered{
requestID: requestID,
registrationID: registrationID,
}
}

func (r *registered) Type() int {
return MessageTypeRegistered
}

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

r.requestID = fields.RequestID
r.registrationID = fields.RegistrationID

return nil
}

func (r *registered) Marshal() []any {
return []any{MessageTypeRegistered, r.requestID, r.registrationID}
}

func (r *registered) RequestID() int64 {
return r.requestID
}

func (r *registered) RegistrationID() int64 {
return r.registrationID
}
67 changes: 67 additions & 0 deletions messages/unregister.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package messages

import "fmt"

const MessageTypeUnRegister = 66
const MessageNameUnRegister = "UNREGISTER"

var unRegisterValidationSpec = ValidationSpec{ //nolint:gochecknoglobals
MinLength: 3,
MaxLength: 3,
Message: MessageNameUnRegister,
Spec: Spec{
1: ValidateRequestID,
2: ValidateRegistrationID,
},
}

type UnRegister interface {
Message

RequestID() int64
RegistrationID() int64
}

type unRegister struct {
requestID int64
registrationID int64
}

func NewEmptyUnRegister() UnRegister {
return &unRegister{}
}

func NewUnRegister(requestID, registrationID int64) UnRegister {
return &unRegister{
requestID: requestID,
registrationID: registrationID,
}
}

func (r *unRegister) Type() int {
return MessageTypeUnRegister
}

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

r.requestID = fields.RequestID
r.registrationID = fields.RegistrationID

return nil
}

func (r *unRegister) Marshal() []any {
return []any{MessageTypeUnRegister, r.requestID, r.registrationID}
}

func (r *unRegister) RequestID() int64 {
return r.requestID
}

func (r *unRegister) RegistrationID() int64 {
return r.registrationID
}
58 changes: 58 additions & 0 deletions messages/unregistered.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package messages

import "fmt"

const MessageTypeUnRegistered = 67
const MessageNameUnRegistered = "UNREGISTERED"

var unRegisteredValidationSpec = ValidationSpec{ //nolint:gochecknoglobals
MinLength: 2,
MaxLength: 2,
Message: MessageNameUnRegistered,
Spec: Spec{
1: ValidateRequestID,
},
}

type UnRegistered interface {
Message

RequestID() int64
}

type unRegistered struct {
requestID int64
}

func NewEmptyUnRegistered() UnRegistered {
return &unRegistered{}
}

func NewUnRegistered(requestID int64) UnRegistered {
return &unRegistered{
requestID: requestID,
}
}

func (r *unRegistered) Type() int {
return MessageTypeUnRegistered
}

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

r.requestID = fields.RequestID

return nil
}

func (r *unRegistered) Marshal() []any {
return []any{MessageTypeUnRegistered, r.requestID}
}

func (r *unRegistered) RequestID() int64 {
return r.requestID
}
40 changes: 40 additions & 0 deletions messages/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,26 @@ func ValidateSessionID(wampMsg []any, index int, fields *Fields) error {
return nil
}

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

fields.RequestID = data
return nil
}

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

fields.RegistrationID = data
return nil
}

func ValidateSignature(wampMsg []any, index int, fields *Fields) error {
data, err := validateString(wampMsg, index)
if err != nil {
Expand All @@ -132,6 +152,16 @@ func ValidateSignature(wampMsg []any, index int, fields *Fields) error {
return nil
}

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

fields.URI = data
return nil
}

func ValidateRealm(wampMsg []any, index int, fields *Fields) error {
data, err := validateString(wampMsg, index)
if err != nil {
Expand Down Expand Up @@ -172,6 +202,16 @@ func ValidateExtra(wampMsg []any, index int, fields *Fields) error {
return nil
}

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

fields.Options = data
return nil
}

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

0 comments on commit 4d3b222

Please sign in to comment.