123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- // Copyright (C) 2009 Internet Systems Consortium, Inc. ("ISC")
- //
- // Permission to use, copy, modify, and/or distribute this software for any
- // purpose with or without fee is hereby granted, provided that the above
- // copyright notice and this permission notice appear in all copies.
- //
- // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
- // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
- // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
- // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
- // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
- // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
- // PERFORMANCE OF THIS SOFTWARE.
- #include <config.h>
- #include <stdint.h>
- #include <cstdio>
- #include <vector>
- #include <iostream>
- #include <sstream>
- #include <boost/foreach.hpp>
- #include <exceptions/exceptions.h>
- #include <cc/data.h>
- #include <config/tests/fake_session.h>
- using namespace std;
- using namespace isc::cc;
- using namespace isc::data;
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- // ok i want these in cc/data
- bool
- listContains(ConstElementPtr list, ConstElementPtr el) {
- if (!list) {
- return (false);
- }
- BOOST_FOREACH(ConstElementPtr l_el, list->listValue()) {
- if (*l_el == *el) {
- return (true);
- }
- }
- return (false);
- }
- void
- listRemove(ElementPtr list, ConstElementPtr el) {
- int i = 0;
- BOOST_FOREACH(ConstElementPtr s_el, list->listValue()) {
- if (*el == *s_el) {
- list->remove(i);
- return;
- }
- i++;
- }
- }
- // endwant
- namespace isc {
- namespace cc {
- FakeSession::FakeSession(isc::data::ElementPtr initial_messages,
- isc::data::ElementPtr subscriptions,
- isc::data::ElementPtr msg_queue) :
- messages_(initial_messages),
- subscriptions_(subscriptions),
- msg_queue_(msg_queue),
- started_(false)
- {
- }
- FakeSession::~FakeSession() {
- }
- void
- FakeSession::disconnect() {
- }
- void
- FakeSession::startRead(boost::function<void()>) {
- started_ = true;
- }
- void
- FakeSession::establish(const char*) {
- }
- bool
- FakeSession::recvmsg(ConstElementPtr& msg, bool nonblock, int) {
- if (started_ && !nonblock) {
- // This would schedule another read for length, leading to
- // corputed data
- isc_throw(DoubleRead, "Second read scheduled from recvmsg");
- }
- //cout << "[XX] client asks for message " << endl;
- if (messages_ &&
- messages_->getType() == Element::list &&
- messages_->size() > 0) {
- msg = messages_->get(0);
- messages_->remove(0);
- } else {
- msg = ElementPtr();
- }
- return (true);
- }
- bool
- FakeSession::recvmsg(ConstElementPtr& env, ConstElementPtr& msg, bool nonblock,
- int)
- {
- if (started_ && !nonblock) {
- // This would schedule another read for length, leading to
- // corputed data
- isc_throw(DoubleRead, "Second read scheduled from recvmsg");
- }
- //cout << "[XX] client asks for message and env" << endl;
- env = ElementPtr();
- if (messages_ &&
- messages_->getType() == Element::list &&
- messages_->size() > 0) {
- // do we need initial message to have env[group] and [to] too?
- msg = messages_->get(0);
- messages_->remove(0);
- return (true);
- } else if (msg_queue_) {
- BOOST_FOREACH(ConstElementPtr c_m, msg_queue_->listValue()) {
- ConstElementPtr to_remove = ElementPtr();
- if (haveSubscription(c_m->get(0), c_m->get(1))) {
- ElementPtr new_env = Element::createMap();
- new_env->set("group", c_m->get(0));
- new_env->set("to", c_m->get(1));
- env = new_env;
- msg = c_m->get(2);
- to_remove = c_m;
- }
- if (to_remove) {
- listRemove(msg_queue_, to_remove);
- return (true);
- }
- }
- }
- msg = ElementPtr();
- env = ElementPtr();
- return (false);
- }
- void
- FakeSession::subscribe(std::string group, std::string instance) {
- //cout << "[XX] client subscribes to " << group << " . " << instance << endl;
- ElementPtr s_el = Element::createList();
- s_el->add(Element::create(group));
- s_el->add(Element::create(instance));
- if (!subscriptions_) {
- subscriptions_ = Element::createList();
- }
- subscriptions_->add(s_el);
- }
- void
- FakeSession::unsubscribe(std::string group, std::string instance) {
- //cout << "[XX] client unsubscribes from " << group << " . " << instance << endl;
- ElementPtr s_el = Element::createList();
- s_el->add(Element::create(group));
- s_el->add(Element::create(instance));
- if (!subscriptions_) {
- return;
- }
- listRemove(subscriptions_, s_el);
- }
- int
- FakeSession::group_sendmsg(ConstElementPtr msg, std::string group,
- std::string to, std::string)
- {
- //cout << "[XX] client sends message: " << msg << endl;
- //cout << "[XX] to: " << group << " . " << instance << "." << to << endl;
- addMessage(msg, group, to);
- return (1);
- }
- bool
- FakeSession::group_recvmsg(ConstElementPtr& envelope, ConstElementPtr& msg,
- bool nonblock, int seq)
- {
- return (recvmsg(envelope, msg, nonblock, seq));
- }
- int
- FakeSession::reply(ConstElementPtr envelope, ConstElementPtr newmsg) {
- //cout << "[XX] client sends reply: " << newmsg << endl;
- //cout << "[XX] env: " << envelope << endl;
- addMessage(newmsg, envelope->get("group")->stringValue(),
- envelope->get("to")->stringValue());
- return (1);
- }
- bool
- FakeSession::hasQueuedMsgs() const {
- return (false);
- }
- ConstElementPtr
- FakeSession::getFirstMessage(std::string& group, std::string& to) const {
- ConstElementPtr el;
- if (msg_queue_ && msg_queue_->size() > 0) {
- el = msg_queue_->get(0);
- msg_queue_->remove(0);
- group = el->get(0)->stringValue();
- to = el->get(1)->stringValue();
- return (el->get(2));
- } else {
- group = "";
- to = "";
- return (ElementPtr());
- }
- }
- void
- FakeSession::addMessage(ConstElementPtr msg, const std::string& group,
- const std::string& to)
- {
- ElementPtr m_el = Element::createList();
- m_el->add(Element::create(group));
- m_el->add(Element::create(to));
- m_el->add(msg);
- if (!msg_queue_) {
- msg_queue_ = Element::createList();
- }
- msg_queue_->add(m_el);
- }
- bool
- FakeSession::haveSubscription(const std::string& group,
- const std::string& instance)
- {
- if (!subscriptions_) {
- return (false);
- }
- ElementPtr s1 = Element::createList();
- ElementPtr s2 = Element::createList();
- s1->add(Element::create(group));
- s1->add(Element::create(instance));
- s2->add(Element::create(group));
- s2->add(Element::create("*"));
- bool result = (listContains(subscriptions_, s1) ||
- listContains(subscriptions_, s2));
- return (result);
- }
- bool
- FakeSession::haveSubscription(ConstElementPtr group, ConstElementPtr instance)
- {
- return (haveSubscription(group->stringValue(), instance->stringValue()));
- }
- }
- }
|