Skip to content

Commit

Permalink
modify core.Start param.
Browse files Browse the repository at this point in the history
  • Loading branch information
sydnash committed May 12, 2017
1 parent 3d603b3 commit f0927c3
Show file tree
Hide file tree
Showing 14 changed files with 89 additions and 33 deletions.
2 changes: 1 addition & 1 deletion core/callhelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (c *CallHelper) getIsAutoReply(cmd CmdType) bool {
func (c *CallHelper) findCallbackDesc(cmd CmdType) *callbackDesc {
cb, ok := c.funcMap[cmd]
if !ok {
if cb, ok := c.funcMap[Cmd_Default]; ok {
if cb, ok = c.funcMap[Cmd_Default]; ok {
log.Info("func: <%v>:%d is not found in {%v}, use default cmd handler.", cmd, len(cmd), c.hostServiceName)
} else {
log.Fatal("func: <%v>:%d is not found in {%v}", cmd, len(cmd), c.hostServiceName)
Expand Down
22 changes: 14 additions & 8 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,26 @@ import (
"reflect"
)

type ModuleParam struct {
N string
M Module
L int
}

// StartService starts the given modules with specific names
//`service' will call module's OnInit after registration
// and registers name to master if the name(of service) is a global name (starting without a dot)
// and starts msg loop in an another goroutine
func StartService(name string, m Module) ServiceID {
s := newService(name)
s.m = m
func StartService(m *ModuleParam) ServiceID {
s := newService(m.N, m.L)
s.m = m.M
id := registerService(s)
m.setService(s)
d := m.getDuration()
if !checkIsLocalName(name) {
globalName(id, name)
m.M.setService(s)
d := m.M.getDuration()
if !checkIsLocalName(m.N) {
globalName(id, m.N)
}
s.m.OnModuleStartup(id, name)
s.m.OnModuleStartup(id, m.N)
if d > 0 {
s.runWithLoop(d)
} else {
Expand Down
12 changes: 10 additions & 2 deletions core/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,16 @@ func (g *Game) OnInit() {

func TestModule(t *testing.T) {
log.Init(conf.LogFilePath, conf.LogFileLevel, conf.LogShellLevel, conf.LogMaxLine, conf.LogBufferSize)
id1 := core.StartService("g1", &Game{Skeleton: core.NewSkeleton(0)})
core.StartService("g2", &Game{Skeleton: core.NewSkeleton(1000), Dst: id1})
id1 := core.StartService(&core.ModuleParam{
N: "g1",
M: &Game{Skeleton: core.NewSkeleton(0)},
L: 0,
})
core.StartService(&core.ModuleParam{
N: "g2",
M: &Game{Skeleton: core.NewSkeleton(1000), Dst: id1},
L: 0,
})

ch := make(chan int)
go func() {
Expand Down
7 changes: 5 additions & 2 deletions core/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,12 @@ var (
ServiceCallTimeout = errors.New("call time out")
)

func newService(name string) *service {
func newService(name string, len int) *service {
s := &service{name: name}
s.msgChan = make(chan *Message, 1024)
if len <= 1024 {
len = 1024
}
s.msgChan = make(chan *Message, len)
s.requestId = 0
s.requestMap = make(map[uint64]requestCB)
s.callChanMap = make(map[uint64]chan []interface{})
Expand Down
13 changes: 4 additions & 9 deletions lotou.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ import (
"time"
)

type ModuleParam struct {
N string
M core.Module
}

type CloseFunc func()

//Start start lotou with given modules which in is in data.
Expand All @@ -30,7 +25,7 @@ type CloseFunc func()
//capture system's SIGKILL SIGTERM signal
//and wait until all service are closed.
//f will be called when SIGKILL or SIGTERM is received.
func Start(f CloseFunc, data ...*ModuleParam) {
func Start(f CloseFunc, data ...*core.ModuleParam) {
rand.Seed(time.Now().UnixNano())
logger := log.Init(conf.LogFilePath, conf.LogFileLevel, conf.LogShellLevel, conf.LogMaxLine, conf.LogBufferSize)
logger.SetColored(conf.LogHasColor)
Expand All @@ -48,7 +43,7 @@ func Start(f CloseFunc, data ...*ModuleParam) {
}

for _, m := range data {
core.StartService(m.N, m.M)
core.StartService(m)
}

/*err := http.ListenAndServe(":10000", nil)
Expand All @@ -74,7 +69,7 @@ func Start(f CloseFunc, data ...*ModuleParam) {
}

//RawStart start lotou, with no wait
func RawStart(data ...*ModuleParam) {
func RawStart(data ...*core.ModuleParam) {
log.Init(conf.LogFilePath, conf.LogFileLevel, conf.LogShellLevel, conf.LogMaxLine, conf.LogBufferSize)

core.InitNode(conf.CoreIsStandalone, conf.CoreIsMaster)
Expand All @@ -89,6 +84,6 @@ func RawStart(data ...*ModuleParam) {
}

for _, m := range data {
core.StartService(m.N, m.M)
core.StartService(m)
}
}
6 changes: 5 additions & 1 deletion master_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,9 @@ func TestMaster(t *testing.T) {
conf.CoreIsStandalone = false
conf.CoreIsMaster = true
game := &Game{core.NewSkeleton(0), 0}
lotou.Start(nil, &lotou.ModuleParam{"game1", game})
lotou.Start(nil, &core.ModuleParam{
N: "game1",
M: game,
L: 0,
})
}
12 changes: 10 additions & 2 deletions network/tcp/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,20 @@ func TestClient(t *testing.T) {

for i := 0; i < 1; i++ {
c := &C{Skeleton: core.NewSkeleton(1000)}
core.StartService(".client", c)
core.StartService(&core.ModuleParam{
N: ".client",
M: c,
L: 0,
})
c.encoder = binary.NewEncoder()
c.decoder = binary.NewDecoder()

client := tcp.NewClient("127.0.0.1", "3333", c.Id)
c.client = core.StartService(".cc", client)
c.client = core.StartService(&core.ModuleParam{
N: ".cc",
M: client,
L: 0,
})
}

for {
Expand Down
6 changes: 5 additions & 1 deletion network/tcp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ func (self *Server) Listen() error {
break
}
a := NewAgent(tcpCon, self.hostService)
core.StartService("", a)
core.StartService(&core.ModuleParam{
N: "",
M: a,
L: 0,
})
}
}()

Expand Down
6 changes: 5 additions & 1 deletion network/tcp/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ func TestServer(t *testing.T) {
log.Init("test", log.FATAL_LEVEL, log.DEBUG_LEVEL, 10000, 1000)
m := &M{Skeleton: core.NewSkeleton(0)}
m.decoder = binary.NewDecoder()
core.StartService(".m", m)
core.StartService(&core.ModuleParam{
N: ".m",
M: m,
L: 0,
})

s := tcp.NewServer("", "3333", m.Id)
s.Listen()
Expand Down
6 changes: 5 additions & 1 deletion slave_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,9 @@ func TestSlave(t *testing.T) {
conf.CoreIsMaster = false

game := &Game{core.NewSkeleton(1000), 0}
lotou.Start(nil, &lotou.ModuleParam{"game2", game})
lotou.Start(nil, &core.ModuleParam{
N: "game2",
M: game,
L: 0,
})
}
6 changes: 5 additions & 1 deletion topology/master.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ func StartMaster(ip, port string) {
m := &master{Skeleton: core.NewSkeleton(0)}
m.nodesMap = make(map[uint64]core.ServiceID)
m.globalNameMap = make(map[string]core.ServiceID)
core.StartService(".router", m)
core.StartService(&core.ModuleParam{
N: ".router",
M: m,
L: 0,
})

if !conf.CoreIsStandalone {
m.tcpServer = tcp.NewServer(ip, port, m.Id)
Expand Down
6 changes: 5 additions & 1 deletion topology/master_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ func TestMaster(t *testing.T) {
core.RegisterNode()

game := &Game{core.NewSkeleton(0)}
id := core.StartService("game1", game)
id := core.StartService(&core.ModuleParam{
N: "game1",
M: game,
L: 0,
})
log.Info("game1's id :%v", id)

log.Info("test")
Expand Down
12 changes: 10 additions & 2 deletions topology/slave.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,17 @@ type slave struct {

func StartSlave(ip, port string) {
m := &slave{Skeleton: core.NewSkeleton(0)}
core.StartService(".router", m)
core.StartService(&core.ModuleParam{
N: ".router",
M: m,
L: 0,
})
c := tcp.NewClient(ip, port, m.Id)
m.client = core.StartService("", c)
m.client = core.StartService(&core.ModuleParam{
N: "",
M: c,
L: 0,
})
}

func (s *slave) OnNormalMSG(msg *core.Message) {
Expand Down
6 changes: 5 additions & 1 deletion topology/slave_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ func TestSlavea(t *testing.T) {
core.RegisterNode()
log.Info("start create service")
game := &Game{core.NewSkeleton(1000)}
core.StartService("game2", game)
core.StartService(&core.ModuleParam{
N: "game2",
M: game,
L: 0,
})
log.Info("game2's id: %v", game.Id)

var err error
Expand Down

0 comments on commit f0927c3

Please sign in to comment.