Skip to content

Commit

Permalink
update asyn ChanRPC
Browse files Browse the repository at this point in the history
  • Loading branch information
name5566 committed Jul 22, 2016
1 parent c631296 commit 6cd7807
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 17 deletions.
17 changes: 7 additions & 10 deletions chanrpc/chanrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ type Server struct {
// func(args []interface{}) []interface{}
functions map[interface{}]interface{}
ChanCall chan *CallInfo
ChanAsynRet chan *RetInfo
}

type CallInfo struct {
Expand All @@ -42,16 +41,15 @@ type RetInfo struct {
}

type Client struct {
s *Server
chanSyncRet chan *RetInfo
ChanAsynRet chan *RetInfo
s *Server
chanSyncRet chan *RetInfo
chanAsynRet chan *RetInfo
}

func NewServer(l int) *Server {
s := new(Server)
s.functions = make(map[interface{}]interface{})
s.ChanCall = make(chan *CallInfo, l)
s.ChanAsynRet = make(chan *RetInfo, l)
return s
}

Expand Down Expand Up @@ -149,9 +147,7 @@ func (s *Server) Open(chanAsynRet chan *RetInfo) *Client {
c := new(Client)
c.s = s
c.chanSyncRet = make(chan *RetInfo, 1)
if chanAsynRet != nil {
c.ChanAsynRet = chanAsynRet
}
c.chanAsynRet = chanAsynRet
return c
}

Expand Down Expand Up @@ -265,12 +261,13 @@ func (c *Client) asynCall(id interface{}, args []interface{}, cb interface{}, n
err = c.call(&CallInfo{
f: f,
args: args,
chanRet: c.ChanAsynRet,
chanRet: c.chanAsynRet,
cb: cb,
}, false)
if err != nil {
return err
}

return nil
}

Expand Down Expand Up @@ -308,7 +305,7 @@ func (c *Client) AsynCall(id interface{}, _args ...interface{}) {
}
}

func Cb(ri *RetInfo) {
func ExecCb(ri *RetInfo) {
switch ri.cb.(type) {
case func(error):
ri.cb.(func(error))(ri.err)
Expand Down
10 changes: 5 additions & 5 deletions chanrpc/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ func Example() {
wg.Add(1)

// goroutine 2
chanAsynRet := make(chan *chanrpc.RetInfo, 10)
go func() {
chanAsynRet := make(chan *chanrpc.RetInfo, 10)
c := s.Open(chanAsynRet)

// sync
Expand Down Expand Up @@ -108,10 +108,10 @@ func Example() {
}
})

chanrpc.Cb(<-chanAsynRet)
chanrpc.Cb(<-chanAsynRet)
chanrpc.Cb(<-chanAsynRet)
chanrpc.Cb(<-chanAsynRet)
chanrpc.ExecCb(<-chanAsynRet)
chanrpc.ExecCb(<-chanAsynRet)
chanrpc.ExecCb(<-chanAsynRet)
chanrpc.ExecCb(<-chanAsynRet)

// go
s.Go("f0")
Expand Down
2 changes: 0 additions & 2 deletions module/skeleton.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ func (s *Skeleton) Run(closeSig chan bool) {
if err != nil {
log.Error("%v", err)
}
case ri := <-s.server.ChanAsynRet:
chanrpc.Cb(ri)
case ci := <-s.commandServer.ChanCall:
err := s.commandServer.Exec(ci)
if err != nil {
Expand Down

0 comments on commit 6cd7807

Please sign in to comment.