Further websockets implementation

This commit is contained in:
Mario Danic 2018-10-15 14:23:25 +02:00
parent 8af047926e
commit 41a16f8f83
6 changed files with 169 additions and 27 deletions

View File

@ -1192,9 +1192,12 @@ public class CallController extends BaseController {
public void onMessageEvent(WebSocketCommunicationEvent webSocketCommunicationEvent) {
if (webSocketCommunicationEvent.getType().equals("hello")) {
callSession = webSocketClient.getSessionId();
webSocketClient.joinRoomWithRoomId(roomToken);
MagicPeerConnectionWrapper magicPeerConnectionWrapper = alwaysGetPeerConnectionWrapperForSessionId(callSession);
} else if (webSocketCommunicationEvent.equals("MCUPeerReady")) {
webSocketClient.joinRoomWithRoomToken(roomToken);
alwaysGetPeerConnectionWrapperForSessionId(callSession);
} else if (webSocketCommunicationEvent.equals("participantsUpdate")) {
if (webSocketCommunicationEvent.getHashMap().get("roomId").equals(roomToken)) {
processUsersInRoom((List<HashMap<String, Object>>) webSocketClient.getJobWithId(Integer.valueOf(webSocketCommunicationEvent.getHashMap().get("jobId"))));
}
}
}
@ -1352,7 +1355,7 @@ public class CallController extends BaseController {
public void onNext(GenericOverall genericOverall) {
if (isMultiSession) {
if (externalSignalingServer != null) {
webSocketClient.joinRoomWithRoomId("");
webSocketClient.joinRoomWithRoomToken("");
}
if (getActivity() != null) {
getActivity().finish();
@ -1387,7 +1390,7 @@ public class CallController extends BaseController {
@Override
public void onNext(GenericOverall genericOverall) {
if (externalSignalingServer != null) {
webSocketClient.joinRoomWithRoomId("");
webSocketClient.joinRoomWithRoomToken("");
}
if (getActivity() != null) {
@ -1506,12 +1509,12 @@ public class CallController extends BaseController {
} else {
hasMCU = webSocketClient != null && webSocketClient.hasMCU();
if (hasMCU) {
if (sessionId.equals(callSession)) {
magicPeerConnectionWrapper = new MagicPeerConnectionWrapper(peerConnectionFactory,
iceServers, sdpConstraintsForMCU, sessionId, callSession, null, hasMCU);
iceServers, sdpConstraintsForMCU, sessionId, callSession, localMediaStream, hasMCU);
} else {
magicPeerConnectionWrapper = new MagicPeerConnectionWrapper(peerConnectionFactory,
iceServers, sdpConstraints, sessionId, callSession, localMediaStream, hasMCU);
iceServers, sdpConstraintsForMCU, sessionId, callSession, null, hasMCU);
}
magicPeerConnectionWrapperList.add(magicPeerConnectionWrapper);
@ -1685,7 +1688,7 @@ public class CallController extends BaseController {
}
});
} else {
webSocketClient.getWebSocket().send(LoganSquare.serialize(ncMessageWrapper));
webSocketClient.sendCallMessage(ncMessageWrapper);
}
}

View File

@ -0,0 +1,36 @@
/*
* Nextcloud Talk application
*
* @author Mario Danic
* Copyright (C) 2017-2018 Mario Danic <mario@lovelyhq.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.nextcloud.talk.models.json.websocket;
import com.bluelinelabs.logansquare.annotation.JsonField;
import com.bluelinelabs.logansquare.annotation.JsonObject;
import org.parceler.Parcel;
import lombok.Data;
@Data
@Parcel
@JsonObject
public class ErrorOverallWebSocketMessage extends BaseWebSocketMessage {
@JsonField(name = "error")
ErrorWebSocketMessage errorWebSocketMessage;
}

View File

@ -20,6 +20,7 @@
package com.nextcloud.talk.models.json.websocket;
import com.bluelinelabs.logansquare.annotation.JsonField;
import com.bluelinelabs.logansquare.annotation.JsonObject;
import org.parceler.Parcel;
@ -27,7 +28,12 @@ import org.parceler.Parcel;
import lombok.Data;
@Data
@JsonObject
@Parcel
public class BaseSignalingWebSocketMessage extends BaseWebSocketMessage {
@JsonObject
public class ErrorWebSocketMessage {
@JsonField(name = "code")
String code;
@JsonField(name = "message")
String message;
}

View File

@ -0,0 +1,40 @@
/*
* Nextcloud Talk application
*
* @author Mario Danic
* Copyright (C) 2017-2018 Mario Danic <mario@lovelyhq.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.nextcloud.talk.models.json.websocket;
import com.bluelinelabs.logansquare.annotation.JsonField;
import com.bluelinelabs.logansquare.annotation.JsonObject;
import org.parceler.Parcel;
import java.util.HashMap;
import lombok.Data;
@Data
@Parcel
@JsonObject
public class EventOverallWebSocketMessage extends BaseWebSocketMessage {
@JsonField(name = "type")
String type;
@JsonField(name = {"message", "update"})
HashMap<String, Object> eventMap;
}

View File

@ -27,15 +27,20 @@ import com.bluelinelabs.logansquare.LoganSquare;
import com.nextcloud.talk.application.NextcloudTalkApplication;
import com.nextcloud.talk.events.WebSocketCommunicationEvent;
import com.nextcloud.talk.models.database.UserEntity;
import com.nextcloud.talk.models.json.signaling.NCMessageWrapper;
import com.nextcloud.talk.models.json.websocket.BaseWebSocketMessage;
import com.nextcloud.talk.models.json.websocket.CallOverallWebSocketMessage;
import com.nextcloud.talk.models.json.websocket.ErrorOverallWebSocketMessage;
import com.nextcloud.talk.models.json.websocket.EventOverallWebSocketMessage;
import com.nextcloud.talk.models.json.websocket.HelloResponseOverallWebSocketMessage;
import com.nextcloud.talk.models.json.websocket.RoomOverallWebSocketMessage;
import com.nextcloud.talk.models.json.websocket.RoomWebSocketMessage;
import org.greenrobot.eventbus.EventBus;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
import javax.inject.Inject;
@ -49,7 +54,7 @@ import okio.ByteString;
@AutoInjector(NextcloudTalkApplication.class)
public class MagicWebSocketInstance extends WebSocketListener {
private static final String TAG = "MagicWebSocketListener";
private static final String TAG = "MagicWebSocketInstance";
@Inject
OkHttpClient okHttpClient;
@ -65,6 +70,7 @@ public class MagicWebSocketInstance extends WebSocketListener {
private boolean connected;
private WebSocketConnectionHelper webSocketConnectionHelper;
private WebSocket webSocket;
private ConcurrentHashMap<Integer, Object> concurrentHashMapQueue;
MagicWebSocketInstance(UserEntity conversationUser, String connectionUrl, String webSocketTicket) {
NextcloudTalkApplication.getSharedApplication().getComponentApplication().inject(this);
@ -75,6 +81,8 @@ public class MagicWebSocketInstance extends WebSocketListener {
this.conversationUser = conversationUser;
this.webSocketTicket = webSocketTicket;
this.webSocketConnectionHelper = new WebSocketConnectionHelper();
concurrentHashMapQueue = new ConcurrentHashMap<>();
}
@Override
@ -107,13 +115,57 @@ public class MagicWebSocketInstance extends WebSocketListener {
eventBus.post(new WebSocketCommunicationEvent("hello", null));
break;
case "error":
// Nothing for now
ErrorOverallWebSocketMessage errorOverallWebSocketMessage = LoganSquare.parse(text, ErrorOverallWebSocketMessage.class);
break;
case "room":
// Nothing for now
break;
case "event":
// Nothing for now
EventOverallWebSocketMessage eventOverallWebSocketMessage = LoganSquare.parse(text, EventOverallWebSocketMessage.class);
if (eventOverallWebSocketMessage.getEventMap() != null) {
String target = (String) eventOverallWebSocketMessage.getEventMap().get("target");
switch (target) {
case "room":
if (eventOverallWebSocketMessage.getType().equals("message") && eventOverallWebSocketMessage.getEventMap() != null) {
if (eventOverallWebSocketMessage.getEventMap().containsKey("data")) {
Map<String, Object> dataHashMap = (Map<String, Object>) eventOverallWebSocketMessage.getEventMap().get("data");
if (dataHashMap.containsKey("chat")) {
boolean shouldRefreshChat;
Map<String, Object> chatMap = (Map<String, Object>) dataHashMap.get("chat");
if (chatMap.containsKey("refresh")) {
shouldRefreshChat = (boolean) chatMap.get("refresh");
if (shouldRefreshChat) {
HashMap<String, String> refreshChatHashMap = new HashMap<>();
refreshChatHashMap.put("roomToken", (String) eventOverallWebSocketMessage.getEventMap().get("roomid"));
eventBus.post(new WebSocketCommunicationEvent("refreshChat", refreshChatHashMap));
}
}
}
}
}
break;
case "participants":
if (eventOverallWebSocketMessage.getType().equals("update") && eventOverallWebSocketMessage.getEventMap() != null) {
Map<String, Object> participantsUpdateMap = eventOverallWebSocketMessage.getEventMap();
HashMap<String, String> refreshChatHashMap = new HashMap<>();
refreshChatHashMap.put("roomToken", (String) eventOverallWebSocketMessage.getEventMap().get("roomid"));
int newId;
do {
Random rand = new Random();
newId = rand.nextInt(1000);
if (!concurrentHashMapQueue.contains(newId)) {
concurrentHashMapQueue.put(newId, participantsUpdateMap.get("users"));
refreshChatHashMap.put("roomToken", (String) eventOverallWebSocketMessage.getEventMap().get("roomid"));
refreshChatHashMap.put("jobId", Integer.toString(newId));
eventBus.post(new WebSocketCommunicationEvent("participantsUpdate", refreshChatHashMap));
}
} while (!concurrentHashMapQueue.contains(newId));
}
break;
}
}
break;
case "message":
CallOverallWebSocketMessage callOverallWebSocketMessage = LoganSquare.parse(text, CallOverallWebSocketMessage.class);
@ -151,21 +203,25 @@ public class MagicWebSocketInstance extends WebSocketListener {
return hasMCU;
}
public WebSocket getWebSocket() {
return webSocket;
}
public void joinRoomWithRoomId(String roomId) {
RoomOverallWebSocketMessage roomOverallWebSocketMessage = new RoomOverallWebSocketMessage();
roomOverallWebSocketMessage.setType("room");
RoomWebSocketMessage roomWebSocketMessage = new RoomWebSocketMessage();
roomWebSocketMessage.setRoomId(roomId);
roomWebSocketMessage.setSessiondId(sessionId);
roomOverallWebSocketMessage.setRoomWebSocketMessage(roomWebSocketMessage);
public void joinRoomWithRoomToken(String roomToken) {
try {
webSocket.send(LoganSquare.serialize(roomOverallWebSocketMessage));
webSocket.send(LoganSquare.serialize(webSocketConnectionHelper.getAssembledJoinOrLeaveRoomModel(roomToken, sessionId)));
} catch (IOException e) {
Log.e(TAG, "Failed to serialize room overall websocket message");
}
}
public void sendCallMessage(NCMessageWrapper ncMessageWrapper) {
try {
webSocket.send(LoganSquare.serialize(webSocketConnectionHelper.getAssembledCallMessageModel(ncMessageWrapper)));
} catch (IOException e) {
Log.e(TAG, "Failed to serialize signaling message");
}
}
public Object getJobWithId(Integer id) {
Object copyJob = concurrentHashMapQueue.get(id);
concurrentHashMapQueue.remove(id);
return copyJob;
}
}

View File

@ -115,6 +115,7 @@ public class WebSocketConnectionHelper {
RoomWebSocketMessage roomWebSocketMessage = new RoomWebSocketMessage();
roomWebSocketMessage.setRoomId(roomId);
roomWebSocketMessage.setSessiondId(sessionId);
roomOverallWebSocketMessage.setRoomWebSocketMessage(roomWebSocketMessage);
return roomOverallWebSocketMessage;
}