Skip to content

Commit

Permalink
Use pointers to timers and asyncs-- less casts
Browse files Browse the repository at this point in the history
  • Loading branch information
liesen committed Jul 23, 2010
1 parent ee9c880 commit d08260f
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 25 deletions.
39 changes: 21 additions & 18 deletions src/session.cc
Expand Up @@ -40,18 +40,18 @@ static void NotifyMainThread(sp_session* session) {
// query sp_session_process_events, which is handled by // query sp_session_process_events, which is handled by
// Session::ProcessEvents. ev_async_send queues a call on the main ev runloop. // Session::ProcessEvents. ev_async_send queues a call on the main ev runloop.
Session* s = static_cast<Session*>(sp_session_userdata(session)); Session* s = static_cast<Session*>(sp_session_userdata(session));
ev_async_send(EV_DEFAULT_UC_ static_cast<ev_async*>(&s->runloop_async_)); ev_async_send(EV_DEFAULT_UC_ s->runloop_async_);
} }


void Session::ProcessEvents() { void Session::ProcessEvents() {
int timeout = 0; int timeout = 0;
ev_timer_stop(EV_DEFAULT_UC_ static_cast<ev_timer*>(&runloop_timer_)); ev_timer_stop(EV_DEFAULT_UC_ runloop_timer_);


if (session_) if (session_)
sp_session_process_events(session_, &timeout); sp_session_process_events(session_, &timeout);


ev_timer_set(static_cast<ev_timer*>(&runloop_timer_), timeout / 1000.0, 0.0); ev_timer_set(runloop_timer_, timeout / 1000.0, 0.0);
ev_timer_start(EV_DEFAULT_UC_ static_cast<ev_timer*>(&runloop_timer_)); ev_timer_start(EV_DEFAULT_UC_ runloop_timer_);
} }


void Session::DequeueLogMessages() { void Session::DequeueLogMessages() {
Expand Down Expand Up @@ -83,7 +83,7 @@ static void LogMessage(sp_session* session, const char* data) {
nt_atomic_enqueue(&s->log_messages_q_, msg, offsetof(log_message_t, next)); nt_atomic_enqueue(&s->log_messages_q_, msg, offsetof(log_message_t, next));
// Signal we need to dequeue the message queue (handled by // Signal we need to dequeue the message queue (handled by
// SpotifyRunloopAsyncLogMessage). // SpotifyRunloopAsyncLogMessage).
ev_async_send(EV_DEFAULT_UC_ static_cast<ev_async*>(&s->logmsg_async_)); ev_async_send(EV_DEFAULT_UC_ s->logmsg_async_);
} }
} }


Expand Down Expand Up @@ -170,11 +170,15 @@ Session::Session(sp_session* session)
, logout_callback_(NULL) , logout_callback_(NULL)
, playlist_container_(NULL) { , playlist_container_(NULL) {
nt_atomic_queue_init(&log_messages_q_); nt_atomic_queue_init(&log_messages_q_);

runloop_timer_ = new ev_timer;
runloop_async_ = new ev_async;
logmsg_async_ = new ev_async;
} }


