-
Notifications
You must be signed in to change notification settings - Fork 0
/
nym.go
134 lines (119 loc) · 3.34 KB
/
nym.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
package nym
import (
"github.com/craftdome/go-nym/request"
"github.com/craftdome/go-nym/response"
"github.com/craftdome/go-nym/tags"
"github.com/pkg/errors"
"io"
)
type Request interface {
ToJSON() ([]byte, error)
//Write(writer io.WriteCloser) error
}
func NewSend(message, recipient string) Request {
return &request.Send{
Message: message,
Recipient: recipient,
}
}
func NewSendAnonymous(message, recipient string, replySurbs int) Request {
return &request.SendAnonymous{
Message: message,
Recipient: recipient,
ReplySurbs: replySurbs,
}
}
func NewReply(message, senderTag string) Request {
return &request.Reply{
Message: message,
SenderTag: senderTag,
}
}
func NewGetSelfAddress() Request {
return &request.GetSelfAddress{}
}
func NewClosedConnection() Request {
return &request.ClosedConnection{}
}
func NewGetLaneQueueLength() Request {
return &request.GetLaneQueueLength{}
}
type Response interface {
Type() tags.ResponseTag
}
func FromJSON(reader io.Reader) (Response, error) {
data, err := io.ReadAll(reader)
if err != nil {
return nil, errors.Wrap(err, "io.ReadAll")
}
tag, err := tags.GetResponseTagFromJSON(data)
if err != nil {
return nil, errors.Wrap(err, "tags.GetResponseTagFromJSON")
}
switch tag {
case tags.Error:
r := &response.Error{}
if err := r.InitFromJSON(data); err != nil {
return nil, errors.Wrapf(err, "cannot InitFromJSON data to %s", tag.Text())
}
return r, nil
case tags.Received:
r := &response.Received{}
if err := r.InitFromJSON(data); err != nil {
return nil, errors.Wrapf(err, "cannot InitFromJSON data to %s", tag.Text())
}
return r, nil
case tags.SelfAddress:
r := &response.SelfAddress{}
if err := r.InitFromJSON(data); err != nil {
return nil, errors.Wrapf(err, "cannot InitFromJSON data to %s", tag.Text())
}
return r, nil
case tags.LaneQueueLength:
r := &response.LaneQueueLength{}
if err := r.InitFromJSON(data); err != nil {
return nil, errors.Wrapf(err, "cannot InitFromJSON data to %s", tag.Text())
}
return r, nil
default:
return nil, errors.New("does not correspond to any valid request")
}
}
func FromBinary(reader io.Reader) (Response, error) {
var firstByte [1]byte
if _, err := reader.Read(firstByte[:]); err != nil {
return nil, errors.Wrap(err, "read first byte")
}
tag, err := tags.GetResponseTagFromBinary(firstByte[0])
if err != nil {
return nil, errors.Wrapf(err, "tags.GetResponseTagFromBinary(0x%X)", firstByte[0])
}
switch tag {
case tags.Error:
r := &response.Error{}
if err := r.InitFromBinary(reader); err != nil {
return nil, errors.Wrapf(err, "cannot InitFromBinary data to %s", tag.Text())
}
return r, nil
case tags.Received:
r := &response.Received{}
if err := r.InitFromBinary(reader); err != nil {
return nil, errors.Wrapf(err, "cannot InitFromBinary data to %s", tag.Text())
}
return r, nil
case tags.SelfAddress:
r := &response.SelfAddress{}
if err := r.InitFromBinary(reader); err != nil {
return nil, errors.Wrapf(err, "cannot InitFromBinary data to %s", tag.Text())
}
return r, nil
case tags.LaneQueueLength:
r := &response.LaneQueueLength{}
if err := r.InitFromBinary(reader); err != nil {
return nil, errors.Wrapf(err, "cannot InitFromBinary data to %s", tag.Text())
}
return r, nil
default:
return nil, errors.New("does not correspond to any valid request")
}
}