comparison Framework/Messages/IObserver.cpp @ 1262:ca2058bd74ef toa2020012702

Changed fingerprint from uuid_t to int64_t + poor man's checksum (to fix emscripten syscall error + make it faster)
author Benjamin Golinvaux <bgo@osimis.io>
date Mon, 27 Jan 2020 15:13:00 +0100
parents f6be9412e42a
children 2d8ab34c8c91
comparison
equal deleted inserted replaced
1261:4c1c9df47d46 1262:ca2058bd74ef
27 #include <Core/Logging.h> 27 #include <Core/Logging.h>
28 #include <Core/Toolbox.h> 28 #include <Core/Toolbox.h>
29 29
30 namespace OrthancStone 30 namespace OrthancStone
31 { 31 {
32 static const uint64_t IObserver_FIRST_UNIQUE_ID = 10973;
33 static uint64_t IObserver_nextUniqueId = IObserver_FIRST_UNIQUE_ID;
34
32 IObserver::IObserver(MessageBroker& broker) 35 IObserver::IObserver(MessageBroker& broker)
33 : broker_(broker) 36 : broker_(broker)
34 , fingerprint_()
35 { 37 {
36 // we store the fingerprint_ as a char array to avoid problems when 38 AssignFingerprint();
37 // reading it in a deceased object.
38 // remember this is panic-level code to track zombie object usage
39 std::string fingerprint = Orthanc::Toolbox::GenerateUuid();
40 const char* fingerprintRaw = fingerprint.c_str();
41 memcpy(fingerprint_, fingerprintRaw, 37);
42 broker_.Register(*this); 39 broker_.Register(*this);
43 } 40 }
44
45 41
46 IObserver::~IObserver() 42 IObserver::~IObserver()
47 { 43 {
48 try 44 try
49 { 45 {
50 LOG(TRACE) << "IObserver(" << std::hex << this << std::dec << ")::~IObserver : fingerprint_ == " << fingerprint_; 46 LOG(TRACE) << "IObserver(" << std::hex << this << std::dec << ")::~IObserver : fingerprint_[0] == " << fingerprint_[0];
51 const char* deadMarker = "deadbeef-dead-dead-0000-0000deadbeef"; 47 fingerprint_[0] = 0xdeadbeef;
52 ORTHANC_ASSERT(strlen(deadMarker) == 36); 48 fingerprint_[1] = 0xdeadbeef;
53 memcpy(fingerprint_, deadMarker, 37); 49 fingerprint_[2] = 0xdeadbeef;
54 broker_.Unregister(*this); 50 broker_.Unregister(*this);
55 } 51 }
56 catch (const Orthanc::OrthancException& e) 52 catch (const Orthanc::OrthancException& e)
57 { 53 {
58 if (e.HasDetails()) 54 if (e.HasDetails())
72 { 68 {
73 LOG(ERROR) << "Unknown exception in ~IObserver"; 69 LOG(ERROR) << "Unknown exception in ~IObserver";
74 } 70 }
75 } 71 }
76 72
73 static const int64_t IObserver_UNIQUE_ID_MAGIC_NUMBER = 2742024;
74
75 void IObserver::AssignFingerprint()
76 {
77 fingerprint_[0] = IObserver_nextUniqueId;
78 fingerprint_[1] = fingerprint_[0] / 2;
79 fingerprint_[2] = fingerprint_[1] + IObserver_UNIQUE_ID_MAGIC_NUMBER;
80 IObserver_nextUniqueId++;
81 }
77 82
78 bool IObserver::DoesFingerprintLookGood() const 83 bool IObserver::DoesFingerprintLookGood() const
79 { 84 {
80 for (size_t i = 0; i < 36; ++i) { 85 return (fingerprint_[0] >= IObserver_FIRST_UNIQUE_ID) &&
81 bool ok = false; 86 (fingerprint_[1] == fingerprint_[0] / 2) &&
82 if (fingerprint_[i] >= 'a' && fingerprint_[i] <= 'f') 87 (fingerprint_[2] == fingerprint_[1] + IObserver_UNIQUE_ID_MAGIC_NUMBER);
83 ok = true;
84 if (fingerprint_[i] >= '0' && fingerprint_[i] <= '9')
85 ok = true;
86 if (fingerprint_[i] == '-')
87 ok = true;
88 if (!ok)
89 return false;
90 }
91 return fingerprint_[36] == 0;
92 } 88 }
93 } 89 }