Permalink
Browse files

Use pointers to timers and asyncs-- less casts

  • Loading branch information...
1 parent ee9c880 commit d08260f972cbac5af70c919d9e8657f35c0ab1db @liesen committed Jul 23, 2010
Showing with 34 additions and 25 deletions.
  1. +21 −18 src/session.cc
  2. +3 −3 src/session.h
  3. +4 −1 src/track.cc
  4. +1 −1 test/test-playlist-iterate-tracks.js
  5. +5 −2 test/test.js
View
39 src/session.cc
@@ -40,18 +40,18 @@ static void NotifyMainThread(sp_session* session) {
// query sp_session_process_events, which is handled by
// Session::ProcessEvents. ev_async_send queues a call on the main ev runloop.
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() {
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_)
sp_session_process_events(session_, &timeout);
- ev_timer_set(static_cast<ev_timer*>(&runloop_timer_), timeout / 1000.0, 0.0);
- ev_timer_start(EV_DEFAULT_UC_ static_cast<ev_timer*>(&runloop_timer_));
+ ev_timer_set(runloop_timer_, timeout / 1000.0, 0.0);
+ ev_timer_start(EV_DEFAULT_UC_ runloop_timer_);
}
void Session::DequeueLogMessages() {
@@ -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));
// Signal we need to dequeue the message queue (handled by
// SpotifyRunloopAsyncLogMessage).
- ev_async_send(EV_DEFAULT_UC_ static_cast<ev_async*>(&s->logmsg_async_));
+ ev_async_send(EV_DEFAULT_UC_ s->logmsg_async_);
}
}
@@ -170,11 +170,15 @@ Session::Session(sp_session* session)
, logout_callback_(NULL)
, playlist_container_(NULL) {
nt_atomic_queue_init(&log_messages_q_);
+
+ runloop_timer_ = new ev_timer;
+ runloop_async_ = new ev_async;
+ logmsg_async_ = new ev_async;
}
Session::~Session() {
- ev_timer_stop(EV_DEFAULT_UC_ static_cast<ev_timer*>(&runloop_timer_));
- ev_async_stop(EV_DEFAULT_UC_ static_cast<ev_async*>(&runloop_async_));
+ ev_timer_stop(EV_DEFAULT_UC_ runloop_timer_);
+ ev_async_stop(EV_DEFAULT_UC_ runloop_async_);
this->DequeueLogMessages();
if (playlist_container_) {
@@ -192,6 +196,8 @@ Session::~Session() {
cb_destroy(logout_callback_);
logout_callback_ = NULL;
}
+
+ delete runloop_timer_, runloop_async_, logmsg_async_;
}
Handle<Value> Session::New(const Arguments& args) {
@@ -270,25 +276,22 @@ Handle<Value> Session::New(const Arguments& args) {
}
// ev_async for libspotify background thread to invoke processing on main
- s->runloop_async_.data = s;
- ev_async_init(static_cast<ev_async*>(&s->runloop_async_),
- SpotifyRunloopAsyncProcess);
- ev_async_start(EV_DEFAULT_UC_ static_cast<ev_async*>(&s->runloop_async_));
+ s->runloop_async_->data = s;
+ ev_async_init(s->runloop_async_, SpotifyRunloopAsyncProcess);
+ ev_async_start(EV_DEFAULT_UC_ s->runloop_async_);
ev_unref(EV_DEFAULT_UC); // don't let a lingering async ev keep the main loop
// ev_timer for triggering libspotify periodic processing
- s->runloop_timer_.data = s;
- ev_timer_init(static_cast<ev_timer*>(&s->runloop_timer_),
- SpotifyRunloopTimerProcess, 60.0, 0.0);
+ s->runloop_timer_->data = s;
+ ev_timer_init(s->runloop_timer_, SpotifyRunloopTimerProcess, 60.0, 0.0);
ev_unref(EV_DEFAULT_UC);
// Note: No need to start the timer as it's started by first invocation after
// NotifyMainThread
// ev_async for libspotify background thread to emit log message on main
- s->logmsg_async_.data = s;
- ev_async_init(static_cast<ev_async*>(&s->logmsg_async_),
- SpotifyRunloopAsyncLogMessage);
- ev_async_start(EV_DEFAULT_UC_ static_cast<ev_async*>(&s->logmsg_async_));
+ s->logmsg_async_->data = s;
+ ev_async_init(s->logmsg_async_, SpotifyRunloopAsyncLogMessage);
+ ev_async_start(EV_DEFAULT_UC_ s->logmsg_async_);
ev_unref(EV_DEFAULT_UC); // don't let a lingering async ev keep the main loop
sp_session* session;
View
6 src/session.h
@@ -38,11 +38,11 @@ class Session : public node::EventEmitter {
v8::Persistent<v8::Object> *playlist_container_;
// Node-Spotify runloop glue
- ev_timer runloop_timer_;
- ev_async runloop_async_;
+ ev_timer* runloop_timer_;
+ ev_async* runloop_async_;
// Spotify background thread-to-node-main glue
- ev_async logmsg_async_;
+ ev_async* logmsg_async_;
// log messages delivered from a background thread
nt_atomic_queue log_messages_q_;
View
5 src/track.cc
@@ -124,6 +124,7 @@ Handle<Value> Track::UriGetter(Local<String> property,
const AccessorInfo& info) {
HandleScope scope;
Track *p = Unwrap<Track>(info.This());
+ fprintf(stderr, "UriGetter\n");
if (!p->track_ || !sp_track_is_loaded(p->track_))
return Undefined();
@@ -137,7 +138,9 @@ Handle<Value> Track::UriGetter(Local<String> property,
char uri_buf[kUriBufferLen];
int uri_len = sp_link_as_string(link, uri_buf, kUriBufferLen);
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) {
View
2 test/test-playlist-iterate-tracks.js
@@ -16,7 +16,7 @@ createSession(function (session) {
sys.puts(sys.inspect(this[0]));
}
- session.logout();
+ session.logout(function() { console.log('Logged out'); });
}
this[0].addListener('updated', f);
View
7 test/test.js
@@ -14,12 +14,15 @@ GLOBAL.createSession = function(dontForwardLogging, onsession) {
onsession = dontForwardLogging;
dontForwardLogging = false;
}
+
var session = new spotify.Session({applicationKey: account.applicationKey});
+
if (!dontForwardLogging) {
- session.addListener('logMessage', function(m){
- sys.log(m.substr(0,m.length-1));
+ session.on('logMessage', function (message) {
+ sys.log(message.substr(0, message.length - 1));
});
}
+
session.login(account.username, account.password, function (err) {
assert.ifError(err);
onsession(session);

0 comments on commit d08260f

Please sign in to comment.