Skip to content

Commit

Permalink
Add our own osc in-port type to handle multiple delegates
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan del Strother authored and Jonathan del Strother committed Sep 16, 2009
1 parent 141de93 commit 5d750a6
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 19 deletions.
6 changes: 6 additions & 0 deletions BBOSC.xcodeproj/project.pbxproj
Expand Up @@ -36,6 +36,7 @@
FC520F6B105936B000FE3004 /* VVOSC.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FC520F58105936A500FE3004 /* VVOSC.framework */; };
FC520F75105936CA00FE3004 /* VVOSC.framework in CopyFiles */ = {isa = PBXBuildFile; fileRef = FC520F58105936A500FE3004 /* VVOSC.framework */; };
FCC290EA1059440700B73F44 /* VVBasics.framework in CopyFiles */ = {isa = PBXBuildFile; fileRef = FC520F52105936A500FE3004 /* VVBasics.framework */; };
FCDF33AE1060FDF2001FAA34 /* BBOSCInPort.m in Sources */ = {isa = PBXBuildFile; fileRef = FCDF33AD1060FDF2001FAA34 /* BBOSCInPort.m */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -194,6 +195,8 @@
FC520E8D1059234900FE3004 /* BBOSCManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BBOSCManager.h; sourceTree = "<group>"; };
FC520E8E1059234900FE3004 /* BBOSCManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BBOSCManager.m; sourceTree = "<group>"; };
FC520F3C105936A500FE3004 /* VVOpenSource.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = VVOpenSource.xcodeproj; path = lib/vvosc/VVOpenSource.xcodeproj; sourceTree = "<group>"; };
FCDF33AC1060FDF2001FAA34 /* BBOSCInPort.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BBOSCInPort.h; sourceTree = "<group>"; };
FCDF33AD1060FDF2001FAA34 /* BBOSCInPort.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BBOSCInPort.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -259,6 +262,8 @@
FC520D2F1058137600FE3004 /* OSCExtensions.m */,
FC520E8D1059234900FE3004 /* BBOSCManager.h */,
FC520E8E1059234900FE3004 /* BBOSCManager.m */,
FCDF33AC1060FDF2001FAA34 /* BBOSCInPort.h */,
FCDF33AD1060FDF2001FAA34 /* BBOSCInPort.m */,
);
name = Classes;
sourceTree = "<group>";
Expand Down Expand Up @@ -491,6 +496,7 @@
FC520CFB1058085100FE3004 /* BBOSCViewController.m in Sources */,
FC520D301058137600FE3004 /* OSCExtensions.m in Sources */,
FC520E8F1059234900FE3004 /* BBOSCManager.m in Sources */,
FCDF33AE1060FDF2001FAA34 /* BBOSCInPort.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
25 changes: 25 additions & 0 deletions BBOSCInPort.h
@@ -0,0 +1,25 @@
//
// BBOSCInPort.h
// BBOSC
//
// Created by Jonathan del Strother on 16/09/2009.
// Copyright 2009 Best Before Media Ltd. All rights reserved.
//

#import <Cocoa/Cocoa.h>

// This is a basic wrapper around OSCInPort, with the addition of handling multiple delegates

@class OSCInPort, OSCManager;
@interface BBOSCInPort : NSObject {
OSCManager* oscManager;
OSCInPort* oscPort;
NSMutableSet* delegates;
}
-(id)initWithManager:(OSCManager*)manager withPort:(unsigned int)p label:(NSString*)l;
-(void)addDelegate:(id)delegate;
-(void)removeDelegate:(id)delegate;
-(unsigned short)port;
-(NSString*)portLabel;
-(void)remove;
@end
47 changes: 47 additions & 0 deletions BBOSCInPort.m
@@ -0,0 +1,47 @@
//
// BBOSCInPort.m
// BBOSC
//
// Created by Jonathan del Strother on 16/09/2009.
// Copyright 2009 Best Before Media Ltd. All rights reserved.
//

#import "BBOSCInPort.h"
#import "OSCExtensions.h"

@implementation BBOSCInPort
-(id)initWithManager:(OSCManager*)manager withPort:(unsigned int)p label:(NSString*)l {
if (self = [super init]) {
oscManager = [manager retain];
oscPort = [[oscManager createNewInputForPort:p withLabel:l] retain];
oscPort.delegate = self;
delegates = [[NSMutableSet alloc] init];
}
return self;
}
-(void)dealloc {
[oscManager release];
[oscPort release];
[delegates release];
[super dealloc];
}
-(void)addDelegate:(id)delegate {
[delegates addObject:delegate];
}
-(void)removeDelegate:(id)delegate {
[delegates removeObject:delegate];
}
- (unsigned short) port {
return [oscPort port];
}
- (NSString *) portLabel {
return [oscPort portLabel];
}
-(void)remove {
[oscManager removeInput:oscPort];
}

- (void) receivedOSCMessage:(OSCMessage *)m {
[delegates makeObjectsPerformSelector:@selector(receivedOSCMessage:) withObject:m];
}
@end
10 changes: 5 additions & 5 deletions BBOSCManager.h
Expand Up @@ -8,16 +8,16 @@

#import <Cocoa/Cocoa.h>

@class OSCManager, OSCInPort, OSCOutPort;
@class OSCManager, BBOSCInPort, OSCOutPort;
@interface BBOSCManager : NSObject {
OSCManager* oscManager;

NSCountedSet* inputPorts;
}
+(id)sharedManager;
+(BBOSCManager*)sharedManager;

- (OSCInPort*)createNewInputForPort:(int)p withLabel:(NSString *)l;
- (BBOSCInPort*)createNewInputForPort:(int)p withLabel:(NSString *)l;
- (OSCOutPort*)createNewOutputToAddress:(NSString *)a atPort:(int)p withLabel:(NSString *)l;
- (void)removeInput:(id)p;
- (void)removeOutput:(id)p;
- (void)removeInput:(BBOSCInPort*)p;
- (void)removeOutput:(OSCOutPort*)p;
@end
21 changes: 12 additions & 9 deletions BBOSCManager.m
Expand Up @@ -9,6 +9,7 @@
#import "BBOSCManager.h"
#import "OSCExtensions.h"
#import "NSArrayExtensions.h"
#import "BBOSCInPort.h"

@interface BBOSCBroadcastPort : NSObject {
OSCManager* oscManager;
Expand Down Expand Up @@ -45,7 +46,7 @@ -(void)sendThisMessage:(OSCMessage*)message {
@implementation BBOSCManager

static id sharedManager=nil;
+(id)sharedManager {
+(BBOSCManager*)sharedManager {
if (!sharedManager)
sharedManager = [[self alloc] init];
return sharedManager;
Expand All @@ -59,19 +60,21 @@ -(id)init {
return self;
}

- (OSCInPort *) createNewInputForPort:(int)p withLabel:(NSString *)l {
- (BBOSCInPort *) createNewInputForPort:(int)p withLabel:(NSString *)l {
// Search for any existing oscPorts with the same port number, and re-use them when possible
OSCInPort* resultingPort = nil;
for(OSCInPort* oscPort in inputPorts) {
BBOSCInPort* resultingPort = nil;
for(BBOSCInPort* oscPort in inputPorts) {
if (oscPort.port == p) {
NSAssert([oscPort.portLabel isEqualToString:l], @"Need to be using the same label");
resultingPort = oscPort;
break;
}
}
if (!resultingPort)
resultingPort = [oscManager createNewInputForPort:p withLabel:l];
if (!resultingPort) {
resultingPort = [[[BBOSCInPort alloc] initWithManager:oscManager withPort:p label:l] autorelease];
}

// This is a counted set, so we always add the port, so we can keep track of how many people are using it.
[inputPorts addObject:resultingPort];

return resultingPort;
Expand All @@ -81,13 +84,13 @@ - (OSCOutPort *) createNewOutputToAddress:(NSString *)a atPort:(int)p withLabel:
return [[[BBOSCBroadcastPort alloc] initWithManager:oscManager atPort:p] autorelease];
return [oscManager createNewOutputToAddress:a atPort:p withLabel:l];
}
- (void) removeInput:(id)p {
- (void) removeInput:(BBOSCInPort*)p {
[inputPorts removeObject:p];
// Once noone is using that input any more, remove it from the oscManager.
if ([inputPorts countForObject:p] == 0)
[oscManager removeInput:p];
[p remove];
}
- (void) removeOutput:(id)p {
- (void) removeOutput:(OSCOutPort*)p {
[oscManager removeOutput:p];
}

Expand Down
4 changes: 2 additions & 2 deletions BBOSCPluginReceiver.h
Expand Up @@ -8,7 +8,7 @@

#import <Quartz/Quartz.h>

@class OSCInPort;
@class BBOSCInPort;
@interface BBOSCPluginReceiver : QCPlugIn {
NSMutableArray* messages;
NSLock* messageLock;
Expand All @@ -17,7 +17,7 @@

@property (nonatomic, readonly, retain) NSArray* oscParameters;

@property (nonatomic, readonly, retain) OSCInPort *oscPort;
@property (nonatomic, readonly, retain) BBOSCInPort *oscPort;

@property (nonatomic, readwrite, assign) BOOL inputDiscardExcessMessages;
@property (nonatomic, readwrite, assign) NSUInteger inputReceivingPort;
Expand Down
7 changes: 4 additions & 3 deletions BBOSCPluginReceiver.m
Expand Up @@ -13,12 +13,13 @@
#import "NSArrayExtensions.h"
#import "OSCExtensions.h"
#import "BBOSCManager.h"
#import "BBOSCInPort.h"

#define kQCPlugIn_Name @"BBOSC Receiver"
#define kQCPlugIn_Description @"Best Before Open Sound Control receiver plugin"

@interface BBOSCPluginReceiver ()
@property (nonatomic, readwrite, retain) OSCOutPort *oscPort;
@property (nonatomic, readwrite, retain) BBOSCInPort *oscPort;
@property (nonatomic, readwrite, retain) NSArray* oscParameters;
@property (nonatomic, readwrite, retain) NSString* listeningPath;
@end
Expand Down Expand Up @@ -192,7 +193,7 @@ Return NO in case of failure during the execution (this will prevent rendering o
if (self.oscPort)
[[BBOSCManager sharedManager] removeInput:self.oscPort];
self.oscPort = [[BBOSCManager sharedManager] createNewInputForPort:self.inputReceivingPort withLabel:@"BB OSC"];
self.oscPort.delegate = self;
[self.oscPort addDelegate:self];
if (!self.oscPort)
NSLog(@"Failed to created input port");
}
Expand Down Expand Up @@ -235,7 +236,7 @@ Return NO in case of failure during the execution (this will prevent rendering o
- (void) disableExecution:(id<QCPlugInContext>)context
{
if (self.oscPort) {
[[BBOSCManager sharedManager] removeOutput:self.oscPort];
[[BBOSCManager sharedManager] removeInput:self.oscPort];
self.oscPort = nil;
}
}
Expand Down

0 comments on commit 5d750a6

Please sign in to comment.