Exemplo n.º 1
0
BC_FileBox::~BC_FileBox()
{
// this has to be destroyed before tables, because it can call for an update!
	delete newfolder_thread;
	delete fs;
	delete_tables();
	for(int i = 0; i < TOTAL_ICONS; i++)
		delete icons[i];
	filter_list.remove_all_objects();
	delete [] list_column;
	delete [] column_type;
	delete [] column_width;
	delete delete_thread;
	recent_dirs.remove_all_objects();
}
Exemplo n.º 2
0
/**
 * @brief Start the storage server.
 *
 * This is the main entry point for the storage server.  It reads the
 * configuration file, starts listening on a port, and proccesses
 * commands from clients.
 */
int main(int argc, char *argv[])
{
    char file_name[MAX_LOG_NAME] = "Server";
    server_time_log = fopen("server_times.log", "a");

    switch (LOGGING)
    {
        case 0:
            server_log = NULL;
            break;
            
        case 1:
            server_log = stdout;
            break;
            
        case 2:
            server_log = fopen(generate_logfile(file_name), "w");
            break;
    }
    
    // Process command line arguments.
    // This program expects exactly one argument: the config file name.
    assert(argc > 0);
    if (argc != 2)
    {
        printf("Usage %s <config_file>\n", argv[0]);
        exit(EXIT_FAILURE);
    }
    char *config_file = argv[1];
    
    // Read the config file.
    
    int status = read_config(config_file, &params);
    if (status != 0)
    {
        printf("Error processing config file.\n");
        exit(EXIT_FAILURE);
    }
    
    
    sprintf(log_buffer, "server main: Server on %s:%d\n", params.server_host, params.server_port);
    logger(server_log, log_buffer);
    
    // Create a socket.
    int listensock = socket(PF_INET, SOCK_STREAM, 0);
    if (listensock < 0)
    {
        printf("Error creating socket.\n");
        exit(EXIT_FAILURE);
    }
    
    // Allow listening port to be reused if defunct.
    int yes = 1;
    status = setsockopt(listensock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes);
    if (status != 0)
    {
        printf("Error configuring socket.\n");
        exit(EXIT_FAILURE);
    }
    
    // Bind it to the listening port.
    struct sockaddr_in listenaddr;
    memset(&listenaddr, 0, sizeof listenaddr);
    listenaddr.sin_family = AF_INET;
    listenaddr.sin_port = htons(params.server_port);
    inet_pton(AF_INET, params.server_host, &(listenaddr.sin_addr)); // bind to local IP address
    status = bind(listensock, (struct sockaddr*) &listenaddr, sizeof listenaddr);
    if (status != 0)
    {
        printf("Error binding socket.\n");
        exit(EXIT_FAILURE);
    }
    
    // Listen for connections.
    status = listen(listensock, MAX_LISTENQUEUELEN);
    if (status != 0)
    {
        printf("Error listening on socket.\n");
        exit(EXIT_FAILURE);
    }

    status = create_tables();
    if (status != 0)
    {
        printf("Error creating tables.\n");
        delete_tables();
        exit(EXIT_FAILURE);
    }

    int i;
    if(strcmp(params.table_names[0], "census") == 0)
        for(i = 0; i < params.num_tables; i++)
        {
            status = populate_database(params.table_names[i]);
            if(status != 0)
            {
                printf("Error populating tables.\n");
                delete_tables();
                exit(EXIT_FAILURE);
            }
        }

    // Listen loop.
    int wait_for_connections = 1;
    while (wait_for_connections)
    {
        // Wait for a connection.
        struct sockaddr_in clientaddr;
        socklen_t clientaddrlen = sizeof clientaddr;
        int clientsock = accept(listensock, (struct sockaddr*)&clientaddr, &clientaddrlen);
        if (clientsock < 0)
        {
            printf("Error accepting a connection.\n");
            delete_tables();
            exit(EXIT_FAILURE);
        }
        
        
        sprintf(log_buffer, "server main: Got a connection from %s:%d.\n", inet_ntoa(clientaddr.sin_addr), clientaddr.sin_port);
        logger(server_log, log_buffer);
        
        // Get commands from client.
        int wait_for_commands = 1;
        do
        {
            // Read a line from the client.
            char cmd[MAX_CMD_LEN] = {0};
            int status = recvline(clientsock, cmd, MAX_CMD_LEN);
            if (status != 0)
            {
                // Either an error occurred or the client closed the connection.
                wait_for_commands = 0;
            }
            else
            {
                // Handle the command from the client.
                int status = handle_command(clientsock, cmd);
                if (status != 0)
                    wait_for_commands = 0; // Oops.  An error occured.
            }
        }
        while (wait_for_commands);
        
        // Close the connection with the client.
        close(clientsock);
        
        
        sprintf(log_buffer, "server main: Closed connection from %s:%d.\n", inet_ntoa(clientaddr.sin_addr), clientaddr.sin_port);
        logger(server_log, log_buffer);
    }
    
    // Stop listening for connections.
    close(listensock);
 
    fprintf(server_time_log, "Total %d gets performed in %ld microseconds\n", n_gets, get_processing_time.tv_usec);
    fprintf(server_time_log, "Total %d sets performed in %ld microseconds\n", n_sets, set_processing_time.tv_usec);
    fclose(server_time_log);
 
    delete_tables();
    fclose(server_log);
    
    return EXIT_SUCCESS;
}
Exemplo n.º 3
0
int BC_FileBox::create_tables()
{
	delete_tables();
	char string[BCTEXTLEN];
	BC_ListBoxItem *new_item;

	fs->set_sort_order(sort_order);
	fs->set_sort_field(column_type[sort_column]);

// Directory is entered before this from a random source
	fs->update(0);
	for(int i = 0; i < fs->total_files(); i++)
	{
		FileItem *file_item = fs->get_entry(i);
		int is_dir = file_item->is_dir;
		BC_Pixmap* icon = get_icon(file_item->name, is_dir);

// Name entry
		new_item = new BC_ListBoxItem(file_item->name,
			icon, 
			is_dir ? get_resources()->directory_color : get_resources()->file_color);
		if(is_dir) new_item->set_searchable(0);
		list_column[column_of_type(FILEBOX_NAME)].append(new_item);
	
// Size entry
// 		if(!want_directory)
// 		{
			if(!is_dir)
			{
				sprintf(string, "%lld", file_item->size);
				new_item = new BC_ListBoxItem(string, get_resources()->file_color);
			}
			else
			{
				new_item = new BC_ListBoxItem("", get_resources()->directory_color);
			}

	 		list_column[column_of_type(FILEBOX_SIZE)].append(new_item);
//		}

// Date entry
		if(!is_dir || 1)
		{
			static const char *month_text[13] = 
			{
				"Null",
				"Jan",
				"Feb",
				"Mar",
				"Apr",
				"May",
				"Jun",
				"Jul",
				"Aug",
				"Sep",
				"Oct",
				"Nov",
				"Dec"
			};
			sprintf(string, 
				"%s %d, %d", 
				month_text[file_item->month],
				file_item->day,
				file_item->year);
			new_item = new BC_ListBoxItem(string, get_resources()->file_color);
		}
		else
		{
			new_item = new BC_ListBoxItem("", get_resources()->directory_color);
		}

		list_column[column_of_type(FILEBOX_DATE)].append(new_item);

// Extension entry
//		if(!want_directory)
//		{
			if(!is_dir)
			{
				extract_extension(string, file_item->name);
				new_item = new BC_ListBoxItem(string, get_resources()->file_color);
			}
			else
			{
				new_item = new BC_ListBoxItem("", get_resources()->directory_color);
			}
			list_column[column_of_type(FILEBOX_EXTENSION)].append(new_item);
//		}
	}

	return 0;
}