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

Re-add static legacy API to allow for easier migration to v2 #183

Merged
merged 5 commits into from
Sep 1, 2021
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
38 changes: 38 additions & 0 deletions session/static.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package session

import (
"context"
)

var DefaultSessionPool SessionPool

// GetSessionByUID return a session bound to an user id
func GetSessionByUID(uid string) Session {
return DefaultSessionPool.GetSessionByUID(uid)
}

// GetSessionByID return a session bound to a frontend server id
func GetSessionByID(id int64) Session {
return DefaultSessionPool.GetSessionByID(id)
}

// OnSessionBind adds a method to be called when a session is bound
// same function cannot be added twice!
luizmiranda7 marked this conversation as resolved.
Show resolved Hide resolved
func OnSessionBind(f func(ctx context.Context, s Session) error) {
DefaultSessionPool.OnSessionBind(f)
}

// OnAfterSessionBind adds a method to be called when session is bound and after all sessionBind callbacks
func OnAfterSessionBind(f func(ctx context.Context, s Session) error) {
DefaultSessionPool.OnAfterSessionBind(f)
}

// OnSessionClose adds a method that will be called when every session closes
func OnSessionClose(f func(s Session)) {
DefaultSessionPool.OnSessionClose(f)
}

// CloseAll calls Close on all sessions
func CloseAll() {
DefaultSessionPool.CloseAll()
}
117 changes: 117 additions & 0 deletions session/test/static_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package test

import (
"testing"

"github.com/golang/mock/gomock"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
"github.com/topfreegames/pitaya/v2/session"
"github.com/topfreegames/pitaya/v2/session/mocks"
)

func TestStaticGetSessionByUID(t *testing.T) {
createSessionMock := func(ctrl *gomock.Controller) session.Session {
return mocks.NewMockSession(ctrl)
}
createNilSession := func(ctrl *gomock.Controller) session.Session {
return mocks.NewMockSession(ctrl)
}

tables := []struct {
name string
uid string
factory func(ctrl *gomock.Controller) session.Session
}{
{"Success", uuid.New().String(), createSessionMock},
{"Error", uuid.New().String(), createNilSession},
}

for _, row := range tables {
t.Run(row.name, func(t *testing.T) {
ctrl := gomock.NewController(t)

expectedSession := row.factory(ctrl)

sessionPool := mocks.NewMockSessionPool(ctrl)
sessionPool.EXPECT().GetSessionByUID(row.uid).Return(expectedSession)

session.DefaultSessionPool = sessionPool
session := session.GetSessionByUID(row.uid)
require.Equal(t, expectedSession, session)
})
}
}

func TestStaticGetSessionByID(t *testing.T) {
createSessionMock := func(ctrl *gomock.Controller) session.Session {
return mocks.NewMockSession(ctrl)
}
createNilSession := func(ctrl *gomock.Controller) session.Session {
return mocks.NewMockSession(ctrl)
}

tables := []struct {
name string
id int64
factory func(ctrl *gomock.Controller) session.Session
}{
{"Success", 3, createSessionMock},
{"Error", 3, createNilSession},
}

for _, row := range tables {
t.Run(row.name, func(t *testing.T) {
ctrl := gomock.NewController(t)

expectedSession := row.factory(ctrl)

sessionPool := mocks.NewMockSessionPool(ctrl)
sessionPool.EXPECT().GetSessionByID(row.id).Return(expectedSession)

session.DefaultSessionPool = sessionPool
session := session.GetSessionByID(row.id)
require.Equal(t, expectedSession, session)
})
}
}

func TestStaticOnSessionBind(t *testing.T) {
ctrl := gomock.NewController(t)

sessionPool := mocks.NewMockSessionPool(ctrl)
sessionPool.EXPECT().OnSessionBind(nil)

session.DefaultSessionPool = sessionPool
session.OnSessionBind(nil)
}

func TestStaticOnAfterSessionBind(t *testing.T) {
ctrl := gomock.NewController(t)

sessionPool := mocks.NewMockSessionPool(ctrl)
sessionPool.EXPECT().OnAfterSessionBind(nil)

session.DefaultSessionPool = sessionPool
session.OnAfterSessionBind(nil)
}

func TestStaticOnSessionClose(t *testing.T) {
ctrl := gomock.NewController(t)

sessionPool := mocks.NewMockSessionPool(ctrl)
sessionPool.EXPECT().OnSessionClose(nil)

session.DefaultSessionPool = sessionPool
session.OnSessionClose(nil)
}

func TestStaticCloseAll(t *testing.T) {
ctrl := gomock.NewController(t)

sessionPool := mocks.NewMockSessionPool(ctrl)
sessionPool.EXPECT().CloseAll()

session.DefaultSessionPool = sessionPool
session.CloseAll()
}
222 changes: 222 additions & 0 deletions static.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
// Copyright (c) TFG Co. All Rights Reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

package pitaya

import (
"context"
"time"

"github.com/golang/protobuf/proto"
"github.com/spf13/viper"
"github.com/topfreegames/pitaya/v2/cluster"
"github.com/topfreegames/pitaya/v2/component"
"github.com/topfreegames/pitaya/v2/config"
"github.com/topfreegames/pitaya/v2/interfaces"
"github.com/topfreegames/pitaya/v2/metrics"
"github.com/topfreegames/pitaya/v2/router"
"github.com/topfreegames/pitaya/v2/session"
"github.com/topfreegames/pitaya/v2/worker"
)

var DefaultApp Pitaya

