1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #import "SettingsController.h" 18 19 #import "UIAlertView+Extensions.h" 20 #import "WALTAppDelegate.h" 21 #import "WALTClient.h" 22 23 @implementation SettingsController { 24 WALTClient *_client; 25 NSString *_status; 26 } 27 28 - (void)viewDidLoad { 29 [super viewDidLoad]; 30 31 _client = ((WALTAppDelegate *)[UIApplication sharedApplication].delegate).client; 32 } 33 34 - (void)viewWillAppear:(BOOL)animated { 35 [super viewWillAppear:animated]; 36 37 _status = [NSString string]; 38 [self.tableView reloadData]; 39 } 40 41 - (IBAction)ping:(id)sender { 42 NSTimeInterval start = _client.currentTime; 43 NSError *error = nil; 44 if (![_client sendCommand:WALTPingCommand error:&error]) { 45 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"WALT Connection Error" error:error]; 46 [alert show]; 47 } else { 48 NSData *response = [_client readResponseWithTimeout:kWALTReadTimeout]; 49 if (!response) { 50 _status = @"Timed out waiting for ping response."; 51 } else { 52 NSTimeInterval delta = _client.currentTime - start; 53 _status = [NSString stringWithFormat:@"Ping response in %.2f ms.", delta * 1000]; 54 } 55 } 56 [self.tableView reloadData]; 57 } 58 59 - (IBAction)checkDrift:(id)sender { 60 NSError *error = nil; 61 if (![_client updateSyncBoundsWithError:&error]) { 62 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"WALT Connection Error" error:error]; 63 [alert show]; 64 } else { 65 _status = [NSString stringWithFormat:@"Remote clock delayed between %lld and %lld s.", 66 _client.minError, _client.maxError]; 67 } 68 [self.tableView reloadData]; 69 } 70 71 - (NSIndexPath *)tableView:(UITableView *)tableView 72 willSelectRowAtIndexPath:(NSIndexPath *)indexPath { 73 if (indexPath.section == 0 && indexPath.row == 0) { 74 // "Ping" 75 [self ping:tableView]; 76 return nil; 77 } else if (indexPath.section == 0 && indexPath.row == 1) { 78 // "Check Drift" 79 [self checkDrift:tableView]; 80 return nil; 81 } 82 return indexPath; 83 } 84 85 - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section { 86 return (section == 0 ? _status : nil); 87 } 88 @end 89