1 package client
2 3 import (
4 "errors"
5 "io"
6 "net/rpc"
7 8 "github.com/p9c/p9/pkg/chainrpc/templates"
9 )
10 11 type Client struct {
12 *rpc.Client
13 }
14 15 // New creates a new client for a kopach_worker. Note that any kind of connection can be used here, other than the
16 // StdConn
17 func New(conn io.ReadWriteCloser) *Client {
18 return &Client{rpc.NewClient(conn)}
19 }
20 21 // NewJob is a delivery of a new job for the worker, this starts a miner
22 // note that since this implements net/rpc by default this is gob encoded
23 func (c *Client) NewJob(templates *templates.Message) (e error) {
24 // T.Ln("sending new templates")
25 // D.S(templates)
26 if templates == nil {
27 e = errors.New("templates is nil")
28 return
29 }
30 var reply bool
31 if e = c.Call("Worker.NewJob", templates, &reply); E.Chk(e) {
32 return
33 }
34 if reply != true {
35 e = errors.New("new templates command not acknowledged")
36 }
37 D.Ln("new job delivered to workers")
38 return
39 }
40 41 // Pause tells the worker to stop working, this is for when the controlling node
42 // is not current
43 func (c *Client) Pause() (e error) {
44 // D.Ln("sending pause")
45 var reply bool
46 e = c.Call("Worker.Pause", 1, &reply)
47 if e != nil {
48 return
49 }
50 if reply != true {
51 e = errors.New("pause command not acknowledged")
52 }
53 return
54 }
55 56 // Stop the workers
57 func (c *Client) Stop() (e error) {
58 D.Ln("stop working (exit)")
59 var reply bool
60 e = c.Call("Worker.Stop", 1, &reply)
61 if e != nil {
62 return
63 }
64 if reply != true {
65 e = errors.New("stop command not acknowledged")
66 }
67 return
68 }
69 70 // SendPass sends the multicast PSK to the workers so they can dispatch their
71 // solutions
72 func (c *Client) SendPass(pass []byte) (e error) {
73 D.Ln("sending dispatch password")
74 var reply bool
75 e = c.Call("Worker.SendPass", pass, &reply)
76 if e != nil {
77 return
78 }
79 if reply != true {
80 e = errors.New("send pass command not acknowledged")
81 }
82 return
83 }
84