Changeset 253 for Evidence/Config.cc


Ignore:
Timestamp:
07/20/10 12:34:13 (14 years ago)
Author:
ogrimm
Message:
Added command ResetAlarm, Evidence servers now always safely translate a DIM string into a C string, added documentation, replaced several vectors my maps
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Evidence/Config.cc

    r229 r253  
    77    "Config/ModifyTime" contains the last modification UNIX time.
    88  - The contents of the configuration file is available through Config/ConfigData.
    9   - The current parser removes all tabs, multiple, leading and trailing spaces, and
    10     concatenates lines ending with '+'.
     9  - The parser removes all tabs, multiple, leading and trailing spaces, and
     10    concatenates all lines until the next item.
    1111
    1212  A mutex is used for preventing concurrent access to the configuration data from
    1313  the methods rpcHandler() and ConfigChanged().
    1414         
    15   Oliver Grimm, April 2010
     15  Oliver Grimm, July 2010
    1616
    1717\********************************************************************/
    1818
    19 #define DEFAULT_CONFIG "../config/Evidence.conf"
    2019#define SERVER_NAME "Config"
    2120
     
    3534
    3635  private:
    37         struct Item {
    38           string Name;
    39           string Data;
    40         };
    41         vector<struct Item> List;
    42 
     36        map<string, string> Map;
    4337    FILE *File;
    4438        char *FileContent;
    4539    DimService *ConfigModified;
    4640        DimService *ConfigContent;
    47     pthread_mutex_t Mutex;
    4841
    4942    void rpcHandler();
     
    6760  FileContent = NULL;
    6861
    69   // Signaling interferes with inotify() mechnism
    70   signal(SIGUSR2, SIG_IGN);
    71  
    72   // Initialise mutex (errno is not set by pthread_mutex_init())
    73   if (pthread_mutex_init(&Mutex, NULL) != 0) {
    74     Message(FATAL, "pthread_mutex_init() failed");
    75   }
    76 
    7762  // Open configuration file
    7863  if ((File = fopen(Filename, "r")) == NULL) {
     
    9782  delete ConfigContent;
    9883  delete[] FileContent;
    99 
    100   if (pthread_mutex_destroy(&Mutex) != 0) Message(ERROR, "pthread_mutex_destroy() failed");
    10184}
    10285
     
    10588void EvidenceConfig::rpcHandler() {
    10689
    107   string Response;
    108  
    109   // Lock because ConfigChange() might access concurrently
    110   if (pthread_mutex_lock(&Mutex) != 0) Message(ERROR, "pthread_mutex_lock() failed in rpcHandler()");
    111 
     90  string Response, Item = ToString((char *) "C", getData(), getSize());
     91 
    11292  // Search for config data in list (default response is empty string)
    113   for (int i=0; i<List.size(); i++) {
    114     if (List[i].Name == getString()) Response = List[i].Data;
    115   }
    116  
    117   // Unlock
    118   if (pthread_mutex_unlock(&Mutex) != 0) Message(ERROR, "pthread_mutex_unlock() failed in rpcHandler()");
     93  // Lock because ConfigChange() might also access concurrently
     94  Lock();
     95  if (Map.count(Item) != 0) Response = Map[Item];
     96  Unlock();
    11997
    12098  // Send data and update Status
     
    122100   
    123101  Message(INFO, "Client '%s' (ID %d) requested '%s'. Send '%s'.",
    124                 DimServer::getClientName(),
    125                 DimServer::getClientId(),
    126                 getString(), Response.c_str());
     102                DimServer::getClientName(),     DimServer::getClientId(),
     103                Item.c_str(), Response.c_str());
    127104}
    128105
     
    227204  if (Section.empty() || Parameter.empty() || Data.empty()) return;
    228205
    229   // Prepare new item of configuration list
    230   struct Item New;
    231   New.Name = Section + ' ' + Parameter;
    232   New.Data = Data;
    233 
    234206  // Add to configuration list
    235   if (pthread_mutex_lock(&Mutex) != 0) Message(ERROR, "pthread_mutex_lock() failed in ConfigChanged()");
    236   List.push_back(New);
    237   if (pthread_mutex_unlock(&Mutex) != 0) Message(ERROR, "pthread_mutex_unlock() failed in ConfigChanged()");
     207  Lock();
     208  Map[Section + " " + Parameter] = Data;
     209  Unlock();
    238210}
    239211
     
    255227//
    256228int main(int argc, char *argv[]) {
    257        
    258   static EvidenceConfig Config(argc<2 ? DEFAULT_CONFIG : argv[1]);
    259 
     229
     230  if (argc != 2) {
     231        printf("Usage: %s <Configuration-File>\n", argv[0]);
     232        exit(EXIT_FAILURE);
     233  }
     234 
     235  static EvidenceConfig Config(argv[1]);
    260236  int Notify;
    261237  struct inotify_event Event;
     
    264240    Config.Message(EvidenceConfig::WARN, "inotify_init() failed, cannot monitor changes of configuration file (%s)\n", strerror(errno));
    265241  }
    266   else if (inotify_add_watch(Notify, argc<2 ? DEFAULT_CONFIG : argv[1], IN_MODIFY) == -1) {
     242  else if (inotify_add_watch(Notify, argv[1], IN_MODIFY) == -1) {
    267243      Config.Message(EvidenceConfig::WARN, "Could not set inotify watch on configuration file (%s)\n", strerror(errno));
    268244          close(Notify);
Note: See TracChangeset for help on using the changeset viewer.