-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstratum.go
142 lines (114 loc) · 2.6 KB
/
stratum.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
package Stratum
import (
"encoding/json"
"errors"
)
// Stratum has three types of messages: notification, request, and response.
// Notification for methods that do not require a response.
type notification struct {
method string `json:"method"`
params []interface{} `json:"params"`
}
func Notification(m Method, params []interface{}) notification {
n, _ := EncodeMethod(m)
return notification{
method: n,
params: params,
}
}
func (n *notification) Method() Method {
m, _ := DecodeMethod(n.method)
return m
}
// request is for methods that require a response.
type request struct {
MessageID MessageID `json:"id"`
method string `json:"method"`
params []interface{} `json:"params"`
}
func Request(id MessageID, m Method, params []interface{}) request {
n, _ := EncodeMethod(m)
return request{
MessageID: id,
method: n,
params: params,
}
}
func (n *request) Method() Method {
m, _ := DecodeMethod(n.method)
return m
}
// Response is what is sent back in response to requests.
type response struct {
MessageID MessageID `json:"id"`
result interface{} `json:"result"`
Error *Error `json:"error"`
}
func Response(id MessageID, r interface{}) response {
return response{
MessageID: id,
result: r,
Error: nil,
}
}
func ErrorResponse(id MessageID, e Error) response {
return response{
MessageID: id,
result: nil,
Error: &e,
}
}
func (r *request) MarshallJSON() ([]byte, error) {
if !ValidMessageID(r.MessageID) {
return []byte{}, errors.New("Invalid id")
}
if r.method == "" {
return []byte{}, errors.New("Invalid method")
}
return json.Marshal(r)
}
func (r *request) UnmarshallJSON(j []byte) error {
err := json.Unmarshal(j, r)
if err != nil {
return err
}
if !ValidMessageID(r.MessageID) {
return errors.New("Invalid id")
}
if r.Method() == Unset {
return errors.New("Invalid method")
}
return nil
}
func (r *response) MarshallJSON() ([]byte, error) {
if !ValidMessageID(r.MessageID) {
return []byte{}, errors.New("Invalid id")
}
return json.Marshal(r)
}
func (r *response) UnmarshallJSON(j []byte) error {
err := json.Unmarshal(j, r)
if err != nil {
return err
}
if !ValidMessageID(r.MessageID) {
return errors.New("Invalid id")
}
return nil
}
func (r *notification) MarshallJSON() ([]byte, error) {
if r.method == "" {
return []byte{}, errors.New("Invalid method")
}
return json.Marshal(r)
}
func (r *notification) UnmarshallJSON(j []byte) error {
err := json.Unmarshal(j, r)
if err != nil {
return err
}
if r.Method() == Unset {
return errors.New("Invalid method")
}
return nil
}