From 86c65de3ae99420ca7a2af406a97ade6aa989774 Mon Sep 17 00:00:00 2001 From: schodet Date: Mon, 17 Jan 2005 15:04:29 +0000 Subject: Initial revision --- 2005/i/robert/src/scheduler/Makefile.defs | 6 ++ 2005/i/robert/src/scheduler/schedulable.hh | 49 +++++++++++ 2005/i/robert/src/scheduler/schedulable_alarm.cc | 56 +++++++++++++ 2005/i/robert/src/scheduler/schedulable_alarm.hh | 53 ++++++++++++ 2005/i/robert/src/scheduler/schedulable_read_fd.cc | 51 ++++++++++++ 2005/i/robert/src/scheduler/schedulable_read_fd.hh | 52 ++++++++++++ 2005/i/robert/src/scheduler/scheduler.cc | 96 ++++++++++++++++++++++ 2005/i/robert/src/scheduler/scheduler.hh | 53 ++++++++++++ 2005/i/robert/src/scheduler/test_scheduler.cc | 68 +++++++++++++++ 9 files changed, 484 insertions(+) create mode 100644 2005/i/robert/src/scheduler/Makefile.defs create mode 100644 2005/i/robert/src/scheduler/schedulable.hh create mode 100644 2005/i/robert/src/scheduler/schedulable_alarm.cc create mode 100644 2005/i/robert/src/scheduler/schedulable_alarm.hh create mode 100644 2005/i/robert/src/scheduler/schedulable_read_fd.cc create mode 100644 2005/i/robert/src/scheduler/schedulable_read_fd.hh create mode 100644 2005/i/robert/src/scheduler/scheduler.cc create mode 100644 2005/i/robert/src/scheduler/scheduler.hh create mode 100644 2005/i/robert/src/scheduler/test_scheduler.cc (limited to '2005/i/robert/src/scheduler') diff --git a/2005/i/robert/src/scheduler/Makefile.defs b/2005/i/robert/src/scheduler/Makefile.defs new file mode 100644 index 0000000..73b5da9 --- /dev/null +++ b/2005/i/robert/src/scheduler/Makefile.defs @@ -0,0 +1,6 @@ +PROGRAMS += test_scheduler + +test_scheduler_OBJECTS = \ + test_scheduler.o scheduler.o schedulable_read_fd.o schedulable_alarm.o + +test_scheduler: $(test_scheduler_OBJECTS) diff --git a/2005/i/robert/src/scheduler/schedulable.hh b/2005/i/robert/src/scheduler/schedulable.hh new file mode 100644 index 0000000..bfe6de3 --- /dev/null +++ b/2005/i/robert/src/scheduler/schedulable.hh @@ -0,0 +1,49 @@ +#ifndef schedulable_hh +#define schedulable_hh +// schedulable.hh +// {{{ +// +// Copyright (C) 2004 Nicolas Schodet +// +// 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. +// +// Contact : +// Web: http://perso.efrei.fr/~schodet/ +// Email: +// }}} + +class FdSet; + +namespace scheduler { + +class Scheduler; + +/// Classe abstraite pour un objet utilisable par le Scheduler. +class Schedulable +{ + public: + /// Destructeur. + virtual ~Schedulable (void) {} + /// Paramètre le Scheduler. + virtual void setup (Scheduler &scheduler, int time, FdSet &readSet, int &timeout) = + 0; + /// Appelé aprés l'attente du Scheduler, renvois true si un événement a + /// été traité. + virtual bool run (Scheduler &scheduler, int time, const FdSet &readSet) = 0; +}; + +} // namespace scheduler + +#endif // schedulable_hh diff --git a/2005/i/robert/src/scheduler/schedulable_alarm.cc b/2005/i/robert/src/scheduler/schedulable_alarm.cc new file mode 100644 index 0000000..c55832e --- /dev/null +++ b/2005/i/robert/src/scheduler/schedulable_alarm.cc @@ -0,0 +1,56 @@ +// schedulable_alarm.cc +// {{{ +// +// Copyright (C) 2004 Nicolas Schodet +// +// 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. +// +// Contact : +// Web: http://perso.efrei.fr/~schodet/ +// Email: +// }}} +#include "schedulable_alarm.hh" + +namespace scheduler { + +/// Paramètre le Scheduler. +void +SchedulableAlarm::setup (Scheduler &scheduler, int time, FdSet &readSet, int + &timeout) +{ + if (start_ != -1) + { + int to = start_ + step_ - time; + timeout = to > 0 ? to : 0; + } +} + +/// Appelé aprés l'attente du Scheduler, renvois true si un événement a +/// été traité. +bool +SchedulableAlarm::run (Scheduler &scheduler, int time, const FdSet &readSet) +{ + if (time > start_ + step_) + { + start_ = periodic_ ? start_ + step_ : -1; + callback_ (); + return true; + } + else + return false; +} + +} // namespace scheduler + diff --git a/2005/i/robert/src/scheduler/schedulable_alarm.hh b/2005/i/robert/src/scheduler/schedulable_alarm.hh new file mode 100644 index 0000000..f416d12 --- /dev/null +++ b/2005/i/robert/src/scheduler/schedulable_alarm.hh @@ -0,0 +1,53 @@ +#ifndef schedulable_alarm_hh +#define schedulable_alarm_hh +// schedulable_alarm.hh +// {{{ +// +// Copyright (C) 2004 Nicolas Schodet +// +// 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. +// +// Contact : +// Web: http://perso.efrei.fr/~schodet/ +// Email: +// }}} +#include "schedulable.hh" +#include "utils/callback.hh" + +namespace scheduler { + +/// Execute un callback à un instant donné. +class SchedulableAlarm : public Schedulable +{ + Callback callback_; + int start_, step_; + bool periodic_; + public: + /// Constructeur. + template + SchedulableAlarm (T callback, int start, int step, bool periodic = true) + : callback_ (callback), + start_ (start), step_ (step), periodic_ (periodic) + { } + /// Paramètre le Scheduler. + void setup (Scheduler &scheduler, int time, FdSet &readSet, int &timeout); + /// Appelé aprés l'attente du Scheduler, renvois true si un événement a + /// été traité. + bool run (Scheduler &scheduler, int time, const FdSet &readSet); +}; + +} // namespace scheduler + +#endif // schedulable_alarm_hh diff --git a/2005/i/robert/src/scheduler/schedulable_read_fd.cc b/2005/i/robert/src/scheduler/schedulable_read_fd.cc new file mode 100644 index 0000000..3c84c9b --- /dev/null +++ b/2005/i/robert/src/scheduler/schedulable_read_fd.cc @@ -0,0 +1,51 @@ +// schedulable_read_fd.cc +// {{{ +// +// Copyright (C) 2004 Nicolas Schodet +// +// 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. +// +// Contact : +// Web: http://perso.efrei.fr/~schodet/ +// Email: +// }}} +#include "schedulable_read_fd.hh" +#include "utils/fd_set.hh" + +namespace scheduler { + +/// Paramètre le Scheduler. +void +SchedulableReadFd::setup (Scheduler &scheduler, int time, FdSet &readSet, int &timeout) +{ + readSet.set (fd_); +} + +/// Appelé aprés l'attente du Scheduler, renvois true si un événement a +/// été traité. +bool +SchedulableReadFd::run (Scheduler &scheduler, int time, const FdSet &readSet) +{ + if (readSet.isSet (fd_)) + { + callback_ (); + return true; + } + else + return false; +} + +} // namespace scheduler + diff --git a/2005/i/robert/src/scheduler/schedulable_read_fd.hh b/2005/i/robert/src/scheduler/schedulable_read_fd.hh new file mode 100644 index 0000000..01d7b26 --- /dev/null +++ b/2005/i/robert/src/scheduler/schedulable_read_fd.hh @@ -0,0 +1,52 @@ +#ifndef schedulable_read_fd_hh +#define schedulable_read_fd_hh +// schedulable_read_fd.hh +// {{{ +// +// Copyright (C) 2004 Nicolas Schodet +// +// 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. +// +// Contact : +// Web: http://perso.efrei.fr/~schodet/ +// Email: +// }}} +#include "schedulable.hh" +#include "utils/callback.hh" + +namespace scheduler { + +/// Execute un callback sur une lecture possible sur un descripteur de +/// fichier. +class SchedulableReadFd : public Schedulable +{ + Callback callback_; + int fd_; + public: + /// Constructeur. + template + SchedulableReadFd (T callback, int fd) + : callback_ (callback), fd_ (fd) + { } + /// Paramètre le Scheduler. + void setup (Scheduler &scheduler, int time, FdSet &readSet, int &timeout); + /// Appelé aprés l'attente du Scheduler, renvois true si un événement a + /// été traité. + bool run (Scheduler &scheduler, int time, const FdSet &readSet); +}; + +} // namespace scheduler + +#endif // schedulable_read_fd_hh diff --git a/2005/i/robert/src/scheduler/scheduler.cc b/2005/i/robert/src/scheduler/scheduler.cc new file mode 100644 index 0000000..e37504f --- /dev/null +++ b/2005/i/robert/src/scheduler/scheduler.cc @@ -0,0 +1,96 @@ +// scheduler.cc +// {{{ +// +// Copyright (C) 2004 Nicolas Schodet +// +// 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. +// +// Contact : +// Web: http://perso.efrei.fr/~schodet/ +// Email: +// }}} +#include "scheduler.hh" +#include "utils/fd_set.hh" + +#include //XXX +#include +#include + +namespace scheduler { + +/// Destructeur. +Scheduler::~Scheduler (void) +{ + // TODO: Test for schedulables_ empty ? +} + +/// Ajoute un élément schedulable. +void +Scheduler::insert (Schedulable &schedulable) +{ + schedulables_.insert (&schedulable); +} + +/// Enlève un élément schedulable. +void +Scheduler::erase (Schedulable &schedulable) +{ + schedulables_.erase (&schedulable); +} + +/// Lance le scheduler. Si timeout est différent de -1, c'est le temps +/// maximal que prend le scheduler avant de rendre la main. Si +/// returnOnEvent est true, le premier évenement arrète le scheduler. +/// Renvois true si au moins un événement a été traité. +bool +Scheduler::schedule (int timeout/*-1*/, bool returnOnEvent/*false*/) +{ + bool event = false; + int t = time (0) * 1000; //XXX + int start = t; + do + { + // Prépare le select. + int to = timeout == -1 ? -1 : start + timeout - t; + FdSet readFds; + for (Schedulables::const_iterator i = schedulables_.begin (); + i != schedulables_.end (); + ++i) + { + int top = -1; + (*i)->setup (*this, t, readFds, top); + if (to == -1 || top != -1 && top < to) + { + to = top; + } + } + // Select. + timeval tv; + tv.tv_sec = to / 1000; + tv.tv_usec = to % 1000 * 1000; + select (FD_SETSIZE, readFds.get (), 0, 0, &tv); + // Run. + t = time (0) * 1000; //XXX + for (Schedulables::const_iterator i = schedulables_.begin (); + i != schedulables_.end (); + ++i) + event |= (*i)->run (*this, t, readFds); + t = time (0) * 1000; //XXX + } while (!(returnOnEvent && event) && start + timeout > t); //XXX + return event; +} + +} // namespace scheduler + diff --git a/2005/i/robert/src/scheduler/scheduler.hh b/2005/i/robert/src/scheduler/scheduler.hh new file mode 100644 index 0000000..3bd885b --- /dev/null +++ b/2005/i/robert/src/scheduler/scheduler.hh @@ -0,0 +1,53 @@ +#ifndef scheduler_hh +#define scheduler_hh +// scheduler.hh +// {{{ +// +// Copyright (C) 2004 Nicolas Schodet +// +// 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. +// +// Contact : +// Web: http://perso.efrei.fr/~schodet/ +// Email: +// }}} +#include "schedulable.hh" + +#include + +namespace scheduler { + +/// Scheduler, répartie le travail en fonction des événements. +class Scheduler +{ + typedef std::set Schedulables; + Schedulables schedulables_; + public: + /// Destructeur. + ~Scheduler (void); + /// Ajoute un élément schedulable. + void insert (Schedulable &schedulable); + /// Enlève un élément schedulable. + void erase (Schedulable &schedulable); + /// Lance le scheduler. Si timeout est différent de -1, c'est le temps + /// maximal que prend le scheduler avant de rendre la main. Si + /// returnOnEvent est true, le premier évenement arrète le scheduler. + /// Renvois true si au moins un événement a été traité. + bool schedule (int timeout = -1, bool returnOnEvent = false); +}; + +} // namespace scheduler + +#endif // scheduler_hh diff --git a/2005/i/robert/src/scheduler/test_scheduler.cc b/2005/i/robert/src/scheduler/test_scheduler.cc new file mode 100644 index 0000000..e16ecb1 --- /dev/null +++ b/2005/i/robert/src/scheduler/test_scheduler.cc @@ -0,0 +1,68 @@ +// test_scheduler.cc +// {{{ +// +// Copyright (C) 2004 Nicolas Schodet +// +// 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. +// +// Contact : +// Web: http://perso.efrei.fr/~schodet/ +// Email: +// }}} +#include "scheduler.hh" +#include "schedulable_read_fd.hh" +#include "schedulable_alarm.hh" +#include "utils/bind.hh" + +#include +#include +#include + +void +ev (void) +{ + std::cout << "event received" << std::endl; + char buf[1024]; + read (0, buf, 1024); +} + +void +to (const char *msg) +{ + std::cout << "timeout received " << msg << " at " << time (0) * 1000 << + std::endl; +} + +int +main (void) +{ + using namespace scheduler; + try + { + Scheduler s; + SchedulableReadFd srf (ev, 0); + s.insert (srf); + std::cout << "start at " << time (0) * 1000 << std::endl; + SchedulableAlarm sa1 (utils::bind (to, "3s"), time (0) * 1000, 1000); + s.insert (sa1); + s.schedule (10000); + } + catch (const std::exception &e) + { + std::cerr << e.what () << std::endl; + return 1; + } + return 0; +} -- cgit v1.2.3