Changeset 253 for Evidence/Config.cc
- Timestamp:
- 07/20/10 12:34:13 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Evidence/Config.cc
r229 r253 7 7 "Config/ModifyTime" contains the last modification UNIX time. 8 8 - The contents of the configuration file is available through Config/ConfigData. 9 - The currentparser removes all tabs, multiple, leading and trailing spaces, and10 concatenates lines ending with '+'.9 - The parser removes all tabs, multiple, leading and trailing spaces, and 10 concatenates all lines until the next item. 11 11 12 12 A mutex is used for preventing concurrent access to the configuration data from 13 13 the methods rpcHandler() and ConfigChanged(). 14 14 15 Oliver Grimm, April201015 Oliver Grimm, July 2010 16 16 17 17 \********************************************************************/ 18 18 19 #define DEFAULT_CONFIG "../config/Evidence.conf"20 19 #define SERVER_NAME "Config" 21 20 … … 35 34 36 35 private: 37 struct Item { 38 string Name; 39 string Data; 40 }; 41 vector<struct Item> List; 42 36 map<string, string> Map; 43 37 FILE *File; 44 38 char *FileContent; 45 39 DimService *ConfigModified; 46 40 DimService *ConfigContent; 47 pthread_mutex_t Mutex;48 41 49 42 void rpcHandler(); … … 67 60 FileContent = NULL; 68 61 69 // Signaling interferes with inotify() mechnism70 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 77 62 // Open configuration file 78 63 if ((File = fopen(Filename, "r")) == NULL) { … … 97 82 delete ConfigContent; 98 83 delete[] FileContent; 99 100 if (pthread_mutex_destroy(&Mutex) != 0) Message(ERROR, "pthread_mutex_destroy() failed");101 84 } 102 85 … … 105 88 void EvidenceConfig::rpcHandler() { 106 89 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 112 92 // 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(); 119 97 120 98 // Send data and update Status … … 122 100 123 101 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()); 127 104 } 128 105 … … 227 204 if (Section.empty() || Parameter.empty() || Data.empty()) return; 228 205 229 // Prepare new item of configuration list230 struct Item New;231 New.Name = Section + ' ' + Parameter;232 New.Data = Data;233 234 206 // 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(); 238 210 } 239 211 … … 255 227 // 256 228 int 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]); 260 236 int Notify; 261 237 struct inotify_event Event; … … 264 240 Config.Message(EvidenceConfig::WARN, "inotify_init() failed, cannot monitor changes of configuration file (%s)\n", strerror(errno)); 265 241 } 266 else if (inotify_add_watch(Notify, arg c<2 ? DEFAULT_CONFIG : argv[1], IN_MODIFY) == -1) {242 else if (inotify_add_watch(Notify, argv[1], IN_MODIFY) == -1) { 267 243 Config.Message(EvidenceConfig::WARN, "Could not set inotify watch on configuration file (%s)\n", strerror(errno)); 268 244 close(Notify);
Note:
See TracChangeset
for help on using the changeset viewer.