Skip to content

Commit

Permalink
clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
ryansu committed Jun 17, 2021
1 parent ed03d9b commit bd60b11
Show file tree
Hide file tree
Showing 11 changed files with 319 additions and 188 deletions.
6 changes: 3 additions & 3 deletions cherry.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/cherry-game/cherry/const"
"github.com/cherry-game/cherry/extend/time"
"github.com/cherry-game/cherry/logger"
"github.com/cherry-game/cherry/net/cluster"
"github.com/cherry-game/cherry/net/node"
"github.com/cherry-game/cherry/profile"
)

Expand Down Expand Up @@ -35,12 +35,12 @@ func NewApp(profilePath, profileName, nodeId string) *Application {
panic(fmt.Sprintf("init profile fail. error = %s", err))
}

err = cherryCluster.Load(config)
err = cherryNode.Load(config)
if err != nil {
panic(fmt.Sprintf("load node config fail. error = %s", err))
}

node, err := cherryCluster.GetNode(nodeId)
node, err := cherryNode.GetNode(nodeId)
if err != nil {
panic(fmt.Sprintf("nodeId = %s not found. error = %s", nodeId, err))
}
Expand Down
39 changes: 20 additions & 19 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
module github.com/cherry-game/cherry

go 1.15

require (
github.com/ahmetb/go-linq/v3 v3.2.0 // indirect
github.com/gin-gonic/gin v1.6.3
github.com/golang/protobuf v1.3.3
github.com/gorilla/websocket v1.4.2
github.com/json-iterator/go v1.1.10
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/radovskyb/watcher v1.0.7
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.16.0
gopkg.in/natefinch/lumberjack.v2 v2.0.0
gorm.io/driver/mysql v1.0.2
gorm.io/gorm v1.20.5
)
module github.com/cherry-game/cherry

go 1.15

require (
github.com/ahmetb/go-linq/v3 v3.2.0 // indirect
github.com/gin-gonic/gin v1.6.3
github.com/golang/protobuf v1.5.2
github.com/gorilla/websocket v1.4.2
github.com/json-iterator/go v1.1.10
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/radovskyb/watcher v1.0.7
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.16.0
google.golang.org/grpc v1.38.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0
gorm.io/driver/mysql v1.0.2
gorm.io/gorm v1.20.5
)
310 changes: 192 additions & 118 deletions go.sum

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions net/agent/agent_remote.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package cherryAgent

import cherryFacade "github.com/cherry-game/cherry/facade"

type AgentRemote struct {
sid cherryFacade.SID
gateClient interface{}
}
4 changes: 4 additions & 0 deletions net/cluster/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ type ClusterComponent struct {
//node discovery

}

func a() {

}
1 change: 1 addition & 0 deletions net/cluster/proto/build.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
protoc --go_out=plugins=grpc:../ --go_opt=paths=source_relative cluster.proto
45 changes: 45 additions & 0 deletions net/cluster/proto/cluster.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
syntax = "proto3";

option go_package = "/cherryCluster";

package cherryCluster;

message NodeId {
string id = 1;
}

message NodeInfo {
string id = 1;
string nodeType = 2;
string address = 3;
}

message NodeList {
repeated NodeInfo nodesInfo = 1;
}

message Response {
}

//service MasterNode {
// rpc Register(NodeInfo) returns(NodeList) {}
// rpc Unregister(NodeId) returns(Response) {}
//}

message ForwardMessage {
int32 type = 1;
string nodeId = 2;
int64 sid = 3;
uint64 id = 4;
string route = 5;
bytes data = 6;
}

message SessionId {
int64 sid = 1;
}

service MemberNode {
rpc Forward(ForwardMessage) returns (Response) {}
rpc CloseSession(SessionId) returns (Response) {}
}
22 changes: 22 additions & 0 deletions net/cluster/rpc_client.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package cherryCluster

import cherryFacade "github.com/cherry-game/cherry/facade"

type RpcNodeInfo struct {
Id string
Host string
Expand Down Expand Up @@ -49,3 +51,23 @@ type RPCClient struct {
Weight int
}
}

func (*RPCClient) All() cherryFacade.NodeMap {
return nil
}

func (*RPCClient) GetType(nodeId string) (nodeType string, err error) {
return "", nil
}

func (*RPCClient) Get(nodeId string) cherryFacade.INode {
return nil
}

func (*RPCClient) Sync() {

}

func (*RPCClient) AddListener(listener cherryFacade.INodeListener) {

}
24 changes: 0 additions & 24 deletions net/cluster/rpc_server.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,4 @@
package cherryCluster

import (
"github.com/cherry-game/cherry/facade"
)

type RPCServer struct {
}

func (*RPCServer) All() cherryFacade.NodeMap {
return nil
}

func (*RPCServer) GetType(nodeId string) (nodeType string, err error) {
return "", nil
}

func (*RPCServer) Get(nodeId string) cherryFacade.INode {
return nil
}

func (*RPCServer) Sync() {

}

func (*RPCServer) AddListener(listener cherryFacade.INodeListener) {

}
2 changes: 1 addition & 1 deletion net/cluster/node.go → net/node/node.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cherryCluster
package cherryNode

import (
"fmt"
Expand Down
46 changes: 23 additions & 23 deletions net/cluster/node_map.go → net/node/node_map.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cherryCluster
package cherryNode

import (
"fmt"
Expand All @@ -25,14 +25,35 @@ func Load(config jsoniter.Any) error {
}

if mode == "nodes" {
loadNodesFromConfigFile(clusterConfig.Get("nodes"))
loadConfigNodes(clusterConfig.Get("nodes"))
} else {
panic(fmt.Sprintf("mode = %s not implemented", mode))
}

return nil
}

func loadConfigNodes(nodesJson jsoniter.Any) {
for _, nodeType := range nodesJson.Keys() {
nodesConfig[nodeType] = make(map[string]cherryFacade.INode)

typeJson := nodesJson.Get(nodeType)
for i := 0; i < typeJson.Size(); i++ {
item := typeJson.Get(i)
nodeId := item.Get("node_id").ToString()

nodesConfig[nodeType][nodeId] = &Node{
nodeId: nodeId,
nodeType: nodeType,
address: item.Get("address").ToString(),
rpcAddress: item.Get("rpc_address").ToString(),
settings: item.Get("__settings__"),
enabled: item.Get("enabled").ToBool(),
}
}
}
}

func Map() *cherryFacade.NodeMap {
return &nodesConfig
}
Expand Down Expand Up @@ -68,24 +89,3 @@ func GetType(nodeId string) (nodeType string, error error) {
}
return "", cherryError.Errorf("nodeId = %s not found. check profile config file please.", nodeId)
}

func loadNodesFromConfigFile(nodesJson jsoniter.Any) {
for _, nodeType := range nodesJson.Keys() {
nodesConfig[nodeType] = make(map[string]cherryFacade.INode)

typeJson := nodesJson.Get(nodeType)
for i := 0; i < typeJson.Size(); i++ {
item := typeJson.Get(i)
nodeId := item.Get("node_id").ToString()

nodesConfig[nodeType][nodeId] = &Node{
nodeId: nodeId,
nodeType: nodeType,
address: item.Get("address").ToString(),
rpcAddress: item.Get("rpc_address").ToString(),
settings: item.Get("__settings__"),
enabled: item.Get("enabled").ToBool(),
}
}
}
}

0 comments on commit bd60b11

Please sign in to comment.