Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.27 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/socket.h>
  4. #include <netinet/in.h>
  5. #include <arpa/inet.h>
  6. #include <unistd.h>
  7. #include <ctype.h>
  8. #include <sys/stat.h>
  9. #include <sys/wait.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <signal.h>
  13. #include <sys/shm.h>
  14. #include <sys/ipc.h>
  15. #include <sys/msg.h>
  16. #include <time.h>
  17. #include <pthread.h>
  18. #include <errno.h>
  19.  
  20.  
  21. typedef struct fic{
  22.     char nome[50];
  23.     struct fic * next;
  24. }fic;
  25.  
  26. typedef struct config{
  27.     int server_port;
  28.     char* scheduling;
  29.     int threadpool;
  30.     fic allowed[SIZE_BUF];
  31. }config;
  32.  
  33. config* cfg;
  34. int shm;
  35.  
  36. int main(int argc, char ** argv)
  37. {
  38.  
  39.  
  40. }
  41.  
  42.  
  43. void init(){
  44.  
  45.     // Criar e mapear memória partilhada
  46.         shm = shmget(1234,sizeof(config),IPC_CREAT|0777);
  47.         cfg = (config *)shmat(shm,NULL,0);
  48.  
  49.     config_inicial();
  50.  
  51.     // criar buffer
  52.     b_requests = (request_buffer*)malloc( sizeof(request_buffer));
  53.     b_requests->next = NULL;
  54.  
  55.     // alocar memória para a estrutura da mesage queue (mudar para memória partilhada)
  56.     statistics = (statistics_gerais*)malloc( sizeof(statistics_gerais));
  57. }
  58.  
  59.  
  60. void config_inicial(){
  61.     FILE * fp;
  62.     char line[1024];
  63.     char split[2] = " ";
  64.     int i;
  65.     char *token;
  66.    
  67.     fic * ptr = cfg->allowed;
  68.     fic * novo;
  69.  
  70.     i=0;
  71.  
  72.     if((fp=fopen("config.txt","rt"))==NULL) {
  73.         // ficheiro not found
  74.         printf("Ficheiro de configuracao nao encontrado. DEFAULT CONFIG\n");
  75.         cfp->server_port = port;
  76.         cfg->scheduling= "NORMAL"
  77.         cfg->threadpool = DEFAULT_NT;
  78.         strcpy (cfg->allowed->nome, "ALL");
  79.     }
  80.  
  81.     else {
  82.         // ficheiro found
  83.    
  84.         while(fgets(line, 1024, fp) != NULL){
  85.             //printf("%s\n", line);
  86.             if (i==0){
  87.                 if (atoi(line) != port){
  88.                     printf("O porto actual não corresponde ao porto no ficheiro de configuracao. O server vais terminar");
  89.                     exit(0);
  90.                 }
  91.             }
  92.             else if (i==1)
  93.                 cfg->scheduling = line;
  94.             else if (i==2)
  95.                 cfg->threadpool = atoi(line);
  96.             else if (i==3){
  97.                 token = strtok(line,split);
  98.                 while (token != NULL){                 
  99.                     novo = (fic *) malloc ( sizeof(fic));
  100.                     novo->next = NULL;
  101.                     strcpy(novo->nome, token);
  102.                     ptr->next = novo;
  103.                     ptr = novo;
  104.                     //printf("%s\n", ptr->nome);
  105.                     token = strtok(NULL,split);
  106.                 }
  107.  
  108.             }
  109.             i++;
  110.         }
  111.  
  112.        
  113.         // Close file
  114.         fclose(fp);
  115.     }
  116.     free(ptr);
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement