Exemplo n.º 1
0
static thing_th *breakout_character(int inputChar) {
    char *label;
    thing_th *atom;
    asprintf(&label, "%c", inputChar);
    atom=Atom(label);
    erase_string(label);
    return atom;
}
Exemplo n.º 2
0
static int identified(grid_th *catalog, void *addy) {
    int result;
    char *idstr;
    if(!catalog)
        return 0;
    asprintf(&idstr, "%d", (int)addy);
    result=grid_key_exists(catalog->data, idstr);
    erase_string(idstr);
    return result;
}
Exemplo n.º 3
0
static thing_th *str_remaining_text(string_th *string) {
    char *text;
    thing_th *atom;
    if(!string->repr || strlen(string->repr)<2)
        return NULL;
    asprintf(&text, "%s", string->repr+1);
    atom=Atom(text);
    erase_string(text);
    return atom;
}
Exemplo n.º 4
0
static thing_th *str_first_char(string_th *string) {
    char *character;
    thing_th *atom;
    if(!string->repr)
        return NULL;
    asprintf(&character, "%c", string->repr[0]);
    atom=Atom(character);
    erase_string(character);
    return atom;
}
Exemplo n.º 5
0
static thing_th *inner_funky_length(thing_th *args) {
    unsigned long len=0;
    char *num;
    thing_th *outcome;
    while(args && (Cdr(args) || Car(args))) {
        ++len;
        args=Cdr(args);
    }
    asprintf(&num, "%ld", len);
    outcome=Number(num);
    erase_string(num);
    return outcome;
}
Exemplo n.º 6
0
thing_th *dirty_sum(thing_th *args) {
    long num=0;
    char *outty;
    thing_th *output;
    if(!args)
        return Number("0");
    while(args) {
        num+=text_to_long(Car(args));
        args=Cdr(args);
    }
    asprintf(&outty, "%ld", num);
    output=Number(outty);
    erase_string(outty);
    return output;
}
Exemplo n.º 7
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 time_log[25] , name[40], message[80];
	// 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.
	struct config_params params;
	record_tables.headTable = NULL; //Initializing the record
	record_tables.numTable = 0;     //to make sure.
	int status = read_config(config_file, &params, &record_tables);
	params.status_auth = -1;
	if (status != 0) {
        free_memory(&record_tables);
		printf("Error processing config file.\n");
		exit(EXIT_FAILURE);
	}

    ///This is the function we used for loading the 'census' file. It's used as a second (or more) parameter and is not strictly necessary.
	if(argc > 2) {
        int y;
        for(y = 2 ; y < argc ; y++) {
            FILE* pfile = fopen( argv[y] , "r");    ///Opens the file
            Table* tmp;
            Key* temp;
            tmp = find_table(argv[y] , &record_tables);
            if (tmp != NULL){                       ///See if the table (name of the file) exists.
                if (pfile != NULL) {
                    char c = 50;
                    char key[MAX_KEY_LEN] , value[MAX_VALUE_LEN];
                    while (c != EOF) {
                        fscanf (pfile , "%s %s" , key , value);     ///Read values from file.
                        temp = find_key(key , tmp);
                        struct storage_record record;
                        strcpy(record.value , value);
                        if(temp == NULL)
                            add_key(key , record  , tmp);           ///If the key doesn't exist it creates a new one.
                        else
                            strcpy(temp->record.value , value);     ///If the key exists it updates the key.
                        c = fgetc(pfile);
                    }
                }
            }
        }
	}

	generate_time_string_log(time_log);
	sprintf(name , "Server-%s", time_log );
	sprintf(message , "Server on %s:%d\n", params.server_host, params.server_port);

	if (LOGGING == 2) {
		log_file = fopen( name , "w" );
		if (log_file == NULL)
			printf("SERVER: An error occured genereting the log file\n");
	}

	else if (LOGGING == 1)
		log_file = stdout;

	if (LOGGING == 1 || LOGGING == 2)
		logger(log_file , message);

	erase_string(message);


	// Create a socket.
	int listensock = socket(PF_INET, SOCK_STREAM, 0);
	if (listensock < 0) {
        free_memory(&record_tables);
		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) {
        free_memory(&record_tables);
		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) {
        free_memory(&record_tables);
		printf("Error binding socket.\n");
		exit(EXIT_FAILURE);
	}

	// Listen for connections.
	status = listen(listensock, MAX_LISTENQUEUELEN);
	if (status != 0) {
        free_memory(&record_tables);
		printf("Error listening on socket.\n");
		exit(EXIT_FAILURE);
	}

	// Listen loop.
	int wait_for_connections = 1;
	pthread_t tip;
	while (wait_for_connections) {
		// Wait for a connection.
		params.status_auth = -1;        ///Every time it starts listening for connections it "de-authenticates" the server.
		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");
			free_memory(&record_tables);
			exit(EXIT_FAILURE);
		}

		sprintf(message , "Got a connection from %s:%d.\n", inet_ntoa(clientaddr.sin_addr), clientaddr.sin_port);

		if (LOGGING == 1 || LOGGING == 2)
			logger(log_file , message);
		erase_string(message);

		ArgThread *argth = (ArgThread*)malloc(sizeof(ArgThread));
		argth->clientsock = clientsock;
		strcpy(argth->parameters_server.username , params.username);
		strcpy(argth->parameters_server.password , params.password);
		argth->parameters_server.status_auth = params.status_auth;
		argth->parameters_server.concurrency = params.concurrency;
		strcpy(argth->client_addr , inet_ntoa(clientaddr.sin_addr));
		argth->client_port = clientaddr.sin_port;

		// Get commands from client.
        if(params.concurrency == 0)
            serve_client((void*) argth);
        else if(params.concurrency == 1)
            pthread_create( &tip , NULL , serve_client , (void*) argth);
        else if(params.concurrency == 2) {
            serve_client((void*) argth);
        }
        else if(params.concurrency == 3) {
            pid_t child;
            if((child = fork()) == 0) {
                close(listensock);
                serve_client((void *) argth);
                exit(0);
            }
            free(argth);
        }

	}

	// Stop listening for connections.
	close(listensock);
	if (LOGGING == 2)
		fclose(log_file);
    free_memory(&record_tables);
    //printf("%.5f - Server\n" , elapsedTime);
	return EXIT_SUCCESS;
}