This commit is contained in:
crusader
2017-10-31 19:27:26 +09:00
parent dc520fdb24
commit 9775e5ea4e
9 changed files with 202 additions and 10 deletions

34
client/client.go Normal file
View File

@@ -0,0 +1,34 @@
package client
import (
"git.loafle.net/commons_go/rpc"
"git.loafle.net/commons_go/server"
rpcClient "git.loafle.net/overflow/overflow_discovery/client/rpc"
)
func New(addr string, registry rpc.Registry) Client {
ch := NewClientHandler(addr, registry)
c := &client{}
c.Client = server.NewClient(ch)
c.ch = ch
return c
}
type Client interface {
server.Client
RPC() rpcClient.Client
}
type client struct {
server.Client
ch *ClientHandlers
}
func (c *client) RPC() rpcClient.Client {
return c.ch.RPCClient
}

47
client/client_handlers.go Normal file
View File

@@ -0,0 +1,47 @@
package client
import (
"io"
"git.loafle.net/commons_go/rpc"
"git.loafle.net/commons_go/server/ipc"
rpcClient "git.loafle.net/overflow/overflow_discovery/client/rpc"
)
func NewClientHandler(addr string, registry rpc.Registry) *ClientHandlers {
ch := &ClientHandlers{}
ch.Addr = addr
ch.RPCClient = rpcClient.New(registry)
return ch
}
type ClientHandlers struct {
ipc.ClientHandlers
RPCClient rpcClient.Client
}
func (ch *ClientHandlers) OnStart() {
// no op
}
func (ch *ClientHandlers) OnStop() {
// no op
}
func (ch *ClientHandlers) OnHandshake(remoteAddr string, rwc io.ReadWriteCloser) error {
return nil
}
func (ch *ClientHandlers) Handle(rwc io.ReadWriteCloser, stopChan chan struct{}) {
ch.RPCClient.Start(rwc)
select {
case <-stopChan:
rwc.Close()
return
default:
}
}

19
client/rpc/client.go Normal file
View File

@@ -0,0 +1,19 @@
package rpc
import (
"git.loafle.net/commons_go/rpc"
rpcClient "git.loafle.net/commons_go/rpc/client"
)
func New(registry rpc.Registry) Client {
ch := NewClientHandler(registry)
c := rpcClient.New(ch)
return c
}
type Client interface {
rpcClient.Client
}

View File

@@ -0,0 +1,28 @@
package rpc
import (
"git.loafle.net/commons_go/rpc"
rpcClient "git.loafle.net/commons_go/rpc/client"
"git.loafle.net/commons_go/rpc/protocol/json"
)
func NewClientHandler(registry rpc.Registry) *ClientHandlers {
ch := &ClientHandlers{}
ch.ContentType = "json"
ch.Codec = json.NewClientCodec()
ch.RPCRegistry = registry
return ch
}
type ClientHandlers struct {
rpcClient.ClientHandlers
}
func (ch *ClientHandlers) OnStart() {
// no op
}
func (ch *ClientHandlers) OnStop() {
// no op
}