From 60b9767fa7be32117cc5dfe11a7227dce61567b6 Mon Sep 17 00:00:00 2001 From: Max Hawkins Date: Tue, 16 Jul 2019 14:37:21 -0700 Subject: [PATCH] more granular end connection logging --- app.go | 37 +++++++++++++++---------------------- ui/events.go | 2 +- ui/reducer.go | 40 ++++++++++++++++++++++++++++++++++++++++ ui/state.go | 17 +++++++++++++++++ 4 files changed, 73 insertions(+), 23 deletions(-) diff --git a/app.go b/app.go index 48d3c1f..ee41679 100644 --- a/app.go +++ b/app.go @@ -151,7 +151,7 @@ func (a *App) run(ctx context.Context) error { a.nextPartner = nextPartner a.cancelMu.Unlock() - skipReason, err := a.connect(connCtx) + endReason, err := a.connect(connCtx) // HACK(maxhawkins): these errors get returned when the context passed // into match is canceled, so we ignore them. There's probably a more elegant // way to close the websocket without all this error munging. @@ -178,12 +178,7 @@ func (a *App) run(ctx context.Context) error { } } - if skipReason != "" { - a.renderer.Dispatch(ui.LogEvent{ - Level: ui.LogLevelInfo, - Text: skipReason, - }) - } + a.renderer.Dispatch(ui.ConnEndedEvent{endReason}) a.renderer.Dispatch(ui.FrameEvent(nil)) time.Sleep(100 * time.Millisecond) @@ -284,7 +279,7 @@ func (a *App) checkConnection(ctx context.Context) error { } } -func (a *App) connect(ctx context.Context) (endReason string, err error) { +func (a *App) connect(ctx context.Context) (ui.EndConnReason, error) { ctx, cancel := context.WithCancel(ctx) defer cancel() @@ -295,10 +290,10 @@ func (a *App) connect(ctx context.Context) (endReason string, err error) { connectTimeout.Stop() if err := a.checkConnection(ctx); err != nil { - return "", err + return ui.EndConnSetupError, err } - ended := make(chan string) + ended := make(chan ui.EndConnReason) conn, err := NewConn(webrtc.Configuration{ ICEServers: []webrtc.ICEServer{ @@ -306,7 +301,7 @@ func (a *App) connect(ctx context.Context) (endReason string, err error) { }, }) if err != nil { - return "", err + return ui.EndConnSetupError, err } a.conn = conn @@ -327,7 +322,7 @@ func (a *App) connect(ctx context.Context) (endReason string, err error) { conn.OnBye = func() { a.renderer.Dispatch(ui.FrameEvent(nil)) - ended <- "Partner left" + ended <- ui.EndConnGone } conn.OnMessage = func(s string) { a.renderer.Dispatch(ui.ReceivedChatEvent(s)) @@ -346,7 +341,7 @@ func (a *App) connect(ctx context.Context) (endReason string, err error) { }) case webrtc.ICEConnectionStateFailed: - ended <- "Lost connection" + ended <- ui.EndConnDisconnected } } conn.OnDataOpen = func() { @@ -357,7 +352,7 @@ func (a *App) connect(ctx context.Context) (endReason string, err error) { dec, err := vpx.NewDecoder(320, 240) if err != nil { - return "", err + return ui.EndConnSetupError, err } conn.OnFrame = func(frame []byte) { frameTimeout.Reset(5 * time.Second) @@ -381,10 +376,10 @@ func (a *App) connect(ctx context.Context) (endReason string, err error) { err = Match(ctx, a.signalerURL, conn.pc) if err == errMatchFailed { - return "", nil + return ui.EndConnMatchError, nil } if err != nil { - return "", err + return ui.EndConnMatchError, err } connectTimeout.Reset(10 * time.Second) @@ -394,20 +389,18 @@ func (a *App) connect(ctx context.Context) (endReason string, err error) { Text: "Found match. Connecting...", }) - var reason string + var reason ui.EndConnReason select { case <-ctx.Done(): - reason = "" + reason = ui.EndConnNormal case <-connectTimeout.C: - reason = "Connection timed out" + reason = ui.EndConnTimedOut case <-frameTimeout.C: - reason = "Lost connection" + reason = ui.EndConnDisconnected case r := <-ended: reason = r } - a.renderer.Dispatch(ui.ConnEndedEvent{reason}) - return reason, nil } diff --git a/ui/events.go b/ui/events.go index 3084633..2265512 100644 --- a/ui/events.go +++ b/ui/events.go @@ -44,7 +44,7 @@ type ConnStartedEvent struct{} // ConnEndedEvent fires when the connection with your partner has been lost type ConnEndedEvent struct { // The reason for the disconnection - Reason string + Reason EndConnReason } // SetPageEvent transitions to the specified page diff --git a/ui/reducer.go b/ui/reducer.go index 1f04836..8617e9b 100644 --- a/ui/reducer.go +++ b/ui/reducer.go @@ -143,6 +143,46 @@ func messagesReducer(s []Message, event Event) []Message { Text: string(e), }) + case ConnEndedEvent: + var msg Message + + switch e.Reason { + + // Error handler will catch this + case EndConnSetupError: + return s + + // We ignore match errors + case EndConnMatchError: + return s + + case EndConnNormal: + msg = Message{ + Type: MessageTypeInfo, + Text: "Skipping...", + } + + case EndConnTimedOut: + msg = Message{ + Type: MessageTypeError, + Text: "Connection timed out.", + } + + case EndConnDisconnected: + msg = Message{ + Type: MessageTypeError, + Text: "Lost connection.", + } + + case EndConnGone: + msg = Message{ + Type: MessageTypeInfo, + Text: "Your partner left the chat.", + } + } + + return append(s, msg) + case ConnStartedEvent: return append(s, Message{ Type: MessageTypeInfo, diff --git a/ui/state.go b/ui/state.go index a093e12..5529517 100644 --- a/ui/state.go +++ b/ui/state.go @@ -42,3 +42,20 @@ type Message struct { User string Text string } + +type EndConnReason int + +const ( + // User closed connection + EndConnNormal EndConnReason = iota + // Error during connection setup + EndConnSetupError + // Error during matching + EndConnMatchError + // Connection timed out + EndConnTimedOut + // Lost connection with partner + EndConnDisconnected + // Partner left + EndConnGone +)