Пример #1
0
void redis_disconnect_callback(const redisAsyncContext* c, int status)
{
    struct nbd_handle* handle;

    if (c->data)
    {
       handle = (struct nbd_handle*) c->data;
    }
    else
    {
        fprintf_light_red(stderr, "FATAL: Handle not passed to disconnect "
                                  "callback.\n");
        assert(c->data != NULL);
        return;
    }

    if (status != REDIS_OK)
    {
        if (c->err == REDIS_ERR_EOF) /* probably standard timeout, reconnect */
        {
            fprintf_red(stderr, "Redis server disconnected us.\n");
            if ((handle->redis_c = redisAsyncConnect(handle->redis_server,
                 handle->redis_port)) != NULL)
            {
                fprintf_blue(stderr, "New Redis context, attaching to "
                                    "libevent.\n");
                handle->redis_c->data = c->data;
                redisLibeventAttach(handle->redis_c, handle->eb);
                fprintf_blue(stderr, "Setting disconnect callback.\n");
                if (redisAsyncSetDisconnectCallback(handle->redis_c,
                    &redis_disconnect_callback) != REDIS_ERR)
                {
                    assert(redisAsyncCommand(handle->redis_c,
                           &redis_async_callback, NULL, "select %d",
                           handle->redis_db) == REDIS_OK);
                    fprintf_light_blue(stderr, "Successfully reconnected to "
                                               "the Redis server.\n");
                }
                else
                {
                    fprintf_light_red(stderr, "Error setting disconnect "
                                              "callback handler for Redis.\n");
                }
            }
            else
            {
                fprintf_light_red(stderr, "Error trying to reconnect to "
                                          "Redis.\n");
            }
            return;
        }
        fprintf_light_red(stderr, "FATAL ERROR DISCONNECTION FROM REDIS\n");
        fprintf_light_blue(stderr, "Error: %s\n", c->errstr);
        assert(false);
    }
}
Пример #2
0
Файл: main.c Проект: orls/Tigger
int listTasks(){
	/*This function does the following:
	 -open up the .tigger file
	 -print out all of the tasks in the .tigger file
	 -close the .tigger file
	 */
	char line[255];
	int count = 0;
	if(!tiggerExists()){
		return 0;
	}
	FILE *file = fopen(".tigger", "rt");
	if (COLOR_FLAG)
                fprintf_white(stdout, "\nLoading tasks from tigger...\n-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-\n");
        else
                printf("\nLoading tasks from tigger...\n-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-\n");
	while(fgets(line, 255, file) != NULL){
		if(!protectedText(line)){
			count += 1;
                        if (COLOR_FLAG) {
                                fprintf_yellow(stdout, "Task ");
        		        fprintf(stdout, "%d", count);
                                fprintf_yellow(stdout, ": \n");
		                fprintf_green(stdout, trimwhitespace(line));
                        } else {
			        printf("Task %d: \n", count);
			        printf("%s", trimwhitespace(line));
                        }
			printf("\n");

		}
	}
	if (count > 0){
		if (COLOR_FLAG) {
                        fprintf_blue(stdout, "You have ");
                        fprintf(stdout, "%d", count);
                        fprintf_blue(stdout, " tasks waiting to be completed.\n");
		        fprintf_white(stdout, "-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-\n");
                } else {
                        printf("You have %d tasks waiting to be completed.\n", count);
                        printf("-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-\n");
               }

	}else{
		if (COLOR_FLAG)
                        fprintf_blue(stdout, "Yay! You have no tasks remaining. Go have a beer.\n");
                else 
                        printf("Yay! You have no tasks remaining. Go have a beer.\n");
	}
	fclose(file);
	return 1;
}    
Пример #3
0
int main(int argc, char* argv[])
{
    int f;
    struct bson_info* bson;
    int ret;

    fprintf_blue(stdout, "BSON Printer -- By: Wolfgang Richter "
                         "<*****@*****.**>\n");

    if (argc < 2)
    {
        fprintf_light_red(stderr, "Usage: %s <BSON file>\n", argv[0]);
        return EXIT_FAILURE;
    }

    fprintf_cyan(stdout, "Analyzing BSON File: %s\n", argv[1]);

    f = open(argv[1], O_RDONLY);

    if (f < 0)
    {
        fprintf_light_red(stderr, "Error opening BSON file.\n");
        return EXIT_FAILURE;
    }

    bson = bson_init();

    while ((ret = bson_readf(bson, f)) == 1)
        bson_print(stdout, bson);
    
    bson_cleanup(bson);
    check_syscall(close(f));

    return EXIT_SUCCESS;
}
Пример #4
0
int main(int argc, char* argv[])
{
    struct nbd_handle* handle;
   
    if (argc < 9)
    {
        fprintf_light_red(stderr, USAGE, argv[0]);
        exit(EXIT_FAILURE);
    }

    fprintf_blue(stdout, "nbd-queuer-test program by: Wolfgang Richter "
                         "<*****@*****.**>\n");

    handle = nbd_init_redis(argv[1], argv[2], atoi(argv[3]), atoi(argv[4]),
                            atoll(argv[5]), argv[6], argv[7],
                            (strncmp(argv[8], "y", 1) == 0) ||
                            (strncmp(argv[8], "Y", 1) == 0));

    assert(handle != NULL);
    assert(handle->fd != 0);
    assert(strncmp(argv[1], handle->export_name, strlen(argv[1])) == 0);
    assert(handle->eb != NULL);
    assert(handle->conn != NULL);
    assert(handle->size >= 0);

    /* special case allow for example /dev/null to appear as a large file */
    if (handle->size == 0) handle->size = 1024*1024*1024*1024LL;
    
    nbd_run_loop(handle);
    nbd_shutdown(handle);

    fprintf_green(stdout, "-- Shutting down --\n");
    return EXIT_SUCCESS;
}
Пример #5
0
Файл: main.c Проект: orls/Tigger
int processCommand(char *args[], int optind){
	if(args[optind] != NULL){
		if(!strcmp(args[optind], "init")){
			if (COLOR_FLAG)
                                fprintf_blue(stdout, "Initializing Tigger in the current directory.\n");
                        else
                                printf("Initializing Tigger in the current directory.\n");
			return initialize(args, optind+1);
		}else if(!strcmp(args[optind], "new")){
			return addTask(args, optind+1);
		}else if(!strcmp(args[optind], "tasks")){
			return listTasks();
		}else if(!strcmp(args[optind], "tig")){
			printf("%s",tigger);
			return 1;
		}else if(!strcmp(args[optind], "completed")){
			return completedTasks();
		}else if(!strcmp(args[optind], "delete")){
			return deleteTask(args[optind+1]);
		}else if(!strcmp(args[optind], "today")){
			tiggerToday();   
			return 1;
		}
	}
	return 0;
}
Пример #6
0
Файл: main.c Проект: orls/Tigger
int tiggerToday(){                                           
	int count = 0;
	char * found;
	char * delim = "<?TIG?>"; 
	char line[255];
	char stime[10]; 
	int itime = 0;
	FILE *file = fopen(".tigger_completed", "r");             
	while(fgets(line, 255, file) != NULL){
		if(!protectedText(line)){     
			found = strstr(line, delim); 
			int place = found-line;
			strncpy(stime, line, place);
			itime = atoi(stime);
			if(itime > (time(NULL) - 86400)){
				count += 1; 
			}
		}
	}
        printf("%d ", count);
        if (COLOR_FLAG)
                fprintf_blue(stdout, "tasks have been completed today.\n");
        else
                printf("tasks have been completed today.\n");

        return 0;
}
Пример #7
0
int main(int argc, char* argv[])
{
    uint64_t i;
    struct bitarray* bits;

    fprintf_blue(stdout, "-- Bitarray Test Suite --\n");

    fprintf_light_blue(stdout, "* test bitarray_init()\n");
    bits = bitarray_init(8*512);
    bitarray_print(bits);
    fprintf_light_blue(stdout, "* test bitarray_set_all()\n");
    bitarray_set_all(bits);
    bitarray_print(bits);
    fprintf_light_blue(stdout, "* test bitarray_clear()\n");
    bitarray_unset_all(bits);
    bitarray_print(bits);

    fprintf_light_blue(stdout, "* test bitarray_set_bit()\n");
    for (i = 0; i < 31; i++)
    {
        bitarray_set_bit(bits, test_nums[i]);
    }

    fprintf_light_blue(stdout, "* test bitarray_get_bit()\n");
    for (i = 0; i < 31; i++)
    {
        assert(bitarray_get_bit(bits, test_nums[i]) == true);
    }


    fprintf_light_blue(stdout, "* test bitarray_unset_bit()\n");
    bitarray_unset_bit(bits, test_nums[4]);
    bitarray_unset_bit(bits, test_nums[9]);
    for (i = 0; i < 31; i++)
    {
        if (i == 4 || i == 9) continue;
        assert(bitarray_get_bit(bits, test_nums[i]) == true);
    }

    for (i = 0; i < 31; i++)
    {
        bitarray_unset_bit(bits, test_nums[i]);
    }

    for (i = 0; i < 31; i++)
    {
        assert(bitarray_get_bit(bits, test_nums[i]) == false);
    }

    bitarray_print(bits);

    bitarray_destroy(bits);

    test_throughput();

    return EXIT_SUCCESS;
}
Пример #8
0
Файл: main.c Проект: orls/Tigger
void printUsage(){
        if (COLOR_FLAG) {
	        fprintf_yellow(stdout, "Tigger -v: 0.26\n");
                fprintf_red(stdout, "Sorry we didn't recognize your command.\n");
                fprintf_blue(stdout, "Usage: tigger [--color|-c, --force|-f] [COMMAND] [PARAMS] \nCommands include but are "
                                     "not limited to:\n\tinit\n\tnew [\"task-name\"]\n\ttasks\n\ttig"
                                     "\n\tcompleted\n\tdelete [\"task-name\"]\n\ttoday\n");
        } else {
	        printf("Tigger -v: 0.26\nSorry we didn't recognize your command.\n"
                       "Usage: tigger [--color|-c, --force|-f] [COMMAND] [PARAMS] \nCommands include but are "
                       "not limited to:\n\tinit\n\tnew [\"task-name\"]\n\ttasks\n\ttig"
                       "\n\tcompleted\n\tdelete [\"task-name\"]\n\ttoday\n");
        }
}
Пример #9
0
Файл: main.c Проект: orls/Tigger
int deleteTask(char * task){ 
	char line[255];
	const char *TIGGER_FILE_NAME = ".tigger";
	const char *TIGGER_TEMP = ".tigger_temp";
	int found = 0;
	if(task == NULL){
		if (COLOR_FLAG)
                        fprintf_red(stdout, "Can't delete a null task.\n");
                else
                        printf("Can't delete a null task.\n");
		return 0;
	}     
	FILE *file = fopen(TIGGER_FILE_NAME, "r");
	FILE *temp = fopen(TIGGER_TEMP, "w");
	while(fgets(line, 255, file) != NULL){
		if(strcmp(trimwhitespace(line), trimwhitespace(task)) != 0){
                        fprintf(temp, "%s",line);
			fprintf(temp, "\n");  
		}else{
			found = 1;
		}
	}
	fclose(file);
	fclose(temp);
	if(found){
		if (COLOR_FLAG)
                        fprintf_blue(stdout, "We found your task and deleted it.\n");
                else
                        printf("We found your task and deleted it.\n");
		system("rm .tigger");
		rename(TIGGER_TEMP, TIGGER_FILE_NAME);  
		return 1;
	}else{
		system("rm .tigger_temp");
		if (COLOR_FLAG)
                        fprintf_red(stdout, "Sorry we couldn't find the task to delete.\n");
                else
                        printf("Sorry we couldn't find the task to delete.\n");
		return 0;
	}
}
Пример #10
0
/* main thread of execution */
int main(int argc, char* args[])
{
    int fd;
    char* index, *db, *stream;
    FILE* indexf;
    struct bitarray* bits;

    fprintf_blue(stdout, "gammaray Async Queuer -- "
                         "By: Wolfgang Richter "
                         "<*****@*****.**>\n");
    redis_print_version();

    if (argc < 4)
    {
        fprintf_light_red(stderr, "Usage: %s <index file> <stream file>"
                                  " <redis db num>\n", args[0]);
        return EXIT_FAILURE;
    }

    index = args[1];
    stream = args[2];
    db = args[3];

    /* ----------------- hiredis ----------------- */
    struct kv_store* handle = redis_init(db, true);
    if (handle == NULL)
    {
        fprintf_light_red(stderr, "Failed getting Redis context "
                                  "(connection failure?).\n");
        return EXIT_FAILURE;
    }

    fprintf_cyan(stdout, "Loading MD filter from: %s\n\n", index);
    indexf = fopen(index, "r");

    if (indexf == NULL)
    {
        fprintf_light_red(stderr, "Error opening index file to get MD "
                                  "filter.\n");
        return EXIT_FAILURE;
    }

    if (qemu_load_md_filter(indexf, &bits))
    {
        fprintf_light_red(stderr, "Error getting MD filter from BSON file.\n");
        bits = bitarray_init(5242880);
        bitarray_set_all(bits);
    }

    if (bits == NULL)
    {
        fprintf_light_red(stderr, "Bitarray is NULL!\n");
        return EXIT_FAILURE;
    }

    fclose(indexf);

    fprintf_cyan(stdout, "Attaching to stream: %s\n\n", stream);

    on_exit((void (*) (int, void *)) redis_shutdown, handle);

    if (strcmp(stream, "-") != 0)
    {
        fd = open(stream, O_RDONLY);
    }
    else
    {
        fd = STDIN_FILENO;
    }
    
    if (fd == -1)
    {
        fprintf_light_red(stderr, "Error opening stream file. "
                                  "Does it exist?\n");
        return EXIT_FAILURE;
    }

    read_loop(fd, handle, bits);
    close(fd);
    return EXIT_SUCCESS;
}
Пример #11
0
Файл: main.c Проект: orls/Tigger
int completedTasks(){
	/*This function does the following:
	 -open up the .tigger_completed file
	 -print out all of the tasks in the .tigger_completed file
	 -close the .tigger_completed file
	 */
	char line[255];
	int count = 0; 
	char * delim = "<?TIG?>";
	char * found;   
	size_t index;  
	char *new_string;
	if(!tiggerExists()){
		return 0;
	}
	FILE *file = fopen(".tigger_completed", "rt");
	if (COLOR_FLAG)
                fprintf_white(stdout, "\nLoading completed tasks from tigger...\n-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-\n");
        else
                printf("\nLoading completed tasks from tigger...\n-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-\n");
	while(fgets(line, 255, file) != NULL){
		if(!protectedText(line)){
			count += 1;    
			found = strstr(line, delim);
			if(found != NULL){
				index = found - line;
			}  
			new_string = &line[index+7];
			if (COLOR_FLAG) {
                                fprintf_yellow(stdout, "Task ");
                                fprintf(stdout, "%d", count);
                                fprintf_yellow(stdout, ": \n");
			        fprintf_green(stdout, trimwhitespace(new_string));
			        printf("\n");
                        } else {
                                printf("Task %d: \n", count);
			        printf("%s",trimwhitespace(new_string));
			        printf("\n");
                        }

		}
	}
	if (count > 0){
                if (COLOR_FLAG) {
		        fprintf_blue(stdout, "You have completed ");
                        fprintf(stdout, "%d", count);
                        fprintf_blue(stdout, " tasks. Congrats!\n");
		        fprintf_white(stdout, "-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-\n");
                } else {
		        printf("You have completed %d tasks. Congrats!\n", count);
		        printf("-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-\n");
                }

	}else{
		if (COLOR_FLAG)
                        fprintf_red(stdout, "You haven't completed any tasks and are lazy. That is all.\n");
                else
                        printf("You haven't completed any tasks and are lazy. That is all.\n");
	}
	fclose(file);
	return 1;

}