-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathPingTestOSCManager.m
More file actions
166 lines (146 loc) · 5.87 KB
/
Copy pathPingTestOSCManager.m
File metadata and controls
166 lines (146 loc) · 5.87 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//
// PingTestOSCManager.m
// VVOpenSource
//
// Created by bagheera on 3/29/13.
//
//
#import "PingTestOSCManager.h"
@implementation PingTestOSCManager
- (void) _generalInit {
[super _generalInit];
[self setInPortLabelBase:@"OSC Ping Test"];
NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
inPort = [self createNewInput];
ignoreReceivedVals = NO;
swatch = [[VVStopwatch alloc] init];
NSString *tmpString = nil;
NSNumber *tmpNum = nil;
// if there's a saved default for the IP address/port, put 'em in the fields
tmpString = [def objectForKey:@"IPAddress"];
tmpNum = [def objectForKey:@"Port"];
if (tmpString!=nil && tmpNum!=nil)
outPort = [self createNewOutputToAddress:tmpString atPort:[tmpNum intValue] withLabel:@"OSC Ping Test"];
else
outPort = [self createNewOutputToAddress:@"127.0.0.1" atPort:[inPort port] withLabel:@"OSC Ping Test"];
}
- (void) awakeFromNib {
//NSLog(@"%s",__func__);
// push the out port's values to the text fields
[ipField setStringValue:[outPort addressString]];
[portField setIntValue:[outPort port]];
// populate the text field with the IP address & port of this machine
NSArray *ips = [OSCManager hostIPv4Addresses];
if (ips!=nil)
[networkAddressField setStringValue:[NSString stringWithFormat:@"%@, port %hd",[ips objectAtIndex:0],[inPort port]]];
// fake an outputs-changed notification to make sure my list of destinations updates (in case it refreshes before i'm awake)
[self oscOutputsChangedNotification:nil];
// register to receive notifications that the list of osc outputs has changed
//[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(oscOutputsChangedNotification:) name:VVOSCOutPortsChangedNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(oscOutputsChangedNotification:) name:OSCOutPortsChangedNotification object:nil];
}
/*
- (NSString *) inPortLabelBase {
return [NSString stringWithString:@"MIDI via OSC"];
}
*/
/*
- (OSCOutPort *) createNewOutputToAddress:(NSString *)a atPort:(int)p withLabel:(NSString *)l {
OSCOutPort *returnMe = [super createNewOutputToAddress:a atPort:p withLabel:l];
// post a notification that the ports have been changed and need to be reloaded
[[NSNotificationCenter defaultCenter] postNotificationName:VVOSCOutPortsChangedNotification object:nil];
return returnMe;
}
- (void) removeInput:(id)p {
[super removeInput:p];
// post a notification that the ports have been changed and need to be reloaded
[[NSNotificationCenter defaultCenter] postNotificationName:VVOSCOutPortsChangedNotification object:nil];
}
- (void) removeOutput:(id)p {
[super removeOutput:p];
// post a notification that the ports have been changed and need to be reloaded
[[NSNotificationCenter defaultCenter] postNotificationName:VVOSCOutPortsChangedNotification object:nil];
}
*/
- (void) oscOutputsChangedNotification:(NSNotification *)note {
//NSLog(@"%s",__func__);
NSMutableArray *portLabelArray = nil;
// remove the items in the pop-up button
[outputDestinationButton removeAllItems];
// get an array of the out port labels
portLabelArray = [[[self outPortLabelArray] mutableCopy] autorelease];
// remove the output corresponding to my out port
[portLabelArray removeObject:@"OSC Ping Test"];
// push the labels to the pop-up button of destinations
[outputDestinationButton addItemsWithTitles:portLabelArray];
}
- (IBAction) outputDestinationButtonUsed:(id)sender {
//NSLog(@"%s",__func__);
OSCOutPort *selectedPort = nil;
selectedPort = [self findOutputWithLabel:[outputDestinationButton titleOfSelectedItem]];
if (selectedPort == nil)
return;
// push the data of the selected output to the fields
[ipField setStringValue:[selectedPort addressString]];
[portField setStringValue:[NSString stringWithFormat:@"%d",[selectedPort port]]];
// bump the fields (which updates the manualOutPort, which is the only out port sending data)
[self setupFieldUsed:nil];
}
- (IBAction) setupFieldUsed:(id)sender {
//NSLog(@"%s",__func__);
// now take care of the ports which relate to sending data
// push the settings on the ui items to the manualOutPort, which is the only out port actually sending data
[outPort setAddressString:[ipField stringValue]];
[ipField setStringValue:[outPort addressString]];
[outPort setPort:[portField intValue]];
[portField setIntValue:[outPort port]];
// update the user defaults
NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
[def setObject:[outPort addressString] forKey:@"IPAddress"];
[def setObject:[NSNumber numberWithInt:[outPort port]] forKey:@"Port"];
[def synchronize];
}
- (IBAction) pingClicked:(id)sender {
NSLog(@"%s",__func__);
OSCMessage *msg = [OSCMessage createWithAddress:@"/a"];
[msg addBOOL:YES];
ignoreReceivedVals = YES;
[swatch start];
//[outPort sendThisMessage:msg];
[inPort _dispatchMessage:msg toOutPort:outPort];
}
- (void) receivedOSCMessage:(OSCMessage *)n {
NSLog(@"%s ... %@",__func__,n);
double roundTripPing = 0.0;
if (ignoreReceivedVals) {
roundTripPing = [swatch timeSinceStart];
ignoreReceivedVals = NO;
NSLog(@"\t\tping was %f",roundTripPing);
}
else {
NSLog(@"\t\tshould be returning the message!");
unsigned int txAddr = [n txAddress];
unsigned int txPort = [n txPort];
NSLog(@"\t\ttxPort is %d",txPort);
if (txAddr==0 || txPort==0)
NSLog(@"\t\terr, addr or port 0 at %s",__func__);
else {
OSCOutPort *thePort = [self findOutputWithRawAddress:txAddr andPort:txPort];
if (thePort == nil) {
NSLog(@"\t\tcouldn't find port at first...");
struct in_addr tmpAddr;
tmpAddr.s_addr = txAddr;
thePort = [self
createNewOutputToAddress:[NSString stringWithCString:inet_ntoa(tmpAddr) encoding:NSASCIIStringEncoding]
atPort:txPort];
}
if (thePort == nil)
NSLog(@"\t\tcouldn't find port a second time!");
else {
NSLog(@"\t\tsending to port %@",thePort);
[thePort sendThisMessage:n];
}
}
}
}
@end