Back to index

nordugrid-arc-nox  1.1.0~rc6
Counter.cpp
Go to the documentation of this file.
00001 // -*- indent-tabs-mode: nil -*-
00002 
00003 #ifdef HAVE_CONFIG_H
00004 #include <config.h>
00005 #endif
00006 
00007 // Counter.cpp
00008 
00009 #include <cstdlib>
00010 
00011 #include "Counter.h"
00012 
00013 namespace Arc {
00014 
00015   const Glib::TimeVal ETERNAL(G_MAXLONG, 0);
00016   const Glib::TimeVal HISTORIC(G_MINLONG, 0);
00017 
00018   Counter::Counter() {
00019     // Nothing needs to be done.
00020   }
00021 
00022   Counter::Counter(const Counter&) {
00023     // Executing this code should be impossible!
00024     exit(EXIT_FAILURE);
00025   }
00026 
00027   Counter::~Counter() {
00028     // Nothing needs to be done.
00029   }
00030 
00031   void Counter::operator=(const Counter&) {
00032     // Executing this code should be impossible!
00033     exit(EXIT_FAILURE);
00034   }
00035 
00036   Glib::TimeVal Counter::getCurrentTime() {
00037     Glib::TimeVal currentTime;
00038     currentTime.assign_current_time();
00039     return currentTime;
00040   }
00041 
00042   Glib::TimeVal Counter::getExpiryTime(Glib::TimeVal duration) {
00043     if (duration < ETERNAL)
00044       return getCurrentTime() + duration;
00045     else
00046       return ETERNAL;
00047   }
00048 
00049   CounterTicket Counter::getCounterTicket(Counter::IDType reservationID,
00050                                           Glib::TimeVal expiryTime,
00051                                           Counter *counter) {
00052     return CounterTicket(reservationID, expiryTime, counter);
00053   }
00054 
00055   ExpirationReminder Counter::getExpirationReminder(Glib::TimeVal expTime,
00056                                                     Counter::IDType resID) {
00057     return ExpirationReminder(expTime, resID);
00058   }
00059 
00060   CounterTicket::CounterTicket()
00061     : reservationID(0),
00062       expiryTime(HISTORIC),
00063       counter(0) {
00064     // Nothing else needs to be done.
00065   }
00066 
00067   CounterTicket::CounterTicket(Counter::IDType reservationID,
00068                                Glib::TimeVal expiryTime,
00069                                Counter *counter)
00070     : reservationID(reservationID),
00071       expiryTime(expiryTime),
00072       counter(counter) {
00073     // Nothing else needs to be done.
00074   }
00075 
00076   bool CounterTicket::isValid() {
00077     return expiryTime > counter->getCurrentTime();
00078   }
00079 
00080   void CounterTicket::extend(Glib::TimeVal duration) {
00081     counter->extend(reservationID, expiryTime, duration);
00082   }
00083 
00084   void CounterTicket::cancel() {
00085     counter->cancel(reservationID);
00086     reservationID = 0;
00087     expiryTime = HISTORIC;
00088     counter = 0;
00089   }
00090 
00091   ExpirationReminder::ExpirationReminder(Glib::TimeVal expiryTime,
00092                                          Counter::IDType reservationID)
00093     : expiryTime(expiryTime),
00094       reservationID(reservationID) {
00095     // Nothing else needs to be done.
00096   }
00097 
00098   bool ExpirationReminder::operator<(const ExpirationReminder& other) const {
00099     // Smaller time has higher priority!
00100     return expiryTime > other.expiryTime;
00101   }
00102 
00103   Glib::TimeVal ExpirationReminder::getExpiryTime() const {
00104     return expiryTime;
00105   }
00106 
00107   Counter::IDType ExpirationReminder::getReservationID() const {
00108     return reservationID;
00109   }
00110 
00111 }