From 0a5a7c7d33b0e450aa06bca54d85ae84b3ac25d4 Mon Sep 17 00:00:00 2001 From: Jeffrey Picard Date: Thu, 17 Nov 2022 21:52:58 +0000 Subject: [PATCH] hook jsonrpc peers subcribe into current federation --- server/jsonrpc_federation.go | 26 +++++++++++++++++++++++--- server/jsonrpc_service.go | 2 +- server/session.go | 2 +- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/server/jsonrpc_federation.go b/server/jsonrpc_federation.go index 9dcd203..9060406 100644 --- a/server/jsonrpc_federation.go +++ b/server/jsonrpc_federation.go @@ -1,10 +1,15 @@ package server import ( + "context" + + pb "github.com/lbryio/herald.go/protobuf/go" log "github.com/sirupsen/logrus" ) -type PeersService struct{} +type PeersService struct { + Server *Server +} type PeersSubscribeReq struct { Ip string `json:"ip"` @@ -12,13 +17,28 @@ type PeersSubscribeReq struct { Details []string `json:"details"` } -type PeersSubscribeResp struct{} +type PeersSubscribeResp string // Features is the json rpc endpoint for 'server.peers.subcribe'. func (t *PeersService) Subscribe(req *PeersSubscribeReq, res **PeersSubscribeResp) error { log.Println("PeersSubscribe") + ctx := context.Background() + var port = "50001" - *res = nil + // FIXME: Get the actual port from the request details + msg := &pb.ServerMessage{ + Address: req.Ip, + Port: port, + } + + peers, err := t.Server.PeerSubscribe(ctx, msg) + if err != nil { + log.Println(err) + *res = nil + return err + } + + *res = (*PeersSubscribeResp)(&peers.Value) return nil } diff --git a/server/jsonrpc_service.go b/server/jsonrpc_service.go index 9952fea..b35036f 100644 --- a/server/jsonrpc_service.go +++ b/server/jsonrpc_service.go @@ -135,7 +135,7 @@ fail1: } // Register "server.peers" handlers. - peersSvc := &PeersService{} + peersSvc := &PeersService{Server: s} err = s1.RegisterTCPService(peersSvc, "server_peers") if err != nil { log.Errorf("RegisterTCPService: %v\n", err) diff --git a/server/session.go b/server/session.go index 4b358bd..3d7c931 100644 --- a/server/session.go +++ b/server/session.go @@ -229,7 +229,7 @@ func (sm *sessionManager) addSession(conn net.Conn) *session { } // Register "server.peers" handlers. - peersSvc := &PeersService{} + peersSvc := &PeersService{Server: sm.server} err = s1.RegisterName("server.peers", peersSvc) if err != nil { log.Errorf("RegisterName: %v\n", err)