changeset 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 4c1c9df47d46
children 6128e1823e49
files Framework/Messages/ICallable.h Framework/Messages/IObserver.cpp Framework/Messages/IObserver.h
diffstat 3 files changed, 29 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/Framework/Messages/ICallable.h	Mon Jan 27 14:34:54 2020 +0100
+++ b/Framework/Messages/ICallable.h	Mon Jan 27 15:13:00 2020 +0100
@@ -65,7 +65,7 @@
 
     TObserver&         observer_;
     MemberFunction     function_;
-    std::string        observerFingerprint_;
+    int64_t            observerFingerprint_;
 
   public:
     Callable(TObserver& observer,
@@ -78,7 +78,7 @@
 
     void ApplyInternal(const TMessage& message)
     {
-      std::string currentFingerprint(observer_.GetFingerprint());
+      int64_t currentFingerprint(observer_.GetFingerprint());
       if (observerFingerprint_ != currentFingerprint)
       {
         LOG(TRACE) << "The observer at address " << 
--- a/Framework/Messages/IObserver.cpp	Mon Jan 27 14:34:54 2020 +0100
+++ b/Framework/Messages/IObserver.cpp	Mon Jan 27 15:13:00 2020 +0100
@@ -29,28 +29,24 @@
 
 namespace OrthancStone 
 {
+  static const uint64_t IObserver_FIRST_UNIQUE_ID = 10973;
+  static uint64_t IObserver_nextUniqueId = IObserver_FIRST_UNIQUE_ID;
+  
   IObserver::IObserver(MessageBroker& broker)
     : broker_(broker)
-    , fingerprint_()
   {
-    // we store the fingerprint_ as a char array to avoid problems when
-    // reading it in a deceased object.
-    // remember this is panic-level code to track zombie object usage
-    std::string fingerprint = Orthanc::Toolbox::GenerateUuid();
-    const char* fingerprintRaw = fingerprint.c_str();
-    memcpy(fingerprint_, fingerprintRaw, 37);
+    AssignFingerprint();
     broker_.Register(*this);
   }
 
-
   IObserver::~IObserver()
   {
     try
     {
-      LOG(TRACE) << "IObserver(" << std::hex << this << std::dec << ")::~IObserver : fingerprint_ == " << fingerprint_;
-      const char* deadMarker = "deadbeef-dead-dead-0000-0000deadbeef";
-      ORTHANC_ASSERT(strlen(deadMarker) == 36);
-      memcpy(fingerprint_, deadMarker, 37);
+      LOG(TRACE) << "IObserver(" << std::hex << this << std::dec << ")::~IObserver : fingerprint_[0] == " << fingerprint_[0];
+      fingerprint_[0] = 0xdeadbeef;
+      fingerprint_[1] = 0xdeadbeef;
+      fingerprint_[2] = 0xdeadbeef;
       broker_.Unregister(*this);
     }
     catch (const Orthanc::OrthancException& e)
@@ -74,20 +70,20 @@
     }
   }
 
+  static const int64_t IObserver_UNIQUE_ID_MAGIC_NUMBER = 2742024;
+
+  void IObserver::AssignFingerprint()
+  {
+    fingerprint_[0] = IObserver_nextUniqueId;
+    fingerprint_[1] = fingerprint_[0] / 2;
+    fingerprint_[2] = fingerprint_[1] + IObserver_UNIQUE_ID_MAGIC_NUMBER;
+    IObserver_nextUniqueId++;
+  }
 
   bool IObserver::DoesFingerprintLookGood() const
   {
-    for (size_t i = 0; i < 36; ++i) {
-      bool ok = false;
-      if (fingerprint_[i] >= 'a' && fingerprint_[i] <= 'f')
-        ok = true;
-      if (fingerprint_[i] >= '0' && fingerprint_[i] <= '9')
-        ok = true;
-      if (fingerprint_[i] == '-')
-        ok = true;
-      if (!ok)
-        return false;
-    }
-    return fingerprint_[36] == 0;
+    return (fingerprint_[0] >= IObserver_FIRST_UNIQUE_ID) &&
+      (fingerprint_[1] == fingerprint_[0] / 2) &&
+      (fingerprint_[2] == fingerprint_[1] + IObserver_UNIQUE_ID_MAGIC_NUMBER);
   }
 }
--- a/Framework/Messages/IObserver.h	Mon Jan 27 14:34:54 2020 +0100
+++ b/Framework/Messages/IObserver.h	Mon Jan 27 15:13:00 2020 +0100
@@ -29,18 +29,20 @@
   {
   private:
     MessageBroker&  broker_;
-    // the following is a UUID that is used to disambiguate different observers
-    // that may have the same address
-    char     fingerprint_[37];
+    // the following is a int64_t with some checks that is used to 
+    // disambiguate different observers that may have the same address
+    int64_t     fingerprint_[3];
+
+    void AssignFingerprint();
 
   public:
     IObserver(MessageBroker& broker);
 
     virtual ~IObserver();
 
-    const char* GetFingerprint() const
+    int64_t GetFingerprint() const
     {
-      return fingerprint_;
+      return fingerprint_[0];
     }
 
     bool DoesFingerprintLookGood() const;