package members

import "github.com/Diarkis/diarkis/members"

Index

Constants

const NotLimited = -1

NotLimited represents a members container has no member limit

const Unexpected = -2

Unexpected represents an unexpected error

Functions

func EncodeClient

func EncodeClient(client *Client) string

EncodeClient converts the client struct instance into a string. Use DecodeClient to revert it back to the client struct instance.

func IsUserInRoom

func IsUserInRoom(name string, userData *user.User) bool

IsUserInRoom returns true if the user with the matching name is in a members container.

func ResetUser

func ResetUser(name string, userData *user.User)

ResetUser resets the user key data with the matching name.

func UnlockExclusive

func UnlockExclusive(userData *user.User, key string)

UnlockExclusive forcefully unlocks members exclusive

Types

type Client

type Client struct {
	SID      string `json:"s"`
	ID       string `json:"i"`
	MeshAddr string `json:"m"`
	JoinedAt int64  `json:"j"`
	// Available for UDP only: P2P
	PublicAddr string `json:"pu"`
	// Available for UDP only: P2P
	PrivateAddrBytes []byte `json:"pr"`
	// User data copy
	UserData map[string]interface{} `json:"u"`
}

Client is a representation of the user client with its IP addresses and IDs. The struct is derived from *user.User

func DecodeClient

func DecodeClient(str string) *Client

DecodeClient reverts back the encoded client struct instance. It returns a nil if the decoding fails.

func (*Client) CreateUserCopy

func (c *Client) CreateUserCopy() *user.User

CreateUserCopy creates a new user.User instance with the same data as the client.

type ExportData

type ExportData struct {
	Name      string             `json:"n"`
	Limit     int                `json:"l"`
	Exclusive bool               `json:"e"`
	Members   map[string]*Client `json:"m"`
	Reserves  []string           `json:"r"`
}

ExportData is a struct to export members data.

type Members

type Members struct {
	// contains filtered or unexported fields
}

Members a container to store member data. shard and storage are exclusive shard is used when the limit is -1 storage is used when the limit more than 0

func DecodeAndNew

func DecodeAndNew(bytes []byte) *Members

DecodeAndNew creates a new members container from the given byte array. This is used to import an exported data by Encode()

func New

func New(name string) *Members

New creates a members container without a limit to the number of members.

func NewExclusive

func NewExclusive(name string) *Members

NewExclusive creates an exclusive members container that does NOT allow the same user to be in another members container without a limit to the number of members.

func NewExclusiveLimited

func NewExclusiveLimited(name string, limit uint) *Members

NewExclusiveLimited creates an exclusive members container that does NOT allow the same user to be in another members container with a limit to the number of members.

func NewLimited

func NewLimited(name string, limit uint) *Members

NewLimited creates a members container with a limit to the number of members.

func (*Members) Add

func (m *Members) Add(userData *user.User, meshAddr string) error

Add adds a userData.ID as key and userData.SID as value to the container.

func (*Members) AddIf

func (m *Members) AddIf(userData *user.User, meshAddr string, check func() bool) error

AddIf allows the user given to be added to the members if the given callback returns true.

func (*Members) AddReserve

func (m *Members) AddReserve(userData *user.User) bool

AddReserve makes a reservation for the given user to make sure that the user may join the members container later for sure.

func (*Members) AddReserveByUserID

func (m *Members) AddReserveByUserID(userID string) bool

AddReserveByUserID makes a reservation for the given user ID to make sure that the user may join the members container later for sure.

func (*Members) Count

func (m *Members) Count() int

Count returns the number of members in the container.

func (*Members) Encode

func (m *Members) Encode() []byte

Encode returns the members data as a byte array.

func (*Members) Exists

func (m *Members) Exists(userID string) bool

Exists returns true if the given key exists in the members container.

func (*Members) Export

func (m *Members) Export() *ExportData

Export returns the members data as an ExportData struct.

func (*Members) GetAll

func (m *Members) GetAll() map[string]*Client

GetAll returns all member SIDs as a map. The key of the map is user ID and the value of the map is user.

func (*Members) GetExclusiveLockKey

func (m *Members) GetExclusiveLockKey() string

GetExclusiveLockKey returns the key of user exclusive lock

func (*Members) GetLimit

func (m *Members) GetLimit() int

GetLimit returns the maximum allowed number of members.

[NOTE] If there is no limit, it returns NotLimited -1.
[NOTE] If *Member is somehow nil, it returns Unexpected -2.

func (*Members) GetName

func (m *Members) GetName() string

GetName returns the name of members container.

func (*Members) GetOne

func (m *Members) GetOne(userID string) *Client

GetOne returns the member user of the given user ID. If the key does not exist, it returns a nil instead.

func (*Members) GetReserves

func (m *Members) GetReserves() []string

GetReserves returns all reserved user IDs as a map.

func (*Members) IsAddAllowed

func (m *Members) IsAddAllowed(clientsToBeAdded int) bool

IsAddAllowed returns true if the members contain still has room to add more users.

func (*Members) Keys

func (m *Members) Keys() []string

Keys returns all member keys as an array. The keys are an array of user IDs.

func (*Members) Remove

func (m *Members) Remove(userData *user.User) bool

Remove removes a member data from the container.

func (*Members) RemoveAll

func (m *Members) RemoveAll()

RemoveAll removes all member users form the members container.

func (*Members) RemoveReserve

func (m *Members) RemoveReserve(userData *user.User) bool

RemoveReserve cancels the reservation for the given user.

func (*Members) RemoveReserveByUserID

func (m *Members) RemoveReserveByUserID(userID string) bool

RemoveReserveByUserID cancels the reservation for the given user ID.

func (*Members) SetReadOnly

func (m *Members) SetReadOnly()

SetReadOnly makes the members container read-only.

func (*Members) UnsetReadOnly

func (m *Members) UnsetReadOnly()

UnsetReadOnly makes the members container writable.

func (*Members) UpdateClientByID

func (m *Members) UpdateClientByID(uid, sid, pubAddr string, privateAddrs []byte, meshAddr string) bool

UpdateClientByID updates internal client data