-
Notifications
You must be signed in to change notification settings - Fork 7
feat: add monitor command #66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
a3a434e
feat: add bridge monitor command
lucarin91 3e3726c
Update internal/monitor/monitor.go
lucarin91 9d7e112
improve description
lucarin91 32c06ae
apply code review suggestions
lucarin91 14a718e
propertly implement the internal function
lucarin91 91eb182
Merge remote-tracking branch 'origin/main' into add-bridge-monitor
lucarin91 976efb4
handle also text type
lucarin91 9196360
Update internal/monitor/monitor.go
lucarin91 37c2d5e
Update internal/monitor/monitor.go
lucarin91 c6afb8c
Update internal/monitor/monitor.go
lucarin91 28da34d
Merge remote-tracking branch 'origin/main' into add-bridge-monitor
lucarin91 ee0d696
add monitor test
lucarin91 21a9f2e
fixup! add monitor test
lucarin91 403b922
fixup! fixup! add monitor test
lucarin91 c2274ec
fixup! fixup! fixup! add monitor test
lucarin91 84bdeb6
fixup! fixup! fixup! fixup! add monitor test
lucarin91 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| // This file is part of arduino-app-cli. | ||
| // | ||
| // Copyright 2025 ARDUINO SA (http://www.arduino.cc/) | ||
| // | ||
| // This software is released under the GNU General Public License version 3, | ||
| // which covers the main part of arduino-app-cli. | ||
| // The terms of this license can be found at: | ||
| // https://www.gnu.org/licenses/gpl-3.0.en.html | ||
| // | ||
| // You can be released from the requirements of the above licenses by purchasing | ||
| // a commercial license. Buying such a license is mandatory if you want to | ||
| // modify or otherwise use the software for commercial activities involving the | ||
| // Arduino software without disclosing the source code of your own applications. | ||
| // To purchase a commercial license, send an email to license@arduino.cc. | ||
|
|
||
| package monitor | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
| "log/slog" | ||
| "net" | ||
| "time" | ||
|
|
||
| "github.com/gorilla/websocket" | ||
| ) | ||
|
|
||
| type MessageReaderWriter interface { | ||
| ReadMessage() (messageType int, p []byte, err error) | ||
| WriteMessage(messageType int, data []byte) error | ||
| Close() error | ||
| } | ||
|
|
||
| func NewMonitorHandler(ws MessageReaderWriter) (func(), error) { | ||
| // Connect to monitor | ||
| mon, err := net.DialTimeout("tcp", "127.0.0.1:7500", time.Second) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return func() { | ||
| monitorStream(mon, ws) | ||
| }, nil | ||
| } | ||
|
|
||
| func monitorStream(mon net.Conn, ws MessageReaderWriter) { | ||
lucarin91 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| logWebsocketError := func(msg string, err error) { | ||
| // Do not log simple close or interruption errors | ||
| if websocket.IsUnexpectedCloseError(err, websocket.CloseNormalClosure, websocket.CloseGoingAway, websocket.CloseNoStatusReceived, websocket.CloseAbnormalClosure) { | ||
| if e, ok := err.(*websocket.CloseError); ok { | ||
| slog.Error(msg, slog.String("closecause", fmt.Sprintf("%d: %s", e.Code, err))) | ||
| } else { | ||
| slog.Error(msg, slog.String("error", err.Error())) | ||
| } | ||
| } | ||
| } | ||
| logSocketError := func(msg string, err error) { | ||
| if !errors.Is(err, net.ErrClosed) && !errors.Is(err, io.EOF) { | ||
| slog.Error(msg, slog.String("error", err.Error())) | ||
| } | ||
| } | ||
lucarin91 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| go func() { | ||
| defer mon.Close() | ||
| defer ws.Close() | ||
| for { | ||
| // Read from websocket and write to monitor | ||
lucarin91 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| _, msg, err := ws.ReadMessage() | ||
| if err != nil { | ||
| logWebsocketError("Error reading from websocket", err) | ||
| return | ||
| } | ||
| if _, err := mon.Write(msg); err != nil { | ||
| logSocketError("Error writing to monitor", err) | ||
| return | ||
| } | ||
| } | ||
| }() | ||
| go func() { | ||
| defer mon.Close() | ||
| defer ws.Close() | ||
| buff := [1024]byte{} | ||
| for { | ||
| // Read from monitor and write to websocket | ||
lucarin91 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| n, err := mon.Read(buff[:]) | ||
| if err != nil { | ||
| logSocketError("Error reading from monitor", err) | ||
| return | ||
| } | ||
|
|
||
| if err := ws.WriteMessage(websocket.BinaryMessage, buff[:n]); err != nil { | ||
| logWebsocketError("Error writing to websocket", err) | ||
| return | ||
| } | ||
| } | ||
| }() | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.