Changeset 159 for Evidence/Evidence.cc
- Timestamp:
- 02/04/10 15:26:55 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Evidence/Evidence.cc
r154 r159 25 25 #include "Evidence.h" 26 26 27 using namespace std; 28 29 ////////////////////////// 30 // EvidenceServer Class // 31 ////////////////////////// 32 27 33 EvidenceServer *ThisServer; 28 34 … … 50 56 start(Name); 51 57 addExitHandler(this); 52 53 58 } 54 59 … … 56 61 EvidenceServer::~EvidenceServer() { 57 62 63 State(INFO, "Server stopped"); 64 58 65 for (unsigned int i=0; i<ConfigList.size(); i++) { 59 66 delete[] ConfigList[i].Name; … … 64 71 // DIM exit handler 65 72 void 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); 67 74 exit(EXIT_SUCCESS); 68 75 } … … 104 111 // Send message to console and log file 105 112 printf("%s\n", TBuf); 106 if (Severity != INFO) DimClient::sendCommand("DColl/Log", TBuf);113 DimClient::sendCommandNB("DColl/Log", TBuf); 107 114 108 115 // Update DIM status service (including severity encoding) … … 188 195 189 196 // 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 191 198 // For string conversion, a terminating \0 is enforced. 192 199 char *EvidenceServer::ToString(DimInfo *Item) { 193 200 194 201 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 211 223 return (R == -1) ? NULL : Text; 212 224 } 225 226 // Checks if service contents indicates not available 227 bool 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 246 const int EvidenceHistory::WrapMark[] = {0, -1}; 247 const int EvidenceHistory::EndMark[] = {0, 0}; 248 249 // Constructor 250 EvidenceHistory::EvidenceHistory() { 251 252 Buffer = NULL; 253 } 254 255 // Destructor 256 EvidenceHistory::~EvidenceHistory() { 257 258 delete[] Buffer; 259 } 260 261 // Requests service history 262 bool 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 281 bool 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.