static void test_queue_one_item(void) { TEST_ASSERT_EQUAL(QUEUE_EMPTY, queue_first(priv_test_queue)); TEST_ASSERT_EQUAL(QUEUE_SUCESS, queue_push(priv_test_queue, (void*) 1002u)); TEST_ASSERT_EQUAL(QUEUE_SUCESS, queue_first(priv_test_queue)); TEST_ASSERT_EQUAL(1002u, queue_get_current(priv_test_queue)); TEST_ASSERT_EQUAL(QUEUE_SUCESS, queue_next(priv_test_queue)); TEST_ASSERT_EQUAL(NULL, queue_get_current(priv_test_queue)); TEST_ASSERT_EQUAL(QUEUE_LAST, queue_next(priv_test_queue)); }
/*! * Builds the dependency by using the observer pattern. All the dependencies are * observed. * * \param this_ptr - A pointer to the task. * \param lookup - A lookup table which contains all the tasks.. */ int task_build_dependency(task_t *this_ptr, struct hash_lookup_t *lookup) { if (this_ptr->dependency_queue != NULL) { task_dependency_t *dependency; task_t *task; queue_first(this_ptr->dependency_queue); while ((dependency = queue_get_current(this_ptr->dependency_queue)) != NULL) { task = hash_lookup_find(lookup, dependency->id); if (task != NULL) { dependency->task = task; this_ptr->counter++; subject_attach((subject_t*) task, (observer_t*) this_ptr); } queue_next(this_ptr->dependency_queue); } } if (this_ptr->counter == 0) { task_handler_run_add_task(this_ptr->task_handler, this_ptr); } return 0; }
/*! * Detach an observer from a subject. * * \param this_ptr - A pointer to the subject. * \param observer - A pointer to the observer that is detached from * the subject. * * \return \c SUBJECT_SUCESS if it was possible to detach the observer, * \return \c SUBJECT_ERROR if it wasn't possible to remove the observer from * the queue. * \return \c SUBJECT_MISSING_OBSERVER if the observer is \c NULL. * \return \c SUBJECT_NULL if the subject is \c NULL. */ int subject_detach(subject_t *this_ptr, observer_t *observer) { int status = SUBJECT_SUCESS; observer_t *current; if (this_ptr != NULL) { if (observer != NULL) { queue_first(&this_ptr->queue); while((current = queue_get_current(&this_ptr->queue)) != NULL) { if (current == observer) { if (queue_remove_current( &this_ptr->queue) != QUEUE_SUCESS) { status = SUBJECT_ERROR; } } queue_next(&this_ptr->queue); } } else { status = SUBJECT_MISSING_OBSERVER; } } else { status = SUBJECT_NULL; } return status; }
static void test_queue_null(void) { TEST_ASSERT_EQUAL(QUEUE_NULL, queue_first(priv_test_queue)); TEST_ASSERT_EQUAL(QUEUE_NULL, queue_next(priv_test_queue)); TEST_ASSERT_EQUAL(QUEUE_NULL, queue_last(priv_test_queue)); TEST_ASSERT_EQUAL(QUEUE_NULL, queue_previous(priv_test_queue)); TEST_ASSERT_EQUAL(QUEUE_NULL, queue_push(priv_test_queue, (void*) 1001u)); TEST_ASSERT_EQUAL(NULL, queue_pop(priv_test_queue)); TEST_ASSERT_EQUAL(NULL, queue_get_current(priv_test_queue)); TEST_ASSERT_EQUAL(QUEUE_NULL, queue_remove_current(priv_test_queue)); }
void command_cur_playing(const struct command * const command) { sp_track *t; if((t = queue_get_current()) == NULL) { sock_send_str(command->sockfd, "Not playing a track right now.\n"); } else { char buf[API_MESSAGE_LEN]; track_to_str(buf, API_MESSAGE_LEN, t); sock_send_str(command->sockfd, buf); sock_send_str(command->sockfd, "\n"); } }
/*! * Internal function which searches for an data item from the queue. * * \param this_ptr - A pointer to the queue. * \param key - A unique key which identifies the data item. * * \return If it was successful return the data item, * otherwise return \c NULL. */ static void * hash_lookup_queue_find(queue_t *this_ptr, unsigned int key) { hash_data_t *current; data_t *data = NULL; queue_first(this_ptr); while ((current = queue_get_current(this_ptr)) != NULL) { if (current->key == key) { data = current->data; queue_last(this_ptr); } queue_next(this_ptr); } return data; }
/*! * Function which is used for notify all the observers. * * \param this_ptr - A pointer to the subject. * \param msg - A pointer to the message that is sent to the observers. */ int subject_notify(subject_t *this_ptr, void *msg) { int status = SUBJECT_SUCESS; if (this_ptr != NULL) { observer_t *current; queue_first(&this_ptr->queue); while((current = queue_get_current(&this_ptr->queue)) != NULL) { if (observer_notify(current, this_ptr, msg) != OBSERVER_SUCESS) { status = SUBJECT_MISSING_OBSERVER; } queue_next(&this_ptr->queue); } } else { status = SUBJECT_NULL; } return status; }