Skip to content

Commit

Permalink
Merge 925e098 into ffe135c
Browse files Browse the repository at this point in the history
  • Loading branch information
rcmgleite committed Aug 21, 2018
2 parents ffe135c + 925e098 commit ccbce9a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 25 deletions.
31 changes: 17 additions & 14 deletions examples/demo/cluster/services/room.go
Expand Up @@ -62,18 +62,6 @@ type (
}
)

// Outbound gets the outbound status
func (Stats *Stats) Outbound(ctx context.Context, in []byte) ([]byte, error) {
Stats.outboundBytes += len(in)
return in, nil
}

// Inbound gets the inbound status
func (Stats *Stats) Inbound(ctx context.Context, in []byte) ([]byte, error) {
Stats.inboundBytes += len(in)
return in, nil
}

// NewRoom returns a new room
func NewRoom() *Room {
return &Room{
Expand All @@ -96,11 +84,13 @@ func (r *Room) AfterInit() {

// Entry is the entrypoint
func (r *Room) Entry(ctx context.Context, msg []byte) (*JoinResponse, error) {
logger := pitaya.GetDefaultLoggerFromCtx(ctx) // The default logger contains a requestId, the route being executed and the sessionId
s := pitaya.GetSessionFromCtx(ctx)
//err := s.Bind(ctx, strconv.Itoa(int(s.ID())))

err := s.Bind(ctx, "helroow")
if err != nil {
logger.Error("Failed to bind session")
logger.Error(err)
return nil, pitaya.Error(err, "RH-000", map[string]string{"failed": "bind"})
}
return &JoinResponse{Result: "ok"}, nil
Expand All @@ -116,9 +106,12 @@ func (r *Room) GetSessionData(ctx context.Context) (*SessionData, error) {

// SetSessionData sets the session data
func (r *Room) SetSessionData(ctx context.Context, data *SessionData) ([]byte, error) {
logger := pitaya.GetDefaultLoggerFromCtx(ctx)
s := pitaya.GetSessionFromCtx(ctx)
err := s.SetData(data.Data)
if err != nil {
logger.Error("Failed to set session data")
logger.Error(err)
return nil, err
}
err = s.PushToFront(ctx)
Expand All @@ -130,32 +123,42 @@ func (r *Room) SetSessionData(ctx context.Context, data *SessionData) ([]byte, e

// Join room
func (r *Room) Join(ctx context.Context) (*JoinResponse, error) {
logger := pitaya.GetDefaultLoggerFromCtx(ctx)
s := pitaya.GetSessionFromCtx(ctx)
err := r.group.Add(s)
if err != nil {
logger.Error("Failed to join room")
logger.Error(err)
return nil, err
}
s.Push("onMembers", &AllMembers{Members: r.group.Members()})
r.group.Broadcast("onNewUser", &NewUser{Content: fmt.Sprintf("New user: %d", s.ID())})
if err != nil {
logger.Error("Failed to broadcast onNewUser")
logger.Error(err)
return nil, err
}
return &JoinResponse{Result: "success"}, nil
}

// Message sync last message to all members
func (r *Room) Message(ctx context.Context, msg *UserMessage) {
logger := pitaya.GetDefaultLoggerFromCtx(ctx)
err := r.group.Broadcast("onMessage", msg)
if err != nil {
fmt.Println("error broadcasting message", err)
logger.Error("Error broadcasting message")
logger.Error(err)
}
}

// SendRPC sends rpc
func (r *Room) SendRPC(ctx context.Context, msg *SendRPCMsg) (*protos.RPCRes, error) {
logger := pitaya.GetDefaultLoggerFromCtx(ctx)
ret := &protos.RPCRes{}
err := pitaya.RPCTo(ctx, msg.ServerID, msg.Route, ret, &protos.RPCMsg{Msg: msg.Msg})
if err != nil {
logger.Errorf("Failed to execute RPCTo %s - %s", msg.ServerID, msg.Route)
logger.Error(err)
return nil, pitaya.Error(err, "RPC-000")
}
return ret, nil
Expand Down
22 changes: 11 additions & 11 deletions examples/demo/pipeline/main.go
Expand Up @@ -5,7 +5,6 @@ import (
"flag"
"fmt"

"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/topfreegames/pitaya"
"github.com/topfreegames/pitaya/acceptor"
Expand All @@ -16,14 +15,11 @@ import (
// MetagameServer ...
type MetagameServer struct {
component.Base
Logger logrus.FieldLogger
}

// NewMetagameMock ...
func NewMetagameMock() *MetagameServer {
return &MetagameServer{
Logger: logrus.New(),
}
return &MetagameServer{}
}

// CreatePlayerCheatArgs is the struct used as parameter for the CreatePlayerCheat handler
Expand All @@ -44,6 +40,8 @@ type CreatePlayerCheatResponse struct {

// CreatePlayerCheat ...
func (g *MetagameServer) CreatePlayerCheat(ctx context.Context, args *CreatePlayerCheatArgs) (*CreatePlayerCheatResponse, error) {
logger := pitaya.GetDefaultLoggerFromCtx(ctx) // The default logger contains a requestId, the route being executed and the sessionId
logger.Info("CreatePlayerChest called")
// Do nothing. This is just an example of how pipelines can be helpful
return &CreatePlayerCheatResponse{
Msg: "ok",
Expand All @@ -56,20 +54,22 @@ func (g *MetagameServer) CreatePlayerCheat(ctx context.Context, args *CreatePlay
// as a pipeline function executes for every handler and each of them
// most probably have different parameter types.
func (g *MetagameServer) simpleBefore(ctx context.Context, in interface{}) (interface{}, error) {
g.Logger.Info("Simple Before exec")
logger := pitaya.GetDefaultLoggerFromCtx(ctx)
logger.Info("Simple Before exec")
createPlayerArgs := in.(*CreatePlayerCheatArgs)

g.Logger.Infof("Name: %s", createPlayerArgs.Name)
g.Logger.Infof("Email: %s", createPlayerArgs.Email)
g.Logger.Infof("SoftCurrency: %d", createPlayerArgs.SoftCurrency)
g.Logger.Infof("HardCurrency: %d", createPlayerArgs.HardCurrency)
logger.Infof("Name: %s", createPlayerArgs.Name)
logger.Infof("Email: %s", createPlayerArgs.Email)
logger.Infof("SoftCurrency: %d", createPlayerArgs.SoftCurrency)
logger.Infof("HardCurrency: %d", createPlayerArgs.HardCurrency)

return in, nil
}

// Simple example of an after pipeline. The 2nd argument is the handler response.
func (g *MetagameServer) simpleAfter(ctx context.Context, resp interface{}) (interface{}, error) {
g.Logger.Info("Simple After exec - response:", resp)
logger := pitaya.GetDefaultLoggerFromCtx(ctx)
logger.Info("Simple After exec - response:", resp)

return resp, nil
}
Expand Down

0 comments on commit ccbce9a

Please sign in to comment.