-
Notifications
You must be signed in to change notification settings - Fork 172
/
stream-wrappers.go
174 lines (141 loc) · 3.62 KB
/
stream-wrappers.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
166
167
168
169
170
171
172
173
174
/*
* Copyright (c) 2018. Abstrium SAS <team (at) pydio.com>
* This file is part of Pydio Cells.
*
* Pydio Cells is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio Cells is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio Cells. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com>.
*/
package views
import (
"encoding/binary"
"fmt"
"io"
"github.com/golang/protobuf/proto"
"github.com/pydio/cells/common/proto/tree"
)
type NodeWrappingStreamer struct {
*wrappingStreamer
}
func NewWrappingStreamer() *NodeWrappingStreamer {
return &NodeWrappingStreamer{newWrappingStreamer()}
}
func (n *NodeWrappingStreamer) Recv() (*tree.ListNodesResponse, error) {
msg, err := n.wrappingStreamer.Recv()
if err != nil {
return nil, err
}
return msg.GetListNodesResponse(), nil
}
type ChangesWrappingStreamer struct {
*wrappingStreamer
}
func NewChangesWrappingStreamer() *ChangesWrappingStreamer {
return &ChangesWrappingStreamer{newWrappingStreamer()}
}
func (l *ChangesWrappingStreamer) Recv() (*tree.NodeChangeEvent, error) {
msg, err := l.wrappingStreamer.Recv()
if err != nil {
return nil, err
}
return msg.GetNodeChangeEvent(), nil
}
type wrappingStreamer struct {
w *io.PipeWriter
r *io.PipeReader
closed bool
recvErr error
}
func newWrappingStreamer() *wrappingStreamer {
r, w := io.Pipe()
return &wrappingStreamer{
w: w,
r: r,
closed: false,
}
}
func (l *wrappingStreamer) Send(in interface{}) error {
msg := new(tree.WrappingStreamerResponse)
switch v := in.(type) {
case *tree.ListNodesResponse:
msg.Data = &tree.WrappingStreamerResponse_ListNodesResponse{
ListNodesResponse: v,
}
break
case *tree.NodeChangeEvent:
msg.Data = &tree.WrappingStreamerResponse_NodeChangeEvent{
NodeChangeEvent: v,
}
break
case error:
msg.Error = v.Error()
default:
return fmt.Errorf("unknown format")
}
if out, err := proto.Marshal(msg); err != nil {
return err
} else {
// First sending the message size 2 bytes
b := make([]byte, 2)
binary.BigEndian.PutUint16(b, uint16(len(out)))
if _, err := l.w.Write(b); err != nil {
return err
}
if _, err := l.w.Write(out); err != nil {
return err
}
}
return nil
}
func (l *wrappingStreamer) SendMsg(msg interface{}) error {
return l.Send(msg)
}
func (l *wrappingStreamer) SendError(err error) error {
return l.Send(err)
}
func (l *wrappingStreamer) Recv() (*tree.WrappingStreamerResponse, error) {
if l.closed {
return nil, io.EOF
}
// Getting the next message size
size := make([]byte, 2)
if _, err := l.r.Read(size); err != nil {
return nil, err
}
in := make([]byte, binary.BigEndian.Uint16(size))
if _, err := l.r.Read(in); err != nil {
return nil, err
}
resp := new(tree.WrappingStreamerResponse)
if err := proto.Unmarshal(in, resp); err != nil {
return nil, err
}
if resp.Error != "" {
return nil, fmt.Errorf(resp.Error)
}
return resp, nil
}
func (l *wrappingStreamer) RecvMsg(m interface{}) error {
resp, err := l.Recv()
if err != nil {
return err
}
m = resp
return nil
}
func (l *wrappingStreamer) Close() error {
l.closed = true
l.w.Close()
return nil
}