tie into session manager in jsonrpc peers subscribe
This commit is contained in:
parent
6c287425ef
commit
99a9d37f8c
3 changed files with 22 additions and 15 deletions
2
main.go
2
main.go
|
@ -43,7 +43,7 @@ func main() {
|
|||
return
|
||||
}
|
||||
|
||||
conn, err := grpc.Dial("localhost:"+string(args.Port),
|
||||
conn, err := grpc.Dial("localhost:"+fmt.Sprintf("%d", args.Port),
|
||||
grpc.WithTransportCredentials(insecure.NewCredentials()),
|
||||
grpc.WithBlock(),
|
||||
)
|
||||
|
|
|
@ -1,14 +1,16 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
pb "github.com/lbryio/herald.go/protobuf/go"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type PeersService struct {
|
||||
Server *Server
|
||||
// needed for subscribe/unsubscribe
|
||||
sessionMgr *sessionManager
|
||||
session *session
|
||||
}
|
||||
|
||||
type PeersSubscribeReq struct {
|
||||
|
@ -22,23 +24,16 @@ 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"
|
||||
// var port = "50001"
|
||||
|
||||
// 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)
|
||||
if t.sessionMgr == nil || t.session == nil {
|
||||
*res = nil
|
||||
return err
|
||||
return errors.New("no session, rpc not supported")
|
||||
}
|
||||
t.sessionMgr.peersSubscribe(t.session, true /*subscribe*/)
|
||||
|
||||
*res = (*PeersSubscribeResp)(&peers.Value)
|
||||
*res = nil
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -313,6 +313,18 @@ func (sm *sessionManager) broadcastTx(rawTx []byte) (*chainhash.Hash, error) {
|
|||
return nil, nil
|
||||
}
|
||||
|
||||
func (sm *sessionManager) peersSubscribe(sess *session, subscribe bool) {
|
||||
sm.sessionsMut.Lock()
|
||||
defer sm.sessionsMut.Unlock()
|
||||
if subscribe {
|
||||
sm.peerSubs[sess.id] = sess
|
||||
sess.peersSub = true
|
||||
return
|
||||
}
|
||||
delete(sm.peerSubs, sess.id)
|
||||
sess.peersSub = false
|
||||
}
|
||||
|
||||
func (sm *sessionManager) headersSubscribe(sess *session, raw bool, subscribe bool) {
|
||||
sm.sessionsMut.Lock()
|
||||
defer sm.sessionsMut.Unlock()
|
||||
|
|
Loading…
Reference in a new issue