Session::~Session() { Session::~Session() {
ev_timer_stop(EV_DEFAULT_UC_ static_cast<ev_timer*>(&runloop_timer_)); ev_timer_stop(EV_DEFAULT_UC_ runloop_timer_);
ev_async_stop(EV_DEFAULT_UC_ static_cast<ev_async*>(&runloop_async_)); ev_async_stop(EV_DEFAULT_UC_ runloop_async_);
this->DequeueLogMessages(); this->DequeueLogMessages();


if (playlist_container_) { if (playlist_container_) {
Expand All @@ -192,6 +196,8 @@ Session::~Session() {
cb_destroy(logout_callback_); cb_destroy(logout_callback_);
logout_callback_ = NULL; logout_callback_ = NULL;
} }

delete runloop_timer_, runloop_async_, logmsg_async_;
} }


Handle<Value> Session::New(const Arguments& args) { Handle<Value> Session::New(const Arguments& args) {
Expand Down Expand Up @@ -270,25 +276,22 @@ Handle<Value> Session::New(const Arguments& args) {
} }


// ev_async for libspotify background thread to invoke processing on main // ev_async for libspotify background thread to invoke processing on main
s->runloop_async_.data = s; s->runloop_async_->data = s;
ev_async_init(static_cast<ev_async*>(&s->runloop_async_), ev_async_init(s->runloop_async_, SpotifyRunloopAsyncProcess);
SpotifyRunloopAsyncProcess); ev_async_start(EV_DEFAULT_UC_ s->runloop_async_);
ev_async_start(EV_DEFAULT_UC_ static_cast<ev_async*>(&s->runloop_async_));
ev_unref(EV_DEFAULT_UC); // don't let a lingering async ev keep the main loop ev_unref(EV_DEFAULT_UC); // don't let a lingering async ev keep the main loop


// ev_timer for triggering libspotify periodic processing // ev_timer for triggering libspotify periodic processing
s->runloop_timer_.data = s; s->runloop_timer_->data = s;
ev_timer_init(static_cast<ev_timer*>(&s->runloop_timer_), ev_timer_init(s->runloop_timer_, SpotifyRunloopTimerProcess, 60.0, 0.0);
SpotifyRunloopTimerProcess, 60.0, 0.0);
ev_unref(EV_DEFAULT_UC); ev_unref(EV_DEFAULT_UC);
// Note: No need to start the timer as it's started by first invocation after // Note: No need to start the timer as it's started by first invocation after
// NotifyMainThread // NotifyMainThread


// ev_async for libspotify background thread to emit log message on main // ev_async for libspotify background thread to emit log message on main
s->logmsg_async_.data = s; s->logmsg_async_->data = s;
ev_async_init(static_cast<ev_async*>(&s->logmsg_async_), ev_async_init(s->logmsg_async_, SpotifyRunloopAsyncLogMessage);
SpotifyRunloopAsyncLogMessage); ev_async_start(EV_DEFAULT_UC_ s->logmsg_async_);
ev_async_start(EV_DEFAULT_UC_ static_cast<ev_async*>(&s->logmsg_async_));
ev_unref(EV_DEFAULT_UC); // don't let a lingering async ev keep the main loop ev_unref(EV_DEFAULT_UC); // don't let a lingering async ev keep the main loop


sp_session* session; sp_session* session;
Expand Down
6 changes: 3 additions & 3 deletions src/session.h
Expand Up @@ -38,11 +38,11 @@ class Session : public node::EventEmitter {
v8::Persistent<v8::Object> *playlist_container_; v8::Persistent<v8::Object> *playlist_container_;


// Node-Spotify runloop glue // Node-Spotify runloop glue
ev_timer runloop_timer_; ev_timer* runloop_timer_;
ev_async runloop_async_; ev_async* runloop_async_;


// Spotify background thread-to-node-main glue // Spotify background thread-to-node-main glue
ev_async logmsg_async_; ev_async* logmsg_async_;


// log messages delivered from a background thread // log messages delivered from a background thread
nt_atomic_queue log_messages_q_; nt_atomic_queue log_messages_q_;
Expand Down
5 changes: 4 additions & 1 deletion src/track.cc
Expand Up @@ -124,6 +124,7 @@ Handle<Value> Track::UriGetter(Local<String> property,
const AccessorInfo& info) { const AccessorInfo& info) {
HandleScope scope; HandleScope scope;
Track *p = Unwrap<Track>(info.This()); Track *p = Unwrap<Track>(info.This());
fprintf(stderr, "UriGetter\n");


if (!p->track_ || !sp_track_is_loaded(p->track_)) if (!p->track_ || !sp_track_is_loaded(p->track_))
return Undefined(); return Undefined();
Expand All @@ -137,7 +138,9 @@ Handle<Value> Track::UriGetter(Local<String> property,
char uri_buf[kUriBufferLen]; char uri_buf[kUriBufferLen];
int uri_len = sp_link_as_string(link, uri_buf, kUriBufferLen); int uri_len = sp_link_as_string(link, uri_buf, kUriBufferLen);
sp_link_release(link); sp_link_release(link);
return scope.Close(String::New(uri_buf, uri_len)); Local<String> uri = String::New(uri_buf, uri_len);
info.This()->ForceSet(property, uri, ReadOnly);
return scope.Close(uri);
} }


void Track::Initialize(Handle<Object> target) { void Track::Initialize(Handle<Object> target) {
Expand Down
2 changes: 1 addition & 1 deletion test/test-playlist-iterate-tracks.js
Expand Up @@ -16,7 +16,7 @@ createSession(function (session) {
sys.puts(sys.inspect(this[0])); sys.puts(sys.inspect(this[0]));
} }


session.logout(); session.logout(function() { console.log('Logged out'); });
} }


this[0].addListener('updated', f); this[0].addListener('updated', f);
Expand Down
7 changes: 5 additions & 2 deletions test/test.js
Expand Up @@ -14,12 +14,15 @@ GLOBAL.createSession = function(dontForwardLogging, onsession) {
onsession = dontForwardLogging; onsession = dontForwardLogging;
dontForwardLogging = false; dontForwardLogging = false;
} }

var session = new spotify.Session({applicationKey: account.applicationKey}); var session = new spotify.Session({applicationKey: account.applicationKey});

if (!dontForwardLogging) { if (!dontForwardLogging) {
session.addListener('logMessage', function(m){ session.on('logMessage', function (message) {
sys.log(m.substr(0,m.length-1)); sys.log(message.substr(0, message.length - 1));
}); });
} }

session.login(account.username, account.password, function (err) { session.login(account.username, account.password, function (err) {
assert.ifError(err); assert.ifError(err);
onsession(session); onsession(session);
Expand Down

0 comments on commit d08260f

Please sign in to comment.