/* * Return the next item in the queue for the item with the given item-id. * * @param queue The queue * @param item_id The id of the item in the queue * @param shuffle If 0 return the next item in the play-queue, if 1 the next item in the shuffle-queue * @param r_mode Repeat mode * @param reshuffle If 1 and repeat mode is "repeat all" reshuffles the queue on wrap around * @return The next item */ struct queue_item * queue_next(struct queue *queue, unsigned int item_id, char shuffle, enum repeat_mode r_mode, int reshuffle) { struct queue_item *item; item = queueitem_get_byitemid(queue, item_id); if (!item) // Item not found, start playing from the start of the queue item = queue->head; if (r_mode == REPEAT_SONG && item != queue->head) return item; item = item_next(item, shuffle); if (item == queue->head && r_mode == REPEAT_ALL) { // Repeat all and end of queue reached, return first item in the queue if (reshuffle) queue_shuffle(queue, 0); item = item_next(queue->head, shuffle); } if (item == queue->head) return NULL; return item; }
int main () { queue_t* queuePtr; random_t* randomPtr; long data[] = {3, 1, 4, 1, 5}; long numData = sizeof(data) / sizeof(data[0]); long i; randomPtr = random_alloc(); assert(randomPtr); random_seed(randomPtr, 0); puts("Starting tests..."); queuePtr = queue_alloc(-1); assert(queue_isEmpty(queuePtr)); for (i = 0; i < numData; i++) { insertData(queuePtr, &data[i]); } assert(!queue_isEmpty(queuePtr)); for (i = 0; i < numData; i++) { long* dataPtr = (long*)queue_pop(queuePtr); printf("Removing %li: ", *dataPtr); printQueue(queuePtr); } assert(!queue_pop(queuePtr)); assert(queue_isEmpty(queuePtr)); puts("All tests passed."); for (i = 0; i < numData; i++) { insertData(queuePtr, &data[i]); } for (i = 0; i < numData; i++) { printf("Shuffle %li: ", i); queue_shuffle(queuePtr, randomPtr); printQueue(queuePtr); } assert(!queue_isEmpty(queuePtr)); queue_free(queuePtr); return 0; }
void command_qrand(sp_session *session, const struct command * const command) { queue_shuffle(); sock_send_str(command->sockfd, "Shuffled queue.\n"); }
/** * Handler to check user input, and see if it matches any avaible commands. * Will call the right methods for executing commands */ void handle_keyboard(sp_session *session, struct play_queue* node) { char buffer[1024]; fgets(buffer, sizeof(buffer), stdin); strtok(buffer, "\n"); if (strcmp(buffer, "search") == 0) { player_reset(); run_search(session); } else if ((strcmp(buffer, "list") == 0) || (strcmp(buffer, "ls") == 0 )) { print_playlists(session, pc); } else if(strcmp(buffer, "qshuffle") == 0) { queue_shuffle(); } else if (strcmp(buffer, "queueadd") == 0) { sp_playlist* pl = parse_play_command(session, buffer, node); printf("done finding playlist \n"); if(pl != NULL) printf("queueadd: %s\n", sp_playlist_name(pl)); else { printf("no playlist\n"); return; } int index; char input[10]; fputs("Song number: ", stdout); fgets(input, sizeof(input) - 1, stdin); sscanf(input, "%d", &index); if(sp_playlist_num_tracks(pl) < index) { printf("index too high!\n"); return; } sp_track* track = pl_find_song_by_id(pl, index); if(track != NULL) queue_add_first(track); } else if (strcmp(buffer, "list songs") == 0 ) { //release all threads sp_playlist* pl = playlist_find_by_num(session, pc); print_tracks_in_playlist(session, pl); } else if (strcmp(buffer, "help") == 0) { print_commands(); } else if (strcmp(buffer, "queue") == 0) { queue_print(node); } else if (strcmp(buffer, "shuffle mode") == 0) { print_commands(); } else if(strncmp(buffer, "play", strlen("play")) == 0) { player_reset(); sp_playlist* pl = parse_play_command(session, buffer, node); if(pl!=NULL) queue_add_playlist(pl); else { printf("ERROR playlist is null\n"); return; } queue_go_next(session); } else if(strncmp(buffer, "shuffle", strlen("shuffle")) == 0) { player_reset(); shuffle_mode = TRUE; sp_playlist* pl = parse_play_command(session, buffer, node); if(pl!=NULL) queue_add_playlist(pl); else { printf("ERROR playlist is null\n"); return; } queue_shuffle(); queue_go_next(session); } else if(strcmp(buffer, "pause") == 0 || strcmp(buffer, "p") == 0) { player_pause(session); play_info(); } else if (strcmp(buffer, "next") == 0 || strcmp(buffer, "n") == 0) { end_track(session); } else if (strcmp(buffer, "stop") == 0) { } else if (strcmp(buffer, "info") == 0) { play_info(); } else if (strcmp(buffer, "quit") == 0) { queue_free(); quit_program(session); } else { printf("Unkown command!\n"); } printf("> "); fflush(stdout); return; }
/* ============================================================================= * stream_generate * -- Returns number of attacks generated * ============================================================================= */ long stream_generate (stream_t* streamPtr, dictionary_t* dictionaryPtr, long numFlow, long seed, long maxLength) { long numAttack = 0; long percentAttack = streamPtr->percentAttack; random_t* randomPtr = streamPtr->randomPtr; vector_t* allocVectorPtr = streamPtr->allocVectorPtr; queue_t* packetQueuePtr = streamPtr->packetQueuePtr; MAP_T* attackMapPtr = streamPtr->attackMapPtr; detector_t* detectorPtr = detector_alloc(); detector_addPreprocessor(detectorPtr, &preprocessor_toLower); random_seed(randomPtr, seed); queue_clear(packetQueuePtr); long range = '~' - ' ' + 1; long f; for (f = 1; f <= numFlow; f++) { char* str; if ((random_generate(randomPtr) % 100) < percentAttack) { long s = random_generate(randomPtr) % global_numDefaultSignature; str = dictionary_get(dictionaryPtr, s); bool_t status = MAP_INSERT(attackMapPtr, (void*)f, (void*)str); numAttack++; } else { /* * Create random string */ long length = (random_generate(randomPtr) % maxLength) + 1; str = (char*)malloc((length + 1) * sizeof(char)); bool_t status = vector_pushBack(allocVectorPtr, (void*)str); long l; for (l = 0; l < length; l++) { str[l] = ' ' + (char)(random_generate(randomPtr) % range); } str[l] = '\0'; char* str2 = (char*)malloc((length + 1) * sizeof(char)); strcpy(str2, str); error_t error = detector_process(detectorPtr, str2); /* updates in-place */ if (error == ERROR_SIGNATURE) { bool_t status = MAP_INSERT(attackMapPtr, (void*)f, (void*)str); numAttack++; } free(str2); } splitIntoPackets(str, f, randomPtr, allocVectorPtr, packetQueuePtr); } queue_shuffle(packetQueuePtr, randomPtr); detector_free(detectorPtr); return numAttack; }