add constructor for core server struct

This commit is contained in:
Olaoluwa Osuntokun 2016-01-16 19:07:44 -08:00
parent 7ecbb2debc
commit 1f1b82fe3f
2 changed files with 54 additions and 15 deletions

@ -24,11 +24,15 @@ var (
) )
// rpcServer... // rpcServer...
type rpcServer struct { // doesn't count as globals I think type rpcServer struct {
lnwallet *lnwallet.LightningWallet // interface to the bitcoin network started int32 // To be used atomically.
CnMap map[[16]byte]net.Conn //interface to the lightning network shutdown int32 // To be used atomically.
OmniChan chan []byte // channel for all incoming messages from LN nodes.
// can split the OmniChan up if that is helpful. So far 1 seems OK. server *server
wg sync.WaitGroup
quit chan struct{}
} }
type LNAdr struct { type LNAdr struct {
@ -94,10 +98,8 @@ func (l *LNAdr) ParseFromString(s string) error {
var _ lnrpc.LightningServer = (*rpcServer)(nil) var _ lnrpc.LightningServer = (*rpcServer)(nil)
// newRpcServer... // newRpcServer...
func newRpcServer(wallet *lnwallet.LightningWallet) *rpcServer { func newRpcServer(s *server) *rpcServer {
return &rpcServer{wallet, return &rpcServer{server: s, quit: make(chan struct{}, 1)}
make(map[[16]byte]net.Conn), // initialize with empty CnMap
make(chan []byte)} // init OmniChan (size 1 ok...?)
} }
// Stop... // Stop...

@ -1,39 +1,76 @@
package main package main
import ( import (
"encoding/hex"
"fmt" "fmt"
"net" "net"
"sync" "sync"
"sync/atomic" "sync/atomic"
"github.com/btcsuite/btcd/btcec"
"github.com/lightningnetwork/lnd/lndc"
"github.com/lightningnetwork/lnd/lnwallet" "github.com/lightningnetwork/lnd/lnwallet"
"github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcwallet/waddrmgr"
"github.com/btcsuite/btcwallet/walletdb" "github.com/btcsuite/btcwallet/walletdb"
) )
// server... // server...
type server struct { type server struct {
listeners []net.Listener
peers map[int32]*peer
started int32 // atomic started int32 // atomic
shutdown int32 // atomic shutdown int32 // atomic
bitcoinNet *chaincfg.Params longTermPriv *btcec.PrivateKey
bitcoinNet *chaincfg.Params
listeners []net.Listener
peers map[int32]*peer
rpcServer *rpcServer rpcServer *rpcServer
lnwallet *lnwallet.LightningWallet lnwallet *lnwallet.LightningWallet
db walletdb.DB
db walletdb.DB
newPeers chan *peer newPeers chan *peer
donePeers chan *peer donePeers chan *peer
queries chan interface{}
wg sync.WaitGroup wg sync.WaitGroup
quit chan struct{} quit chan struct{}
} }
// newServer...
func newServer(listenAddrs []string, bitcoinNet *chaincfg.Params,
wallet *lnwallet.LightningWallet) (*server, error) {
privKey, err := getIdentityPrivKey(wallet)
if err != nil {
return nil, err
}
listeners := make([]net.Listener, len(listenAddrs))
for i, addr := range listenAddrs {
listeners[i], err = lndc.NewListener(privKey, addr)
if err != nil {
return nil, err
}
}
s := &server{
longTermPriv: privKey,
listeners: listeners,
peers: make(map[int32]*peer),
newPeers: make(chan *peer, 100),
donePeers: make(chan *peer, 100),
lnwallet: wallet,
queries: make(chan interface{}),
quit: make(chan struct{}),
}
s.rpcServer = newRpcServer(s)
return s, nil
}
// addPeer... // addPeer...
func (s *server) addPeer(p *peer) { func (s *server) addPeer(p *peer) {
} }