view Framework/Messages/IObserver.h @ 975:e75fd08d6c75 toa2019083101

Cleaning in ICallable + changed fingerprint to plain char array to allow for dead object examination + additional check in FetchContext callback to avoid the unexplained rogue callbacks I have seen + protection in LoaderStateMachine::HandleSuccessMessage in case things go wrong anyway
author Benjamin Golinvaux <bgo@osimis.io>
date Sat, 31 Aug 2019 13:45:04 +0200
parents 38409549db43
children 262a0244e9b2
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/>.
 **/


#pragma once

#include "MessageBroker.h"
#include "IMessage.h"

#include <Core/Toolbox.h>

namespace OrthancStone 
{
  class IObserver : public boost::noncopyable
  {
  private:
    MessageBroker&  broker_;
    // the following is a UUID that is used to disambiguate different observers
    // that may have the same address
    char     fingerprint_[37];
  public:
    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();
      ORTHANC_ASSERT(strlen(fingerprintRaw) == 36);
      ORTHANC_ASSERT(fingerprintRaw[36] == 0);
      memcpy(fingerprint_, fingerprintRaw, 37);
      LOG(TRACE) << "IObserver(" << std::hex << this << std::dec << ")::IObserver : fingerprint_ == " << fingerprint_;
      broker_.Register(*this);
    }

    virtual ~IObserver()
    {
      LOG(TRACE) << "IObserver(" << std::hex << this << std::dec << ")::~IObserver : fingerprint_ == " << fingerprint_;
      broker_.Unregister(*this);
    }

    const char* GetFingerprint() const
    {
      return fingerprint_;
    }

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

    MessageBroker& GetBroker() const
    {
      return broker_;
    }
  };
}