tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

ARDAppEngineClient.m (6234B)


      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 
     11 #import "ARDAppEngineClient.h"
     12 
     13 #import "sdk/objc/base/RTCLogging.h"
     14 
     15 #import "ARDJoinResponse.h"
     16 #import "ARDMessageResponse.h"
     17 #import "ARDSignalingMessage.h"
     18 #import "ARDUtilities.h"
     19 
     20 // TODO(tkchin): move these to a configuration object.
     21 static NSString *const kARDRoomServerHostUrl = @"https://appr.tc";
     22 static NSString *const kARDRoomServerJoinFormat = @"https://appr.tc/join/%@";
     23 static NSString *const kARDRoomServerJoinFormatLoopback =
     24    @"https://appr.tc/join/%@?debug=loopback";
     25 static NSString *const kARDRoomServerMessageFormat =
     26    @"https://appr.tc/message/%@/%@";
     27 static NSString *const kARDRoomServerLeaveFormat =
     28    @"https://appr.tc/leave/%@/%@";
     29 
     30 static NSString *const kARDAppEngineClientErrorDomain = @"ARDAppEngineClient";
     31 static NSInteger const kARDAppEngineClientErrorBadResponse = -1;
     32 
     33 @implementation ARDAppEngineClient
     34 
     35 #pragma mark - ARDRoomServerClient
     36 
     37 - (void)joinRoomWithRoomId:(NSString *)roomId
     38                isLoopback:(BOOL)isLoopback
     39         completionHandler:(void (^)(ARDJoinResponse *response,
     40                                     NSError *error))completionHandler {
     41  NSParameterAssert(roomId.length);
     42 
     43  NSString *urlString = nil;
     44  if (isLoopback) {
     45    urlString =
     46        [NSString stringWithFormat:kARDRoomServerJoinFormatLoopback, roomId];
     47  } else {
     48    urlString = [NSString stringWithFormat:kARDRoomServerJoinFormat, roomId];
     49  }
     50 
     51  NSURL *roomURL = [NSURL URLWithString:urlString];
     52  RTCLog(@"Joining room:%@ on room server.", roomId);
     53  NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:roomURL];
     54  request.HTTPMethod = @"POST";
     55  [NSURLConnection
     56       sendAsyncRequest:request
     57      completionHandler:^(
     58          NSURLResponse *response __unused, NSData *data, NSError *error) {
     59        if (error) {
     60          if (completionHandler) {
     61            completionHandler(nil, error);
     62          }
     63          return;
     64        }
     65        ARDJoinResponse *joinResponse =
     66            [ARDJoinResponse responseFromJSONData:data];
     67        if (!joinResponse) {
     68          if (completionHandler) {
     69            NSError *err = [[self class] badResponseError];
     70            completionHandler(nil, err);
     71          }
     72          return;
     73        }
     74        if (completionHandler) {
     75          completionHandler(joinResponse, nil);
     76        }
     77      }];
     78 }
     79 
     80 - (void)sendMessage:(ARDSignalingMessage *)message
     81            forRoomId:(NSString *)roomId
     82             clientId:(NSString *)clientId
     83    completionHandler:(void (^)(ARDMessageResponse *response,
     84                                NSError *error))completionHandler {
     85  NSParameterAssert(message);
     86  NSParameterAssert(roomId.length);
     87  NSParameterAssert(clientId.length);
     88 
     89  NSData *messageData = [message JSONData];
     90  NSString *urlString =
     91      [NSString stringWithFormat:kARDRoomServerMessageFormat, roomId, clientId];
     92  NSURL *url = [NSURL URLWithString:urlString];
     93  RTCLog(@"C->RS POST: %@", message);
     94  NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
     95  request.HTTPMethod = @"POST";
     96  request.HTTPBody = messageData;
     97  [NSURLConnection sendAsyncRequest:request
     98                  completionHandler:^(NSURLResponse *response __unused,
     99                                      NSData *data,
    100                                      NSError *responseError) {
    101                    if (responseError) {
    102                      if (completionHandler) {
    103                        completionHandler(nil, responseError);
    104                      }
    105                      return;
    106                    }
    107                    ARDMessageResponse *messageResponse =
    108                        [ARDMessageResponse responseFromJSONData:data];
    109                    if (!messageResponse) {
    110                      if (completionHandler) {
    111                        NSError *err = [[self class] badResponseError];
    112                        completionHandler(nil, err);
    113                      }
    114                      return;
    115                    }
    116                    if (completionHandler) {
    117                      completionHandler(messageResponse, nil);
    118                    }
    119                  }];
    120 }
    121 
    122 - (void)leaveRoomWithRoomId:(NSString *)roomId
    123                   clientId:(NSString *)clientId
    124          completionHandler:(void (^)(NSError *error))completionHandler {
    125  NSParameterAssert(roomId.length);
    126  NSParameterAssert(clientId.length);
    127 
    128  NSString *urlString =
    129      [NSString stringWithFormat:kARDRoomServerLeaveFormat, roomId, clientId];
    130  NSURL *url = [NSURL URLWithString:urlString];
    131  NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    132  request.HTTPMethod = @"POST";
    133 
    134  RTCLog(@"C->RS: BYE");
    135  __block NSError *error = nil;
    136 
    137  // We want a synchronous request so that we know that we've left the room on
    138  // room server before we do any further work.
    139  dispatch_semaphore_t sem = dispatch_semaphore_create(0);
    140  [NSURLConnection
    141       sendAsyncRequest:request
    142      completionHandler:^(
    143          NSURLResponse *response __unused, NSData *data __unused, NSError *e) {
    144        if (e) {
    145          error = e;
    146        }
    147        dispatch_semaphore_signal(sem);
    148      }];
    149 
    150  dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
    151  if (error) {
    152    RTCLogError(@"Error leaving room %@ on room server: %@",
    153                roomId,
    154                error.localizedDescription);
    155    if (completionHandler) {
    156      completionHandler(error);
    157    }
    158    return;
    159  }
    160  RTCLog(@"Left room:%@ on room server.", roomId);
    161  if (completionHandler) {
    162    completionHandler(nil);
    163  }
    164 }
    165 
    166 #pragma mark - Private
    167 
    168 + (NSError *)badResponseError {
    169  NSError *error = [[NSError alloc]
    170      initWithDomain:kARDAppEngineClientErrorDomain
    171                code:kARDAppEngineClientErrorBadResponse
    172            userInfo:@{
    173              NSLocalizedDescriptionKey : @"Error parsing response.",
    174            }];
    175  return error;
    176 }
    177 
    178 @end