Exemplo n.º 1
0
static void initializeDb()
{
    QFontDatabasePrivate *db = privateDb();
    if (!db || db->count)
        return;

    populate_database(QString());

#ifdef QFONTDATABASE_DEBUG
    // print the database
    for (int f = 0; f < db->count; f++) {
        QtFontFamily *family = db->families[f];
        qDebug("    %s: %p", qPrintable(family->name), family);
        populate_database(family->name);

#if 0
        qDebug("        scripts supported:");
        for (int i = 0; i < QUnicodeTables::ScriptCount; i++)
            if(family->writingSystems[i] & QtFontFamily::Supported)
                qDebug("            %d", i);
        for (int fd = 0; fd < family->count; fd++) {
            QtFontFoundry *foundry = family->foundries[fd];
            qDebug("        %s", foundry->name.latin1());
            for (int s = 0; s < foundry->count; s++) {
                QtFontStyle *style = foundry->styles[s];
                qDebug("            style: style=%d weight=%d smooth=%d",  style->key.style,
                       style->key.weight, style->smoothScalable );
                if(!style->smoothScalable) {
                    for(int i = 0; i < style->count; ++i) {
                        qDebug("                %d", style->pixelSizes[i].pixelSize);
                    }
                }
            }
        }
#endif
    }
#endif // QFONTDATABASE_DEBUG

}
Exemplo n.º 2
0
static inline void load(const QString &family = QString(), int = -1)
{
    populate_database(family);
}
Exemplo n.º 3
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.º 4
0
/* main: this is supposedly required for a C program
 * 1. Create threads to handle clients
 * 2. Add clients from the file
 * 3. Join threads and leave
 * Returns 0 if everything was done correctly.
 */
int main(int argc, char** argv)
{	
	int num_queries, num_threads;
	long i;
	pthread_t * tids;

	if(argc != 3)
	{	
		fprintf(stderr,
			"USAGE: %s [input_file_path] [num_threads]\n",
			argv[0]);
		exit(1);
	}	

	// do preliminary work (database, queue, etc.)
	setup_queue();
	populate_database("TwitterDB.txt");

	num_threads = atoi(argv[2]);

	sem_init(&queue_access, 0, 1);
	sem_init(&empty, 0, 0);
	sem_init(&served, 0, 1);
	sem_init(&full, 0, num_threads);


	// create threads to handle clients
	tids = calloc(num_threads, sizeof(pthread_t));
	for(i = 0; i < num_threads; i++)
	{
	 	if(pthread_create(&tids[i], NULL, serveClients, (void *)(i + 1)))
		{
			perror("main: could not create create thread");
		    exit(1);
		}
	}	

	// populate queue
	num_queries = buildQueue(argv[1]);

	while(num_served != num_queries);
	

   	// cancel threads
	for (i = 0; i < num_threads; i++) 
	{
		if(pthread_cancel(tids[i])) 
		{
			perror("main: could not join thread");
		    exit(1);
		}
	}

	// clean up
	destroy_database();
	destroy_queue();

	free(tids);

	printf("Finished handling all clients\n");
	return 0;
}