Alexander Afanasyev | 14b0948 | 2013-10-11 18:24:45 +0200 | [diff] [blame] | 1 | /* -*- Mode: obj; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
Alexander Afanasyev | 88f0b3a | 2013-09-24 23:52:08 -0700 | [diff] [blame] | 2 | /* |
| 3 | * @copyright See LICENCE for copyright and license information. |
| 4 | * |
| 5 | * @author Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 6 | * @author Ilya Moiseenko <iliamo@ucla.edu> |
| 7 | */ |
| 8 | |
Alexander Afanasyev | 2beff7f | 2013-09-27 17:50:36 -0700 | [diff] [blame] | 9 | #include "config.h" |
Alexander Afanasyev | 88f0b3a | 2013-09-24 23:52:08 -0700 | [diff] [blame] | 10 | #import "menu-delegate.h" |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame] | 11 | #import "ndnd-status-operation.h" |
Ilya Moiseenko | 0c9ab7c | 2013-10-29 15:08:23 -0700 | [diff] [blame] | 12 | #import "tight-menu-item-view.h" |
Ilya Moiseenko | 2dffcf5 | 2013-09-27 15:08:04 -0700 | [diff] [blame] | 13 | |
Alexander Afanasyev | 88f0b3a | 2013-09-24 23:52:08 -0700 | [diff] [blame] | 14 | @implementation MenuDelegate |
| 15 | |
Alexander Afanasyev | 506d0ab | 2013-10-29 19:18:10 -0700 | [diff] [blame^] | 16 | @synthesize interestSent; |
| 17 | @synthesize interestRecv; |
| 18 | @synthesize dataSent; |
| 19 | @synthesize dataRecv; |
| 20 | |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame] | 21 | -(id)init |
| 22 | { |
| 23 | if (![super init]) { |
| 24 | return nil; |
| 25 | } |
| 26 | |
Alexander Afanasyev | 506d0ab | 2013-10-29 19:18:10 -0700 | [diff] [blame^] | 27 | interestSent = @"N/A"; |
| 28 | interestRecv = @"N/A"; |
| 29 | dataSent = @"N/A"; |
| 30 | dataRecv = @"N/A"; |
| 31 | |
Alexander Afanasyev | b9024d1 | 2013-10-14 18:10:18 +0300 | [diff] [blame] | 32 | m_autoconfInProgress = false; |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame] | 33 | m_operationQueue = [[NSOperationQueue alloc] init]; |
| 34 | return self; |
| 35 | } |
| 36 | |
Alexander Afanasyev | 88f0b3a | 2013-09-24 23:52:08 -0700 | [diff] [blame] | 37 | - (void)applicationDidFinishLaunching:(NSNotification *)aNotification |
| 38 | { |
Alexander Afanasyev | 1d794ce | 2013-10-22 12:48:30 -0700 | [diff] [blame] | 39 | // Register the preference defaults early. |
| 40 | NSDictionary *appDefaults = |
| 41 | [NSDictionary dictionaryWithObjectsAndKeys: |
| 42 | [NSNumber numberWithBool:YES], @"allowSoftwareUpdates", |
| 43 | [NSNumber numberWithBool:YES], @"enableHubDiscovery", |
| 44 | [NSNumber numberWithBool:NO], @"shutdownNdndOnExit", |
| 45 | nil |
| 46 | ]; |
| 47 | [[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults]; |
| 48 | |
| 49 | // Other initialization... |
| 50 | |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame] | 51 | m_daemonStarted = false; |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame] | 52 | |
| 53 | NSBundle *bundle = [NSBundle bundleForClass:[self class]]; |
| 54 | m_connectedIcon = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"FlatConnected" ofType:@"png"]]; |
| 55 | m_disconnectedIcon = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"FlatDisconnected" ofType:@"png"]]; |
| 56 | m_statusXslt = [NSData dataWithContentsOfFile:[bundle pathForResource:@"status" ofType:@"xslt"]]; |
Ilya Moiseenko | b4aca05 | 2013-10-06 15:10:19 -0700 | [diff] [blame] | 57 | m_statusToFibXslt = [NSData dataWithContentsOfFile:[bundle pathForResource:@"status-to-fib" ofType:@"xslt"]]; |
Ilya Moiseenko | 6d4086c | 2013-09-27 16:56:02 -0700 | [diff] [blame] | 58 | |
Alexander Afanasyev | b9024d1 | 2013-10-14 18:10:18 +0300 | [diff] [blame] | 59 | [NSTimer scheduledTimerWithTimeInterval: 1.0 |
| 60 | target: self |
| 61 | selector:@selector(onTick:) |
| 62 | userInfo: nil |
| 63 | repeats:YES]; |
Alexander Afanasyev | 4e1c7e9 | 2013-10-11 18:25:09 +0200 | [diff] [blame] | 64 | [self updateStatus]; |
Alexander Afanasyev | 1792287 | 2013-10-15 11:45:58 +0300 | [diff] [blame] | 65 | |
| 66 | m_systemEvents = [[SystemEvents alloc] init]; |
| 67 | } |
| 68 | |
| 69 | - (void)applicationWillTerminate:(NSNotification *)aNotification |
| 70 | { |
| 71 | [m_systemEvents disable]; |
Alexander Afanasyev | 88f0b3a | 2013-09-24 23:52:08 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Alexander Afanasyev | 88f0b3a | 2013-09-24 23:52:08 -0700 | [diff] [blame] | 74 | -(void)awakeFromNib |
| 75 | { |
| 76 | statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength]; |
Alexander Afanasyev | 88f0b3a | 2013-09-24 23:52:08 -0700 | [diff] [blame] | 77 | [statusItem setMenu:statusMenu]; |
Ilya Moiseenko | 350b9b5 | 2013-09-25 16:38:41 -0700 | [diff] [blame] | 78 | [statusItem setToolTip:@"NDN Control Center"]; |
| 79 | [statusItem setEnabled:YES]; |
Alexander Afanasyev | 88f0b3a | 2013-09-24 23:52:08 -0700 | [diff] [blame] | 80 | [statusItem setHighlightMode:YES]; |
Ilya Moiseenko | 350b9b5 | 2013-09-25 16:38:41 -0700 | [diff] [blame] | 81 | //[statusItem setTarget:self]; |
| 82 | |
Ilya Moiseenko | 350b9b5 | 2013-09-25 16:38:41 -0700 | [diff] [blame] | 83 | [statusItem setTitle:@""]; |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame] | 84 | [statusItem setImage:m_disconnectedIcon]; |
Ilya Moiseenko | 0126432 | 2013-09-26 15:34:21 -0700 | [diff] [blame] | 85 | |
Ilya Moiseenko | 0c9ab7c | 2013-10-29 15:08:23 -0700 | [diff] [blame] | 86 | float menuItemHeight = 20; |
| 87 | |
| 88 | NSRect viewRect = NSMakeRect(0, 0, /* width autoresizes */ 1, menuItemHeight); |
| 89 | connectionStatusView = [[TightMenuItemView alloc] initWithFrame:viewRect]; |
| 90 | connectionStatusView.autoresizingMask = NSViewWidthSizable; |
| 91 | |
| 92 | [connectionStatus setView:connectionStatusView]; |
Ilya Moiseenko | 0126432 | 2013-09-26 15:34:21 -0700 | [diff] [blame] | 93 | [connectionStatus setTarget:self]; |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame] | 94 | |
Ilya Moiseenko | 0126432 | 2013-09-26 15:34:21 -0700 | [diff] [blame] | 95 | [daemonStatus setView: daemonStatusView]; |
| 96 | [daemonStatus setTarget:self]; |
Ilya Moiseenko | a10ef8d | 2013-09-25 17:23:05 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Ilya Moiseenko | 350b9b5 | 2013-09-25 16:38:41 -0700 | [diff] [blame] | 99 | -(IBAction)openDaemonStatus:(id)sender |
| 100 | { |
Ilya Moiseenko | 350b9b5 | 2013-09-25 16:38:41 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Ilya Moiseenko | e7058e7 | 2013-10-02 15:56:45 -0700 | [diff] [blame] | 103 | -(IBAction)showExitConfirmationWindow:(id)sender |
Ilya Moiseenko | 350b9b5 | 2013-09-25 16:38:41 -0700 | [diff] [blame] | 104 | { |
Alexander Afanasyev | 1d794ce | 2013-10-22 12:48:30 -0700 | [diff] [blame] | 105 | if ([[NSUserDefaults standardUserDefaults] boolForKey:@"shutdownNdndOnExit"]) { |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame] | 106 | [m_operationQueue cancelAllOperations]; |
Alexander Afanasyev | 4e1c7e9 | 2013-10-11 18:25:09 +0200 | [diff] [blame] | 107 | |
| 108 | [m_operationQueue addOperationWithBlock:^{ |
| 109 | NSTask *task = [[NSTask alloc] init]; |
| 110 | [task setLaunchPath: @NDND_STOP_COMMAND]; |
| 111 | [task launch]; |
| 112 | [task waitUntilExit]; |
| 113 | }]; |
| 114 | |
| 115 | [m_operationQueue waitUntilAllOperationsAreFinished]; |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame] | 116 | [NSApp terminate:self]; |
Alexander Afanasyev | 1d794ce | 2013-10-22 12:48:30 -0700 | [diff] [blame] | 117 | } |
| 118 | else { |
| 119 | NSAlert *alert = [[NSAlert alloc] init]; |
| 120 | [alert addButtonWithTitle:@"Yes"]; |
| 121 | [alert addButtonWithTitle:@"No"]; |
| 122 | [alert addButtonWithTitle:@"Cancel"]; |
| 123 | [alert setMessageText:@"Shutdown NDN daemon as well?"]; |
| 124 | [alert setInformativeText:@"All NDN operations will be become unavailable."]; |
| 125 | [alert setAlertStyle:NSCriticalAlertStyle]; |
| 126 | // [alert setShowsSuppressionButton: YES]; |
| 127 | |
| 128 | NSInteger res = [alert runModal]; |
| 129 | if (res == NSAlertFirstButtonReturn) { |
| 130 | // "YES" stop ndnd |
| 131 | [m_operationQueue cancelAllOperations]; |
| 132 | |
| 133 | [m_operationQueue addOperationWithBlock:^{ |
| 134 | NSTask *task = [[NSTask alloc] init]; |
| 135 | [task setLaunchPath: @NDND_STOP_COMMAND]; |
| 136 | [task launch]; |
| 137 | [task waitUntilExit]; |
| 138 | }]; |
| 139 | |
| 140 | [m_operationQueue waitUntilAllOperationsAreFinished]; |
| 141 | [NSApp terminate:self]; |
| 142 | } else if (res == NSAlertSecondButtonReturn) { |
| 143 | // "NO" terminate app but keep ndnd running |
| 144 | [m_operationQueue cancelAllOperations]; |
| 145 | [NSApp terminate:self]; |
| 146 | } |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame] | 147 | } |
Alexander Afanasyev | 88f0b3a | 2013-09-24 23:52:08 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Ilya Moiseenko | 6d4086c | 2013-09-27 16:56:02 -0700 | [diff] [blame] | 150 | -(void)onTick:(NSTimer *)timer |
| 151 | { |
Alexander Afanasyev | 4e1c7e9 | 2013-10-11 18:25:09 +0200 | [diff] [blame] | 152 | [self updateStatus]; |
| 153 | } |
| 154 | |
| 155 | -(void)updateStatus |
| 156 | { |
| 157 | NSOperation *operation = [[NdndStatusOperation alloc] initWithDelegate:self]; |
| 158 | [m_operationQueue addOperation:operation]; |
| 159 | } |
| 160 | |
| 161 | -(void)updateStatusWithDependency:(NSOperation*)dependency |
| 162 | { |
| 163 | NSOperation *operation = [[NdndStatusOperation alloc] initWithDelegate:self]; |
| 164 | [operation addDependency:dependency]; |
| 165 | |
| 166 | [m_operationQueue addOperation:dependency]; |
| 167 | [m_operationQueue addOperation:operation]; |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | - (void)statusUpdated:(NSXMLDocument*)document |
| 171 | { |
| 172 | if (!m_daemonStarted) { |
| 173 | m_daemonStarted = true; |
Ilya Moiseenko | 0c9ab7c | 2013-10-29 15:08:23 -0700 | [diff] [blame] | 174 | [connectionStatusView setStatus:@"Active"]; |
Ilya Moiseenko | 6d4086c | 2013-09-27 16:56:02 -0700 | [diff] [blame] | 175 | |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame] | 176 | [statusItem setImage:m_connectedIcon]; |
Ilya Moiseenko | 6d4086c | 2013-09-27 16:56:02 -0700 | [diff] [blame] | 177 | } |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame] | 178 | |
Ilya Moiseenko | b4aca05 | 2013-10-06 15:10:19 -0700 | [diff] [blame] | 179 | NSXMLDocument *statusXml = [document objectByApplyingXSLT:m_statusXslt |
| 180 | arguments:nil |
| 181 | error:nil]; |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame] | 182 | |
Ilya Moiseenko | b4aca05 | 2013-10-06 15:10:19 -0700 | [diff] [blame] | 183 | NSXMLDocument *statusFibXml = [document objectByApplyingXSLT:m_statusToFibXslt |
| 184 | arguments:nil |
| 185 | error:nil]; |
Alexander Afanasyev | 506d0ab | 2013-10-29 19:18:10 -0700 | [diff] [blame^] | 186 | |
| 187 | NSXMLNode *element = [[statusXml rootElement] childAtIndex:0]; //data |
| 188 | [self setDataRecv:[[element childAtIndex:0] stringValue]]; |
| 189 | [self setDataSent:[[element childAtIndex:1] stringValue]]; |
| 190 | |
| 191 | element = [[statusXml rootElement] childAtIndex:1]; //interests |
| 192 | [self setInterestRecv:[[element childAtIndex:0] stringValue]]; |
| 193 | [self setInterestSent:[[element childAtIndex:1] stringValue]]; |
Ilya Moiseenko | b4aca05 | 2013-10-06 15:10:19 -0700 | [diff] [blame] | 194 | |
| 195 | [preferencesDelegate updateFibStatus:statusFibXml]; |
Alexander Afanasyev | b9024d1 | 2013-10-14 18:10:18 +0300 | [diff] [blame] | 196 | |
Alexander Afanasyev | 1d794ce | 2013-10-22 12:48:30 -0700 | [diff] [blame] | 197 | if ([[NSUserDefaults standardUserDefaults] boolForKey:@"enableHubDiscovery"]) { |
| 198 | NSArray *autoconf = [[statusFibXml rootElement] nodesForXPath:@"//fib/prefix[text()='ndn:/autoconf-route']" error:nil]; |
| 199 | if ([autoconf count] == 0) |
| 200 | { |
| 201 | [self restartDaemon:nil]; |
| 202 | } |
| 203 | } |
Ilya Moiseenko | 6d4086c | 2013-09-27 16:56:02 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame] | 206 | - (void)statusUnavailable:(id)none |
Ilya Moiseenko | e7058e7 | 2013-10-02 15:56:45 -0700 | [diff] [blame] | 207 | { |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame] | 208 | // try start ndnd if it is not started yet |
| 209 | if (m_daemonStarted) { |
| 210 | m_daemonStarted = false; |
| 211 | |
Ilya Moiseenko | 0c9ab7c | 2013-10-29 15:08:23 -0700 | [diff] [blame] | 212 | [connectionStatusView setStatus:@"Starting..."]; |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame] | 213 | |
| 214 | [statusItem setImage:m_disconnectedIcon]; |
| 215 | } |
Ilya Moiseenko | b4aca05 | 2013-10-06 15:10:19 -0700 | [diff] [blame] | 216 | |
Alexander Afanasyev | 506d0ab | 2013-10-29 19:18:10 -0700 | [diff] [blame^] | 217 | [self setInterestSent:@"N/A"]; |
| 218 | [self setInterestRecv:@"N/A"]; |
| 219 | [self setDataSent:@"N/A"]; |
| 220 | [self setDataRecv:@"N/A"]; |
| 221 | |
Ilya Moiseenko | b4aca05 | 2013-10-06 15:10:19 -0700 | [diff] [blame] | 222 | [preferencesDelegate updateFibStatus:nil]; |
| 223 | |
Alexander Afanasyev | b9024d1 | 2013-10-14 18:10:18 +0300 | [diff] [blame] | 224 | m_autoconfInProgress = true; |
| 225 | |
| 226 | NSOperation *startOp = [NSBlockOperation blockOperationWithBlock:^{ |
Alexander Afanasyev | 6cacb97 | 2013-10-03 23:39:06 -0700 | [diff] [blame] | 227 | NSTask *task = [[NSTask alloc] init]; |
| 228 | [task setLaunchPath: @NDND_START_COMMAND]; |
| 229 | [task launch]; |
| 230 | }]; |
Alexander Afanasyev | b9024d1 | 2013-10-14 18:10:18 +0300 | [diff] [blame] | 231 | |
Alexander Afanasyev | 1d794ce | 2013-10-22 12:48:30 -0700 | [diff] [blame] | 232 | if ([[NSUserDefaults standardUserDefaults] boolForKey:@"enableHubDiscovery"]) { |
| 233 | NSOperation *autoconfOp = [NSBlockOperation blockOperationWithBlock:^{ |
| 234 | NSTask *task = [[NSTask alloc] init]; |
| 235 | [task setLaunchPath: @NDND_AUTOCONFIG_COMMAND]; |
| 236 | [task launch]; |
| 237 | [task waitUntilExit]; |
Alexander Afanasyev | b9024d1 | 2013-10-14 18:10:18 +0300 | [diff] [blame] | 238 | |
Alexander Afanasyev | 1d794ce | 2013-10-22 12:48:30 -0700 | [diff] [blame] | 239 | m_autoconfInProgress = false; |
| 240 | }]; |
Alexander Afanasyev | b9024d1 | 2013-10-14 18:10:18 +0300 | [diff] [blame] | 241 | |
Alexander Afanasyev | 1d794ce | 2013-10-22 12:48:30 -0700 | [diff] [blame] | 242 | [autoconfOp addDependency:startOp]; |
| 243 | [m_operationQueue addOperation:autoconfOp]; |
| 244 | } |
Alexander Afanasyev | b9024d1 | 2013-10-14 18:10:18 +0300 | [diff] [blame] | 245 | |
| 246 | [m_operationQueue addOperation:startOp]; |
Alexander Afanasyev | b9024d1 | 2013-10-14 18:10:18 +0300 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | -(void)restartDaemon:(id)none |
| 250 | { |
Alexander Afanasyev | 1d794ce | 2013-10-22 12:48:30 -0700 | [diff] [blame] | 251 | if (![[NSUserDefaults standardUserDefaults] boolForKey:@"enableHubDiscovery"]) |
| 252 | return; |
| 253 | |
Alexander Afanasyev | b9024d1 | 2013-10-14 18:10:18 +0300 | [diff] [blame] | 254 | if (m_autoconfInProgress) |
| 255 | return; |
| 256 | |
Alexander Afanasyev | 1792287 | 2013-10-15 11:45:58 +0300 | [diff] [blame] | 257 | NSLog (@"No automatically detected route configured, trying to get one"); |
| 258 | |
Alexander Afanasyev | b9024d1 | 2013-10-14 18:10:18 +0300 | [diff] [blame] | 259 | m_autoconfInProgress = true; |
| 260 | [m_operationQueue addOperationWithBlock:^{ |
| 261 | NSTask *task = [[NSTask alloc] init]; |
| 262 | [task setLaunchPath: @NDND_AUTOCONFIG_COMMAND]; |
| 263 | [task launch]; |
| 264 | [task waitUntilExit]; |
| 265 | |
| 266 | m_autoconfInProgress = false; |
| 267 | }]; |
Ilya Moiseenko | e7058e7 | 2013-10-02 15:56:45 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Alexander Afanasyev | 88f0b3a | 2013-09-24 23:52:08 -0700 | [diff] [blame] | 270 | @end |