Skip to content
Browse files

Rough working state

Features:
- Play/Stop/Progress

ZeroMQ:
- Async receive data works but causes odd log output

Publishing:
- Sending right data, but haven't confirmed successful reception of data
  on server end
  • Loading branch information...
1 parent f4ebbf3 commit 7ae140c82bb0d07300dd8df6d3b5a639da4b02b1 @tonywok tonywok committed Mar 12, 2012
View
28 CoccoaSpotbox.xcodeproj/project.pbxproj
@@ -7,6 +7,9 @@
objects = {
/* Begin PBXBuildFile section */
+ 15628E4B1509471B00288D72 /* ZmqDispatch.m in Sources */ = {isa = PBXBuildFile; fileRef = 15628E4A1509471B00288D72 /* ZmqDispatch.m */; };
+ 15628E4C1509480600288D72 /* libzmq.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 15628E47150946EF00288D72 /* libzmq.dylib */; };
+ 15628E56150972F800288D72 /* libzmqobjc.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 15628E55150972F800288D72 /* libzmqobjc.a */; };
1566CCDE1509116E008ED86F /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1566CCDD1509116E008ED86F /* Foundation.framework */; };
1566CCE11509116E008ED86F /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 1566CCE01509116E008ED86F /* main.m */; };
1566CCE51509116E008ED86F /* CoccoaSpotbox.1 in CopyFiles */ = {isa = PBXBuildFile; fileRef = 1566CCE41509116E008ED86F /* CoccoaSpotbox.1 */; };
@@ -43,6 +46,11 @@
/* End PBXCopyFilesBuildPhase section */
/* Begin PBXFileReference section */
+ 15628E47150946EF00288D72 /* libzmq.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libzmq.dylib; path = usr/local/lib/libzmq.dylib; sourceTree = SDKROOT; };
+ 15628E491509471B00288D72 /* ZmqDispatch.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ZmqDispatch.h; sourceTree = "<group>"; };
+ 15628E4A1509471B00288D72 /* ZmqDispatch.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ZmqDispatch.m; sourceTree = "<group>"; };
+ 15628E4F1509490300288D72 /* ZMQObjC.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ZMQObjC.h; sourceTree = "<group>"; };
+ 15628E55150972F800288D72 /* libzmqobjc.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libzmqobjc.a; path = "../../sandbox/objc-zmq/build/Debug/libzmqobjc.a"; sourceTree = "<group>"; };
1566CCD91509116E008ED86F /* CoccoaSpotbox */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = CoccoaSpotbox; sourceTree = BUILT_PRODUCTS_DIR; };
1566CCDD1509116E008ED86F /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
1566CCE01509116E008ED86F /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
@@ -65,6 +73,8 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
+ 15628E56150972F800288D72 /* libzmqobjc.a in Frameworks */,
+ 15628E4C1509480600288D72 /* libzmq.dylib in Frameworks */,
15E97AAC1509195C0015A560 /* Cocoa.framework in Frameworks */,
15E97AAA150919540015A560 /* AudioUnit.framework in Frameworks */,
15E97AA8150919470015A560 /* CoreAudio.framework in Frameworks */,
@@ -98,6 +108,9 @@
1566CCDC1509116E008ED86F /* Frameworks */ = {
isa = PBXGroup;
children = (
+ 15628E55150972F800288D72 /* libzmqobjc.a */,
+ 15628E4F1509490300288D72 /* ZMQObjC.h */,
+ 15628E47150946EF00288D72 /* libzmq.dylib */,
15E97AAB1509195C0015A560 /* Cocoa.framework */,
15E97AA9150919540015A560 /* AudioUnit.framework */,
15E97AA7150919470015A560 /* CoreAudio.framework */,
@@ -109,10 +122,12 @@
1566CCDF1509116E008ED86F /* CoccoaSpotbox */ = {
isa = PBXGroup;
children = (
- 1566CCE01509116E008ED86F /* main.m */,
+ 1566CCE41509116E008ED86F /* CoccoaSpotbox.1 */,
15E97AAD150919860015A560 /* SpotboxPlayer.h */,
+ 15628E491509471B00288D72 /* ZmqDispatch.h */,
+ 1566CCE01509116E008ED86F /* main.m */,
15E97AAE150919860015A560 /* SpotboxPlayer.m */,
- 1566CCE41509116E008ED86F /* CoccoaSpotbox.1 */,
+ 15628E4A1509471B00288D72 /* ZmqDispatch.m */,
1566CCE21509116E008ED86F /* Supporting Files */,
);
path = CoccoaSpotbox;
@@ -216,6 +231,7 @@
15E97AAF150919860015A560 /* SpotboxPlayer.m in Sources */,
15E97ABC15091B2A0015A560 /* SPCircularBuffer.m in Sources */,
15E97ABD15091B2A0015A560 /* SPPlaybackManager.m in Sources */,
+ 15628E4B1509471B00288D72 /* ZmqDispatch.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -278,6 +294,10 @@
);
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "CoccoaSpotbox/CoccoaSpotbox-Prefix.pch";
+ LIBRARY_SEARCH_PATHS = (
+ "$(inherited)",
+ "\"$(SRCROOT)/../../sandbox/objc-zmq/build/Debug\"",
+ );
PRODUCT_NAME = "$(TARGET_NAME)";
};
name = Debug;
@@ -291,6 +311,10 @@
);
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "CoccoaSpotbox/CoccoaSpotbox-Prefix.pch";
+ LIBRARY_SEARCH_PATHS = (
+ "$(inherited)",
+ "\"$(SRCROOT)/../../sandbox/objc-zmq/build/Debug\"",
+ );
PRODUCT_NAME = "$(TARGET_NAME)";
};
name = Release;
View
9 CoccoaSpotbox/SpotboxPlayer.h
@@ -8,13 +8,16 @@
#import <Foundation/Foundation.h>
#import "SPPlaybackManager.h"
+#import "ZmqDispatch.h"
-@interface SpotboxPlayer : NSObject <SPSessionDelegate> {
- SPPlaybackManager *playback_manager;
+@interface SpotboxPlayer : NSObject <SPSessionDelegate, ZmqDispatchDelegate> {
+ SPPlaybackManager *playback_manager;
}
@property(retain) SPPlaybackManager *playback_manager;
-- (void)play_track:(NSString *)track_url;
+- (void) play_track:(NSString *)track_url;
+- (void) stop_track;
+- (void) pause_track;
@end
View
51 CoccoaSpotbox/SpotboxPlayer.m
@@ -15,7 +15,8 @@ @implementation SpotboxPlayer
- (id) init {
self = [super init];
if (self) {
- playback_manager = [[SPPlaybackManager alloc] initWithPlaybackSession:[SPSession sharedSession]];
+ SPSession *session = [SPSession sharedSession];
+ playback_manager = [[SPPlaybackManager alloc] initWithPlaybackSession:session];
}
return self;
}
@@ -34,19 +35,51 @@ - (void) play_track:(NSString *)track_str {
NSError *error = nil;
if (![self.playback_manager playTrack:track error:&error]) {
- NSLog(@"shit went wrong");
+ NSLog(@"shit went wrong: %@", error);
}
return;
}
NSBeep();
}
-// ********** Session Delegate *********** //
+- (void) pause_track {
+
+}
+
+- (void) stop_track {
+ [playback_manager.playbackSession unloadPlayback];
+}
+
+// ********** ZmqDispatch Delegate ******* //
+
+- (void) zmqDispatchDidReceiveData:(ZmqDispatch *)dispatcher {
+ if ([playback_manager isPlaying]) {
+ NSTimeInterval pos = [playback_manager trackPosition];
+ NSString *track_position = [[NSString alloc] initWithFormat:@"%d", ((long)pos % 60)];
+ NSString *message = [NSString stringWithFormat:@"%@::%@::%@", @"spotbox:server", @"track_progres", track_position];
+ NSData* data = [message dataUsingEncoding:NSUTF8StringEncoding];
+
+ [dispatcher.pub sendData:data withFlags:ZMQ_NOBLOCK];
+ }
+}
+
+- (void) zmqDispatchDidReceivePlay:(NSString *)track_url {
+ NSLog(@"PLAY");
+ [self play_track:track_url];
+}
+
+- (void) zmqDispatchDidReceiveStop {
+ [self stop_track];
+}
+
+- (void) zmqDispatchDidReceivePause {
+ [self pause_track];
+}
+
+// ********** Session Delegate ********** //
- (void) sessionDidLoginSuccessfully:(SPSession *)aSession {
NSLog(@"logged in");
- NSString *spotify_str = @"spotify:track:18lwMD3frXxiVWBlztdijW";
- [self play_track:spotify_str];
}
- (void) session:(SPSession *)aSession didFailToLoginWithError:(NSError *)error {
@@ -62,11 +95,17 @@ - (void) session:(SPSession *)aSession didLogMessage:(NSString *)aMessage {
}
- (void)sessionDidChangeMetadata:(SPSession *)aSession; {
- NSLog(@"session meta data changed");
+ NSLog(@"meta data changed");
}
- (void)session:(SPSession *)aSession recievedMessageForUser:(NSString *)aMessage; {
NSLog(@"received msg for user: %@", aMessage);
}
+// Unregister observers
+
+- (void) dealloc {
+
+}
+
@end
View
28 CoccoaSpotbox/ZmqDispatch.h
@@ -7,7 +7,35 @@
//
#import <Foundation/Foundation.h>
+#import "ZMQObjC.h"
+
+@class ZmqDispatch;
+
+@protocol ZmqDispatchDelegate <NSObject>
+
+- (void) zmqDispatchDidReceivePlay:(NSString *)track_url;
+
+@optional
+- (void) zmqDispatchDidReceiveData:(ZmqDispatch *)dispatcher;
+- (void) zmqDispatchDidReceiveStop;
+- (void) zmqDispatchDidReceiveNext;
+- (void) zmqDispatchDidReceivePause;
+
+@end
@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;
+
+- (void) receiveData:(NSTimer *)timer;
+
+- (NSDictionary *) parseMessage:(NSString *)message;
+
@end
View
64 CoccoaSpotbox/ZmqDispatch.m
@@ -10,4 +10,68 @@
@implementation ZmqDispatch
+@synthesize pub, sub, ctx, delegate;
+
+- (id) initWithContext:(ZMQContext *)zmq_context
+ publishTo:(NSString *)pub_port
+ subscribeTo:(NSString *)sub_port {
+ self.ctx = zmq_context;
+ self.sub = [ctx socketWithType:ZMQ_SUB];
+ self.pub = [ctx socketWithType:ZMQ_PUB];
+
+ [pub bindToEndpoint:pub_port];
+ [sub connectToEndpoint:sub_port];
+
+ const char *filter = "spotbox:players:spotify";
+ NSData *filterData = [NSData dataWithBytes:filter length:strlen(filter)];
+ [sub setData:filterData forOption:ZMQ_SUBSCRIBE];
+
+ return self;
+}
+
+- (NSDictionary *) parseMessage:(NSString *)zmq_message {
+ NSArray *msg = [zmq_message componentsSeparatedByString:@"::"];
+ NSString *destination = [msg objectAtIndex:0];
+ NSString *method = [msg objectAtIndex:1];
+ NSArray *args = NULL;
+
+ if ([msg count] > 2) {
+ NSRange args_range = NSMakeRange(2, [msg count] - 2);
+ args = [msg subarrayWithRange:args_range];
+ } else {
+ args = [[NSArray alloc] init];
+ }
+
+ NSArray *keys = [NSArray arrayWithObjects:@"destination", @"method", @"args", nil];
+ NSArray *vals = [NSArray arrayWithObjects:destination, method, args, nil];
+ NSDictionary *data = [[NSDictionary alloc] initWithObjects:vals forKeys:keys];
+
+ return data;
+}
+
+// ***** Called during Run loop ****** //
+
+- (void) receiveData:(NSTimer *)timer {
+ NSData *data = [sub receiveDataWithFlags:ZMQ_NOBLOCK];
+
+ [delegate zmqDispatchDidReceiveData:self]; // provide hook for delegates
+
+ if (data) {
+ NSString *msg = [[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSUTF8StringEncoding];
+ NSDictionary *parsedMessage = [self parseMessage:msg];
+ NSString *method_name = [parsedMessage valueForKey:@"method"];
+
+ if ([method_name isEqual:@"play"]) {
+ NSString *track_str = [[parsedMessage valueForKey:@"args"] objectAtIndex:0];
+ [delegate zmqDispatchDidReceivePlay:track_str];
+ } else if ([method_name isEqual:@"stop"]) {
+ [delegate zmqDispatchDidReceiveStop];
+ } else if ([method_name isEqual:@"pause"]) {
+ [delegate zmqDispatchDidReceivePause];
+ } else {
+ NSLog(@"Unsupported method: %@ w/ raw msg: %@", method_name, msg);
+ }
+ }
+}
+
@end
View
77 CoccoaSpotbox/main.m
@@ -7,58 +7,55 @@
//
#import <Foundation/Foundation.h>
+#import "ZMQObjC.h"
#import <CocoaLibSpotify/CocoaLibSpotify.h>
#import "SpotboxPlayer.h"
+#import "ZmqDispatch.h"
-int main (int argc, const char * argv[])
-{
+// EdgeCase specific includes
+#import "config.h"
+#include "app_key.c"
+
+int main (int argc, const char * argv[]) {
@autoreleasepool {
- const uint8_t g_appkey[] = {
- 0x01, 0xA7, 0xA9, 0x5E, 0x3D, 0x21, 0x0E, 0x4E, 0x3C, 0x98, 0xC2, 0x26, 0x2A, 0xAF, 0x51, 0xC2,
- 0x06, 0xE4, 0xD1, 0x30, 0xE7, 0x53, 0x08, 0x9D, 0xD4, 0x82, 0xBC, 0xA1, 0x9C, 0x3B, 0x93, 0xF1,
- 0x1C, 0xB8, 0x1F, 0x20, 0x54, 0xFB, 0x91, 0x4A, 0x50, 0xBD, 0xF6, 0x4E, 0x41, 0x6E, 0x20, 0x93,
- 0xE4, 0x7D, 0xA9, 0xD4, 0x3F, 0xDC, 0xC5, 0x6C, 0xA2, 0xCE, 0xA0, 0xB7, 0x39, 0x8C, 0x75, 0xDC,
- 0xAA, 0x90, 0xC8, 0x87, 0xE2, 0x7D, 0xB0, 0xEB, 0xD4, 0x40, 0xFB, 0x64, 0x35, 0xF6, 0x34, 0x4D,
- 0xCC, 0x58, 0x94, 0x09, 0x5B, 0xDD, 0xC5, 0xCD, 0x0F, 0xAD, 0x62, 0x8C, 0x54, 0x8F, 0xA3, 0x73,
- 0xD3, 0x57, 0xFF, 0x00, 0x49, 0xA9, 0x34, 0x66, 0x23, 0x5C, 0x95, 0xA3, 0xC2, 0x39, 0x7B, 0xDF,
- 0xD6, 0x87, 0x4C, 0xEB, 0xA7, 0x40, 0xA0, 0x79, 0x5C, 0x3B, 0x17, 0xC4, 0x71, 0xF7, 0x05, 0x3C,
- 0x9B, 0x3F, 0x9B, 0xED, 0xDE, 0x68, 0x37, 0xC8, 0x41, 0x0D, 0x0C, 0x0F, 0xDF, 0xA6, 0xDD, 0x76,
- 0x2F, 0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ey);
-
- [SPSession initializeSharedSessionWithApplicationKey:[NSData dataWithBytes:&g_appkey length:g_appkey_size]
+
+ // ZMQ Initialization
+ ZMQContext *zmq_ctx = [[ZMQContext alloc] initWithIOThreads:1];
+ NSString *pub_port = @"tcp://127.0.0.1:12001";
+ NSString *sub_port = @"tcp://127.0.0.1:12000";
+ ZmqDispatch *dispatcher = [[ZmqDispatch alloc] initWithContext:zmq_ctx publishTo:pub_port subscribeTo:sub_port];
+
+ // Initialize and fiddle with Spotify Session
+ [SPSession initializeSharedSessionWithApplicationKey:[NSData dataWithBytes:&g_spotify_appkey length:g_spotify_appkey_size]
userAgent:@"com.edgecase.spotbox"
error:nil];
+
+ // Initialize spotify player
SpotboxPlayer *player = [[SpotboxPlayer alloc] init];
+ [dispatcher setDelegate:player];
[[SPSession sharedSession] setDelegate:player];
- [[SPSession sharedSession] attemptLoginWithUserName:@"128693083" password:@"1edgecase" rememberCredentials:NO];
- // create a timer
- NSDate *now = [[NSDate alloc] init];
- NSTimer *timer = [[NSTimer alloc] initWithFireDate:now
- interval:0.1
- target:nil
- selector:nil
- userInfo:nil
- repeats:YES];
-
+ NSFileManager *fm = [NSFileManager defaultManager];
+ NSString *username = [fm stringWithFileSystemRepresentation:SPOTIFY_USERNAME length:10];
+ NSString *password = [fm stringWithFileSystemRepresentation:SPOTIFY_PASSWORD length:10];
+ [[SPSession sharedSession] attemptLoginWithUserName:username
+ password:password
+ rememberCredentials:NO];
+
+ // create a timer for run loop
+ NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.2
+ target:dispatcher
+ selector:@selector(receiveData:)
+ userInfo:nil
+ repeats:YES];
+ // Run Loop
NSRunLoop *run_loop = [NSRunLoop currentRunLoop];
[run_loop addTimer:timer forMode:NSDefaultRunLoopMode];
- [run_loop run];
-
+ [run_loop run];
+ // Close sockets later (TODO: not sure if necessary)
+ [[zmq_ctx sockets] makeObjectsPerformSelector:@selector(close)];
+ return EXIT_SUCCESS;
}
}

0 comments on commit 7ae140c

Please sign in to comment.
Something went wrong with that request. Please try again.