Changeset 142 for Evidence/Config
- Timestamp:
- 01/13/10 12:47:33 (15 years ago)
- Location:
- Evidence/Config
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
Evidence/Config/Config.cc
r127 r142 5 5 - The configuration file is opened without buffering to catch changes 6 6 without closing/opening. 7 - If a configuration file change is detected through inotify, the service "Config/Modified" 8 is updated (it contains no data) to inform applications. 9 - The employed line buffer has conservatively at least the size of the configuration file. If 10 needed, it will be enlarged. 7 - The name of a configuration file can be given as command line argument 8 - If a configuration file change is detected through inotify, the service 9 "Config/ModifyTime" is updated with the current UNIX time to inform applications. 10 The initial value of the server is the last file modification time. 11 - The employed line buffer has conservatively at least the size of the 12 configuration file. If needed, it will be enlarged. 11 13 12 14 Oliver Grimm, November 2009 … … 14 16 \********************************************************************/ 15 17 16 #define CONFIG_FILE"configuration.txt"18 #define DEFAULT_CONFIG "configuration.txt" 17 19 #define SERVER_NAME "Config" 18 20 19 21 #include "../Evidence.h" 22 #include <ctype.h> 20 23 #include <sys/stat.h> 21 24 #include <sys/inotify.h> … … 31 34 unsigned int BufferLength; 32 35 DimService *ConfigModified; 33 36 int ModifyTime; 37 34 38 void rpcHandler(); 35 39 … … 45 49 DimRpc("ConfigRequest", "C", "C"), EvidenceServer(SERVER_NAME) { 46 50 47 // Create DIM service to indicate changes of configuration file48 ConfigModified = new DimService (SERVER_NAME"/Modified", (char *) "");49 50 51 // Open configuration file 51 52 if ((File = fopen(Filename, "r")) == NULL) { 52 Msg(FATAL, "Could not open configuration file '%s' (%s)\n", Filename, strerror(errno));53 State(FATAL, "Could not open configuration file '%s' (%s)\n", Filename, strerror(errno)); 53 54 } 54 55 56 // Create DIM service to indicate changes of configuration file 57 struct stat Stat; 58 if (stat(Filename, &Stat) == -1) { 59 State(WARN, "Could not read last modification time of configuration file '%s' (%s)", Filename, strerror(errno)); 60 ModifyTime = 0; 61 } 62 else ModifyTime = Stat.st_mtime; 63 ConfigModified = new DimService (SERVER_NAME"/ModifyTime", ModifyTime); 64 55 65 // Disable buffering, so file modifications are immediately seen 56 if (setvbuf(File, NULL, _IONBF, 0) != 0) {57 Msg(WARN, "Error setting configuration file '%s' to unbuffered mode.\n", Filename);66 if (setvbuf(File, NULL, _IONBF, 0) != 0) { 67 State(WARN, "Error setting configuration file '%s' to unbuffered mode", Filename); 58 68 } 59 69 … … 77 87 // Check if Buffer[] is large enough to hold full file, enlarge if necessary 78 88 if (fstat(fileno(File), &FileStatus) == -1) { 79 Msg(ERROR, "Could not determine size of configuration file to allocate buffer (%s)\n", strerror(errno));89 State(FATAL, "Could not determine size of configuration file to allocate buffer (%s)", strerror(errno)); 80 90 } 81 91 else if(BufferLength < FileStatus.st_size) { … … 105 115 if(Token1==NULL || Token2==NULL || Token3==NULL) continue; 106 116 107 // Check for match and then send data 117 // Check for match and then send data (removing trainlin whitespace) 108 118 if (strstr(Request, Token1)!=NULL && strstr(Request, Token2)!=NULL) { 119 while (isspace(*Token3) != 0) Token3++; 109 120 setData(Token3); 110 121 break; 111 122 } 112 123 } 113 Msg(INFO, "Client '%s' (ID %d) requested '%s'. Send '%s'.", 124 125 // If configuration data not found, send empty string 126 if (feof(File)!=0) setData((char *) ""); 127 128 State(INFO, "Client '%s' (ID %d) requested '%s'. Send '%s'.", 114 129 DimServer::getClientName(), 115 130 DimServer::getClientId(), … … 120 135 void EvidenceConfig::ConfigChanged() { 121 136 137 ModifyTime = time(NULL); 122 138 ConfigModified->updateService(); 123 139 } … … 126 142 // Declaring class static ensures destructor is called when exit() is invoked 127 143 // 128 int main( ) {144 int main(int argc, char *argv[]) { 129 145 130 static EvidenceConfig Config( CONFIG_FILE);146 static EvidenceConfig Config(argc<2 ? DEFAULT_CONFIG : argv[1]); 131 147 132 148 int Notify; … … 134 150 135 151 if ((Notify = inotify_init()) == -1) { 136 Config. Msg(EvidenceConfig::WARN, "inotify_init() failed, cannot monitor changes of configuration file (%s)\n", strerror(errno));152 Config.State(EvidenceConfig::WARN, "inotify_init() failed, cannot monitor changes of configuration file (%s)\n", strerror(errno)); 137 153 } 138 else if (inotify_add_watch(Notify, CONFIG_FILE, IN_MODIFY) == -1) {139 Config. Msg(EvidenceConfig::WARN, "Could not set inotify watch on configuration file (%s)\n", strerror(errno));154 else if (inotify_add_watch(Notify, argc<2 ? DEFAULT_CONFIG : argv[1], IN_MODIFY) == -1) { 155 Config.State(EvidenceConfig::WARN, "Could not set inotify watch on configuration file (%s)\n", strerror(errno)); 140 156 close(Notify); 141 157 Notify = -1; -
Evidence/Config/Makefile
r127 r142 2 2 3 3 PROG=Config 4 CPPFLAGS += -I../DIM/ 4 CPPFLAGS += -I../DIM/ 5 5 LDLIBS += -lpthread 6 6
Note:
See TracChangeset
for help on using the changeset viewer.