From 992cfd4220c1efde46eb35b05f7c4eb09ab712fb Mon Sep 17 00:00:00 2001 From: Matthias Fulz Date: Tue, 5 Nov 2019 18:21:41 +0100 Subject: [PATCH] Initial registration and call mocup --- server.go | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/server.go b/server.go index bb31a05..98770ae 100644 --- a/server.go +++ b/server.go @@ -1,6 +1,10 @@ package srpc import ( + "bytes" + "fmt" + "olznet.de/ssob" + "reflect" "sync" ) @@ -26,6 +30,13 @@ type responseHeader struct { status responseStatus } +type parseInputsFunc func(in []byte) (ret []reflect.Value) + +type service struct { + f reflect.Value + fin parseInputsFunc +} + type Server struct { serviceMap sync.Map } @@ -35,3 +46,49 @@ func NewServer() *Server { } var DefaultServer = NewServer() + +func (server *Server) RegisterName(name string, rcvr interface{}) (err error) { + ft := reflect.TypeOf(rcvr) + fv := reflect.ValueOf(rcvr) + + nIn := ft.NumIn() + //nOut := ft.NumOut() + + fs := service{} + + f := func(in []byte) (ret []reflect.Value) { + var b bytes.Buffer + if _, err = b.Write(in); err != nil { + return nil + } + decoder := ssob.NewDecoder(&b) + + ret = make([]reflect.Value, nIn) + for i := 0; i < nIn; i++ { + in := reflect.New(ft.In(i)) + if err = decoder.Decode(in.Interface()); err != nil { + fmt.Println(err) + return nil + } + ret[i] = reflect.Indirect(in) + } + + return ret + } + + fs.fin = f + fs.f = fv + server.serviceMap.Store(name, fs) + + return nil +} + +func (server *Server) CallName(name string, in []byte) { + fs, _ := server.serviceMap.Load(name) + + fmt.Printf("3. ") + fmt.Println(in) + inputs := fs.(service).fin(in) + fmt.Println(inputs) + fs.(service).f.Call(inputs) +}