deamon: be able to run the server over a unix socket

Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
This commit is contained in:
Vincenzo Palazzo 2024-03-26 18:28:12 +01:00
parent 56a391bcdf
commit cb1014f238
Signed by: vincenzopalazzo
GPG Key ID: 8B6DC2B870B80D5F
3 changed files with 30 additions and 13 deletions

View File

@ -1,6 +1,6 @@
// / Lightning Network protocol integration tests
// /
// / Author: Vincenzo Palazzo <vincenzopalazzo@member.fsf.org>
// Lightning Network protocol integration tests
//
// Author: Vincenzo Palazzo <vincenzopalazzo@member.fsf.org>
package main
import (
@ -8,6 +8,8 @@ import (
"os"
"github.com/akamensky/argparse"
"github.com/vincenzopalazzo/lnprototest-v2/server"
)
type CmdParser struct {
@ -31,8 +33,6 @@ func buildCmdParser() (*CmdParser, error) {
return nil, err
}
dataDir := parser.String("d", "data-dir", &argparse.Options{Required: false, Default: home, Help: "data directory for ocean market deamon"})
_ = parser.Int("p", "port", &argparse.Options{Required: false, Default: 9090, Help: "Default port where the liquidity provider will listen"})
_ = parser.String("a", "address", &argparse.Options{Required: false, Default: "127.0.0.1", Help: "Host where the liquidity provider will listen about new connection"})
network := parser.String("n", "network", &argparse.Options{Required: false, Default: "testnet", Help: "The Bitcoin Network"})
return &CmdParser{parser: parser, dataDir: dataDir, network: network}, nil
}
@ -46,4 +46,12 @@ func main() {
if err := parser.Parse(os.Args); err != nil {
panic(parser.Usage(err))
}
server, err := server.Make(*parser.dataDir)
if err != nil {
panic(err)
}
if err := server.Listen(); err != nil {
panic(err)
}
}

View File

@ -1,4 +1,4 @@
package main
package server
import (
"bytes"
@ -7,12 +7,12 @@ import (
"github.com/btcsuite/btcd/wire"
)
type ConnectRequest struct {
type ConnectRPC struct {
NodeId string
port uint64
}
func Connect(request *ConnectRequest, response *ConnectRequest) error {
func (_ *ConnectRPC) Call(request *ConnectRPC, response *ConnectRPC) error {
if err := lnprototestServer.Connect(request.NodeId, uint32(request.port), wire.SimNet); err != nil {
return err
}
@ -21,11 +21,11 @@ func Connect(request *ConnectRequest, response *ConnectRequest) error {
return nil
}
type SendRequest struct {
type SendRPC struct {
msg string
}
func Send(request *SendRequest, response *SendRequest) error {
func (_ *SendRPC) Call(request *SendRPC, response *SendRPC) error {
buff, err := hex.DecodeString(request.msg)
if err != nil {
return err

View File

@ -1,6 +1,7 @@
package main
package server
import (
"errors"
"fmt"
"net"
"net/rpc"
@ -23,8 +24,12 @@ func Make(datadir string) (*Server, error) {
}
func (self *Server) RegisterRPCs() error {
if err := rpc.Register(Connect); err != nil {
return nil
if err := rpc.Register(new(ConnectRPC)); err != nil {
return err
}
if err := rpc.Register(new(SendRPC)); err != nil {
return err
}
return nil
}
@ -35,6 +40,10 @@ func (self *Server) Listen() error {
}
unixPath := fmt.Sprintf("%s/lnprototest.sock", self.dataDir)
if _, err := os.Stat(unixPath); !errors.Is(err, os.ErrNotExist) {
os.Remove(unixPath)
}
listener, err := net.Listen("unix", unixPath)
if err != nil {
return err