view Framework/Messages/IObserver.cpp @ 1142:76705b430c78 toa2019110801

Merge
author Benjamin Golinvaux <bgo@osimis.io>
date Fri, 08 Nov 2019 14:36:24 +0100
parents f6be9412e42a
children ca2058bd74ef
line wrap: on
line source

/**
 * Stone of Orthanc
 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
 * Department, University Hospital of Liege, Belgium
 * Copyright (C) 2017-2019 Osimis S.A., Belgium
 *
 * This program is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public License
 * as published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 **/


#include "IObserver.h"

#include "IMessage.h"
#include "../StoneException.h"

#include <Core/Logging.h>
#include <Core/Toolbox.h>

namespace OrthancStone 
{
  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);
    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);
      broker_.Unregister(*this);
    }
    catch (const Orthanc::OrthancException& e)
    {
      if (e.HasDetails())
      {
        LOG(ERROR) << "OrthancException in ~IObserver: " << e.What() << " Details: " << e.GetDetails();
      }
      else
      {
        LOG(ERROR) << "OrthancException in ~IObserver: " << e.What();
      }
    }
    catch (const std::exception& e)
    {
      LOG(ERROR) << "std::exception in ~IObserver: " << e.what();
    }
    catch (...)
    {
      LOG(ERROR) << "Unknown exception in ~IObserver";
    }
  }


  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;
  }
}