Skip to content
This repository has been archived by the owner on Oct 6, 2023. It is now read-only.

Commit

Permalink
generator: add receiver class.
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthieu Kermagoret committed Feb 10, 2017
1 parent 932f134 commit df0294f
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
1 change: 1 addition & 0 deletions build/generator/CMakeLists.txt
Expand Up @@ -30,6 +30,7 @@ add_library("${GENERATOR}" SHARED
"${SRC_DIR}/endpoint.cc"
"${SRC_DIR}/factory.cc"
"${SRC_DIR}/main.cc"
"${SRC_DIR}/receiver.cc"
"${SRC_DIR}/sender.cc"
# Headers.
"${INC_DIR}/dummy.hh"
Expand Down
4 changes: 4 additions & 0 deletions generator/inc/com/centreon/broker/generator/receiver.hh
Expand Up @@ -20,6 +20,7 @@
# define CCB_GENERATOR_RECEIVER_HH

# include "com/centreon/broker/io/stream.hh"
# include "com/centreon/broker/misc/unordered_hash.hh"
# include "com/centreon/broker/namespace.hh"

CCB_BEGIN()
Expand All @@ -43,6 +44,9 @@ namespace generator {
private:
receiver(receiver const& other);
receiver& operator=(receiver const& other);

umap<unsigned int, unsigned int>
_last_numbers;
};
}

Expand Down
82 changes: 82 additions & 0 deletions generator/src/receiver.cc
@@ -0,0 +1,82 @@
/*
** Copyright 2017 Centreon
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
**
** For more information : contact@centreon.com
*/

#include "com/centreon/broker/exceptions/msg.hh"
#include "com/centreon/broker/io/exceptions/shutdown.hh"
#include "com/centreon/broker/generator/dummy.hh"
#include "com/centreon/broker/generator/receiver.hh"

using namespace com::centreon::broker;
using namespace com::centreon::broker::generator;

/**
* Constructor.
*/
receiver::receiver() {}

/**
* Destructor.
*/
receiver::~receiver() {}

/**
* Throw an exception.
*
* @param[out] d
* @param[in] deadline Unused.
*
* @return This method will throw.
*/
bool receiver::read(misc::shared_ptr<io::data>& d, time_t deadline) {
(void)deadline;
d.clear();
throw (io::exceptions::shutdown(true, false)
<< "cannot read from event receiver (generator)");
return (true);
}

/**
* Receive a new event.
*
* @param[in] d New event.
*
* @return 1.
*/
int receiver::write(misc::shared_ptr<io::data> const& d) {
if (!d.isNull() && (d->type() == dummy::static_type())) {
dummy const& e(d.ref_as<dummy>());

// Find last number of the Broker instance.
umap<unsigned int, unsigned int>::iterator
it(_last_numbers.find(e.source_id));
if (it == _last_numbers.end()) {
_last_numbers[e.source_id] = 0;
it = _last_numbers.find(e.source_id);
}

// Check current number.
if (e.number == 1)
it->second = e.number;
else if (e.number != ++(it->second))
throw (exceptions::msg()
<< "invalid sequence number for Centreon Broker instance "
<< e.source_id << ": got " << e.number << ", expected "
<< it->second - 1);
}
return (1);
}

0 comments on commit df0294f

Please sign in to comment.