Files
gocommon/wshandler/api_handler_peer.go

127 lines
3.3 KiB
Go
Raw Normal View History

2023-12-18 22:21:40 +09:00
package wshandler
import (
"encoding/json"
2023-12-19 17:04:14 +09:00
"fmt"
2023-12-18 23:22:49 +09:00
"io"
2023-12-18 22:21:40 +09:00
"reflect"
"strings"
"go.mongodb.org/mongo-driver/bson/primitive"
"repositories.action2quare.com/ayo/gocommon/logger"
)
2023-12-20 10:02:49 +09:00
type PeerInterface interface {
ClientDisconnected(string)
}
type peerApiFuncType[T PeerInterface] func(T, io.Reader) (any, error)
2023-12-18 22:21:40 +09:00
2023-12-20 10:02:49 +09:00
type WebsocketPeerApiHandler[T PeerInterface] struct {
methods map[string]peerApiFuncType[T]
2023-12-18 22:21:40 +09:00
originalReceiverName string
}
2023-12-20 10:02:49 +09:00
func MakeWebsocketPeerApiHandler[T PeerInterface](receiverName string) WebsocketPeerApiHandler[T] {
methods := make(map[string]peerApiFuncType[T])
2023-12-18 22:21:40 +09:00
2023-12-20 10:02:49 +09:00
var archetype T
2023-12-18 22:21:40 +09:00
tp := reflect.TypeOf(archetype)
if len(receiverName) == 0 {
receiverName = tp.Elem().Name()
}
for i := 0; i < tp.NumMethod(); i++ {
method := tp.Method(i)
if method.Type.In(0) != tp {
continue
}
2023-12-20 10:02:49 +09:00
if method.Name == ClientDisconnected {
continue
}
2023-12-18 22:21:40 +09:00
2023-12-20 10:02:49 +09:00
var intypes []reflect.Type
for i := 1; i < method.Type.NumIn(); i++ {
intypes = append(intypes, method.Type.In(i))
}
var outconv func([]reflect.Value) (any, error)
if method.Type.NumOut() == 0 {
outconv = func([]reflect.Value) (any, error) { return nil, nil }
} else if method.Type.NumOut() == 1 {
if method.Type.Out(0).Implements(reflect.TypeOf((*error)(nil)).Elem()) {
outconv = func(out []reflect.Value) (any, error) {
if out[0].Interface() == nil {
return nil, nil
2023-12-18 22:21:40 +09:00
}
2023-12-20 10:02:49 +09:00
return nil, out[0].Interface().(error)
2023-12-18 22:21:40 +09:00
}
2023-12-20 10:02:49 +09:00
} else {
2023-12-18 22:21:40 +09:00
outconv = func(out []reflect.Value) (any, error) {
2023-12-20 10:02:49 +09:00
return out[0].Interface(), nil
2023-12-18 22:21:40 +09:00
}
}
2023-12-20 10:02:49 +09:00
} else if method.Type.NumOut() == 2 && method.Type.Out(1).Implements(reflect.TypeOf((*error)(nil)).Elem()) {
outconv = func(out []reflect.Value) (any, error) {
if out[1].Interface() == nil {
return out[0].Interface(), nil
2023-12-18 22:21:40 +09:00
}
2023-12-20 10:02:49 +09:00
return out[0].Interface(), out[1].Interface().(error)
}
}
2023-12-18 22:21:40 +09:00
2023-12-20 10:02:49 +09:00
methods[receiverName+"."+method.Name] = func(recv T, r io.Reader) (any, error) {
decoder := json.NewDecoder(r)
inargs := make([]any, len(intypes))
2023-12-18 22:21:40 +09:00
2023-12-20 10:02:49 +09:00
for i, intype := range intypes {
zerovalueptr := reflect.New(intype)
inargs[i] = zerovalueptr.Interface()
}
2023-12-18 22:21:40 +09:00
2023-12-20 10:02:49 +09:00
err := decoder.Decode(&inargs)
if err != nil {
return nil, err
2023-12-18 22:21:40 +09:00
}
2023-12-20 10:02:49 +09:00
reflectargs := make([]reflect.Value, 0, len(inargs)+1)
reflectargs = append(reflectargs, reflect.ValueOf(recv))
for _, p := range inargs {
reflectargs = append(reflectargs, reflect.ValueOf(p).Elem())
}
return outconv(method.Func.Call(reflectargs))
2023-12-18 22:21:40 +09:00
}
}
2023-12-20 10:02:49 +09:00
return WebsocketPeerApiHandler[T]{
2023-12-18 22:21:40 +09:00
methods: methods,
originalReceiverName: tp.Elem().Name(),
}
}
2023-12-20 10:02:49 +09:00
type WebsocketPeerApiBroker[T PeerInterface] struct {
methods map[string]peerApiFuncType[T]
CreatePeer func(primitive.ObjectID) T
2023-12-18 22:21:40 +09:00
}
2023-12-20 10:02:49 +09:00
func (hc *WebsocketPeerApiBroker[T]) AddHandler(receiver WebsocketPeerApiHandler[T]) {
2023-12-18 22:21:40 +09:00
if hc.methods == nil {
2023-12-20 10:02:49 +09:00
hc.methods = make(map[string]peerApiFuncType[T])
2023-12-18 22:21:40 +09:00
}
for k, v := range receiver.methods {
ab := strings.Split(k, ".")
logger.Printf("ws api registered : %s.%s -> %s\n", receiver.originalReceiverName, ab[1], k)
hc.methods[k] = v
}
}
2023-12-20 10:02:49 +09:00
func (hc *WebsocketPeerApiBroker[T]) Call(recv T, funcname string, r io.Reader) (any, error) {
2023-12-18 22:21:40 +09:00
if found := hc.methods[funcname]; found != nil {
2023-12-19 17:04:14 +09:00
return found(recv, r)
2023-12-18 22:21:40 +09:00
}
2023-12-19 17:04:14 +09:00
return nil, fmt.Errorf("api is not found : %s", funcname)
2023-12-18 22:21:40 +09:00
}