Last active
August 29, 2015 14:19
-
-
Save jonas-lundqvist/4bb759a1f1ed09de46a1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <libspotify/api.h> | |
#include <pthread.h> | |
#include <stdio.h> | |
#include <sys/time.h> | |
extern const uint8_t g_appkey[]; | |
extern const size_t g_appkey_size; | |
extern const char *username; | |
extern const char *password; | |
typedef enum { | |
STATE_UNKNOWN, | |
STATE_LOGGING_IN_ONGOING, | |
STATE_LOGGED_IN, | |
STATE_LOGGING_OUT_ONGOING, | |
STATE_LOGGED_OUT | |
} state_e; | |
pthread_mutex_t g_lock; | |
pthread_cond_t g_wait; | |
int g_notify; | |
state_e g_state; | |
static SP_CALLCONV void logged_in_cb(sp_session *s, sp_error er); | |
static SP_CALLCONV void logged_out_cb(sp_session *s); | |
static SP_CALLCONV void notify_main_thread_cb(sp_session *s); | |
static sp_session_callbacks callbacks = { | |
.logged_in = &logged_in_cb, | |
.logged_out = &logged_out_cb, | |
.notify_main_thread = ¬ify_main_thread_cb, | |
}; | |
static sp_session_config config = { | |
.api_version = SPOTIFY_API_VERSION, | |
.application_key = g_appkey, | |
.application_key_size = 0, | |
.cache_location = "tmp", | |
.user_agent = "just-a-silly-test", | |
.callbacks = &callbacks | |
}; | |
int main(int argc, char *argv[]) | |
{ | |
sp_session *session; | |
sp_error err; | |
int next_timeout = 0; | |
int iterations = 0; | |
pthread_mutex_init(&g_lock, NULL); | |
pthread_cond_init(&g_wait, NULL); | |
config.application_key_size = g_appkey_size; | |
while(1) { | |
g_notify = 0; | |
next_timeout = 0; | |
g_state = STATE_UNKNOWN; | |
printf("Creating session #%d\n", iterations++); | |
err = sp_session_create(&config, &session); | |
if (SP_ERROR_OK != err) | |
printf("Error creating session: %s\n", sp_error_message(err)); | |
while (1) { | |
if (g_state == STATE_LOGGED_OUT) | |
break; | |
pthread_mutex_lock(&g_lock); | |
if (next_timeout == 0) { | |
while (!g_notify) { | |
printf("---- Waiting\n"); | |
pthread_cond_wait(&g_wait, &g_lock); | |
} | |
} else { | |
struct timespec ts; | |
struct timeval tv; | |
gettimeofday(&tv, NULL); | |
ts.tv_sec = tv.tv_sec + (next_timeout / 1000); | |
ts.tv_nsec = (tv.tv_usec+1000UL) * (next_timeout % 1000); | |
printf("---- Timed wait %d ms\n", next_timeout); | |
pthread_cond_timedwait(&g_wait, &g_lock, &ts); | |
} | |
g_notify = 0; | |
pthread_mutex_unlock(&g_lock); | |
if (g_state == STATE_UNKNOWN) { | |
printf("-- Login\n"); | |
err = sp_session_login(session, username, password, 0, NULL); | |
if (SP_ERROR_OK != err) | |
printf("Error logging in: %s\n", sp_error_message(err)); | |
g_state = STATE_LOGGING_IN_ONGOING; | |
} | |
if (g_state == STATE_LOGGED_IN) { | |
printf("-- Logout\n"); | |
err = sp_session_logout(session); | |
if (SP_ERROR_OK != err) | |
printf("Error logging out: %s\n", sp_error_message(err)); | |
g_state = STATE_LOGGING_OUT_ONGOING; | |
} | |
do { | |
printf("---- Processing\n"); | |
sp_session_process_events(session, &next_timeout); | |
} while (next_timeout == 0); | |
} | |
printf("Releasing session\n"); | |
sp_session_release(session); | |
} | |
pthread_cond_destroy(&g_wait); | |
pthread_mutex_destroy(&g_lock); | |
return 0; | |
} | |
static SP_CALLCONV void logged_in_cb(sp_session *s, sp_error er) | |
{ | |
printf("-- Logged in\n"); | |
g_state = STATE_LOGGED_IN; | |
} | |
static SP_CALLCONV void logged_out_cb(sp_session *s) | |
{ | |
printf("-- Logged out\n"); | |
g_state = STATE_LOGGED_OUT; | |
} | |
static SP_CALLCONV void notify_main_thread_cb(sp_session *s) | |
{ | |
printf("---- Notify\n"); | |
pthread_mutex_lock(&g_lock); | |
g_notify = 1; | |
pthread_cond_signal(&g_wait); | |
pthread_mutex_unlock(&g_lock); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Creating session #0 | |
---- Notify | |
-- Login | |
---- Processing | |
---- Timed wait 300821 ms | |
---- Notify | |
---- Processing | |
---- Timed wait 300657 ms | |
---- Notify | |
---- Processing | |
---- Timed wait 300513 ms | |
---- Notify | |
---- Processing | |
---- Timed wait 300374 ms | |
---- Notify | |
---- Processing | |
---- Timed wait 300233 ms | |
---- Notify | |
---- Processing | |
---- Timed wait 300094 ms | |
---- Notify | |
---- Processing | |
---- Timed wait 299950 ms | |
---- Notify | |
---- Processing | |
-- Logged in | |
---- Notify | |
-- Logged out | |
Releasing session | |
Creating session #1 | |
---- Notify | |
-- Login | |
---- Processing | |
---- Timed wait 300948 ms | |
---- Notify | |
---- Processing | |
---- Timed wait 300749 ms | |
---- Notify | |
---- Processing | |
---- Timed wait 300607 ms | |
---- Notify | |
---- Processing | |
---- Timed wait 300468 ms | |
---- Notify | |
---- Processing | |
---- Timed wait 300327 ms | |
---- Notify | |
---- Processing | |
---- Timed wait 300184 ms | |
---- Notify | |
---- Processing | |
---- Timed wait 300040 ms | |
---- Notify | |
---- Processing | |
-- Logged in | |
---- Timed wait 300040 ms | |
---- Notify | |
-- Logout | |
---- Processing | |
-- Logged out | |
Releasing session | |
Creating session #2 | |
---- Notify | |
-- Login | |
---- Processing | |
---- Timed wait 300038 ms | |
---- Notify | |
---- Processing | |
---- Timed wait 299840 ms | |
---- Notify | |
---- Processing | |
---- Timed wait 299694 ms | |
---- Notify | |
---- Processing | |
---- Timed wait 299549 ms | |
---- Notify | |
---- Processing | |
---- Timed wait 299405 ms | |
---- Notify | |
---- Processing | |
---- Timed wait 299265 ms | |
---- Notify | |
---- Processing | |
---- Timed wait 299117 ms | |
---- Notify | |
---- Processing | |
-- Logged in | |
---- Timed wait 299117 ms | |
---- Notify | |
-- Logout | |
---- Processing | |
-- Logged out | |
Releasing session | |
Creating session #3 | |
---- Notify | |
-- Login | |
---- Processing | |
---- Timed wait 300115 ms | |
---- Notify | |
---- Processing | |
---- Timed wait 299915 ms | |
---- Notify | |
---- Processing | |
---- Timed wait 299779 ms | |
---- Notify | |
---- Processing | |
---- Timed wait 299640 ms | |
---- Notify | |
---- Processing | |
---- Timed wait 299498 ms | |
---- Notify | |
---- Processing | |
---- Timed wait 299358 ms | |
---- Notify | |
---- Processing | |
---- Timed wait 299213 ms | |
---- Notify | |
---- Processing | |
-- Logged in | |
---- Timed wait 299213 ms | |
---- Notify | |
-- Logout |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment