Changeset 142 for Evidence/Config


Ignore:
Timestamp:
01/13/10 12:47:33 (15 years ago)
Author:
ogrimm
Message:
Various updates
Location:
Evidence/Config
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • Evidence/Config/Config.cc

    r127 r142  
    55  - The configuration file is opened without buffering to catch changes
    66    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.
    1113         
    1214  Oliver Grimm, November 2009
     
    1416\********************************************************************/
    1517
    16 #define CONFIG_FILE "configuration.txt"
     18#define DEFAULT_CONFIG "configuration.txt"
    1719#define SERVER_NAME "Config"
    1820
    1921#include "../Evidence.h"
     22#include <ctype.h>
    2023#include <sys/stat.h>
    2124#include <sys/inotify.h>
     
    3134    unsigned int BufferLength;
    3235    DimService *ConfigModified;
    33 
     36    int ModifyTime;
     37       
    3438    void rpcHandler();
    3539 
     
    4549        DimRpc("ConfigRequest", "C", "C"), EvidenceServer(SERVER_NAME) {
    4650
    47   // Create DIM service to indicate changes of configuration file
    48   ConfigModified = new DimService (SERVER_NAME"/Modified", (char *) "");
    49 
    5051  // Open configuration file
    5152  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));
    5354  }
    5455
     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
    5565  // 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);
    5868  }
    5969   
     
    7787  // Check if Buffer[] is large enough to hold full file, enlarge if necessary
    7888  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));
    8090  }
    8191  else if(BufferLength < FileStatus.st_size) {
     
    105115    if(Token1==NULL || Token2==NULL || Token3==NULL) continue;
    106116
    107     // Check for match and then send data
     117    // Check for match and then send data (removing trainlin whitespace)
    108118    if (strstr(Request, Token1)!=NULL && strstr(Request, Token2)!=NULL) {
     119          while (isspace(*Token3) != 0) Token3++;
    109120      setData(Token3);
    110121      break;
    111122    }
    112123  }
    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'.",
    114129                DimServer::getClientName(),
    115130                DimServer::getClientId(),
     
    120135void EvidenceConfig::ConfigChanged() {
    121136
     137  ModifyTime = time(NULL);
    122138  ConfigModified->updateService();
    123139}
     
    126142// Declaring class static ensures destructor is called when exit() is invoked
    127143//
    128 int main() {
     144int main(int argc, char *argv[]) {
    129145       
    130   static EvidenceConfig Config(CONFIG_FILE);
     146  static EvidenceConfig Config(argc<2 ? DEFAULT_CONFIG : argv[1]);
    131147
    132148  int Notify;
     
    134150 
    135151  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));
    137153  }
    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));
    140156          close(Notify);
    141157          Notify = -1;
  • Evidence/Config/Makefile

    r127 r142  
    22
    33PROG=Config
    4 CPPFLAGS += -I../DIM/ 
     4CPPFLAGS += -I../DIM/
    55LDLIBS += -lpthread
    66
Note: See TracChangeset for help on using the changeset viewer.