Changeset 159 for Evidence/Evidence.cc


Ignore:
Timestamp:
02/04/10 15:26:55 (15 years ago)
Author:
ogrimm
Message:
Change in history handling
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Evidence/Evidence.cc

    r154 r159  
    2525#include "Evidence.h"
    2626
     27using namespace std;
     28
     29//////////////////////////
     30// EvidenceServer Class //
     31//////////////////////////
     32
    2733EvidenceServer *ThisServer;
    2834
     
    5056  start(Name);
    5157  addExitHandler(this);
    52 
    5358}
    5459
     
    5661EvidenceServer::~EvidenceServer() {
    5762
     63  State(INFO, "Server stopped");
     64 
    5865  for (unsigned int i=0; i<ConfigList.size(); i++) {
    5966        delete[] ConfigList[i].Name;
     
    6471// DIM exit handler
    6572void EvidenceServer::exitHandler(int Code) {
    66   State(INFO, "Server stopped (DIM exit code %d)", Code);
     73  State(INFO, "Exit handler called (DIM exit code %d)", Code);
    6774  exit(EXIT_SUCCESS);
    6875}
     
    104111  // Send message to console and log file
    105112  printf("%s\n", TBuf);
    106   if (Severity != INFO) DimClient::sendCommand("DColl/Log", TBuf);
     113  DimClient::sendCommandNB("DColl/Log", TBuf);
    107114
    108115  // Update DIM status service (including severity encoding)
     
    188195
    189196// Translates DIMInfo to string (memory has to be freed by caller)
    190 // No DIM structures are supported (only a single number or string is converted)
     197// DIM structures are converted to hexadecimal representation
    191198// For string conversion, a terminating \0 is enforced.
    192199char *EvidenceServer::ToString(DimInfo *Item) {
    193200
    194201  char *Text;
    195   int R;
    196  
    197   if (strlen(Item->getFormat()) != 1) return NULL;
    198  
    199   switch (*(Item->getFormat())) {
    200     case 'I':  R = asprintf(&Text, "%d", Item->getInt());   break;
    201     case 'S':  R = asprintf(&Text, "%hd", Item->getShort());   break;
    202     case 'F':  R = asprintf(&Text, "%.5f", Item->getFloat());   break;
    203     case 'D':  R = asprintf(&Text, "%.5f", Item->getDouble());   break;
    204     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;
    208     default: return NULL;
    209   }
    210 
     202  int R=0;
     203 
     204  if (strlen(Item->getFormat()) != 1) {
     205    if ((Text = (char *) malloc(3*Item->getSize()+1)) != NULL) {
     206          for (int i=0; i<Item->getSize(); i++) sprintf(Text+3*i, "%02x", *((char *) Item->getData() + i));
     207        }
     208  }
     209  else {
     210        switch (*(Item->getFormat())) {
     211      case 'I':  R = asprintf(&Text, "%d", Item->getInt());   break;
     212      case 'S':  R = asprintf(&Text, "%hd", Item->getShort());   break;
     213      case 'F':  R = asprintf(&Text, "%.5f", Item->getFloat());   break;
     214      case 'D':  R = asprintf(&Text, "%.5f", Item->getDouble());   break;
     215      case 'X':  R = asprintf(&Text, "%lld", Item->getLonglong());   break;
     216      case 'C':  *(Item->getString() + Item->getSize() - 1) = '\0';
     217                                 R = asprintf(&Text, "%s", Item->getString());
     218                                 break;
     219      default: return NULL;
     220        }
     221  }
     222 
    211223  return (R == -1) ? NULL : Text;
    212224}
     225
     226// Checks if service contents indicates not available
     227bool EvidenceServer::ServiceOK(DimInfo *Item) {
     228
     229  return !((Item->getSize() == strlen(NO_LINK)+1) && 
     230          (memcmp(Item->getData(), NO_LINK, Item->getSize()) == 0));
     231
     232}
     233
     234
     235///////////////////////////
     236// EvidenceHistory Class //
     237///////////////////////////
     238
     239// Organisaztion of history buffer
     240// F | T S D | T S D | 0 0 ......  | T S D | T S D | 0 -1
     241//
     242// F: Offset of oldest entry  T: Time  S: Size  D: Data
     243// F, T, S: int
     244
     245// Marker for history buffer
     246const int EvidenceHistory::WrapMark[] = {0, -1};
     247const int EvidenceHistory::EndMark[] = {0, 0};
     248
     249// Constructor
     250EvidenceHistory::EvidenceHistory() {
     251
     252  Buffer = NULL;
     253}
     254
     255// Destructor
     256EvidenceHistory::~EvidenceHistory() {
     257
     258  delete[] Buffer;
     259}
     260
     261// Requests service history
     262bool EvidenceHistory::GetHistory(char *Name) {
     263
     264  DimCurrentInfo Info((string(Name)+".hist").c_str(), NO_LINK);
     265
     266  // Check if service available
     267  if (((Info.getSize() == strlen(NO_LINK)+1) && 
     268          (memcmp(Info.getData(), NO_LINK, Info.getSize()) == 0))) return false;
     269
     270  delete[] Buffer;
     271  BufferSize = Info.getSize();
     272  Buffer = new char [BufferSize];
     273
     274  memcpy(Buffer, Info.getData(), BufferSize);
     275  Offset = *(int *) Buffer;
     276 
     277  return true;
     278}
     279
     280// Returns next item in history buffer
     281bool EvidenceHistory::Next(int &Time, int &Size, void *&Data) {
     282
     283  if (Buffer == NULL) return false;
     284
     285  // Check for wrap around
     286  if (memcmp(Buffer+Offset, WrapMark, sizeof(WrapMark)) == 0) Offset = 4;
     287 
     288  // Check if at end of ring buffer
     289  if (memcmp(Buffer+Offset, EndMark, sizeof(EndMark)) == 0) return false;
     290
     291  Time = *(int *) (Buffer + Offset);
     292  Size = *(int *) (Buffer + Offset + sizeof(int));
     293  Data = Buffer + Offset + 2*sizeof(int);
     294
     295  Offset += *((int *) (Buffer + Offset) + 1) + 2*sizeof(int);
     296
     297  return true;
     298}
Note: See TracChangeset for help on using the changeset viewer.