2022-04-23 00:12:30 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2022-05-08 04:23:42 +00:00
|
|
|
"io"
|
2022-05-01 18:36:28 +00:00
|
|
|
"net"
|
2022-04-23 00:12:30 +00:00
|
|
|
|
2023-01-03 21:06:38 +00:00
|
|
|
"go.arsenm.dev/drpc/muxconn"
|
2023-01-03 06:30:17 +00:00
|
|
|
"go.arsenm.dev/itd/internal/rpc"
|
2023-01-03 08:54:00 +00:00
|
|
|
"storj.io/drpc"
|
2022-04-23 00:12:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const DefaultAddr = "/tmp/itd/socket"
|
|
|
|
|
2023-01-03 17:30:04 +00:00
|
|
|
// Client is a client for ITD's socket API
|
2022-04-23 00:12:30 +00:00
|
|
|
type Client struct {
|
2023-01-03 08:54:00 +00:00
|
|
|
conn drpc.Conn
|
2023-01-03 06:30:17 +00:00
|
|
|
client rpc.DRPCITDClient
|
2022-04-23 00:12:30 +00:00
|
|
|
}
|
|
|
|
|
2023-01-03 17:30:04 +00:00
|
|
|
// New connects to the UNIX socket at the given
|
|
|
|
// path, and returns a client that communicates
|
|
|
|
// with that socket.
|
2022-04-23 00:12:30 +00:00
|
|
|
func New(sockPath string) (*Client, error) {
|
2022-05-01 18:36:28 +00:00
|
|
|
conn, err := net.Dial("unix", sockPath)
|
2022-04-23 00:12:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-01-03 08:54:00 +00:00
|
|
|
|
2023-01-03 21:06:38 +00:00
|
|
|
mconn, err := muxconn.New(conn)
|
2023-01-03 08:54:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-04-23 00:12:30 +00:00
|
|
|
|
2023-01-03 06:30:17 +00:00
|
|
|
return &Client{
|
2023-01-03 08:54:00 +00:00
|
|
|
conn: mconn,
|
|
|
|
client: rpc.NewDRPCITDClient(mconn),
|
2023-01-03 06:30:17 +00:00
|
|
|
}, nil
|
2022-04-23 00:12:30 +00:00
|
|
|
}
|
|
|
|
|
2023-01-03 17:30:04 +00:00
|
|
|
// NewFromConn returns a client that communicates
|
|
|
|
// over the given connection.
|
2023-01-03 17:24:36 +00:00
|
|
|
func NewFromConn(conn io.ReadWriteCloser) (*Client, error) {
|
2023-01-03 21:06:38 +00:00
|
|
|
mconn, err := muxconn.New(conn)
|
2023-01-03 17:24:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-01-03 06:30:17 +00:00
|
|
|
|
2022-05-08 04:23:42 +00:00
|
|
|
return &Client{
|
2023-01-03 17:24:36 +00:00
|
|
|
conn: mconn,
|
|
|
|
client: rpc.NewDRPCITDClient(mconn),
|
|
|
|
}, nil
|
2022-05-08 04:23:42 +00:00
|
|
|
}
|
|
|
|
|
2023-01-03 17:30:04 +00:00
|
|
|
// FS returns the filesystem API client
|
2023-01-03 06:30:17 +00:00
|
|
|
func (c *Client) FS() *FSClient {
|
|
|
|
return &FSClient{rpc.NewDRPCFSClient(c.conn)}
|
|
|
|
}
|
|
|
|
|
2023-01-03 17:30:04 +00:00
|
|
|
// Close closes the client connection
|
2022-04-23 00:12:30 +00:00
|
|
|
func (c *Client) Close() error {
|
2023-01-03 06:30:17 +00:00
|
|
|
return c.conn.Close()
|
2022-04-23 00:12:30 +00:00
|
|
|
}
|