-
Notifications
You must be signed in to change notification settings - Fork 13
/
submission.go
165 lines (137 loc) · 3.48 KB
/
submission.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright (c) 2014-2017 Bitmark Inc.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package proof
import (
"encoding/json"
"github.com/bitmark-inc/bitmarkd/fault"
"github.com/bitmark-inc/bitmarkd/util"
"github.com/bitmark-inc/bitmarkd/zmqutil"
"github.com/bitmark-inc/logger"
zmq "github.com/pebbe/zmq4"
)
const (
submissionZapDomain = "submit"
submissionSignal = "inproc://bitmark-submission-signal"
)
type submission struct {
log *logger.L
push *zmq.Socket
pull *zmq.Socket
socket4 *zmq.Socket
socket6 *zmq.Socket
}
// initialise the submission
func (sub *submission) initialise(configuration *Configuration) error {
log := logger.New("submission")
if nil == log {
return fault.ErrInvalidLoggerChannel
}
sub.log = log
log.Info("initialising…")
// read the keys
privateKey, err := zmqutil.ReadPrivateKeyFile(configuration.PrivateKey)
if nil != err {
return err
}
publicKey, err := zmqutil.ReadPublicKeyFile(configuration.PublicKey)
if nil != err {
return err
}
log.Tracef("server public: %x", publicKey)
log.Tracef("server private: %x", privateKey)
// signalling channel
sub.push, sub.pull, err = zmqutil.NewSignalPair(submissionSignal)
if nil != err {
return err
}
// create connections
c, err := util.NewConnections(configuration.Submit)
// allocate IPv4 and IPv6 sockets
sub.socket4, sub.socket6, err = zmqutil.NewBind(log, zmq.REP, submissionZapDomain, privateKey, publicKey, c)
if nil != err {
log.Errorf("bind error: %v", err)
return err
}
return nil
}
// wait for new blocks or new payment items
// to ensure the queue integrity as heap is not thread-safe
func (sub *submission) Run(args interface{}, shutdown <-chan struct{}) {
log := sub.log
log.Info("starting…")
go func() {
poller := zmq.NewPoller()
if nil != sub.socket4 {
poller.Add(sub.socket4, zmq.POLLIN)
}
if nil != sub.socket6 {
poller.Add(sub.socket6, zmq.POLLIN)
}
poller.Add(sub.pull, zmq.POLLIN)
loop:
for {
log.Info("waiting…")
sockets, _ := poller.Poll(-1)
for _, socket := range sockets {
switch s := socket.Socket; s {
case sub.pull:
s.Recv(0)
break loop
default:
sub.process(s)
}
}
}
sub.pull.Close()
if nil != sub.socket4 {
sub.socket4.Close()
}
if nil != sub.socket6 {
sub.socket6.Close()
}
}()
// wait for shutdown
<-shutdown
sub.push.SendMessage("stop")
sub.push.Close()
}
// process the request and return response to prooferd
func (sub *submission) process(socket *zmq.Socket) {
log := sub.log
data, err := socket.RecvMessage(0)
if nil != err {
log.Errorf("JSON encode error: %v", err)
return
}
log.Infof("received message: %q", data)
var request SubmittedItem
err = json.Unmarshal([]byte(data[0]), &request)
if nil != err {
log.Errorf("JSON decode error: %v", err)
}
log.Infof("received message: %v", request)
ok := matchToJobQueue(&request)
log.Infof("maches: %v", ok)
response := struct {
Job string `json:"job"`
OK bool `json:"ok"`
}{
Job: request.Job,
OK: ok,
}
result, err := json.Marshal(response)
if nil != err {
log.Errorf("JSON encode error: %v", err)
return
}
log.Infof("json to send: %s", result)
// if _, err := socket.Send(to, zmq.SNDMORE|zmq.DONTWAIT); nil != err {
// return err
// }
// if _, err := socket.Send(command, zmq.SNDMORE|zmq.DONTWAIT); nil != err {
// return err
// }
_, err = socket.SendBytes(result, 0|zmq.DONTWAIT)
logger.PanicIfError("Submission", err)
}