Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "tokenizer.h"
- #include <stdlib.h>
- #include <stdbool.h>
- #include <linux/limits.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <inttypes.h>
- #include <arpa/inet.h>
- #define _GNU_SOURCE
- #include <stdio.h>
- typedef struct{
- char storage_name [50];
- char storage_mountpoint [PATH_MAX];
- int storage_mode;
- int storage_server_count;
- uint32_t storage_server_ips [10];
- int storage_server_ports [10];
- uint32_t storage_hotswap_ip;
- int storage_hotswap_port;
- } storage_struct;
- typedef struct{
- FILE * clinet_errorlog;
- int client_cache_size;
- bool client_cache_rep;
- int client_timeout;
- storage_struct client_storages[10];
- int client_storage_count;
- } client_struct;
- void readClientData(struct tokens *tokens, client_struct * client){
- char * token_0 = tokens_get_token(tokens, 0);
- char * token_2 = tokens_get_token(tokens, 2);
- if( strcmp(token_0, "errorlog") == 0){
- FILE * errorlog = fopen(token_2, "w");
- client->clinet_errorlog = errorlog;
- } else if(strcmp(token_0 , "cache_size") == 0){
- char * size = token_2;
- int len = strlen(size);
- size[len-1] = '\0';
- client->client_cache_size = atoi(size);
- } else if(strcmp(token_0 , "cache_replacment") == 0){
- client->client_cache_rep = strcmp(token_2, "rlu") == 0;
- } else if(strcmp(token_0 , "timeout") == 0){
- client->client_timeout = atoi(token_2);
- }
- }
- void readServerData(struct tokens *tokens, client_struct * client, int storage_count){
- char * token_0 = tokens_get_token(tokens, 0);
- char * token_2 = tokens_get_token(tokens, 2);
- if(strcmp(token_0, "diskname") == 0){
- strcpy(client->client_storages[storage_count].storage_name, token_2);
- }else if(strcmp(token_0, "mountpoint") == 0){
- strcpy(client->client_storages[storage_count].storage_mountpoint, token_2);
- }else if(strcmp(token_0, "raid") == 0){
- if(token_2[0] == '1'){
- client->client_storages[storage_count].storage_mode = 1;
- } else{
- client->client_storages[storage_count].storage_mode = 5;
- }
- }else if(strcmp(token_0, "hotswap") == 0){
- client->client_storages[storage_count].storage_hotswap_port = atoi(tokens_get_token(tokens, 3));
- inet_pton(AF_INET, token_2, &(client->client_storages[storage_count].storage_hotswap_ip));
- } else if(strcmp(token_0, "servers") == 0){
- for(int i = 2; i<tokens_get_length(tokens); i++){
- if(i%2==0){
- inet_pton(AF_INET, token_2, &(client->client_storages[storage_count].storage_server_ips[i/2-1]));
- } else{
- client->client_storages[storage_count].storage_server_ports[i/2-1] = atoi(tokens_get_token(tokens, i));
- }
- }
- client->client_storages[storage_count].storage_server_count = tokens_get_length(tokens) / 2;
- }
- // for(int i = 0; i<tokens_get_length(tokens); i++){
- // printf("%s\n", tokens_get_token(tokens, i));
- // }
- }
- int main(int argv, char** argc){
- client_struct client;
- bool reading_client = true;
- int storage_count = -1;
- FILE * fp;
- char * line = NULL;
- size_t len = 0;
- size_t read;
- fp = fopen("config_file", "r");
- if (fp == NULL)
- exit(EXIT_FAILURE);
- while ((read = getline(&line, &len, fp)) != -1) {
- struct tokens *tokens = tokenize(line);
- if(reading_client){
- if(tokens_get_length(tokens) > 0){
- readClientData(tokens, &client);
- } else{
- reading_client = false;
- storage_count++;
- }
- } else{
- if(tokens_get_length(tokens) > 0){
- readServerData(tokens, &client, storage_count);
- } else{
- storage_count++;
- }
- }
- }
- client.client_storage_count = storage_count+1;
- fclose(fp);
- if (line)
- free(line);
- for(int i = 0; i<client.client_storage_count; i++){
- printf("%s\n", client.client_storages[i].storage_name);
- printf("%s\n", client.client_storages[i].storage_mountpoint);
- printf("%d\n", client.client_storages[i].storage_mode);
- printf("%d\n", client.client_storages[i].storage_hotswap_port);
- printf("hswap%" PRIu32 "\n",client.client_storages[i].storage_hotswap_ip);
- for(int j = 0; j< client.client_storages[i].storage_server_count; j++){
- printf("ports: %d\n", client.client_storages[i].storage_server_ports[j]);
- printf("ports: %d\n", client.client_storages[i].storage_server_ips[j]);
- }
- }
- exit(EXIT_SUCCESS);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement