Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
lookfirst committed Feb 7, 2018
0 parents commit dd3c74d
Show file tree
Hide file tree
Showing 10 changed files with 702 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
*.iml
vendor
31 changes: 31 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
language: go
go:
- 1.x
env:
- DEP_VERSION="v0.4.1"
before_install:
- curl -L -s https://github.com/golang/dep/releases/download/${DEP_VERSION}/dep-linux-amd64 -o $GOPATH/bin/dep
- chmod +x $GOPATH/bin/dep
install:
- dep ensure
stages:
- test
- name: deploy
if: branch = master
jobs:
include:
- stage: test
script: make test
- stage: deploy
script: make arm
before_deploy:
- git config --local user.name "lookfirst"
- git config --local user.email "latchkey@gmail.com"
- git tag "$(date '+%Y%m%d-%H%M%S')-$(git log --format=%h -1)-arm"
deploy:
provider: releases
api_key:
secure: YefyKiC0Jl5lMGgQugfwnfJOC+O+sn5RPwEAp47gtgsQE1PIlkdc0eLEJFgNLU0YNuDD5asJijW12XpY6j3ff6FMtAhggxvJSZJRwSZkcbZLW+3sSU1hqJme5Qg/68zDZxaMT0H6cv+i9Nj72y+U4RiBO9KCI02baNn4oq6nDLEiafNQNY3+2sl4eHwsN/pELgYrZYJtBDyrQDyVS7QuVAg7z4DpcQBSDVzMmAfvsbIU10JGlvVceXLqhGRKQXlPWnlz8Ss7yveD922ZBP4Oq6isRqIhkJ+UIsA0rbDLIWecplT4LTy9isALalurhlALuLxSlm95N4QBnftm7JwKVh0932wQKRW2HIHyvwd08TK+8chlseb+ggYY3R/eioNCsidfqRZ3508VQKIso0fOkS8t0yPrWwXzfFKKSCOs3UAyoxQs6sDVfD20Wp6h+tz1WLQ9OhfgZW09XdYcBKtlXIEprOhZdGKEJuHSAMSqFmWMSXLtKYSuC0ltvWJUWk+OQsWhlkdANNdb+wsrrRiE0p457jg4aG7L56rlp1soad/qBoB/HGuolT+xEfL8QZhb3rGaQ5EV4hNO/EyO93PLlADfdW/563Al4GZt4uiW2TDdYjCnRMLKtjywzoaOzceLipvOD1tWFeEqFhViJR6osRkeCwH3yeGnd6OGbXBi2vk=
skip_cleanup: true
on:
repo: blockassets/cgminer_client
15 changes: 15 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Gopkg.toml example
#
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"
#
# [prune]
# non-go = false
# go-tests = true
# unused-packages = true


[[constraint]]
name = "github.com/json-iterator/go"
version = "1.0.5"

[prune]
go-tests = true
unused-packages = true
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 Jon Stevens

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
15 changes: 15 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
DATE=$(shell date -u '+%Y-%m-%d %H:%M:%S')
COMMIT=$(shell git log --format=%h -1)
VERSION=client.version=${TRAVIS_BUILD_NUMBER} ${COMMIT} ${DATE}
COMPILE_FLAGS=-ldflags="-X '${VERSION}'"

build:
@go build ${COMPILE_FLAGS}

test:
@go test .

dep:
@dep ensure

