Skip to content

Commit

Permalink
make a client interface
Browse files Browse the repository at this point in the history
  • Loading branch information
lookfirst committed Mar 24, 2018
1 parent 06e2ded commit 74d91ee
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,29 @@ var (
version = ""
)

type Client struct {
type Client interface {
Devs() (*[]Dev, error)
Quit() error
Restart() error
Summary() (*Summary, error)
ChipStat() (*[]ChipStat, error)
}

type CgClient struct {
server string
timeout time.Duration
}

// New returns a Client pointer, which is used to communicate with a running
// cgminer instance. Note that New does not attempt to connect to the miner.
func New(hostname string, port int64, timeout time.Duration) *Client {
miner := &Client{}
func New(hostname string, port int64, timeout time.Duration) Client {
miner := &CgClient{}
miner.server = fmt.Sprintf("%s:%d", hostname, port)
miner.timeout = time.Second * timeout
return miner
}

func (miner *Client) runCommand(command, argument string) (string, error) {
func (miner *CgClient) runCommand(command, argument string) (string, error) {
conn, err := net.DialTimeout("tcp", miner.server, miner.timeout)
if err != nil {
return "", err
Expand Down Expand Up @@ -64,7 +72,7 @@ func (miner *Client) runCommand(command, argument string) (string, error) {
}

// Devs returns basic information on the miner.
func (miner *Client) Devs() (*[]Dev, error) {
func (miner *CgClient) Devs() (*[]Dev, error) {
response, err := miner.runCommand("devs", "")
if err != nil {
return nil, err
Expand All @@ -85,7 +93,7 @@ func processDevs(response string) (*DevsResponse, error) {
return devsResponse, err
}

func (miner *Client) ChipStat() (*[]ChipStat, error) {
func (miner *CgClient) ChipStat() (*[]ChipStat, error) {
response, err := miner.runCommand("chipstat", "")
if err != nil {
return nil, err
Expand Down Expand Up @@ -130,7 +138,7 @@ func processChipStat(response string) (*ChipStatResponse, error) {
}

// Summary returns basic information on the miner.
func (miner *Client) Summary() (*Summary, error) {
func (miner *CgClient) Summary() (*Summary, error) {
response, err := miner.runCommand("summary", "")
if err != nil {
return nil, err
Expand All @@ -155,13 +163,13 @@ func processSummary(response string) (*SummaryResponse, error) {
return summaryResponse, err
}

func (miner *Client) Restart() error {
func (miner *CgClient) Restart() error {
_, err := miner.runCommand("restart", "")
return err
}

//
func (miner *Client) Quit() error {
func (miner *CgClient) Quit() error {
_, err := miner.runCommand("quit", "")
return err
}
Expand Down

0 comments on commit 74d91ee

Please sign in to comment.