You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

175 lines
6.0 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2014 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #import "ARDAppEngineClient.h"
  11. #import <WebRTC/RTCLogging.h>
  12. #import "ARDJoinResponse.h"
  13. #import "ARDMessageResponse.h"
  14. #import "ARDSignalingMessage.h"
  15. #import "ARDUtilities.h"
  16. // TODO(tkchin): move these to a configuration object.
  17. static NSString * const kARDRoomServerHostUrl =
  18. @"https://appr.tc";
  19. static NSString * const kARDRoomServerJoinFormat =
  20. @"https://appr.tc/join/%@";
  21. static NSString * const kARDRoomServerJoinFormatLoopback =
  22. @"https://appr.tc/join/%@?debug=loopback";
  23. static NSString * const kARDRoomServerMessageFormat =
  24. @"https://appr.tc/message/%@/%@";
  25. static NSString * const kARDRoomServerLeaveFormat =
  26. @"https://appr.tc/leave/%@/%@";
  27. static NSString * const kARDAppEngineClientErrorDomain = @"ARDAppEngineClient";
  28. static NSInteger const kARDAppEngineClientErrorBadResponse = -1;
  29. @implementation ARDAppEngineClient
  30. #pragma mark - ARDRoomServerClient
  31. - (void)joinRoomWithRoomId:(NSString *)roomId
  32. isLoopback:(BOOL)isLoopback
  33. completionHandler:(void (^)(ARDJoinResponse *response,
  34. NSError *error))completionHandler {
  35. NSParameterAssert(roomId.length);
  36. NSString *urlString = nil;
  37. if (isLoopback) {
  38. urlString =
  39. [NSString stringWithFormat:kARDRoomServerJoinFormatLoopback, roomId];
  40. } else {
  41. urlString =
  42. [NSString stringWithFormat:kARDRoomServerJoinFormat, roomId];
  43. }
  44. NSURL *roomURL = [NSURL URLWithString:urlString];
  45. RTCLog(@"Joining room:%@ on room server.", roomId);
  46. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:roomURL];
  47. request.HTTPMethod = @"POST";
  48. [NSURLConnection sendAsyncRequest:request
  49. completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
  50. if (error) {
  51. if (completionHandler) {
  52. completionHandler(nil, error);
  53. }
  54. return;
  55. }
  56. ARDJoinResponse *joinResponse = [ARDJoinResponse responseFromJSONData:data];
  57. if (!joinResponse) {
  58. if (completionHandler) {
  59. NSError *error = [[self class] badResponseError];
  60. completionHandler(nil, error);
  61. }
  62. return;
  63. }
  64. if (completionHandler) {
  65. completionHandler(joinResponse, nil);
  66. }
  67. }];
  68. }
  69. - (void)sendMessage:(ARDSignalingMessage *)message
  70. forRoomId:(NSString *)roomId
  71. clientId:(NSString *)clientId
  72. completionHandler:(void (^)(ARDMessageResponse *response,
  73. NSError *error))completionHandler {
  74. NSParameterAssert(message);
  75. NSParameterAssert(roomId.length);
  76. NSParameterAssert(clientId.length);
  77. NSData *data = [message JSONData];
  78. NSString *urlString =
  79. [NSString stringWithFormat:
  80. kARDRoomServerMessageFormat, roomId, clientId];
  81. NSURL *url = [NSURL URLWithString:urlString];
  82. RTCLog(@"C->RS POST: %@", message);
  83. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
  84. request.HTTPMethod = @"POST";
  85. request.HTTPBody = data;
  86. [NSURLConnection sendAsyncRequest:request
  87. completionHandler:^(NSURLResponse *response,
  88. NSData *data,
  89. NSError *error) {
  90. if (error) {
  91. if (completionHandler) {
  92. completionHandler(nil, error);
  93. }
  94. return;
  95. }
  96. ARDMessageResponse *messageResponse =
  97. [ARDMessageResponse responseFromJSONData:data];
  98. if (!messageResponse) {
  99. if (completionHandler) {
  100. NSError *error = [[self class] badResponseError];
  101. completionHandler(nil, error);
  102. }
  103. return;
  104. }
  105. if (completionHandler) {
  106. completionHandler(messageResponse, nil);
  107. }
  108. }];
  109. }
  110. - (void)leaveRoomWithRoomId:(NSString *)roomId
  111. clientId:(NSString *)clientId
  112. completionHandler:(void (^)(NSError *error))completionHandler {
  113. NSParameterAssert(roomId.length);
  114. NSParameterAssert(clientId.length);
  115. NSString *urlString =
  116. [NSString stringWithFormat:kARDRoomServerLeaveFormat, roomId, clientId];
  117. NSURL *url = [NSURL URLWithString:urlString];
  118. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
  119. request.HTTPMethod = @"POST";
  120. RTCLog(@"C->RS: BYE");
  121. __block NSError *error = nil;
  122. // We want a synchronous request so that we know that we've left the room on
  123. // room server before we do any further work.
  124. dispatch_semaphore_t sem = dispatch_semaphore_create(0);
  125. [NSURLConnection sendAsyncRequest:request
  126. completionHandler:^(NSURLResponse *response, NSData *data, NSError *e) {
  127. if (e) {
  128. error = e;
  129. }
  130. dispatch_semaphore_signal(sem);
  131. }];
  132. dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
  133. if (error) {
  134. RTCLogError(@"Error leaving room %@ on room server: %@", roomId, error.localizedDescription);
  135. if (completionHandler) {
  136. completionHandler(error);
  137. }
  138. return;
  139. }
  140. RTCLog(@"Left room:%@ on room server.", roomId);
  141. if (completionHandler) {
  142. completionHandler(nil);
  143. }
  144. }
  145. #pragma mark - Private
  146. + (NSError *)badResponseError {
  147. NSError *error =
  148. [[NSError alloc] initWithDomain:kARDAppEngineClientErrorDomain
  149. code:kARDAppEngineClientErrorBadResponse
  150. userInfo:@{
  151. NSLocalizedDescriptionKey: @"Error parsing response.",
  152. }];
  153. return error;
  154. }
  155. @end