// Configure configures the app
func Configure(
isFrontend bool,
serverType string,
serverMode ServerMode,
serverMetadata map[string]string,
cfgs ...*viper.Viper,
) {
builder := NewBuilderWithConfigs(
isFrontend,
serverType,
serverMode,
serverMetadata,
config.NewConfig(cfgs...),
)
DefaultApp = builder.Build()
session.DefaultSessionPool = builder.SessionPool
}

func GetDieChan() chan bool {
return DefaultApp.GetDieChan()
}

func SetDebug(debug bool) {
DefaultApp.SetDebug(debug)
}

func SetHeartbeatTime(interval time.Duration) {
DefaultApp.SetHeartbeatTime(interval)
}

func GetServerID() string {
return DefaultApp.GetServerID()
}

func GetMetricsReporters() []metrics.Reporter {
return DefaultApp.GetMetricsReporters()
}

func GetServer() *cluster.Server {
return DefaultApp.GetServer()
}

func GetServerByID(id string) (*cluster.Server, error) {
return DefaultApp.GetServerByID(id)
}

func GetServersByType(t string) (map[string]*cluster.Server, error) {
return DefaultApp.GetServersByType(t)
}

func GetServers() []*cluster.Server {
return DefaultApp.GetServers()
}

func GetSessionFromCtx(ctx context.Context) session.Session {
return DefaultApp.GetSessionFromCtx(ctx)
}

func Start() {
DefaultApp.Start()
}

func SetDictionary(dict map[string]uint16) error {
return DefaultApp.SetDictionary(dict)
}

func AddRoute(serverType string, routingFunction router.RoutingFunc) error {
return DefaultApp.AddRoute(serverType, routingFunction)
}

func Shutdown() {
DefaultApp.Shutdown()
}

func StartWorker() {
DefaultApp.StartWorker()
}

func RegisterRPCJob(rpcJob worker.RPCJob) error {
return DefaultApp.RegisterRPCJob(rpcJob)
}

func Documentation(getPtrNames bool) (map[string]interface{}, error) {
return DefaultApp.Documentation(getPtrNames)
}

func IsRunning() bool {
return DefaultApp.IsRunning()
}

func RPC(ctx context.Context, routeStr string, reply proto.Message, arg proto.Message) error {
return DefaultApp.RPC(ctx, routeStr, reply, arg)
}

func RPCTo(ctx context.Context, serverID, routeStr string, reply proto.Message, arg proto.Message) error {
return DefaultApp.RPCTo(ctx, serverID, routeStr, reply, arg)
}

func ReliableRPC(routeStr string, metadata map[string]interface{}, reply, arg proto.Message) (jid string, err error) {
return DefaultApp.ReliableRPC(routeStr, metadata, reply, arg)
}

func ReliableRPCWithOptions(routeStr string, metadata map[string]interface{}, reply, arg proto.Message, opts *config.EnqueueOpts) (jid string, err error) {
return DefaultApp.ReliableRPCWithOptions(routeStr, metadata, reply, arg, opts)
}

func SendPushToUsers(route string, v interface{}, uids []string, frontendType string) ([]string, error) {
return DefaultApp.SendPushToUsers(route, v, uids, frontendType)
}

func SendKickToUsers(uids []string, frontendType string) ([]string, error) {
return DefaultApp.SendKickToUsers(uids, frontendType)
}

func GroupCreate(ctx context.Context, groupName string) error {
return DefaultApp.GroupCreate(ctx, groupName)
}

func GroupCreateWithTTL(ctx context.Context, groupName string, ttlTime time.Duration) error {
return DefaultApp.GroupCreateWithTTL(ctx, groupName, ttlTime)
}

func GroupMembers(ctx context.Context, groupName string) ([]string, error) {
return DefaultApp.GroupMembers(ctx, groupName)
}

func GroupBroadcast(ctx context.Context, frontendType, groupName, route string, v interface{}) error {
return DefaultApp.GroupBroadcast(ctx, frontendType, groupName, route, v)
}

func GroupContainsMember(ctx context.Context, groupName, uid string) (bool, error) {
return DefaultApp.GroupContainsMember(ctx, groupName, uid)
}

func GroupAddMember(ctx context.Context, groupName, uid string) error {
return DefaultApp.GroupAddMember(ctx, groupName, uid)
}

func GroupRemoveMember(ctx context.Context, groupName, uid string) error {
return DefaultApp.GroupRemoveMember(ctx, groupName, uid)
}

func GroupRemoveAll(ctx context.Context, groupName string) error {
return DefaultApp.GroupRemoveAll(ctx, groupName)
}

func GroupCountMembers(ctx context.Context, groupName string) (int, error) {
return DefaultApp.GroupCountMembers(ctx, groupName)
}

func GroupRenewTTL(ctx context.Context, groupName string) error {
return DefaultApp.GroupRenewTTL(ctx, groupName)
}

func GroupDelete(ctx context.Context, groupName string) error {
return DefaultApp.GroupDelete(ctx, groupName)
}

func Register(c component.Component, options ...component.Option) {
DefaultApp.Register(c, options...)
}

func RegisterRemote(c component.Component, options ...component.Option) {
DefaultApp.RegisterRemote(c, options...)
}

func RegisterModule(module interfaces.Module, name string) error {
return DefaultApp.RegisterModule(module, name)
}

func RegisterModuleAfter(module interfaces.Module, name string) error {
return DefaultApp.RegisterModuleAfter(module, name)
}

func RegisterModuleBefore(module interfaces.Module, name string) error {
return DefaultApp.RegisterModuleBefore(module, name)
}

func GetModule(name string) (interfaces.Module, error) {
return DefaultApp.GetModule(name)
}
Loading