Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.55 KB | None | 0 0
  1. #include "tokenizer.h"
  2.  
  3. #include <stdlib.h>
  4. #include <stdbool.h>
  5. #include <linux/limits.h>
  6. #include <string.h>
  7. #include <sys/types.h>
  8. #include <sys/socket.h>
  9.  
  10. #include <netinet/in.h>
  11.  
  12. #include <sys/stat.h>
  13. #include <sys/types.h>
  14.  
  15. #include <unistd.h>
  16. #include <inttypes.h>
  17. #include <arpa/inet.h>
  18. #define _GNU_SOURCE
  19. #include <stdio.h>
  20.  
  21. typedef struct{
  22. char storage_name [50];
  23. char storage_mountpoint [PATH_MAX];
  24. int storage_mode;
  25. int storage_server_count;
  26. uint32_t storage_server_ips [10];
  27. int storage_server_ports [10];
  28. uint32_t storage_hotswap_ip;
  29. int storage_hotswap_port;
  30. } storage_struct;
  31.  
  32. typedef struct{
  33. FILE * clinet_errorlog;
  34. int client_cache_size;
  35. bool client_cache_rep;
  36. int client_timeout;
  37. storage_struct client_storages[10];
  38. int client_storage_count;
  39.  
  40. } client_struct;
  41.  
  42.  
  43. void readClientData(struct tokens *tokens, client_struct * client){
  44.  
  45. char * token_0 = tokens_get_token(tokens, 0);
  46. char * token_2 = tokens_get_token(tokens, 2);
  47.  
  48. if( strcmp(token_0, "errorlog") == 0){
  49. FILE * errorlog = fopen(token_2, "w");
  50. client->clinet_errorlog = errorlog;
  51. } else if(strcmp(token_0 , "cache_size") == 0){
  52. char * size = token_2;
  53. int len = strlen(size);
  54. size[len-1] = '\0';
  55. client->client_cache_size = atoi(size);
  56. } else if(strcmp(token_0 , "cache_replacment") == 0){
  57.  
  58. client->client_cache_rep = strcmp(token_2, "rlu") == 0;
  59.  
  60. } else if(strcmp(token_0 , "timeout") == 0){
  61. client->client_timeout = atoi(token_2);
  62. }
  63.  
  64. }
  65.  
  66. void readServerData(struct tokens *tokens, client_struct * client, int storage_count){
  67.  
  68.  
  69. char * token_0 = tokens_get_token(tokens, 0);
  70. char * token_2 = tokens_get_token(tokens, 2);
  71.  
  72. if(strcmp(token_0, "diskname") == 0){
  73. strcpy(client->client_storages[storage_count].storage_name, token_2);
  74. }else if(strcmp(token_0, "mountpoint") == 0){
  75. strcpy(client->client_storages[storage_count].storage_mountpoint, token_2);
  76. }else if(strcmp(token_0, "raid") == 0){
  77. if(token_2[0] == '1'){
  78. client->client_storages[storage_count].storage_mode = 1;
  79. } else{
  80. client->client_storages[storage_count].storage_mode = 5;
  81. }
  82. }else if(strcmp(token_0, "hotswap") == 0){
  83.  
  84. client->client_storages[storage_count].storage_hotswap_port = atoi(tokens_get_token(tokens, 3));
  85. inet_pton(AF_INET, token_2, &(client->client_storages[storage_count].storage_hotswap_ip));
  86.  
  87. } else if(strcmp(token_0, "servers") == 0){
  88. for(int i = 2; i<tokens_get_length(tokens); i++){
  89. if(i%2==0){
  90. inet_pton(AF_INET, token_2, &(client->client_storages[storage_count].storage_server_ips[i/2-1]));
  91. } else{
  92. client->client_storages[storage_count].storage_server_ports[i/2-1] = atoi(tokens_get_token(tokens, i));
  93. }
  94. }
  95. client->client_storages[storage_count].storage_server_count = tokens_get_length(tokens) / 2;
  96. }
  97.  
  98.  
  99.  
  100. // for(int i = 0; i<tokens_get_length(tokens); i++){
  101. // printf("%s\n", tokens_get_token(tokens, i));
  102. // }
  103. }
  104.  
  105.  
  106. int main(int argv, char** argc){
  107.  
  108. client_struct client;
  109.  
  110. bool reading_client = true;
  111. int storage_count = -1;
  112.  
  113. FILE * fp;
  114. char * line = NULL;
  115. size_t len = 0;
  116. size_t read;
  117.  
  118. fp = fopen("config_file", "r");
  119. if (fp == NULL)
  120. exit(EXIT_FAILURE);
  121.  
  122. while ((read = getline(&line, &len, fp)) != -1) {
  123. struct tokens *tokens = tokenize(line);
  124.  
  125. if(reading_client){
  126. if(tokens_get_length(tokens) > 0){
  127. readClientData(tokens, &client);
  128. } else{
  129. reading_client = false;
  130. storage_count++;
  131. }
  132. } else{
  133. if(tokens_get_length(tokens) > 0){
  134. readServerData(tokens, &client, storage_count);
  135. } else{
  136. storage_count++;
  137. }
  138. }
  139. }
  140.  
  141. client.client_storage_count = storage_count+1;
  142.  
  143. fclose(fp);
  144. if (line)
  145. free(line);
  146.  
  147.  
  148.  
  149. for(int i = 0; i<client.client_storage_count; i++){
  150. printf("%s\n", client.client_storages[i].storage_name);
  151. printf("%s\n", client.client_storages[i].storage_mountpoint);
  152. printf("%d\n", client.client_storages[i].storage_mode);
  153. printf("%d\n", client.client_storages[i].storage_hotswap_port);
  154. printf("hswap%" PRIu32 "\n",client.client_storages[i].storage_hotswap_ip);
  155.  
  156. for(int j = 0; j< client.client_storages[i].storage_server_count; j++){
  157. printf("ports: %d\n", client.client_storages[i].storage_server_ports[j]);
  158. printf("ports: %d\n", client.client_storages[i].storage_server_ips[j]);
  159. }
  160. }
  161.  
  162. exit(EXIT_SUCCESS);
  163.  
  164. return 0;
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement