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
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

Daemon.ih

Go to the documentation of this file.
00001 // $Id: Daemon.ih 1781 2011-04-11 12:10:19Z tho $
00002 //
00003 // Copyright (C) 2007
00004 // Fraunhofer (FOKUS)
00005 // Competence Center NETwork research (NET), St. Augustin, GERMANY
00006 //     Stefan Bund <g0dil@berlios.de>
00007 //
00008 // This program is free software; you can redistribute it and/or modify
00009 // it under the terms of the GNU General Public License as published by
00010 // the Free Software Foundation; either version 2 of the License, or
00011 // (at your option) any later version.
00012 //
00013 // This program is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU General Public License
00019 // along with this program; if not, write to the
00020 // Free Software Foundation, Inc.,
00021 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00022 
00026 #ifndef IH_SENF_Utils_Daemon_Daemon_
00027 #define IH_SENF_Utils_Daemon_Daemon_ 1
00028 
00029 // Custom includes
00030 #include <deque>
00031 #include <senf/boost_intrusive/iset.hpp>
00032 #include <senf/boost_intrusive/iset_hook.hpp>
00033 #include <boost/utility.hpp>
00034 #include <boost/function.hpp>
00035 #include <senf/Scheduler/Scheduler.hh>
00036 
00037 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00038 
00039 namespace senf {
00040 namespace detail {
00041 
00043     class DaemonWatcher
00044         : boost::noncopyable
00045     {
00046     public:
00047 
00048         DaemonWatcher(int pid, int coutpipe, int cerrpipe, int stdout, int stderr);
00049 
00050         void run();
00051 
00052     private:
00053 
00054         class Forwarder
00055         {
00056         public:
00057             typedef boost::function<void ()> Callback;
00058 
00059             Forwarder(int src, Callback cb);
00060             ~Forwarder();
00061 
00062             void addTarget(int fd);
00063 
00064         private:
00065 
00066             // This is awkward ... we'll need to erase an element from the target list given
00067             // only the target object. This is best implement using an intrusive container.
00068             // However, this makes memory-management explicit and we'll need to be careful.
00069             typedef std::deque<char> Buffer;
00070             struct TargetListTag;
00071             typedef boost::intrusive::ilist_base_hook<TargetListTag> TargetListBase;
00072 
00073             struct Target : public TargetListBase
00074             {
00075                 Target(Forwarder & fwd, int fd);
00076 
00077                 int fd;
00078                 Buffer::size_type offset;
00079                 scheduler::FdEvent writeevent;
00080             };
00081             typedef boost::intrusive::ilist<TargetListBase::value_traits<Target>,false> Targets;
00082 
00083             struct DestroyDelete
00084             {
00085                 template <class T>
00086                 void operator()(T * t) { delete t; }
00087             };
00088 
00089             void readData(int event);
00090             void writeData(int event, Target * target);
00091 
00092             Buffer buffer_;
00093             int src_;
00094             Targets targets_;
00095             Callback cb_;
00096             scheduler::FdEvent readevent_;
00097         };
00098 
00099         void pipeClosed(int id);
00100         void sigChld(siginfo_t const &);
00101         void childDied();
00102         void childOk();
00103 
00104         int childPid_;
00105         int coutpipe_;
00106         int cerrpipe_;
00107         int stdout_;
00108         int stderr_;
00109         bool sigChld_;
00110 
00111         scheduler::SignalEvent cldSignal_;
00112         scheduler::TimerEvent timer_;
00113         Forwarder coutForwarder_;
00114         Forwarder cerrForwarder_;
00115     };
00116 
00117 }}
00118 
00119 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00120 #endif
00121 
00122 
00123 // Local Variables:
00124 // mode: c++
00125 // fill-column: 100
00126 // comment-column: 40
00127 // c-file-style: "senf"
00128 // indent-tabs-mode: nil
00129 // ispell-local-dictionary: "american"
00130 // compile-command: "scons -u test"
00131 // End:

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