Changeset 154 for Evidence/Evidence.cc


Ignore:
Timestamp:
01/29/10 14:27:34 (14 years ago)
Author:
ogrimm
Message:
Updates to status service
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Evidence/Evidence.cc

    r152 r154  
    2525#include "Evidence.h"
    2626
    27 bool EvidenceServer::ExitRequest = false;
    28 string __StatusName;
     27EvidenceServer *ThisServer;
    2928
    3029// Constructor starts server with given name
     
    3332  // Initialize
    3433  Status = NULL;
    35   ConfigList = NULL;
    36   ConfigNum = 0;
    37   __StatusName = string(Name) + "/Status";
     34  ExitRequest = false;
     35  ThisServer = this;
    3836 
    3937  // Catch some signals
     
    4745 
    4846  // Start server
    49   Status = new DimService(__StatusName.c_str(), (char *) "Server started");
     47  static char Init[] = "Server started";
     48  Status = new DimService((string(Name) + "/Status").c_str(), (char *) "C", Init, sizeof(Init));
    5049
    5150  start(Name);
    5251  addExitHandler(this);
     52
    5353}
    5454
     
    5656EvidenceServer::~EvidenceServer() {
    5757
    58   for (unsigned int i=0; i<ConfigNum; i++) {
     58  for (unsigned int i=0; i<ConfigList.size(); i++) {
    5959        delete[] ConfigList[i].Name;
    6060        delete[] ConfigList[i].Value;
    6161  }
    62   free(ConfigList);
    6362}
    6463
     
    9392  va_end(ArgumentPointer);
    9493
    95   snprintf(TBuf, sizeof(TBuf), "%s (%s): %s", __StatusName.c_str(), StateString[Severity], Tmp); // Normal string
    96   snprintf(SBuf, sizeof(SBuf), "%s*%c", Tmp, (char) Severity);
    97   *(strrchr(SBuf, '*')) = '\0';  // String with severity encoding
     94  // Create normal string
     95  snprintf(TBuf, sizeof(TBuf), "%s (%s): %s", Status->getName(), StateString[Severity], Tmp);
     96
     97  // Create string with severity encoding
     98  snprintf(SBuf, sizeof(SBuf), "%s**", Tmp);
     99  SBuf[strlen(SBuf)-2] = '\0';
     100  SBuf[strlen(SBuf)+1] = Severity;
     101
    98102  if (Tmp != ErrorString) free(Tmp);
    99103 
     
    120124 
    121125  // Check if configuration request already in list
    122   for (unsigned int i=0; i<ConfigNum; i++) {
     126  for (unsigned int i=0; i<ConfigList.size(); i++) {
    123127    if (strcmp(ConfigList[i].Name, Item) == 0) {
    124128          // Return original value if still up to date
     
    144148  }
    145149
    146   // Enlarge memory to hold new pointer if necessary
     150  // Enlarge list if necessary
    147151  if (ItemNo == -1) {
    148         void *N = realloc(ConfigList, sizeof(struct ConfigItem)*(++ConfigNum));
    149         if (N == NULL) {
    150           State(WARN, "Could not realloc() memory for configuration, will lose memory (%s)", strerror(errno));
    151           ConfigNum--; 
    152         }
    153         else ConfigList = (struct ConfigItem *) N;
    154        
    155         ItemNo = ConfigNum-1;
    156   }
    157  
    158   // Allocate memory for strings, and copy data to this memory
     152    struct ConfigItem New;
     153    ConfigList.push_back(New);
     154        ItemNo = ConfigList.size()-1;
     155  }
     156
     157  // Create new entry in item list, allocate memory and copy data to this memory
    159158  ConfigList[ItemNo].Value = new char [strlen(Result)+1];
    160159  ConfigList[ItemNo].Name = new char [strlen(Item)+1];
    161160  strcpy(ConfigList[ItemNo].Name, Item);
    162161  strcpy(ConfigList[ItemNo].Value, Result);
    163 
    164162  ConfigList[ItemNo].Time = Time;
    165  
     163   
    166164  // Return address to configuration value 
    167165  return ConfigList[ItemNo].Value;
     
    174172void EvidenceServer::SignalHandler(int) {
    175173
    176   EvidenceServer::ExitRequest = true;
     174  ThisServer->ExitRequest = true;
    177175}
    178176
     
    180178void EvidenceServer::Terminate() {
    181179
    182   string Msg = __StatusName + ": Caught unhandled exception";
     180  string Msg = string(ThisServer->Status->getName()) + ": Caught unhandled exception";
     181
    183182  printf("%s\n", Msg.c_str());
    184183  DimClient::sendCommand("DColl/Log", Msg.c_str());
     
    187186}
    188187
     188
    189189// Translates DIMInfo to string (memory has to be freed by caller)
    190190// No DIM structures are supported (only a single number or string is converted)
     191// For string conversion, a terminating \0 is enforced.
    191192char *EvidenceServer::ToString(DimInfo *Item) {
    192193
     
    198199  switch (*(Item->getFormat())) {
    199200    case 'I':  R = asprintf(&Text, "%d", Item->getInt());   break;
    200     case 'C':  R = asprintf(&Text, "%s", Item->getString());   break;
    201201    case 'S':  R = asprintf(&Text, "%hd", Item->getShort());   break;
    202202    case 'F':  R = asprintf(&Text, "%.5f", Item->getFloat());   break;
    203203    case 'D':  R = asprintf(&Text, "%.5f", Item->getDouble());   break;
    204204    case 'X':  R = asprintf(&Text, "%lld", Item->getLonglong());   break;
     205    case 'C':  *(Item->getString() + Item->getSize()) = '\0';
     206                           R = asprintf(&Text, "%s", Item->getString());
     207                           break;
    205208    default: return NULL;
    206209  }
Note: See TracChangeset for help on using the changeset viewer.