Permalink
Browse files

convert dispatcher delegate to use notifications

  • Loading branch information...
1 parent daf71e2 commit 2f3537690e3dcd816310065b0867c0e97b4400d3 @tonywok tonywok committed Apr 8, 2012
@@ -11,7 +11,7 @@
1522F232150E6647008E2EA3 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 1522F22C150E6647008E2EA3 /* main.m */; };
1522F233150E6647008E2EA3 /* SpotboxPlayer.m in Sources */ = {isa = PBXBuildFile; fileRef = 1522F22E150E6647008E2EA3 /* SpotboxPlayer.m */; };
1522F234150E6647008E2EA3 /* ZmqDispatch.m in Sources */ = {isa = PBXBuildFile; fileRef = 1522F230150E6647008E2EA3 /* ZmqDispatch.m */; };
- 152ADC35151A50C20042128D /* PlaylistBootstrap.m in Sources */ = {isa = PBXBuildFile; fileRef = 152ADC34151A50C20042128D /* PlaylistBootstrap.m */; };
+ 152ADC35151A50C20042128D /* SpotboxPlaylist.m in Sources */ = {isa = PBXBuildFile; fileRef = 152ADC34151A50C20042128D /* SpotboxPlaylist.m */; };
15628E4C1509480600288D72 /* libzmq.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 15628E47150946EF00288D72 /* libzmq.dylib */; };
1566CCDE1509116E008ED86F /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1566CCDD1509116E008ED86F /* Foundation.framework */; };
15DD48E6150E648C002870E0 /* CocoaLibSpotify.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 15DD48E5150E648C002870E0 /* CocoaLibSpotify.framework */; };
@@ -43,8 +43,8 @@
1522F22E150E6647008E2EA3 /* SpotboxPlayer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SpotboxPlayer.m; sourceTree = "<group>"; };
1522F22F150E6647008E2EA3 /* ZmqDispatch.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ZmqDispatch.h; sourceTree = "<group>"; };
1522F230150E6647008E2EA3 /* ZmqDispatch.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ZmqDispatch.m; sourceTree = "<group>"; };
- 152ADC33151A50C20042128D /* PlaylistBootstrap.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PlaylistBootstrap.h; sourceTree = "<group>"; };
- 152ADC34151A50C20042128D /* PlaylistBootstrap.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PlaylistBootstrap.m; sourceTree = "<group>"; };
+ 152ADC33151A50C20042128D /* SpotboxPlaylist.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SpotboxPlaylist.h; sourceTree = "<group>"; };
+ 152ADC34151A50C20042128D /* SpotboxPlaylist.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SpotboxPlaylist.m; sourceTree = "<group>"; };
15628E47150946EF00288D72 /* libzmq.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libzmq.dylib; path = usr/local/lib/libzmq.dylib; sourceTree = SDKROOT; };
15628E4F1509490300288D72 /* ZMQObjC.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ZMQObjC.h; sourceTree = "<group>"; };
1566CCD91509116E008ED86F /* cocoa_spotify */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = cocoa_spotify; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -85,8 +85,8 @@
1522F22C150E6647008E2EA3 /* main.m */,
1522F22D150E6647008E2EA3 /* SpotboxPlayer.h */,
1522F22E150E6647008E2EA3 /* SpotboxPlayer.m */,
- 152ADC33151A50C20042128D /* PlaylistBootstrap.h */,
- 152ADC34151A50C20042128D /* PlaylistBootstrap.m */,
+ 152ADC33151A50C20042128D /* SpotboxPlaylist.h */,
+ 152ADC34151A50C20042128D /* SpotboxPlaylist.m */,
1522F22F150E6647008E2EA3 /* ZmqDispatch.h */,
1522F230150E6647008E2EA3 /* ZmqDispatch.m */,
);
@@ -201,7 +201,7 @@
1522F232150E6647008E2EA3 /* main.m in Sources */,
1522F233150E6647008E2EA3 /* SpotboxPlayer.m in Sources */,
1522F234150E6647008E2EA3 /* ZmqDispatch.m in Sources */,
- 152ADC35151A50C20042128D /* PlaylistBootstrap.m in Sources */,
+ 152ADC35151A50C20042128D /* SpotboxPlaylist.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
+ LastUpgradeVersion = "0430"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
@@ -39,11 +40,12 @@
</MacroExpansion>
</TestAction>
<LaunchAction
- selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.GDB"
+ selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.GDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
buildConfiguration = "Debug"
+ ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
allowLocationSimulation = "YES">
<BuildableProductRunnable>
@@ -10,18 +10,14 @@
#import "SPPlaybackManager.h"
#import "ZmqDispatch.h"
-@interface SpotboxPlayer : NSObject <SPSessionDelegate, ZmqDispatchDelegate> {
- SPPlaybackManager *playback_manager;
+@interface SpotboxPlayer : NSObject <SPSessionDelegate> {
+ SPPlaybackManager *playbackManager;
}
-@property(retain) SPPlaybackManager *playback_manager;
+@property(retain) SPPlaybackManager *playbackManager;
@property(retain) ZmqDispatch *dispatcher;
-- (id) initWithDispatcher:(ZmqDispatch *)aDispatcher;
-
+- (id) initWithDispatcher:(ZmqDispatch *)aDispatcher;
- (void) sendMessage:(NSString *)msg;
-- (void) play_track:(NSString *)track_url;
-- (void) stop_track;
-- (void) pause_track;
@end
@@ -10,19 +10,28 @@
@implementation SpotboxPlayer
-@synthesize playback_manager, dispatcher;
+@synthesize playbackManager, dispatcher;
- (id) initWithDispatcher:(ZmqDispatch *)aDispatcher {
self = [super init];
if (self) {
- SPSession *session = [SPSession sharedSession];
- self.playback_manager = [[SPPlaybackManager alloc] initWithPlaybackSession:session];
- self.dispatcher = aDispatcher;
+ SPSession *session = [SPSession sharedSession];
+ self.playbackManager = [[SPPlaybackManager alloc] initWithPlaybackSession:session];
+ self.dispatcher = aDispatcher;
+
+ // Add observers
[self addObserver:self
- forKeyPath:@"playback_manager.currentTrack"
+ forKeyPath:@"playbackManager.currentTrack"
options:NSKeyValueObservingOptionNew
context:nil];
+
+ // Register for notifications
+
+ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reportTrackProgress:) name:@"reportTrackProgress" object:NULL];
+ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playTrack:) name:@"play" object:NULL];
+ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pauseTrack:) name:@"pause" object:NULL];
+ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stopTrack:) name:@"stop" object:NULL];
}
return self;
}
@@ -35,85 +44,58 @@ - (void) sendMessage:(NSString *)msg {
// Observe currentTrack going to NULL, this signifies the end of a track
//
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
- if ([keyPath isEqualToString:@"playback_manager.currentTrack"]) {
+ if ([keyPath isEqualToString:@"playbackManager.currentTrack"]) {
if ([change valueForKey:@"new"] == [NSNull null]) {
NSLog(@"track ended");
[self sendMessage:@"spotbox:server::track_ended"];
}
}
}
-// Play a spotify track
-//
-- (void) play_track:(NSString *)track_str {
- NSURL *track_url = [[NSURL alloc] initWithString:track_str];
- SPTrack *track = [[SPSession sharedSession] trackForURL:track_url];
+// ********** ZmqDispatch Notifications ******* //
+
+- (void) playTrack:(NSNotification *) notification {
+ NSString *trackStr = [[[notification userInfo] valueForKey:@"args"] objectAtIndex:0];
+ NSURL *trackUrl = [[NSURL alloc] initWithString:trackStr];
+ SPTrack *track = [[SPSession sharedSession] trackForURL:trackUrl];
if (track != nil) {
if (!track.isLoaded) {
- [self performSelector:@selector(play_track:) withObject:track_str afterDelay:0.1]; // track might not be loaded.
+ [self performSelector:@selector(playTrack:) withObject:trackStr afterDelay:0.1]; // track might not be loaded.
return;
}
NSError *error = nil;
- if (![self.playback_manager playTrack:track error:&error]) {
- NSLog(@"shit went wrong: %@", error);
- }
+ if (![self.playbackManager playTrack:track error:&error]) { NSLog(@"Unable to play track: %@", error); }
- [self sendMessage:[NSString stringWithFormat:@"%@::%@", @"spotbox:server::playing", track_str]];
+ [self sendMessage:[NSString stringWithFormat:@"%@::%@", @"spotbox:server::playing", trackStr]];
- } else { NSLog(@"WAT"); }
-}
+ } else { NSLog(@"Track not found."); }
+}
-// Pause/Unpause track
-//
-- (void) pause_track {
- if ([playback_manager isPlaying]) {
- [playback_manager setIsPlaying:NO];
+- (void) pauseTrack:(NSNotification *) notification {
+ if ([playbackManager isPlaying]) {
+ [playbackManager setIsPlaying:NO];
[self sendMessage:@"spotbox:server::paused"];
} else {
- NSTimeInterval track_position = [playback_manager trackPosition];
- [playback_manager seekToTrackPosition:track_position];
- [playback_manager setIsPlaying:YES];
+ NSTimeInterval trackPosition = [playbackManager trackPosition];
+ [playbackManager seekToTrackPosition:trackPosition];
+ [playbackManager setIsPlaying:YES];
[self sendMessage:@"spotbox:server::unpaused"];
}
}
-// Stop a track
-//
-- (void) stop_track {
- [playback_manager.playbackSession unloadPlayback];
- [self sendMessage:@"spotbox:server::stopped"];
-}
-
-// Report track progress of currently playing track
-//
-- (void) report_track_progress {
- if ([playback_manager isPlaying]) {
- NSTimeInterval pos = [playback_manager trackPosition];
- NSString *track_position = [[NSString alloc] initWithFormat:@"%d", (long)pos];
- [self sendMessage:[NSString stringWithFormat:@"%@::%@", @"spotbox:server::track_progress", track_position]];
+- (void) reportTrackProgress:(NSNotification *) notification {
+ if ([playbackManager isPlaying]) {
+ NSTimeInterval pos = [playbackManager trackPosition];
+ NSString *trackPosition = [[NSString alloc] initWithFormat:@"%d", (long)pos];
+ [self sendMessage:[NSString stringWithFormat:@"%@::%@", @"spotbox:server::track_progress", trackPosition]];
}
}
-// ********** ZmqDispatch Delegate ******* //
-
-// Called when zmq dispatcher receives data. Assigns self.dispatcher so this class can publish
-//
-- (void) zmqDispatchDidReceiveData {
- [self report_track_progress];
-}
-
-- (void) zmqDispatchDidReceivePlay:(NSString *)track_url {
- [self play_track:track_url];
-}
-
-- (void) zmqDispatchDidReceiveStop {
- [self stop_track];
-}
-
-- (void) zmqDispatchDidReceivePause {
- [self pause_track];
+- (void) stopTrack:(NSNotification *) notification {
+ [playbackManager.playbackSession unloadPlayback];
+ [self sendMessage:@"spotbox:server::stopped"];
}
// ********** Session Delegate ********** //
@@ -146,6 +128,7 @@ - (void)session:(SPSession *)aSession recievedMessageForUser:(NSString *)aMessag
- (void) dealloc {
[self removeObserver:self forKeyPath:@"playback_manager.currentTrack"];
+ [[NSNotificationCenter defaultCenter] removeObserver:self];
}
-@end
+@end
@@ -10,12 +10,12 @@
#import "ZmqDispatch.h"
#import <CocoaLibSpotify/CocoaLibSpotify.h>
-@interface PlaylistBootstrap : NSObject
+@interface SpotboxPlaylist : NSObject
-- (id) initWithDispatcher:(ZmqDispatch *)aDispatcher;
+- (id) initWithDispatcher:(ZmqDispatch *)aDispatcher;
- (void) reportPlaylistTracks:(SPPlaylist *)playlist;
-- (void) loadTracksFromPlaylist:(NSURL *)playlist_url;
+- (void) loadTracksFromPlaylist:(NSURL *)playlistUrl;
-@property(retain) ZmqDispatch *dispatcher;
+@property(strong) ZmqDispatch *dispatcher;
@end
@@ -6,9 +6,9 @@
// Copyright (c) 2012 Edgecase. All rights reserved.
//
-#import "PlaylistBootstrap.h"
+#import "SpotboxPlaylist.h"
-@implementation PlaylistBootstrap
+@implementation SpotboxPlaylist
@synthesize dispatcher;
@@ -18,36 +18,37 @@ - (id) initWithDispatcher:(ZmqDispatch *)aDispatcher {
self.dispatcher = aDispatcher;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(load_playlist:)
- name:@"load_playlist"
+ name:@"loadPlaylist"
object:nil];
}
return self;
}
- (void) reportPlaylistTracks:(SPPlaylist *)playlist {
- NSMutableArray *track_urls = [NSMutableArray array];
- NSMutableArray *tracks = [playlist items];
+ NSMutableArray *trackUrls = [NSMutableArray array];
+ NSMutableArray *tracks = [playlist items];
for (int index = 0; index < [tracks count]; index++) {
SPPlaylistItem *item = [tracks objectAtIndex:index];
if ([item itemURLType] == SP_LINKTYPE_TRACK) {
- [track_urls addObject:[item itemURL]];
+ [trackUrls addObject:[item itemURL]];
}
}
- NSString *playlist_url_str = [[playlist spotifyURL] absoluteString];
- NSString *playlist_name = [playlist name];
- NSString *tracks_str = [track_urls componentsJoinedByString:@","];
- NSString *msg = [NSString stringWithFormat:@"spotbox:server::playlist_loaded::%@,%@,%@", playlist_url_str, playlist_name, tracks_str];
+ NSString *playlistUrlStr = [[playlist spotifyURL] absoluteString];
+ NSString *playlistName = [playlist name];
+ NSString *tracksStr = [trackUrls componentsJoinedByString:@","];
+ NSString *msg = [NSString stringWithFormat:@"spotbox:server::playlist_loaded::%@,%@,%@", playlistUrlStr, playlistName, tracksStr];
+
[[dispatcher pub] sendData:[msg dataUsingEncoding:NSUTF8StringEncoding] withFlags:ZMQ_NOBLOCK];
}
-- (void) loadTracksFromPlaylist:(NSURL *)playlist_url {
- SPPlaylist *playlist = [[SPSession sharedSession] playlistForURL:playlist_url];
+- (void) loadTracksFromPlaylist:(NSURL *)playlistUrl {
+ SPPlaylist *playlist = [[SPSession sharedSession] playlistForURL:playlistUrl];
if (playlist != nil) {
if (!playlist.isLoaded) {
- [self performSelector:@selector(loadTracksFromPlaylist:) withObject:playlist_url afterDelay:0.1];
+ [self performSelector:@selector(loadTracksFromPlaylist:) withObject:playlistUrl afterDelay:0.1];
return;
}
}
@@ -58,10 +59,10 @@ - (void) loadTracksFromPlaylist:(NSURL *)playlist_url {
//**************** ZmqDispatch Notifications *********************//
- (void) load_playlist:(NSNotification *)notification {
- NSString *playlist_str = [[[notification userInfo] valueForKey:@"args"] objectAtIndex:0];
- NSURL *playlist_url = [[NSURL alloc] initWithString:playlist_str];
+ NSString *playlistStr = [[[notification userInfo] valueForKey:@"args"] objectAtIndex:0];
+ NSURL *playlistUrl = [[NSURL alloc] initWithString:playlistStr];
- [self loadTracksFromPlaylist:playlist_url];
+ [self loadTracksFromPlaylist:playlistUrl];
}
//************************* Dealloc *****************************//
@@ -11,28 +11,15 @@
@class ZmqDispatch;
-@protocol ZmqDispatchDelegate <NSObject>
-
-@optional
-- (void) zmqDispatchDidReceivePlay:(NSString *)track_url;
-- (void) zmqDispatchDidReceiveData;
-- (void) zmqDispatchDidReceiveStop;
-- (void) zmqDispatchDidReceivePause;
-
-@end
-
-@interface ZmqDispatch : NSObject {
- NSNotificationCenter *zmqNotificationCenter;
-}
+@interface ZmqDispatch : NSObject
@property (strong)ZMQSocket *pub;
@property (strong)ZMQSocket *sub;
@property (strong)ZMQContext *ctx;
-@property (nonatomic, weak) id <ZmqDispatchDelegate> delegate;
- (id) initWithContext:(ZMQContext *)ctx
- publishTo:(NSString *)pub_port
- subscribeTo:(NSString *)sub_port;
+ publishTo:(NSString *)pubPort
+ subscribeTo:(NSString *)subPort;
- (void) receiveData:(NSTimer *)timer;
Oops, something went wrong.

0 comments on commit 2f35376

Please sign in to comment.