-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAppDelegate.m
132 lines (111 loc) · 4.43 KB
/
AppDelegate.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//
// AppDelegate.m
// ShakeMate
//
// Copyright (c) 2010 FFFF00 Agents AB
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
#import "AppDelegate.h"
@implementation AppDelegate
@synthesize window, graphView;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// setup UDP socket
udpSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
struct sockaddr_in sin;
memset(&sin, 0, sizeof(sin));
// listen on all interfaces
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_len = sizeof(struct sockaddr_in);
sin.sin_family = AF_INET;
sin.sin_port = htons(kSeismometerPort);
bind(udpSocket, (const struct sockaddr*)&sin, sizeof(sin));
// setup threaded notification support
[self setUpThreadingSupport];
// start listening to accelerometer notifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(processNotification:)
name:@"AccelerometerNotification"
object:nil];
// create a separate thread for receiving UDP packets
networkThread = [[NSThread alloc] initWithTarget:self selector:@selector(threadLoop:) object:nil];
[networkThread start];
}
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender {
return YES;
}
- (void)setUpThreadingSupport {
if (notifications) return;
notifications = [[NSMutableArray alloc] init];
notificationLock = [[NSLock alloc] init];
notificationThread = [[NSThread currentThread] retain];
notificationPort = [[NSMachPort alloc] init];
[notificationPort setDelegate:self];
[[NSRunLoop currentRunLoop] addPort:notificationPort forMode:(NSString *)kCFRunLoopCommonModes];
}
- (void)processNotification:(NSNotification *)notification {
if([NSThread currentThread] != notificationThread) {
// Forward the notification to the correct thread, this is the socket thread
NSDate* date = [[NSDate alloc] init];
[notificationLock lock];
[notifications addObject:notification];
[notificationLock unlock];
[notificationPort sendBeforeDate:date components:nil from:nil reserved:0];
[date release];
} else {
// now we are in the main thread
measurement_t meas;
[(NSData *)[notification object] getBytes:&meas];
[graphView updateHistoryWithX:meas.x y:meas.y z:meas.z];
}
}
- (void)handleMachMessage:(void *)msg {
[notificationLock lock];
while ([notifications count]) {
NSNotification *notification = [[notifications objectAtIndex:0] retain];
[notifications removeObjectAtIndex:0];
[notificationLock unlock];
[self processNotification:notification];
[notification release];
[notificationLock lock];
}
[notificationLock unlock];
}
- (void)threadLoop:(id)object {
measurement_net_t netmeas;
while(!HELL_IS_FROZEN) {
int count = recv(udpSocket, &netmeas, sizeof(netmeas), 0);
if (count >= sizeof(measurement_net_t)) {
// got something! pass it along
measurement_t meas;
if (seis_net_type(&netmeas) != SEIS_NET_ACCELERATION) {
NSLog(@"Ignoring message %d", netmeas.type);
continue;
}
measurement_netUnpack(&netmeas, &meas);
NSString *msg = [[NSData alloc] initWithBytes:&meas length:sizeof(meas)];
[[NSNotificationCenter defaultCenter] postNotificationName:@"AccelerometerNotification" object:msg];
[msg release];
}
}
}
@end