Changeset 213 for Evidence/DColl.cc


Ignore:
Timestamp:
05/25/10 08:54:18 (16 years ago)
Author:
ogrimm
Message:
History read from file if not found in memory
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Evidence/DColl.cc

    r212 r213  
    8080        void AddService(string);
    8181        void RemoveService(string);
    82         float FileSize(FILE *);
     82        off_t FileSize(FILE *);
    8383        FILE *OpenHistFile(string, const char *);
    8484   
     
    124124  DataSizeService = new DimService(SERVER_NAME "/DataSizekB", DataSizekB);
    125125 
    126   LogSizekB = FileSize(LogFile);
     126  LogSizekB = FileSize(LogFile)/1024.0;
    127127  LogSizeService = new DimService(SERVER_NAME "/LogSizekB", LogSizekB);
    128128
     
    220220  // ====== Part A: Handle service subscriptions ===
    221221  //
     222  // Services are added here, removal only in constructor.
    222223 
    223224  // If service is DIS_DNS/SERVER_LIST, subscribe to all SERVICE_LIST services
     
    225226        char *Token = strtok(Info->getString(), "+-!@");       
    226227        while (Token != NULL) {
    227           if (*Info->getString()=='+') AddService(string(Token)+"/SERVICE_LIST");
     228          AddService(string(Token)+"/SERVICE_LIST"); // 'add' also for '-' and '!'
    228229          Token = strtok(NULL, "|"); // Skip server IP address
    229230          Token = strtok(NULL, "@");
     
    240241          char *Type = strtok(NULL, "\n");
    241242          if (Type == NULL) return; // for safety, should not happen
    242       if (strstr(Type, "|CMD")==NULL && strstr(Type, "|RPC")==NULL) {
    243             // Add or remove service
    244             if (*Info->getString()=='+') AddService(Name);
    245           }
     243      if (strstr(Type, "|CMD")==NULL && strstr(Type, "|RPC")==NULL) AddService(Name); // 'add' also for '-' and '!'
    246244          Name = strtok(NULL, "|");
    247245        }
     
    346344      fflush(DataFile); // not continuously to reduce load
    347345
    348           DataSizekB = FileSize(DataFile);
     346          DataSizekB = FileSize(DataFile)/1024.0;
    349347          DataSizeService->updateService();
    350348          DataSizeLastUpdate = time(NULL);
     
    454452  // Update logfile size service
    455453  if (time(NULL) - LogSizeLastUpdate > SizeUpdateDelay) {
    456         LogSizekB = FileSize(LogFile);
     454        LogSizekB = FileSize(LogFile)/1024.0;
    457455        LogSizeService->updateService();
    458456        LogSizeLastUpdate = time(NULL);
     
    473471        }
    474472  }
    475  
    476   // Default response
    477   setData(NULL, 0);
     473
     474  // Try to open history file if not found in memory
     475  FILE *File = OpenHistFile(getString(), "rb");
     476  if (File == NULL) {
     477    setData(NULL, 0);
     478        return;
     479  }
     480
     481  // Read history file
     482  off_t Size = FileSize(File);
     483  if (Size != -1) {
     484        char *Buffer = new char [Size-sizeof(int)];
     485        fseek(File, sizeof(int), SEEK_SET);
     486        fread(Buffer, sizeof(char), Size-sizeof(int), File);
     487        if (ferror(File) != 0) {
     488          State(WARN, "Error reading history file '%s' in rpcHandler()", getString());
     489          setData(NULL, 0);             // Default response
     490        }
     491        else setData((void *) Buffer, Size);
     492        delete[] Buffer;
     493  }
     494 
     495  if (fclose(File) != 0) State(WARN, "Error closing history file '%s' in rpcHandler()", getString());
    478496}
    479497
     
    517535      fread(&New.Next, sizeof(New.Next), 1, File);
    518536      fread(New.Buffer, sizeof(char), New.HistSize, File);
    519       fclose(File);
     537          if (ferror(File) != 0) State(WARN, "Error reading history file '%s' in AddService()", Name.c_str());
     538      if (fclose(File) != 0) State(WARN, "Error closing history file '%s' in AddService()", Name.c_str());;
    520539        }
    521540  }
     
    545564      fwrite(&(*E).Next, sizeof((*E).Next), 1, File);
    546565      fwrite((*E).Buffer, sizeof(char), (*E).HistSize, File);
    547       fclose(File);
     566          if (ferror(File) != 0) State(WARN, "Error writing history file '%s' in RemoveService()", Name.c_str());
     567      if (fclose(File) != 0) State(WARN, "Error closing history file '%s' in RemoveService()", Name.c_str());;
    548568        }
    549569       
     
    558578// Determine size of file in kB
    559579//
    560 float DataHandler::FileSize(FILE *File) {
     580off_t DataHandler::FileSize(FILE *File) {
    561581
    562582  struct stat FileStatus;
     
    567587  }
    568588
    569   return (float) FileStatus.st_size/1024;
     589  return FileStatus.st_size;
    570590}
    571591
Note: See TracChangeset for help on using the changeset viewer.