Skip to content

Commit cacaebe

Browse files
authored
replace Codec with yomo-codec-golang (#47)
close #46
1 parent 847b6b2 commit cacaebe

6 files changed

Lines changed: 141 additions & 9 deletions

File tree

configs/echo.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package configs
2+
3+
import "github.com/yomorun/yomo/pkg/env"
4+
5+
var (
6+
DefaultEchoConf EchoConf
7+
)
8+
9+
func init() {
10+
DefaultEchoConf = GetEchoConf()
11+
}
12+
13+
type EchoConf struct {
14+
EchoServerAddr string
15+
}
16+
17+
const (
18+
echoServerAddr = "YOMO_ECHO_SERVER_ADDR"
19+
)
20+
21+
func GetEchoConf() EchoConf {
22+
conf := EchoConf{}
23+
conf.EchoServerAddr = env.GetString(echoServerAddr, "161.189.140.133:11520")
24+
return conf
25+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ module github.com/yomorun/yomo
33
go 1.14
44

55
require (
6-
github.com/10cella/yomo-json-codec v0.2.6
6+
github.com/yomorun/yomo-codec-golang v0.4.0
77
github.com/lucas-clemente/quic-go v0.17.1
88
)

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
144144
github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cbyO7IOYJZWg1U88JhDg3PB6klq9Hg2pA=
145145
github.com/viant/assertly v0.4.8/go.mod h1:aGifi++jvCrUaklKEKT0BU95igDNaqkvz+49uaYMPRU=
146146
github.com/viant/toolbox v0.24.0/go.mod h1:OxMCG57V0PXuIP2HNQrtJf2CjqdmbrOx5EkMILuUhzM=
147+
github.com/yomorun/yomo-codec-golang v0.1.0 h1:K0hKnpw4/SDwvsgjl17lPIdOJqsOo193XU7iIn76w+4=
148+
github.com/yomorun/yomo-codec-golang v0.1.0/go.mod h1:r/611MRnZbd31sd9wNzoDKxYy4Tnt8WHnC4iDTAW3NY=
149+
github.com/yomorun/yomo-codec-golang v0.4.0 h1:/6jQ5rhY5B6Q8qCk2zJF31WUsBkFsLHOF5dTQZyc+ZA=
150+
github.com/yomorun/yomo-codec-golang v0.4.0/go.mod h1:r/611MRnZbd31sd9wNzoDKxYy4Tnt8WHnC4iDTAW3NY=
147151
go.opencensus.io v0.18.0/go.mod h1:vKdFvxhtzZ9onBp9VKHK8z/sRpBMnKAsufL7wlDrCOA=
148152
go4.org v0.0.0-20180809161055-417644f6feb5/go.mod h1:MkTOUMDaeVYJUOUsaDXIhWPZYa1yOyC1qaOBpL57BhE=
149153
golang.org/x/build v0.0.0-20190111050920-041ab4dc3f9d/go.mod h1:OWs+y06UdEOHN4y+MfF/py+xQ/tYqIWW03b70/CG9Rw=

internal/framework/server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package framework
22

33
import (
4-
json "github.com/10cella/yomo-json-codec"
4+
"github.com/yomorun/yomo-codec-golang/pkg/codes"
55
"github.com/yomorun/yomo/pkg/plugin"
66
"github.com/yomorun/yomo/pkg/util"
77
)
88

99
func NewServer(endpoint string, p plugin.YomoObjectPlugin) {
10-
codec := json.NewCodec(p.Observed())
10+
codec := codes.NewCodec(p.Observed())
1111
util.QuicServer(endpoint, p, codec)
1212
}

pkg/util/quic.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ import (
1616
"net"
1717
"time"
1818

19-
json "github.com/10cella/yomo-json-codec"
2019
"github.com/lucas-clemente/quic-go"
2120
quicGo "github.com/lucas-clemente/quic-go"
21+
22+
"github.com/yomorun/yomo-codec-golang/pkg/codes"
2223
"github.com/yomorun/yomo/pkg/plugin"
2324
)
2425

@@ -27,7 +28,7 @@ var logger = GetLogger("yomo::quic")
2728
// YomoFrameworkStreamWriter is the stream of framework
2829
type YomoFrameworkStreamWriter struct {
2930
Name string
30-
Codec *json.Codec
31+
Codec codes.YomoCodec
3132
Plugin plugin.YomoObjectPlugin
3233
io.Writer
3334
}
@@ -145,7 +146,7 @@ func QuicClient(endpoint string) (quicGo.Stream, error) {
145146
}
146147

147148
// QuicServer create a QUIC server
148-
func QuicServer(endpoint string, plugin plugin.YomoObjectPlugin, codec *json.Codec) {
149+
func QuicServer(endpoint string, plugin plugin.YomoObjectPlugin, codec codes.YomoCodec) {
149150
// Lock to use QUIC draft-29 version
150151
conf := &quic.Config{
151152
Versions: []quicGo.VersionNumber{0xff00001d},

pkg/yomo/run.go

Lines changed: 105 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
package yomo
22

33
import (
4+
"fmt"
45
"io"
56
"log"
6-
"os"
77
"time"
88

9+
y3 "github.com/yomorun/yomo-codec-golang"
10+
"github.com/yomorun/yomo-codec-golang/pkg/codes"
11+
"github.com/yomorun/yomo-codec-golang/pkg/codes/packetstructure"
12+
13+
"github.com/yomorun/yomo-codec-golang/pkg/packetutils"
14+
15+
"github.com/yomorun/yomo/configs"
16+
917
"github.com/yomorun/yomo/pkg/pprof"
1018

1119
"github.com/yomorun/yomo/pkg/plugin"
@@ -37,15 +45,28 @@ func RunStream(plugin plugin.YomoStreamPlugin, endpoint string) {
3745

3846
// RunDev makes test plugin connect to a demo YoMo server
3947
func RunDev(plugin plugin.YomoObjectPlugin, endpoint string) {
48+
RunDevWith(plugin, endpoint, OutputPacketPrinter)
49+
}
4050

51+
type OutputFormatter int32
52+
53+
const (
54+
OutputHexString OutputFormatter = 0
55+
OutputPacketPrinter OutputFormatter = 1
56+
OutputEchoData OutputFormatter = 2
57+
OutputThermometerData OutputFormatter = 3
58+
)
59+
60+
// RunDev makes test plugin connect to a demo YoMo server. OutputFormatter: OutputHexString/OutputPacketPrinter/OutputEchoData
61+
func RunDevWith(plugin plugin.YomoObjectPlugin, endpoint string, formatter OutputFormatter) {
4162
go func() {
4263
logger.Infof("plugin service [%s] start... [%s]", plugin.Name(), endpoint)
4364

4465
// activation service
4566
framework.NewServer(endpoint, plugin)
4667
}()
4768

48-
yomoEchoClient, err := util.QuicClient("161.189.140.133:11520")
69+
yomoEchoClient, err := util.QuicClient(configs.DefaultEchoConf.EchoServerAddr)
4970
//yomoEchoClient, err := util.QuicClient("localhost:11520")
5071
if err != nil {
5172
panic(err)
@@ -57,7 +78,22 @@ func RunDev(plugin plugin.YomoObjectPlugin, endpoint string) {
5778
}
5879

5980
go io.Copy(yomoPluginClient, yomoEchoClient) // nolint
60-
go io.Copy(os.Stdout, yomoPluginClient) // nolint
81+
82+
// select formatter
83+
var w io.Writer
84+
switch formatter {
85+
case OutputHexString:
86+
w = &hexStringFormatter{}
87+
case OutputPacketPrinter:
88+
w = &packetPrinterFormatter{}
89+
case OutputEchoData:
90+
w = &echoDataFormatter{}
91+
case OutputThermometerData:
92+
w = &thermometerDataFormatter{}
93+
default:
94+
w = &packetPrinterFormatter{}
95+
}
96+
go util.CopyTo(w, yomoPluginClient) // nolint
6197

6298
for {
6399
time.Sleep(time.Second)
@@ -66,5 +102,71 @@ func RunDev(plugin plugin.YomoObjectPlugin, endpoint string) {
66102
log.Fatal(err)
67103
}
68104
}
105+
}
106+
107+
// hexStringFormatter
108+
type hexStringFormatter struct {
109+
io.Writer
110+
}
111+
112+
func (w *hexStringFormatter) Write(b []byte) (int, error) {
113+
fmt.Printf("%v:\t %s\n", time.Now().Format("2006-01-02 15:04:05"), packetutils.FormatBytes(b)) // debug:
114+
return 0, nil
115+
}
116+
117+
// packetPrinterFormatter
118+
type packetPrinterFormatter struct {
119+
io.Writer
120+
}
121+
122+
func (w *packetPrinterFormatter) Write(b []byte) (int, error) {
123+
res, _, _ := y3.DecodeNodePacket(b)
124+
fmt.Printf("%v:\t", time.Now().Format("2006-01-02 15:04:05")) // debug:
125+
packetutils.PrintNodePacket(res)
126+
fmt.Println()
127+
return 0, nil
128+
}
129+
130+
// echoDataFormatter
131+
type echoDataFormatter struct {
132+
io.Writer
133+
}
134+
135+
func (w *echoDataFormatter) Write(b []byte) (int, error) {
136+
var mold = echoData{}
137+
res, _, _ := y3.DecodeNodePacket(b)
138+
_ = packetstructure.Decode(res, &mold)
139+
fmt.Printf("%v:\t %v\n", time.Now().Format("2006-01-02 15:04:05"), mold) // debug:
140+
return 0, nil
141+
}
142+
143+
type echoData struct {
144+
Id int32 `yomo:"0x10"`
145+
Name string `yomo:"0x11"`
146+
Test test `yomo:"0x20"`
147+
}
148+
149+
type test struct {
150+
Tag []string `yomo:"0x13"`
151+
}
152+
153+
// thermometerDataFormatter
154+
type thermometerDataFormatter struct {
155+
io.Writer
156+
}
157+
158+
func (w *thermometerDataFormatter) Write(b []byte) (int, error) {
159+
var mold = []thermometerData{}
160+
161+
proto := codes.NewProtoCodec("0x20")
162+
proto.UnmarshalStruct(b, &mold)
163+
fmt.Printf("%v:\t %v\n", time.Now().Format("2006-01-02 15:04:05"), mold) // debug:
164+
return 0, nil
165+
}
69166

167+
type thermometerData struct {
168+
Id string `yomo:"0x10"`
169+
Temperature float32 `yomo:"0x11"`
170+
Humidity float32 `yomo:"0x12"`
171+
Stored bool `yomo:"0x13"`
70172
}

0 commit comments

Comments
 (0)