| 1 | #include "LogStatus_impl.h" |
|---|
| 2 | |
|---|
| 3 | #include <iostream> |
|---|
| 4 | |
|---|
| 5 | LogStatus_impl::LogStatus_impl() |
|---|
| 6 | { |
|---|
| 7 | LogStatus_impl::LogStatus_impl(MAX_SIZE_DEFAULT); |
|---|
| 8 | } |
|---|
| 9 | |
|---|
| 10 | LogStatus_impl::LogStatus_impl(unsigned long long len) |
|---|
| 11 | { |
|---|
| 12 | circLogSeq = new CircularLogRecordSequence(len); |
|---|
| 13 | |
|---|
| 14 | test_mutex = new omni_mutex(); |
|---|
| 15 | test_mutex->release(); |
|---|
| 16 | |
|---|
| 17 | operationalState = LogService::enabled; |
|---|
| 18 | administrativeState = LogService::unlocked; |
|---|
| 19 | logFullAction = LogService::HALT; |
|---|
| 20 | |
|---|
| 21 | availabilityStatus.offDuty = false; |
|---|
| 22 | availabilityStatus.logFull = circLogSeq->isFull(); |
|---|
| 23 | } |
|---|
| 24 | |
|---|
| 25 | LogStatus_impl::~LogStatus_impl() |
|---|
| 26 | { |
|---|
| 27 | // |
|---|
| 28 | // Undefined behavior |
|---|
| 29 | // |
|---|
| 30 | delete circLogSeq; |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | CORBA::ULongLong LogStatus_impl::getMaxSize() |
|---|
| 34 | { |
|---|
| 35 | return circLogSeq->getMaxLen(); |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | CORBA::ULongLong LogStatus_impl::getCurrentSize() |
|---|
| 39 | { |
|---|
| 40 | // Should be byte size of Log |
|---|
| 41 | return circLogSeq->getCurrentLen(); |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | CORBA::ULongLong LogStatus_impl::getNumRecords() |
|---|
| 45 | { |
|---|
| 46 | return circLogSeq->getCurrentLen(); |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | LogService::LogFullAction LogStatus_impl::getLogFullAction() |
|---|
| 50 | { |
|---|
| 51 | return logFullAction; |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | LogService::AvailabilityStatus LogStatus_impl::getAvailabilityStatus() |
|---|
| 55 | { |
|---|
| 56 | return availabilityStatus; |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | LogService::AdministrativeState LogStatus_impl::getAdministrativeState() |
|---|
| 60 | { |
|---|
| 61 | return administrativeState; |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | LogService::OperationalState LogStatus_impl::getOperationalState() |
|---|
| 65 | { |
|---|
| 66 | return operationalState; |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | CircularLogRecordSequence::CircularLogRecordSequence(unsigned long long len) |
|---|
| 70 | { |
|---|
| 71 | maxLen = len; |
|---|
| 72 | currentLen = 0; |
|---|
| 73 | wrIndex = 0; |
|---|
| 74 | currentId = 0; |
|---|
| 75 | |
|---|
| 76 | seq = new LogService::LogRecordSequence(len); |
|---|
| 77 | seq->length(len); |
|---|
| 78 | |
|---|
| 79 | if (maxLen == 0) |
|---|
| 80 | logFull = true; |
|---|
| 81 | else |
|---|
| 82 | logFull = false; |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | CircularLogRecordSequence::CircularLogRecordSequence() |
|---|
| 86 | { |
|---|
| 87 | // Create null sequence |
|---|
| 88 | CircularLogRecordSequence(0); |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | void |
|---|
| 92 | CircularLogRecordSequence::writeLogRecord(const LogService::LogRecord& record) |
|---|
| 93 | { |
|---|
| 94 | // Special case, do nothing |
|---|
| 95 | if (maxLen == 0) { |
|---|
| 96 | std::cerr << "Error: Log is unwritable" << std::endl; |
|---|
| 97 | return; |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | // Wrap write index if necessary |
|---|
| 101 | if (wrIndex >= maxLen) { |
|---|
| 102 | std::cout << "Wrapping write index from " << wrIndex << " to zero" << std::endl; |
|---|
| 103 | wrIndex = 0; |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | // Write entry |
|---|
| 107 | (*seq)[wrIndex] = record; |
|---|
| 108 | wrIndex++; |
|---|
| 109 | |
|---|
| 110 | // Increase log length if not full |
|---|
| 111 | if (!logFull) { |
|---|
| 112 | currentLen++; |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | // Check if maximum size is reached |
|---|
| 116 | if (currentLen == maxLen) { |
|---|
| 117 | logFull = true; |
|---|
| 118 | } |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | LogService::LogRecord |
|---|
| 122 | CircularLogRecordSequence::readLogRecord(unsigned long long index) |
|---|
| 123 | throw (InvalidIndex) |
|---|
| 124 | { |
|---|
| 125 | if (index >= currentLen) { |
|---|
| 126 | std::cerr << "Error: Invalid index" << std::endl; |
|---|
| 127 | throw InvalidIndex(index); |
|---|
| 128 | } |
|---|
| 129 | else { |
|---|
| 130 | return (*seq)[index]; |
|---|
| 131 | } |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | LogService::LogRecordSequence* |
|---|
| 135 | CircularLogRecordSequence::readLogRecords(unsigned long long index, |
|---|
| 136 | CORBA::ULong &howMany, LogService::RecordId &id) |
|---|
| 137 | throw (InternalFailure) |
|---|
| 138 | { |
|---|
| 139 | LogService::LogRecordSequence_var newSeq = new LogService::LogRecordSequence(); |
|---|
| 140 | newSeq->length(0); |
|---|
| 141 | |
|---|
| 142 | // Requested Index > Log Size (Invalid) |
|---|
| 143 | if (index >= currentLen) { |
|---|
| 144 | std::cerr << "Error: Invalid index" << std::endl; |
|---|
| 145 | howMany = 0; |
|---|
| 146 | id = currentId; |
|---|
| 147 | return newSeq._retn(); |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | // Requested Index < Current Index & !Truncate |
|---|
| 151 | if (index < wrIndex && (index + howMany) <= wrIndex) { |
|---|
| 152 | newSeq->length(howMany); |
|---|
| 153 | |
|---|
| 154 | for (unsigned long long i = 0; i < howMany; i++) { |
|---|
| 155 | newSeq[i] = (*seq)[index + i]; |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | // Set currentId |
|---|
| 159 | if ( (index + howMany) == wrIndex) |
|---|
| 160 | id = currentId; |
|---|
| 161 | else |
|---|
| 162 | id = (*seq)[index + howMany].id; |
|---|
| 163 | |
|---|
| 164 | return newSeq._retn();; |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | // Requested Index < Current Index & Truncate |
|---|
| 168 | if (index < wrIndex && (index + howMany) > wrIndex) { |
|---|
| 169 | howMany = wrIndex - index; |
|---|
| 170 | newSeq->length(howMany); |
|---|
| 171 | |
|---|
| 172 | for (unsigned long long i = 0; i < howMany; i++) { |
|---|
| 173 | newSeq[i] = (*seq)[index + i]; |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | // Set currentId |
|---|
| 177 | id = currentId; |
|---|
| 178 | |
|---|
| 179 | return newSeq._retn();; |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | // Requested Index > Current Index & !Wrap |
|---|
| 183 | if (index >= wrIndex && (index + howMany) <= currentLen) { |
|---|
| 184 | seq->length(howMany); |
|---|
| 185 | |
|---|
| 186 | for (unsigned long long i = 0; i < howMany; i++) { |
|---|
| 187 | newSeq[i] = (*seq)[index + i]; |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | // Set currentId |
|---|
| 191 | if ( (index + howMany) == currentLen) { |
|---|
| 192 | if (wrIndex == 0) |
|---|
| 193 | id = currentId; |
|---|
| 194 | else |
|---|
| 195 | id = (*seq)[0].id; |
|---|
| 196 | } |
|---|
| 197 | else { |
|---|
| 198 | id = (*seq)[index + howMany].id; |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | return newSeq._retn();; |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | // Requested Index > Current Index & Wrap & !Truncate |
|---|
| 205 | if (index >= wrIndex && (index + howMany) > currentLen && |
|---|
| 206 | howMany <= (currentLen - index) + wrIndex ) { |
|---|
| 207 | |
|---|
| 208 | newSeq->length(howMany); |
|---|
| 209 | |
|---|
| 210 | unsigned long long len1 = currentLen - index; |
|---|
| 211 | unsigned long long len2 = howMany - (currentLen - index); |
|---|
| 212 | |
|---|
| 213 | for (unsigned long long i = 0; i < len1; i++) { |
|---|
| 214 | newSeq[i] = (*seq)[index + i]; |
|---|
| 215 | } |
|---|
| 216 | for (unsigned long long i = len1; i < len2; i++) { |
|---|
| 217 | newSeq[i] = (*seq)[0 + i]; |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | // Set currentId |
|---|
| 221 | if ( len2 == wrIndex) |
|---|
| 222 | id = currentId; |
|---|
| 223 | else |
|---|
| 224 | id = (*seq)[len2].id; |
|---|
| 225 | |
|---|
| 226 | return newSeq._retn();; |
|---|
| 227 | } |
|---|
| 228 | |
|---|
| 229 | // Requested Index > Current Index & Wrap & Truncate |
|---|
| 230 | if (index >= wrIndex && (index + howMany) > currentLen && |
|---|
| 231 | howMany > (currentLen - index) + wrIndex ) { |
|---|
| 232 | |
|---|
| 233 | howMany = (currentLen - index) + wrIndex; |
|---|
| 234 | newSeq->length(howMany); |
|---|
| 235 | |
|---|
| 236 | unsigned long long len1 = currentLen - index; |
|---|
| 237 | unsigned long long len2 = wrIndex; |
|---|
| 238 | |
|---|
| 239 | for (unsigned long long i = 0; i < len1; i++) { |
|---|
| 240 | newSeq[i] = (*seq)[index + i]; |
|---|
| 241 | } |
|---|
| 242 | for (unsigned long long i = len1; i < len2; i++) { |
|---|
| 243 | newSeq[i] = (*seq)[0 + i]; |
|---|
| 244 | } |
|---|
| 245 | |
|---|
| 246 | // Set currentId |
|---|
| 247 | id = currentId; |
|---|
| 248 | |
|---|
| 249 | return newSeq._retn();; |
|---|
| 250 | } |
|---|
| 251 | // Never reached |
|---|
| 252 | std::cerr << "Error: Internal failure" << std::endl; |
|---|
| 253 | throw InternalFailure(); |
|---|
| 254 | } |
|---|
| 255 | |
|---|
| 256 | unsigned long long |
|---|
| 257 | CircularLogRecordSequence::findIndexFromTime(const LogService::LogTime& fromTime) |
|---|
| 258 | { |
|---|
| 259 | bool searchFront = true; |
|---|
| 260 | unsigned long long index = currentLen; |
|---|
| 261 | |
|---|
| 262 | if (wrIndex < currentLen) { |
|---|
| 263 | for (unsigned long long i = wrIndex; i < currentLen; i++) { |
|---|
| 264 | LogService::LogRecord record = (*seq)[i]; |
|---|
| 265 | |
|---|
| 266 | if (record.time.seconds > fromTime.seconds) { |
|---|
| 267 | index = i; |
|---|
| 268 | searchFront = false; |
|---|
| 269 | break; |
|---|
| 270 | } |
|---|
| 271 | if (record.time.seconds == fromTime.seconds && |
|---|
| 272 | record.time.nanoseconds >= fromTime.nanoseconds) { |
|---|
| 273 | index = i; |
|---|
| 274 | searchFront = false; |
|---|
| 275 | break; |
|---|
| 276 | } |
|---|
| 277 | } |
|---|
| 278 | } |
|---|
| 279 | if (searchFront) { |
|---|
| 280 | for (unsigned long long i = 0; i < wrIndex; i++) { |
|---|
| 281 | LogService::LogRecord record = (*seq)[i]; |
|---|
| 282 | |
|---|
| 283 | if (record.time.seconds > fromTime.seconds) { |
|---|
| 284 | index = i; |
|---|
| 285 | break; |
|---|
| 286 | } |
|---|
| 287 | if (record.time.seconds == fromTime.seconds && |
|---|
| 288 | record.time.nanoseconds >= fromTime.nanoseconds) { |
|---|
| 289 | |
|---|
| 290 | index = i; |
|---|
| 291 | break; |
|---|
| 292 | } |
|---|
| 293 | } |
|---|
| 294 | } |
|---|
| 295 | |
|---|
| 296 | return index; |
|---|
| 297 | } |
|---|
| 298 | |
|---|
| 299 | unsigned long long |
|---|
| 300 | CircularLogRecordSequence::findIndexById(unsigned long long id) |
|---|
| 301 | { |
|---|
| 302 | bool searchFront = true; |
|---|
| 303 | unsigned long long index = currentLen; |
|---|
| 304 | |
|---|
| 305 | if (wrIndex < currentLen) { |
|---|
| 306 | for (unsigned long long i = wrIndex; i < currentLen; i++) { |
|---|
| 307 | if ( (*seq)[i].id == id) { |
|---|
| 308 | index = i; |
|---|
| 309 | searchFront = false; |
|---|
| 310 | break; |
|---|
| 311 | } |
|---|
| 312 | } |
|---|
| 313 | } |
|---|
| 314 | if (searchFront) { |
|---|
| 315 | for (unsigned long long i = 0; i < wrIndex; i++) { |
|---|
| 316 | if ( (*seq)[i].id == id) { |
|---|
| 317 | index = i; |
|---|
| 318 | break; |
|---|
| 319 | } |
|---|
| 320 | } |
|---|
| 321 | } |
|---|
| 322 | |
|---|
| 323 | return index; |
|---|
| 324 | } |
|---|
| 325 | |
|---|
| 326 | unsigned long long |
|---|
| 327 | CircularLogRecordSequence::getMaxLen() |
|---|
| 328 | { |
|---|
| 329 | return maxLen; |
|---|
| 330 | } |
|---|
| 331 | |
|---|
| 332 | void |
|---|
| 333 | CircularLogRecordSequence::setMaxLen(unsigned long long len) |
|---|
| 334 | { |
|---|
| 335 | maxLen = len; |
|---|
| 336 | seq->length(len); |
|---|
| 337 | |
|---|
| 338 | // Truncate | Unchanged |
|---|
| 339 | if (currentLen >= maxLen) { |
|---|
| 340 | currentLen = maxLen; |
|---|
| 341 | logFull = true; |
|---|
| 342 | } |
|---|
| 343 | // Expand |
|---|
| 344 | else { |
|---|
| 345 | // Should probably re-sort the log from wrapping |
|---|
| 346 | // Overwrite/delete out-of-order entries instead |
|---|
| 347 | if (currentLen != wrIndex) { |
|---|
| 348 | currentLen = wrIndex; |
|---|
| 349 | } |
|---|
| 350 | logFull = false; |
|---|
| 351 | } |
|---|
| 352 | |
|---|
| 353 | } |
|---|
| 354 | |
|---|
| 355 | unsigned long long |
|---|
| 356 | CircularLogRecordSequence::getCurrentLen() |
|---|
| 357 | { |
|---|
| 358 | return currentLen; |
|---|
| 359 | } |
|---|
| 360 | |
|---|
| 361 | bool |
|---|
| 362 | CircularLogRecordSequence::isFull() |
|---|
| 363 | { |
|---|
| 364 | return logFull; |
|---|
| 365 | } |
|---|
| 366 | |
|---|
| 367 | void |
|---|
| 368 | CircularLogRecordSequence::clearLog() |
|---|
| 369 | { |
|---|
| 370 | wrIndex = 0; |
|---|
| 371 | currentLen = 0; |
|---|
| 372 | |
|---|
| 373 | if (maxLen == 0) |
|---|
| 374 | logFull = true; |
|---|
| 375 | else |
|---|
| 376 | logFull = false; |
|---|
| 377 | } |
|---|
| 378 | |
|---|
| 379 | CircularLogRecordSequence::~CircularLogRecordSequence() |
|---|
| 380 | { |
|---|
| 381 | delete seq; |
|---|
| 382 | } |
|---|
| 383 | |
|---|
| 384 | |
|---|