Search:

SENF Extensible Network Framework

  • Home
  • Download
  • Wiki
  • BerliOS
  • ChangeLog
  • Browse SVN
  • Bug Tracker
  • Overview
  • Examples
  • HowTos
  • Glossary
  • PPI
  • Packets
  • Scheduler
  • Socket
  • Utils
  • Console
  • Daemon
  • Logger
  • Termlib
  • Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

WriteHelper.ct

Go to the documentation of this file.
00001 // $Id: WriteHelper.ct 1781 2011-04-11 12:10:19Z tho $
00002 //
00003 // Copyright (C) 2006 Stefan Bund <g0dil@berlios.de>
00004 //
00005 // This program is free software; you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation; either version 2 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // This program is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with this program; if not, write to the
00017 // Free Software Foundation, Inc.,
00018 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019 // Copyright (C) 2006
00020 
00024 //#include "WriteHelper.ih"
00025 
00026 // Custom includes
00027 #include <boost/bind.hpp>
00028 #include <senf/Utils/Exception.hh>
00029 
00030 #define prefix_
00031 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00032 
00033 template <class Handle>
00034 prefix_ senf::WriteHelper<Handle>::WriteHelper(Handle handle, std::string const & data,
00035                                                       Callback callback)
00036     : handle_(handle),
00037       fde_("WriteHelper", boost::bind(&WriteHelper::dispatchProcess, ptr(this), _1, _2),
00038            handle, scheduler::FdEvent::EV_WRITE),
00039       data_(data), callback_(callback), offset_(data_.begin()), errno_(0)
00040 {}
00041 
00042 template <class Handle>
00043 prefix_ std::string const & senf::WriteHelper<Handle>::data()
00044     const
00045 {
00046     if (offset_ > data_.begin()) {
00047         data_.erase(data_.begin(),offset_);
00048         offset_ = data_.begin();
00049     }
00050     return data_;
00051 }
00052 
00053 template <class Handle>
00054 prefix_ void senf::WriteHelper<Handle>::revoke()
00055 {
00056     ptr guard (this); // To ensure, 'this' is deleted only after this method terminates ...
00057     fde_.disable();
00058     fde_.action(0); // To remove the smart pointer reference to this
00059 }
00060 
00061 template <class Handle>
00062 prefix_ void senf::WriteHelper<Handle>::dispatchProcess(ptr helper, Handle handle,
00063                                                         scheduler::FdEvent::Events event)
00064 {
00065     // since we have a 'ptr' argument, the instance cannot be deleted
00066     // before this method returns
00067     helper->process(handle, event);
00068 }
00069 
00070 template <class Handle>
00071 prefix_ void senf::WriteHelper<Handle>::process(Handle handle,
00072                                                 scheduler::FdEvent::Events event)
00073 {
00074     bool complete_ (false);
00075     try {
00076         if (event != scheduler::FdEvent::EV_WRITE)
00077             throw SystemException(EPIPE SENF_EXC_DEBUGINFO);
00078         offset_ = handle.write(std::make_pair(offset_,data_.end()));
00079         if (offset_ == data_.end()) {
00080             data_.erase();
00081             offset_ = data_.begin();
00082             complete_ = true;
00083         }
00084     }
00085     catch (senf::SystemException const & ex) {
00086         errno_ = ex.errorNumber();
00087         done();
00088         return;
00089     }
00090     if (complete_)
00091         done();
00092 }
00093 
00094 template <class Handle>
00095 prefix_ void senf::WriteHelper<Handle>::done()
00096 {
00097     revoke();
00098     callback_(ptr(this));
00099 }
00100 
00101 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00102 #undef prefix_
00103 
00104 
00105 // Local Variables:
00106 // mode: c++
00107 // fill-column: 100
00108 // c-file-style: "senf"
00109 // indent-tabs-mode: nil
00110 // ispell-local-dictionary: "american"
00111 // compile-command: "scons -u test"
00112 // comment-column: 40
00113 // End:

Contact: senf-dev@lists.berlios.de | © 2006-2010 Fraunhofer Institute for Open Communication Systems, Network Research