all: clean test build
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[![Build Status](https://travis-ci.org/blockassets/cgminer_client.svg?branch=master)](https://travis-ci.org/blockassets/cgminer_client)

# cgminer golang client

Just enough of the client to get some work done. PR's welcome.

Thanks to [HyperBitShop.io](https://hyperbitshop.io) for sponsoring this project.

### Usage:

``
client := cgminer_client.New("10.0.0.1", 4028, 5)
``

### Build

Install [dep](https://github.com/golang/dep) and the dependencies...

`make dep`
`make`
240 changes: 240 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
package cgminer_client

import (
"bufio"
"errors"
"fmt"
"net"
"strings"
"time"

"github.com/json-iterator/go"
)

var (
// Makefile build
version = ""
)

// Why isn't this part of the stdlib?!
// Oh right: https://github.com/json-iterator/go/issues/231
var json = jsoniter.ConfigDefault

type Client 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{}
miner.server = fmt.Sprintf("%s:%d", hostname, port)
miner.timeout = time.Second * timeout
return miner
}

func (miner *Client) runCommand(command, argument string) (string, error) {
conn, err := net.DialTimeout("tcp", miner.server, miner.timeout)
if err != nil {
return "", err
}
defer conn.Close()

type commandRequest struct {
Command string `json:"command"`
Parameter string `json:"parameter,omitempty"`
}

request := &commandRequest{
Command: command,
}

if argument != "" {
request.Parameter = argument
}

requestBody, err := json.Marshal(request)
if err != nil {
return "", err
}

fmt.Fprintf(conn, "%s", requestBody)
result, err := bufio.NewReader(conn).ReadString('\x00')
if err != nil {
return "", err
}
return strings.TrimRight(result, "\x00"), nil
}

// Devs returns basic information on the miner.
func (miner *Client) Devs() (*[]Dev, error) {
response, err := miner.runCommand("devs", "")
if err != nil {
return nil, err
}

result, err := processDevs(response)
return &result.Devs, err
}

/* Makes testing easier */
func processDevs(response string) (*DevsResponse, error) {
devsResponse := &DevsResponse{}
err := json.Unmarshal([]byte(response), devsResponse)
if err != nil {
return nil, err
}

return devsResponse, err
}

func (miner *Client) ChipStat() (*[]ChipStat, error) {
response, err := miner.runCommand("chipstat", "")
if err != nil {
return nil, err
}

result, err := processChipStat(response)
return &result.ChipStats, err
}

/* Makes testing easier */
func processChipStat(response string) (*ChipStatResponse, error) {
chipStatResponse := &ChipStatResponse{}
err := json.Unmarshal([]byte(response), chipStatResponse)
if err != nil {
return nil, err
}

chipStatResponse.ChipStats = make([]ChipStat, len(chipStatResponse.Data))
// All the json contents now lives in the generic []Data map.
for i, data := range chipStatResponse.Data {

// There is no good way of mapping json -> struct when there is an
// unknown set of properties on an object. In this, case it is all
// the 1_accept properties. Map the known properties by hand, delete
// them and then map all the accept properties.
chipStat := chipStatResponse.ChipStats[i]
chipStat.ASC = int(data["ASC"].(float64))
chipStat.Name = data["Name"].(string)
chipStat.ID = int(data["ID"].(float64))

delete(data, "ASC")
delete(data, "Name")
delete(data, "ID")

chipStat.Accept = make(map[string]int64)
for key, val := range data {
chipStat.Accept[key] = int64(val.(float64))
}
}

return chipStatResponse, err
}

// Summary returns basic information on the miner.
func (miner *Client) Summary() (*Summary, error) {
response, err := miner.runCommand("summary", "")
if err != nil {
return nil, err
}

result, err := processSummary(response)
return &result.Summary[0], err
}

/* Makes testing easier */
func processSummary(response string) (*SummaryResponse, error) {
summaryResponse := &SummaryResponse{}
err := json.Unmarshal([]byte(response), summaryResponse)
if err != nil {
return nil, err
}

if len(summaryResponse.Summary) != 1 {
return nil, errors.New("Received multiple Summary objects")
}

return summaryResponse, err
}

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

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

//// Pools returns a slice of Pool structs, one per pool.
//func (miner *Client) Pools() ([]Pool, error) {
// result, err := miner.runCommand("pools", "")
// if err != nil {
// return nil, err
// }
//
// var poolsResponse poolsResponse
// err = json.Unmarshal([]byte(result), &poolsResponse)
// if err != nil {
// return nil, err
// }
//
// var pools = poolsResponse.Pools
// return pools, nil
//}

//// AddPool adds the given URL/username/password combination to the miner's
//// pool list.
//func (miner *Client) AddPool(url, username, password string) error {
// // TODO: Don't allow adding a pool that's already in the pool list
// // TODO: Escape commas in the URL, username, and password
// parameter := fmt.Sprintf("%s,%s,%s", url, username, password)
// result, err := miner.runCommand("addpool", parameter)
// if err != nil {
// return err
// }
//
// var addPoolResponse addPoolResponse
// err = json.Unmarshal([]byte(result), &addPoolResponse)
// if err != nil {
// // If there an error here, it's possible that the pool was actually added
// return err
// }
//
// status := addPoolResponse.Status[0]
//
// if status.Status != "S" {
// return errors.New(fmt.Sprintf("%d: %s", status.Code, status.Description))
// }
//
// return nil
//}
//
//func (miner *Client) Enable(pool *Pool) error {
// parameter := fmt.Sprintf("%d", pool.Pool)
// _, err := miner.runCommand("enablepool", parameter)
// return err
//}
//
//func (miner *Client) Disable(pool *Pool) error {
// parameter := fmt.Sprintf("%d", pool.Pool)
// _, err := miner.runCommand("disablepool", parameter)
// return err
//}
//
//func (miner *Client) Delete(pool *Pool) error {
// parameter := fmt.Sprintf("%d", pool.Pool)
// _, err := miner.runCommand("removepool", parameter)
// return err
//}
//
//func (miner *Client) SwitchPool(pool *Pool) error {
// parameter := fmt.Sprintf("%d", pool.Pool)
// _, err := miner.runCommand("switchpool", parameter)
// return err
//}
//
Loading

0 comments on commit dd3c74d

Please sign in to comment.