Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Domino style menu to show approximate ping times at a glance #6

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added dominos.psd
Binary file not shown.
2 changes: 1 addition & 1 deletion iconping/iconping-Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<key>CFBundleIconFile</key>
<string>world_icon.icns</string>
<key>CFBundleIdentifier</key>
<string>com.antirez.${PRODUCT_NAME:rfc1034identifier}</string>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
Expand Down
2 changes: 1 addition & 1 deletion iconping/iconpingAppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@interface iconpingAppDelegate : NSObject <NSApplicationDelegate> {
NSWindow *window;
NSStatusItem *myStatusItem;
NSImage *myStatusImageOK, *myStatusImageSLOW, *myStatusImageKO;
NSImage *ping1, *ping2, *ping3, *ping4, *ping5, *ping6, *ping7, *ping8, *ping9, *pingError;
NSMenu *myMenu;
NSMenuItem *statusMenuItem, *openAtStartupMenuItem;
uint16_t icmp_id;
Expand Down
63 changes: 45 additions & 18 deletions iconping/iconpingAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ @implementation iconpingAppDelegate
#define ICMP_TYPE_ECHO_REPLY 0
#define ICMP_TYPE_ECHO_REQUEST 8

#define CONN_STATE_KO 0
#define CONN_STATE_SLOW 1
#define CONN_STATE_OK 2
#define CONN_STATE_KO -1

/* This is the standard BSD checksum code, modified to use modern types. */
static uint16_t in_cksum(const void *buffer, size_t bufferLen)
Expand Down Expand Up @@ -150,7 +148,7 @@ - (void) receivePing {
return;
}

NSLog(@"OK received an ICMP packet that matches!\n");
NSLog(@"OK received an ICMP packet that matches.\n");
if (reply->sentTime > last_received_time) {
last_rtt = (int)(ustime()-reply->sentTime)/1000;
last_received_time = reply->sentTime;
Expand All @@ -168,7 +166,7 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
[self methodSignatureForSelector:@selector(timerHandler:)]];
[invocation setTarget:self];
[invocation setSelector:@selector(timerHandler:)];
[[NSRunLoop mainRunLoop] addTimer:[NSTimer timerWithTimeInterval:0.1 invocation:invocation repeats:YES] forMode:NSRunLoopCommonModes];
[[NSRunLoop mainRunLoop] addTimer:[NSTimer timerWithTimeInterval:0.05 invocation:invocation repeats:YES] forMode:NSRunLoopCommonModes];

myMenu = [[NSMenu alloc] initWithTitle:@"Menu Title"];
NSMenuItem *menuItem = [[NSMenuItem alloc] initWithTitle:@"Quit Icon Ping" action:@selector(exitAction) keyEquivalent:@"q"];
Expand All @@ -187,10 +185,18 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification

myStatusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength] retain];

myStatusImageOK = [[NSImage alloc] initWithContentsOfFile: [bundle pathForResource:@"iconok" ofType:@"png"]];
myStatusImageSLOW = [[NSImage alloc] initWithContentsOfFile: [bundle pathForResource:@"iconslow" ofType:@"png"]];
myStatusImageKO = [[NSImage alloc] initWithContentsOfFile: [bundle pathForResource:@"iconko" ofType:@"png"]];
[myStatusItem setImage:myStatusImageKO];
ping1 = [NSImage imageNamed:@"ping_1"];
ping2 = [NSImage imageNamed:@"ping_2"];
ping3 = [NSImage imageNamed:@"ping_3"];
ping4 = [NSImage imageNamed:@"ping_4"];
ping5 = [NSImage imageNamed:@"ping_5"];
ping6 = [NSImage imageNamed:@"ping_6"];
ping7 = [NSImage imageNamed:@"ping_7"];
ping8 = [NSImage imageNamed:@"ping_8"];
ping9 = [NSImage imageNamed:@"ping_9"];
pingError = [NSImage imageNamed:@"ping_error"];

[myStatusItem setImage:pingError];
[myStatusItem setMenu: myMenu];
[self changeConnectionState: CONN_STATE_KO];

Expand All @@ -217,12 +223,11 @@ - (void) timerHandler: (NSTimer *) t

/* Update the current state accordingly */
elapsed = (ustime() - last_received_time)/1000; /* in milliseconds */
//NSLog(@"Ping took %d\n", (int)last_rtt);
if (elapsed > 3000) {
state = CONN_STATE_KO;
} else if (last_rtt < 300) {
state = CONN_STATE_OK;
} else {
state = CONN_STATE_SLOW;
state = (int)((last_rtt + 10) / 100);
}
if (state != connection_state) {
[self changeConnectionState: state];
Expand All @@ -231,15 +236,37 @@ - (void) timerHandler: (NSTimer *) t

- (void) changeConnectionState: (int) state
{
//NSLog(@"State change to %d\n", state);
if (state == CONN_STATE_KO) {
[myStatusItem setImage:myStatusImageKO];
[myStatusItem setImage:pingError];
[statusMenuItem setTitle:@"No Connection!"];
} else if (state == CONN_STATE_OK) {
[myStatusItem setImage:myStatusImageOK];
} else if (state <= 1) {
[myStatusItem setImage:ping1];
[statusMenuItem setTitle:@"Connection OK"];
} else if (state == 2) {
[myStatusItem setImage:ping2];
[statusMenuItem setTitle:@"Connection OK"];
} else if (state == 3) {
[myStatusItem setImage:ping3];
[statusMenuItem setTitle:@"Connection OK"];
} else if (state == 4) {
[myStatusItem setImage:ping4];
[statusMenuItem setTitle:@"Connection OK"];
} else if (state == 5) {
[myStatusItem setImage:ping5];
[statusMenuItem setTitle:@"Connection OK"];
} else if (state == 6) {
[myStatusItem setImage:ping6];
[statusMenuItem setTitle:@"Connection OK"];
} else if (state == 7) {
[myStatusItem setImage:ping7];
[statusMenuItem setTitle:@"Connection OK"];
} else if (state == 8) {
[myStatusItem setImage:ping8];
[statusMenuItem setTitle:@"Connection OK"];
} else if (state == 9) {
[myStatusItem setImage:ping9];
[statusMenuItem setTitle:@"Connection OK"];
} else if (state == CONN_STATE_SLOW) {
[myStatusItem setImage:myStatusImageSLOW];
[statusMenuItem setTitle:@"Connection is slow"];
}
connection_state = state;
}
Expand Down
Binary file added iconping/ping_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added iconping/ping_1@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added iconping/ping_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added iconping/ping_2@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added iconping/ping_3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added iconping/ping_3@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added iconping/ping_4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added iconping/ping_4@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added iconping/ping_5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added iconping/ping_5@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added iconping/ping_6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added iconping/ping_6@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added iconping/ping_7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added iconping/ping_7@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added iconping/ping_8.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added iconping/ping_8@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added iconping/ping_9.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added iconping/ping_9@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added iconping/ping_error.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added iconping/ping_error@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added iconping@2x.psd
Binary file not shown.