blob: d92a4742234a40e3383037fa1cd5b3a9671c4736 [file] [log] [blame]
Alexander Afanasyev88f0b3a2013-09-24 23:52:08 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
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 Afanasyev2beff7f2013-09-27 17:50:36 -07009#include "config.h"
Alexander Afanasyev88f0b3a2013-09-24 23:52:08 -070010#import "menu-delegate.h"
11
Alexander Afanasyev2beff7f2013-09-27 17:50:36 -070012#define NDND_START_COMMAND @ NDNX_ROOT "/bin/ndndstart"
13#define NDND_STOP_COMMAND @ NDNX_ROOT "/bin/ndndstop"
14#define NDND_STATUS_COMMAND @ NDNX_ROOT "/bin/ndndstatus"
Ilya Moiseenko2dffcf52013-09-27 15:08:04 -070015
Alexander Afanasyev88f0b3a2013-09-24 23:52:08 -070016@implementation MenuDelegate
17
18- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
19{
Ilya Moiseenko6d4086c2013-09-27 16:56:02 -070020 daemonStarted = false;
21 allowSoftwareUpdates = true;
22 enableHubDiscovery = true;
23
24 NSTimer *t = [NSTimer scheduledTimerWithTimeInterval: 1.0
25 target: self
26 selector:@selector(onTick:)
27 userInfo: nil repeats:YES];
28 [[NSRunLoop mainRunLoop] addTimer:t forMode:NSRunLoopCommonModes];
Alexander Afanasyev88f0b3a2013-09-24 23:52:08 -070029}
30
Alexander Afanasyev88f0b3a2013-09-24 23:52:08 -070031-(void)awakeFromNib
32{
33 statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
Alexander Afanasyev88f0b3a2013-09-24 23:52:08 -070034 [statusItem setMenu:statusMenu];
Ilya Moiseenko350b9b52013-09-25 16:38:41 -070035 [statusItem setToolTip:@"NDN Control Center"];
36 [statusItem setEnabled:YES];
Alexander Afanasyev88f0b3a2013-09-24 23:52:08 -070037 [statusItem setHighlightMode:YES];
Ilya Moiseenko350b9b52013-09-25 16:38:41 -070038 //[statusItem setTarget:self];
39
40 NSBundle *bundle = [NSBundle bundleForClass:[self class]];
Ilya Moiseenkoa10ef8d2013-09-25 17:23:05 -070041 NSString *path = [bundle pathForResource:@"FlatDisconnected" ofType:@"png"];
Ilya Moiseenko350b9b52013-09-25 16:38:41 -070042 menuIcon = [[NSImage alloc] initWithContentsOfFile:path];
43 [statusItem setTitle:@""];
44 [statusItem setImage:menuIcon];
Ilya Moiseenkoa10ef8d2013-09-25 17:23:05 -070045
Ilya Moiseenko01264322013-09-26 15:34:21 -070046
Ilya Moiseenko01264322013-09-26 15:34:21 -070047 [connectionStatus setView: connectionStatusView];
48 [connectionStatus setTarget:self];
49 [daemonStatus setView: daemonStatusView];
50 [daemonStatus setTarget:self];
Ilya Moiseenkoa10ef8d2013-09-25 17:23:05 -070051}
52
53-(IBAction)switchDaemon:(id)sender
54{
55 if (daemonStarted)
56 {
57 daemonStarted = false;
58 [sender setTitle:@"Start"];
Ilya Moiseenko01264322013-09-26 15:34:21 -070059 [connectionStatusText setStringValue:@"Disconnected"];
Ilya Moiseenkoa10ef8d2013-09-25 17:23:05 -070060
Ilya Moiseenko2dffcf52013-09-27 15:08:04 -070061 NSTask *task = [[NSTask alloc] init];
62 [task setLaunchPath: NDND_STOP_COMMAND];
63 [task launch];
64
Ilya Moiseenkoa10ef8d2013-09-25 17:23:05 -070065 NSBundle *bundle = [NSBundle bundleForClass:[self class]];
66 NSString *path = [bundle pathForResource:@"FlatDisconnected" ofType:@"png"];
67 menuIcon = [[NSImage alloc] initWithContentsOfFile:path];
68 [statusItem setImage:menuIcon];
69 }
70 else
71 {
72 daemonStarted = true;
73 [sender setTitle:@"Stop"];
Ilya Moiseenko01264322013-09-26 15:34:21 -070074 [connectionStatusText setStringValue:@"Connected"];
Ilya Moiseenkoa10ef8d2013-09-25 17:23:05 -070075
Ilya Moiseenko2dffcf52013-09-27 15:08:04 -070076 NSTask *task = [[NSTask alloc] init];
77 [task setLaunchPath: NDND_START_COMMAND];
78 [task launch];
79
Ilya Moiseenkoa10ef8d2013-09-25 17:23:05 -070080 NSBundle *bundle = [NSBundle bundleForClass:[self class]];
81 NSString *path = [bundle pathForResource:@"FlatConnected" ofType:@"png"];
82 menuIcon = [[NSImage alloc] initWithContentsOfFile:path];
83 [statusItem setImage:menuIcon];
84 }
Ilya Moiseenko350b9b52013-09-25 16:38:41 -070085}
86
87-(IBAction)openDaemonStatus:(id)sender
88{
Ilya Moiseenko350b9b52013-09-25 16:38:41 -070089}
90
91-(IBAction)openRoutingStatusPage:(id)sender
92{
93 NSURL *pageURL = [NSURL URLWithString:@"http://netlab.cs.memphis.edu/script/htm/status.htm"];
94
95 [[NSWorkspace sharedWorkspace] openURL: pageURL];
96}
97
98-(IBAction)openTrafficMapPage:(id)sender
99{
100
101 NSURL *pageURL = [NSURL URLWithString:@"http://ndnmap.arl.wustl.edu"];
102
103 [[NSWorkspace sharedWorkspace] openURL: pageURL];
Alexander Afanasyev88f0b3a2013-09-24 23:52:08 -0700104}
105
Ilya Moiseenko01264322013-09-26 15:34:21 -0700106-(void)menu:(NSMenu *)menu willHighlightItem:(NSMenuItem *)item
107{
108
109 if( ([item view]!=nil) && (item == daemonStatus) )
110 {
111 [statusPopover showRelativeToRect:[[item view] bounds]
112 ofView:[item view]
113 preferredEdge:NSMinXEdge];
Ilya Moiseenko01264322013-09-26 15:34:21 -0700114 }
115 else
116 {
117 [statusPopover performClose:nil];
118 }
119}
120
Ilya Moiseenko6d4086c2013-09-27 16:56:02 -0700121-(void)onTick:(NSTimer *)timer
122{
123 if (daemonStarted)
124 {
125 NSTask *task = [[NSTask alloc] init];
126 [task setLaunchPath: NDND_STATUS_COMMAND];
127
128 NSPipe * out = [NSPipe pipe];
129 [task setStandardOutput:out];
130
131 [task launch];
132 [task waitUntilExit];
133
134 NSFileHandle * read = [out fileHandleForReading];
135 NSData * dataRead = [read readDataToEndOfFile];
136 NSString *stringRead = [[NSString alloc] initWithData:dataRead encoding:NSUTF8StringEncoding];
137
138 [daemonStatusText setStringValue:stringRead];
139 }
140}
141
Alexander Afanasyev88f0b3a2013-09-24 23:52:08 -0700142@end