WebSocketConnectionHelper.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Nextcloud Talk application
  3. *
  4. * @author Mario Danic
  5. * Copyright (C) 2017-2018 Mario Danic <mario@lovelyhq.com>
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. package com.nextcloud.talk.webrtc;
  21. import autodagger.AutoInjector;
  22. import com.nextcloud.talk.application.NextcloudTalkApplication;
  23. import com.nextcloud.talk.models.database.UserEntity;
  24. import com.nextcloud.talk.models.json.signaling.NCMessageWrapper;
  25. import com.nextcloud.talk.models.json.websocket.*;
  26. import com.nextcloud.talk.utils.ApiUtils;
  27. import okhttp3.OkHttpClient;
  28. import javax.inject.Inject;
  29. import java.util.HashMap;
  30. import java.util.Map;
  31. @AutoInjector(NextcloudTalkApplication.class)
  32. public class WebSocketConnectionHelper {
  33. private static Map<Long, MagicWebSocketInstance> magicWebSocketInstanceMap = new HashMap<>();
  34. @Inject
  35. OkHttpClient okHttpClient;
  36. public WebSocketConnectionHelper() {
  37. NextcloudTalkApplication.Companion.getSharedApplication().getComponentApplication().inject(this);
  38. }
  39. public static synchronized MagicWebSocketInstance getMagicWebSocketInstanceForUserId(long userId) {
  40. if (userId != -1 && magicWebSocketInstanceMap.containsKey(userId)) {
  41. return magicWebSocketInstanceMap.get(userId);
  42. }
  43. return null;
  44. }
  45. public static synchronized MagicWebSocketInstance getExternalSignalingInstanceForServer(String url, UserEntity userEntity, String webSocketTicket, boolean isGuest) {
  46. String generatedURL = url.replace("https://", "wss://").replace("http://", "ws://");
  47. if (generatedURL.endsWith("/")) {
  48. generatedURL += "spreed";
  49. } else {
  50. generatedURL += "/spreed";
  51. }
  52. long userId = isGuest ? -1 : userEntity.getId();
  53. MagicWebSocketInstance magicWebSocketInstance;
  54. if (userId != -1 && magicWebSocketInstanceMap.containsKey(userEntity.getId()) && (magicWebSocketInstance = magicWebSocketInstanceMap.get(userEntity.getId())) != null) {
  55. return magicWebSocketInstance;
  56. } else {
  57. if (userId == -1) {
  58. deleteExternalSignalingInstanceForUserEntity(userId);
  59. }
  60. magicWebSocketInstance = new MagicWebSocketInstance(userEntity, generatedURL, webSocketTicket);
  61. magicWebSocketInstanceMap.put(userEntity.getId(), magicWebSocketInstance);
  62. return magicWebSocketInstance;
  63. }
  64. }
  65. public static synchronized void deleteExternalSignalingInstanceForUserEntity(long id) {
  66. MagicWebSocketInstance magicWebSocketInstance;
  67. if ((magicWebSocketInstance = magicWebSocketInstanceMap.get(id)) != null) {
  68. if (magicWebSocketInstance.isConnected()) {
  69. magicWebSocketInstance.sendBye();
  70. magicWebSocketInstanceMap.remove(id);
  71. }
  72. }
  73. }
  74. HelloOverallWebSocketMessage getAssembledHelloModel(UserEntity userEntity, String ticket) {
  75. int apiVersion = ApiUtils.getSignalingApiVersion(userEntity, new int[] {2, 1});
  76. HelloOverallWebSocketMessage helloOverallWebSocketMessage = new HelloOverallWebSocketMessage();
  77. helloOverallWebSocketMessage.setType("hello");
  78. HelloWebSocketMessage helloWebSocketMessage = new HelloWebSocketMessage();
  79. helloWebSocketMessage.setVersion("1.0");
  80. AuthWebSocketMessage authWebSocketMessage = new AuthWebSocketMessage();
  81. authWebSocketMessage.setUrl(ApiUtils.getUrlForSignalingBackend(apiVersion, userEntity.getBaseUrl()));
  82. AuthParametersWebSocketMessage authParametersWebSocketMessage = new AuthParametersWebSocketMessage();
  83. authParametersWebSocketMessage.setTicket(ticket);
  84. if (!userEntity.getUserId().equals("?")) {
  85. authParametersWebSocketMessage.setUserid(userEntity.getUserId());
  86. }
  87. authWebSocketMessage.setAuthParametersWebSocketMessage(authParametersWebSocketMessage);
  88. helloWebSocketMessage.setAuthWebSocketMessage(authWebSocketMessage);
  89. helloOverallWebSocketMessage.setHelloWebSocketMessage(helloWebSocketMessage);
  90. return helloOverallWebSocketMessage;
  91. }
  92. HelloOverallWebSocketMessage getAssembledHelloModelForResume(String resumeId) {
  93. HelloOverallWebSocketMessage helloOverallWebSocketMessage = new HelloOverallWebSocketMessage();
  94. helloOverallWebSocketMessage.setType("hello");
  95. HelloWebSocketMessage helloWebSocketMessage = new HelloWebSocketMessage();
  96. helloWebSocketMessage.setVersion("1.0");
  97. helloWebSocketMessage.setResumeid(resumeId);
  98. helloOverallWebSocketMessage.setHelloWebSocketMessage(helloWebSocketMessage);
  99. return helloOverallWebSocketMessage;
  100. }
  101. RoomOverallWebSocketMessage getAssembledJoinOrLeaveRoomModel(String roomId, String sessionId) {
  102. RoomOverallWebSocketMessage roomOverallWebSocketMessage = new RoomOverallWebSocketMessage();
  103. roomOverallWebSocketMessage.setType("room");
  104. RoomWebSocketMessage roomWebSocketMessage = new RoomWebSocketMessage();
  105. roomWebSocketMessage.setRoomId(roomId);
  106. roomWebSocketMessage.setSessiondId(sessionId);
  107. roomOverallWebSocketMessage.setRoomWebSocketMessage(roomWebSocketMessage);
  108. return roomOverallWebSocketMessage;
  109. }
  110. RequestOfferOverallWebSocketMessage getAssembledRequestOfferModel(String sessionId, String roomType) {
  111. RequestOfferOverallWebSocketMessage requestOfferOverallWebSocketMessage = new RequestOfferOverallWebSocketMessage();
  112. requestOfferOverallWebSocketMessage.setType("message");
  113. RequestOfferSignalingMessage requestOfferSignalingMessage = new RequestOfferSignalingMessage();
  114. ActorWebSocketMessage actorWebSocketMessage = new ActorWebSocketMessage();
  115. actorWebSocketMessage.setType("session");
  116. actorWebSocketMessage.setSessionId(sessionId);
  117. requestOfferSignalingMessage.setActorWebSocketMessage(actorWebSocketMessage);
  118. SignalingDataWebSocketMessageForOffer signalingDataWebSocketMessageForOffer = new SignalingDataWebSocketMessageForOffer();
  119. signalingDataWebSocketMessageForOffer.setRoomType(roomType);
  120. signalingDataWebSocketMessageForOffer.setType("requestoffer");
  121. requestOfferSignalingMessage.setSignalingDataWebSocketMessageForOffer(signalingDataWebSocketMessageForOffer);
  122. requestOfferOverallWebSocketMessage.setRequestOfferOverallWebSocketMessage(requestOfferSignalingMessage);
  123. return requestOfferOverallWebSocketMessage;
  124. }
  125. CallOverallWebSocketMessage getAssembledCallMessageModel(NCMessageWrapper ncMessageWrapper) {
  126. CallOverallWebSocketMessage callOverallWebSocketMessage = new CallOverallWebSocketMessage();
  127. callOverallWebSocketMessage.setType("message");
  128. CallWebSocketMessage callWebSocketMessage = new CallWebSocketMessage();
  129. ActorWebSocketMessage actorWebSocketMessage = new ActorWebSocketMessage();
  130. actorWebSocketMessage.setType("session");
  131. actorWebSocketMessage.setSessionId(ncMessageWrapper.getSignalingMessage().getTo());
  132. callWebSocketMessage.setRecipientWebSocketMessage(actorWebSocketMessage);
  133. callWebSocketMessage.setNcSignalingMessage(ncMessageWrapper.getSignalingMessage());
  134. callOverallWebSocketMessage.setCallWebSocketMessage(callWebSocketMessage);
  135. return callOverallWebSocketMessage;
  136. }
  137. }