This repository was archived by the owner on Jul 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtdaq.go
More file actions
192 lines (161 loc) · 3.7 KB
/
Copy pathtdaq.go
File metadata and controls
192 lines (161 loc) · 3.7 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Copyright 2019 The go-daq Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package tdaq is a minimal toolkit to implement a tiny data acquisition system.
package tdaq // import "github.com/go-daq/tdaq"
import (
"bytes"
"context"
"fmt"
"github.com/go-daq/tdaq/log"
)
type Context struct {
Ctx context.Context
Msg log.MsgStream
}
type Marshaler interface {
MarshalTDAQ() ([]byte, error)
}
type Unmarshaler interface {
UnmarshalTDAQ(p []byte) error
}
type Sender interface {
Send(msg []byte) error
}
type Recver interface {
Recv() ([]byte, error)
}
// Frame is the datum being exchanged between tdaq processes.
type Frame struct {
Type FrameType // type of frame (cmd,data,err,ok)
Path string // end-point path
Body []byte // frame payload
}
func (f Frame) encode() []byte {
psz := len(f.Path)
bsz := len(f.Body)
beg := 2
end := beg + psz
msg := make([]byte, 1+1+psz+bsz)
msg[0] = byte(f.Type)
msg[1] = byte(psz)
copy(msg[beg:end], []byte(f.Path))
copy(msg[end:], f.Body)
return msg
}
// FrameType describes the type of a Frame.
type FrameType byte
const (
FrameUnknown FrameType = iota
FrameCmd
FrameData
FrameMsg
FrameOK
FrameEOF
FrameErr
)
func (ft FrameType) String() string {
switch ft {
case FrameUnknown:
return "unknown-frame"
case FrameCmd:
return "cmd-frame"
case FrameData:
return "data-frame"
case FrameMsg:
return "msg-frame"
case FrameOK:
return "ok-frame"
case FrameEOF:
return "eof-frame"
case FrameErr:
return "err-frame"
default:
panic(fmt.Errorf("invalid frame-type %d", byte(ft)))
}
}
var (
eofFrame = []byte{byte(FrameEOF), 0}
)
func SendMsg(ctx context.Context, sck Sender, msg MsgFrame) error {
raw, err := msg.MarshalTDAQ()
if err != nil {
return err
}
return sendFrame(ctx, sck, FrameMsg, []byte("/log"), raw)
}
func SendFrame(ctx context.Context, sck Sender, frame Frame) error {
return sendFrame(ctx, sck, frame.Type, []byte(frame.Path), frame.Body)
}
func sendFrame(ctx context.Context, sck Sender, ftype FrameType, path, body []byte) error {
psz := len(path)
bsz := len(body)
beg := 2
end := beg + psz
msg := make([]byte, 1+1+psz+bsz)
msg[0] = byte(ftype)
msg[1] = byte(psz)
copy(msg[beg:end], []byte(path))
copy(msg[end:], body)
return sck.Send(msg)
}
func RecvFrame(ctx context.Context, sck Recver) (frame Frame, err error) {
msg, err := sck.Recv()
if err != nil {
return frame, fmt.Errorf("could not receive TDAQ frame: %w", err)
}
frame.Type = FrameType(msg[0])
psz := int(msg[1])
beg := 2
end := beg + psz
frame.Path = string(msg[beg:end])
if len(msg[end:]) > 0 {
frame.Body = msg[end:]
}
return frame, nil
}
type MsgFrame struct {
Name string
Level log.Level
Msg string
}
func (frame MsgFrame) MarshalTDAQ() ([]byte, error) {
buf := new(bytes.Buffer)
enc := NewEncoder(buf)
enc.WriteStr(frame.Name)
enc.WriteI8(int8(frame.Level))
enc.WriteStr(frame.Msg)
err := enc.Err()
return buf.Bytes(), err
}
func (frame *MsgFrame) UnmarshalTDAQ(p []byte) error {
dec := NewDecoder(bytes.NewReader(p))
frame.Name = dec.ReadStr()
frame.Level = log.Level(dec.ReadI8())
frame.Msg = dec.ReadStr()
return dec.Err()
}
type EndPoint struct {
Name string
Addr string
Type string
}
func (ep EndPoint) MarshalTDAQ() ([]byte, error) {
buf := new(bytes.Buffer)
enc := NewEncoder(buf)
enc.WriteStr(ep.Name)
enc.WriteStr(ep.Addr)
enc.WriteStr(ep.Type)
return buf.Bytes(), enc.err
}
func (ep *EndPoint) UnmarshalTDAQ(b []byte) error {
dec := NewDecoder(bytes.NewReader(b))
ep.Name = dec.ReadStr()
ep.Addr = dec.ReadStr()
ep.Type = dec.ReadStr()
return dec.err
}
var (
_ Marshaler = (*MsgFrame)(nil)
_ Unmarshaler = (*MsgFrame)(nil)
)