From 61ff44a64348722e43bff5688aa15172aa8b669b Mon Sep 17 00:00:00 2001 From: Marcel Hibbe Date: Mon, 17 Jul 2023 16:01:25 +0200 Subject: [PATCH] add logging and minor refactoring Signed-off-by: Marcel Hibbe --- .../com/nextcloud/talk/chat/ChatActivity.kt | 2 +- .../talk/jobs/WebsocketConnectionsWorker.java | 8 +-- .../webrtc/WebSocketConnectionHelper.java | 15 +++-- .../talk/webrtc/WebSocketInstance.kt | 66 ++++++++----------- 4 files changed, 43 insertions(+), 48 deletions(-) diff --git a/app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt b/app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt index 0bb27c194..0ec4bad4e 100644 --- a/app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt @@ -2473,7 +2473,7 @@ class ChatActivity : if (conversationUser == null) { return } - webSocketInstance = WebSocketConnectionHelper.getWebSocketInstanceForUserId(conversationUser!!.id!!) + webSocketInstance = WebSocketConnectionHelper.getWebSocketInstanceForUser(conversationUser!!) if (webSocketInstance == null) { Log.d(TAG, "webSocketInstance not set up. This should only happen when not using the HPB") diff --git a/app/src/main/java/com/nextcloud/talk/jobs/WebsocketConnectionsWorker.java b/app/src/main/java/com/nextcloud/talk/jobs/WebsocketConnectionsWorker.java index d500657d1..f02146eab 100644 --- a/app/src/main/java/com/nextcloud/talk/jobs/WebsocketConnectionsWorker.java +++ b/app/src/main/java/com/nextcloud/talk/jobs/WebsocketConnectionsWorker.java @@ -60,21 +60,21 @@ public class WebsocketConnectionsWorker extends Worker { NextcloudTalkApplication.Companion.getSharedApplication().getComponentApplication().inject(this); List users = userManager.getUsers().blockingGet(); - WebSocketConnectionHelper webSocketConnectionHelper = new WebSocketConnectionHelper(); for (User user : users) { if (user.getExternalSignalingServer() != null && user.getExternalSignalingServer().getExternalSignalingServer() != null && !TextUtils.isEmpty(user.getExternalSignalingServer().getExternalSignalingServer()) && !TextUtils.isEmpty(user.getExternalSignalingServer().getExternalSignalingTicket())) { - Log.d(TAG, - "WebsocketConnectionsWorker - getExternalSignalingInstanceForServer for user " + user.getDisplayName()); + Log.d(TAG, "trying to getExternalSignalingInstanceForServer for user " + user.getDisplayName()); - webSocketConnectionHelper.getExternalSignalingInstanceForServer( + WebSocketConnectionHelper.getExternalSignalingInstanceForServer( user.getExternalSignalingServer().getExternalSignalingServer(), user, user.getExternalSignalingServer().getExternalSignalingTicket(), false); + } else { + Log.d(TAG, "skipped to getExternalSignalingInstanceForServer for user " + user.getDisplayName()); } } diff --git a/app/src/main/java/com/nextcloud/talk/webrtc/WebSocketConnectionHelper.java b/app/src/main/java/com/nextcloud/talk/webrtc/WebSocketConnectionHelper.java index 762a9e577..96b0a6fdb 100644 --- a/app/src/main/java/com/nextcloud/talk/webrtc/WebSocketConnectionHelper.java +++ b/app/src/main/java/com/nextcloud/talk/webrtc/WebSocketConnectionHelper.java @@ -59,11 +59,15 @@ public class WebSocketConnectionHelper { } @SuppressLint("LongLogTag") - public static synchronized WebSocketInstance getWebSocketInstanceForUserId(long userId) { - WebSocketInstance webSocketInstance = webSocketInstanceMap.get(userId); + public static synchronized WebSocketInstance getWebSocketInstanceForUser(User user) { + WebSocketInstance webSocketInstance = webSocketInstanceMap.get(user.getId()); if (webSocketInstance == null) { - Log.e(TAG, "No webSocketInstance found for user " + userId); + Log.d(TAG, "No webSocketInstance found for user " + user.getDisplayName() + + " (userId:" + user.getId() + ")"); + } else { + Log.d(TAG, "Existing webSocketInstance found for user " + user.getDisplayName() + + " (userId:" + user.getId() + ")"); } return webSocketInstance; @@ -71,7 +75,8 @@ public class WebSocketConnectionHelper { public static synchronized WebSocketInstance getExternalSignalingInstanceForServer(String url, User user, - String webSocketTicket, boolean isGuest) { + String webSocketTicket, + boolean isGuest) { String generatedURL = url.replace("https://", "wss://").replace("http://", "ws://"); if (generatedURL.endsWith("/")) { @@ -82,7 +87,7 @@ public class WebSocketConnectionHelper { long userId = isGuest ? -1 : user.getId(); - WebSocketInstance webSocketInstance = webSocketInstanceMap.get(user.getId()); + WebSocketInstance webSocketInstance = getWebSocketInstanceForUser(user); if (userId != -1 && webSocketInstance != null && webSocketInstance.isConnected()) { return webSocketInstance; diff --git a/app/src/main/java/com/nextcloud/talk/webrtc/WebSocketInstance.kt b/app/src/main/java/com/nextcloud/talk/webrtc/WebSocketInstance.kt index dec297dd7..7e4c5b4ac 100644 --- a/app/src/main/java/com/nextcloud/talk/webrtc/WebSocketInstance.kt +++ b/app/src/main/java/com/nextcloud/talk/webrtc/WebSocketInstance.kt @@ -148,8 +148,6 @@ class WebSocketInstance internal constructor( fun restartWebSocket() { reconnecting = true - - // TODO when improving logging, keep in mind this issue: https://github.com/nextcloud/talk-android/issues/1013 Log.d(TAG, "restartWebSocket: $connectionUrl") val request = Request.Builder().url(connectionUrl).build() okHttpClient!!.newWebSocket(request, this) @@ -371,7 +369,7 @@ class WebSocketInstance internal constructor( } override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) { - Log.d(TAG, "Error : WebSocket " + webSocket.hashCode() + " onFailure: " + t.message) + Log.e(TAG, "Error : WebSocket " + webSocket.hashCode(), t) closeWebSocket(webSocket) } @@ -387,32 +385,20 @@ class WebSocketInstance internal constructor( val message = LoganSquare.serialize( webSocketConnectionHelper.getAssembledJoinOrLeaveRoomModel(roomToken, normalBackendSession) ) - if (!isConnected || reconnecting) { - messagesQueue.add(message) - - if (!reconnecting) { - restartWebSocket() - } + if (roomToken == "") { + Log.d(TAG, "sending 'leave room' via websocket") + currentNormalBackendSession = "" + sendMessage(message) + } else if (roomToken == currentRoomToken && normalBackendSession == currentNormalBackendSession) { + Log.d(TAG, "roomToken & session are unchanged. Joining locally without to send websocket message") + sendRoomJoinedEvent() } else { - if (roomToken == "") { - Log.d(TAG, "sending 'leave room' via websocket") - currentNormalBackendSession = "" - internalWebSocket!!.send(message) - } else if (roomToken == currentRoomToken && normalBackendSession == currentNormalBackendSession) { - Log.d(TAG, "roomToken&session are unchanged. Joining locally without to send websocket message") - sendRoomJoinedEvent() - } else { - Log.d(TAG, "Sending join room message via websocket") - currentNormalBackendSession = normalBackendSession - - if (!internalWebSocket!!.send(message)) { - messagesQueue.add(message) - restartWebSocket() - } - } + Log.d(TAG, "Sending join room message via websocket") + currentNormalBackendSession = normalBackendSession + sendMessage(message) } } catch (e: IOException) { - Log.e(TAG, e.message, e) + Log.e(TAG, "Failed to serialize signaling message", e) } } @@ -421,23 +407,27 @@ class WebSocketInstance internal constructor( val message = LoganSquare.serialize( webSocketConnectionHelper.getAssembledCallMessageModel(ncSignalingMessage) ) - if (!isConnected || reconnecting) { - messagesQueue.add(message) - - if (!reconnecting) { - restartWebSocket() - } - } else { - if (!internalWebSocket!!.send(message)) { - messagesQueue.add(message) - restartWebSocket() - } - } + sendMessage(message) } catch (e: IOException) { Log.e(TAG, "Failed to serialize signaling message", e) } } + private fun sendMessage(message: String) { + if (!isConnected || reconnecting) { + messagesQueue.add(message) + + if (!reconnecting) { + restartWebSocket() + } + } else { + if (!internalWebSocket!!.send(message)) { + messagesQueue.add(message) + restartWebSocket() + } + } + } + fun sendBye() { if (isConnected) { try {