Skip to content
Snippets Groups Projects
ReadHelper.ct 3.88 KiB
Newer Older
g0dil's avatar
g0dil committed
// $Id$
//
// Copyright (C) 2006 Stefan Bund <g0dil@berlios.de>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the
// Free Software Foundation, Inc.,
// 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
// Copyright (C) 2006
g0dil's avatar
g0dil committed

/** \file
    \brief ReadHelper non-inline template implementation */
g0dil's avatar
g0dil committed

#include "ReadHelper.ih"

// Custom includes
#include <errno.h>
#include "Utils/membind.hh"
#include "Utils/Exception.hh"
#include "Scheduler.hh"

#define prefix_
///////////////////////////////ct.p////////////////////////////////////////

template <class Handle>
prefix_ senf::ReadHelper<Handle>::ReadHelper(Handle handle, std::string::size_type maxSize,
g0dil's avatar
g0dil committed
                                                    InternalPredicate * predicate, Callback cb)
    : handle_(handle), maxSize_(maxSize), predicate_(predicate), callback_(cb),
g0dil's avatar
g0dil committed
      errno_(0), complete_(false)
{
    // Here we add a *static* member taking a *smart* pointer as first
    // argumnet instead of a simple bound-member as callback to the
    // scheduler. This ensures, that the refcount is at least 1 as
    // long as the helper is registered with the scheduler.
    senf::Scheduler::instance()
        .add(handle,boost::bind(&ReadHelper::dispatchProcess,ptr(this),_1,_2),
             senf::Scheduler::EV_READ);
g0dil's avatar
g0dil committed
}

template <class Handle>
prefix_ void senf::ReadHelper<Handle>::revoke()
g0dil's avatar
g0dil committed
{
    ptr guard (this); // To ensure, 'this' is deleted only after this method terminates ...
    senf::Scheduler::instance()
        .remove(handle_,senf::Scheduler::EV_READ);
g0dil's avatar
g0dil committed
}

template <class Handle>
prefix_ void
senf::ReadHelper<Handle>::dispatchProcess(ptr helper, Handle handle,
                                                 senf::Scheduler::EventId event)
g0dil's avatar
g0dil committed
{
    // since we have a 'ptr' argument, the instance cannot be deleted
    // before this method returns
    return helper->process(handle,event);
}

template <class Handle>
prefix_ void senf::ReadHelper<Handle>::process(Handle handle,
                                                      senf::Scheduler::EventId event)
g0dil's avatar
g0dil committed
{
    try {
        if (event != senf::Scheduler::EV_READ)
            throw SystemException(EPIPE);
        std::string rcv;
        handle.read(rcv, maxSize_ - data_.size());
        data_.append(rcv);
        std::string::size_type n = predicate_ ? (*predicate_)(data_) : std::string::npos;
        if (n != std::string::npos || data_.size() >= maxSize_ || rcv.size() == 0) {
            complete_ = true;
            if (n < data_.size()) {
                tail_.assign(data_,n,std::string::npos);
                data_.erase(n);
            }
        }
g0dil's avatar
g0dil committed
    }
    catch (senf::SystemException const & ex) {
        errno_ = ex.err;
        done();
g0dil's avatar
g0dil committed
        return;
g0dil's avatar
g0dil committed
    }
g0dil's avatar
g0dil committed
    if (complete_)
        done();
g0dil's avatar
g0dil committed
}

template <class Handle>
prefix_ void senf::ReadHelper<Handle>::done()
g0dil's avatar
g0dil committed
{
    revoke();
    callback_(ptr(this));
}

template <class Handle>
template <class Predicate>
prefix_ std::string::size_type
senf::ReadHelper<Handle>::InternalPredicate::Dispatcher<Predicate>::
g0dil's avatar
g0dil committed
operator()(std::string const & data)
{
    return predicate(data);
}

///////////////////////////////ct.e////////////////////////////////////////
#undef prefix_


// Local Variables:
// mode: c++
// fill-column: 100
// c-file-style: "senf"
// indent-tabs-mode: nil
// ispell-local-dictionary: "american"
// compile-command: "scons -u test"
// comment-column: 40
g0dil's avatar
g0dil committed
// End: