Skip to content

Commit f2cccab

Browse files
committed
Removed possibility to enable/disable logging. Just subscribe to appropriate channel
1 parent 5f0dcd4 commit f2cccab

File tree

4 files changed

+74
-48
lines changed

4 files changed

+74
-48
lines changed

README.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ I tested it with cc253**1**, but it might work with cc253**X**
1111

1212
## Example
1313

14-
To use it you need to provide a reference to a serial port:
14+
To use it you need to provide a reference to an unp instance:
1515

1616
```go
1717
import (
1818
"go.bug.st/serial.v1"
19+
"github.com/davecgh/go-spew/spew"
1920
"github.com/dyrkin/unp-go"
2021
"github.com/dyrkin/znp-go"
2122
)
@@ -33,6 +34,7 @@ func main() {
3334

3435
u := unp.New(1, port)
3536
z := znp.New(u)
37+
z.Start()
3638
}
3739
```
3840

@@ -50,5 +52,35 @@ if err != nil {
5052
}
5153
```
5254

55+
To receive async commands and errors, use `AsyncInbound()` and `Errors()` channels:
56+
57+
```go
58+
go func() {
59+
for {
60+
select {
61+
case err := <-z.Errors():
62+
fmt.Printf("Error received: %s\n", err)
63+
case async := <-z.AsyncInbound():
64+
fmt.Printf("Async received: %s\n", spew.Sdump(async))
65+
}
66+
}
67+
}()
68+
```
69+
70+
To log all ingoing and outgoing unp frames, use `InFramesLog()` and `OutFramesLog()` channels:
71+
72+
```go
73+
go func() {
74+
for {
75+
select {
76+
case frame := <-z.OutFramesLog():
77+
fmt.Printf("Frame sent: %s\n", spew.Sdump(frame))
78+
case frame := <-z.InFramesLog():
79+
fmt.Printf("Frame received: %s\n", spew.Sdump(frame))
80+
}
81+
}
82+
}()
83+
```
84+
5385
See more [examples](example/example.go)
5486

example/example.go

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
package main
22

33
import (
4-
"encoding/json"
54
"fmt"
65
"log"
76
"time"
87

98
"github.com/davecgh/go-spew/spew"
10-
unp "github.com/dyrkin/unp-go"
11-
znp "github.com/dyrkin/znp-go"
12-
serial "go.bug.st/serial.v1"
9+
"github.com/dyrkin/unp-go"
10+
"github.com/dyrkin/znp-go"
11+
"go.bug.st/serial.v1"
1312
)
1413

1514
func main() {
@@ -21,38 +20,32 @@ func main() {
2120

2221
port, err := serial.Open("/dev/tty.usbmodem14101", mode)
2322
if err != nil {
24-
log.Fatal(err)
23+
log.Fatalf("Can't open port. Reason: %s", err)
2524
}
2625
port.SetRTS(true)
2726

2827
u := unp.New(1, port)
2928
z := znp.New(u)
30-
z.LogInFrames(true)
31-
z.LogOutFrames(true)
3229
z.Start()
3330

34-
printChan := make(chan string)
35-
3631
go func() {
3732
for {
3833
select {
39-
case msg := <-printChan:
40-
fmt.Print(msg)
34+
case err := <-z.Errors():
35+
fmt.Printf("Error received: %s\n", err)
36+
case async := <-z.AsyncInbound():
37+
fmt.Printf("Async received: %s\n", spew.Sdump(async))
4138
}
4239
}
4340
}()
4441

4542
go func() {
4643
for {
4744
select {
48-
case err := <-z.Errors():
49-
printChan <- fmt.Sprintf("Error: %s\n", err)
50-
case async := <-z.AsyncInbound():
51-
printChan <- fmt.Sprintf("Async: %s\n", spew.Sdump(async))
52-
case _ = <-z.OutFramesLog():
53-
// printChan <- fmt.Sprintf("Frame sent: %s\n", spew.Sdump(frame))
54-
case _ = <-z.InFramesLog():
55-
// printChan <- fmt.Sprintf("Frame received: %s\n", spew.Sdump(frame))
45+
case frame := <-z.OutFramesLog():
46+
fmt.Printf("Frame sent: %s\n", spew.Sdump(frame))
47+
case frame := <-z.InFramesLog():
48+
fmt.Printf("Frame received: %s\n", spew.Sdump(frame))
5649
}
5750
}
5851
}()
@@ -386,8 +379,3 @@ func main() {
386379
func PrintStruct(v interface{}) {
387380
spew.Dump(v)
388381
}
389-
390-
func RenderStruct(v interface{}) string {
391-
jsonBytes, _ := json.Marshal(v)
392-
return string(jsonBytes)
393-
}

processor.go

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,10 @@ func startProcessors(znp *Znp) {
8989
case unp.C_AREQ:
9090
asyncResponseProcessor(frame)
9191
default:
92-
znp.errors <- fmt.Errorf("unsupported frame received type: %v ", frame)
92+
select {
93+
case znp.errors <- fmt.Errorf("unsupported frame received type: %v ", frame):
94+
default:
95+
}
9396
}
9497
}
9598
}
@@ -103,9 +106,12 @@ func startIncomingFrameLoop(znp *Znp) {
103106
for znp.started {
104107
frame, err := znp.u.ReadFrame()
105108
if err != nil {
106-
znp.errors <- err
109+
select {
110+
case znp.errors <- err:
111+
default:
112+
}
107113
} else {
108-
logFrame(frame, znp.logInFrames, znp.inFramesLog)
114+
logFrame(frame, znp.inFramesLog)
109115
znp.inbound <- frame
110116
}
111117
}
@@ -117,7 +123,7 @@ func makeSyncRequestProcessor(znp *Znp, syncRsp chan *unp.Frame, syncErr chan er
117123
return func(req *request.Sync) {
118124
frame := req.Frame()
119125
deadline := time.NewTimer(5 * time.Second)
120-
logFrame(frame, znp.logOutFrames, znp.outFramesLog)
126+
logFrame(frame, znp.outFramesLog)
121127
err := znp.u.WriteFrame(frame)
122128
if err != nil {
123129
req.SyncErr() <- err
@@ -140,7 +146,7 @@ func makeSyncRequestProcessor(znp *Znp, syncRsp chan *unp.Frame, syncErr chan er
140146

141147
func makeAsyncRequestProcessor(znp *Znp) func(req *request.Async) {
142148
return func(req *request.Async) {
143-
logFrame(req.Frame(), znp.logOutFrames, znp.outFramesLog)
149+
logFrame(req.Frame(), znp.outFramesLog)
144150
znp.u.WriteFrame(req.Frame())
145151
}
146152
}
@@ -169,15 +175,25 @@ func makeAsyncResponseProcessor(znp *Znp) func(frame *unp.Frame) {
169175
if value, ok := asyncCommandRegistry[key]; ok {
170176
cp := reflection.Copy(value)
171177
bin.Decode(frame.Payload, cp)
172-
znp.asyncInbound <- cp
178+
select {
179+
case znp.asyncInbound <- cp:
180+
default:
181+
}
173182
} else {
174-
znp.errors <- fmt.Errorf("unknown async command received: %v", frame)
183+
select {
184+
case znp.errors <- fmt.Errorf("unknown async command received: %v", frame):
185+
default:
186+
}
175187
}
176188
}
177189
}
178190

179-
func logFrame(frame *unp.Frame, log bool, logger chan *unp.Frame) {
180-
if log {
181-
logger <- frame
182-
}
191+
func logFrame(frame *unp.Frame, logger chan *unp.Frame) {
192+
go func() {
193+
select {
194+
case logger <- frame:
195+
default:
196+
}
197+
}()
198+
183199
}

znp.go

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

33
import (
4-
unp "github.com/dyrkin/unp-go"
4+
"github.com/dyrkin/unp-go"
55

66
"github.com/dyrkin/znp-go/request"
77
)
@@ -14,8 +14,6 @@ type Znp struct {
1414
errors chan error
1515
inFramesLog chan *unp.Frame
1616
outFramesLog chan *unp.Frame
17-
logInFrames bool
18-
logOutFrames bool
1917
started bool
2018
}
2119

@@ -25,17 +23,13 @@ func New(u *unp.Unp) *Znp {
2523
outbound: make(chan request.Outgoing),
2624
inbound: make(chan *unp.Frame),
2725
asyncInbound: make(chan interface{}),
28-
errors: make(chan error),
26+
errors: make(chan error, 100),
2927
inFramesLog: make(chan *unp.Frame, 100),
3028
outFramesLog: make(chan *unp.Frame, 100),
3129
}
3230
return znp
3331
}
3432

35-
func (znp *Znp) LogInFrames(enabled bool) {
36-
znp.logInFrames = enabled
37-
}
38-
3933
func (znp *Znp) Errors() chan error {
4034
return znp.errors
4135
}
@@ -52,10 +46,6 @@ func (znp *Znp) OutFramesLog() chan *unp.Frame {
5246
return znp.outFramesLog
5347
}
5448

55-
func (znp *Znp) LogOutFrames(enabled bool) {
56-
znp.logOutFrames = enabled
57-
}
58-
5949
func (znp *Znp) IsStarted() bool {
6050
return znp.started
6151
}

0 commit comments

Comments
 (0)