lbcdblocknotify: support multiple stratum server

This commit is contained in:
peter 2022-10-18 16:56:43 +08:00
parent f513fca6a7
commit 4d9a2f5cc1

View file

@ -2,6 +2,7 @@ package main
import ( import (
"flag" "flag"
"fmt"
"log" "log"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
@ -10,56 +11,73 @@ import (
"github.com/lbryio/lbcutil" "github.com/lbryio/lbcutil"
) )
type stratumFlag []string
var ( var (
lbcdHomeDir = lbcutil.AppDataDir("lbcd", false) lbcdHomeDir = lbcutil.AppDataDir("lbcd", false)
defaultCert = filepath.Join(lbcdHomeDir, "rpc.cert") defaultCert = filepath.Join(lbcdHomeDir, "rpc.cert")
stratumList stratumFlag
) )
var ( var (
coinid = flag.String("coinid", "1425", "Coin ID") coinid = flag.String("coinid", "1425", "Coin ID")
stratumServer = flag.String("stratum", "", "Stratum server") stratumPass = flag.String("stratumpass", "", "Stratum server password")
stratumPass = flag.String("stratumpass", "", "Stratum server password") rpcserver = flag.String("rpcserver", "localhost:9245", "LBCD RPC server")
rpcserver = flag.String("rpcserver", "localhost:9245", "LBCD RPC server") rpcuser = flag.String("rpcuser", "rpcuser", "LBCD RPC username")
rpcuser = flag.String("rpcuser", "rpcuser", "LBCD RPC username") rpcpass = flag.String("rpcpass", "rpcpass", "LBCD RPC password")
rpcpass = flag.String("rpcpass", "rpcpass", "LBCD RPC password") rpccert = flag.String("rpccert", defaultCert, "LBCD RPC certificate")
rpccert = flag.String("rpccert", defaultCert, "LBCD RPC certificate") notls = flag.Bool("notls", false, "Connect to LBCD with TLS disabled")
notls = flag.Bool("notls", false, "Connect to LBCD with TLS disabled") run = flag.String("run", "", "Run custom shell command")
run = flag.String("run", "", "Run custom shell command") quiet = flag.Bool("quiet", false, "Do not print logs")
quiet = flag.Bool("quiet", false, "Do not print logs")
) )
func main() { func main() {
flag.Var(&stratumList, "stratum", "--stratum=stratum1 --stratum=stratum2")
flag.Parse() flag.Parse()
for _, stratum := range stratumList {
go func(stratum string) {
// Setup notification handler
b := newBridge(stratum, *stratumPass, *coinid)
// Setup notification handler if len(*run) > 0 {
b := newBridge(*stratumServer, *stratumPass, *coinid) // Check if ccommand exists.
strs := strings.Split(*run, " ")
cmd := strs[0]
_, err := exec.LookPath(cmd)
if err != nil {
log.Fatalf("ERROR: %s not found: %s", cmd, err)
}
b.customCmd = *run
}
if len(*run) > 0 { // Start the eventt handler.
// Check if ccommand exists. go b.start()
strs := strings.Split(*run, " ")
cmd := strs[0] // Adaptater receives lbcd notifications, and emit events.
_, err := exec.LookPath(cmd) adpt := adapter{b}
if err != nil {
log.Fatalf("ERROR: %s not found: %s", cmd, err) client := newLbcdClient(*rpcserver, *rpcuser, *rpcpass, *notls, adpt)
}
b.customCmd = *run go func() {
err := <-b.errorc
log.Fatalf("ERROR: %s", err)
client.Shutdown()
}()
// Wait until the client either shuts down gracefully (or the user
// terminates the process with Ctrl+C).
client.WaitForShutdown()
}(stratum)
} }
// Start the eventt handler. quit := make(chan bool)
go b.start() <-quit
}
// Adaptater receives lbcd notifications, and emit events.
adpt := adapter{b} func (f *stratumFlag) String() string {
return fmt.Sprintf("%v", []string(*f))
client := newLbcdClient(*rpcserver, *rpcuser, *rpcpass, *notls, adpt) }
go func() { func (f *stratumFlag) Set(value string) error {
err := <-b.errorc *f = append(*f, value)
log.Fatalf("ERROR: %s", err) return nil
client.Shutdown()
}()
// Wait until the client either shuts down gracefully (or the user
// terminates the process with Ctrl+C).
client.WaitForShutdown()
} }