Annotation of fam/fam/Pollster.c++, Revision 1.1
1.1 ! trev 1: // Copyright (C) 1999 Silicon Graphics, Inc. All Rights Reserved.
! 2: //
! 3: // This program is free software; you can redistribute it and/or modify it
! 4: // under the terms of version 2 of the GNU General Public License as
! 5: // published by the Free Software Foundation.
! 6: //
! 7: // This program is distributed in the hope that it would be useful, but
! 8: // WITHOUT ANY WARRANTY; without even the implied warranty of
! 9: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Further, any
! 10: // license provided herein, whether implied or otherwise, is limited to
! 11: // this program in accordance with the express provisions of the GNU
! 12: // General Public License. Patent licenses, if any, provided herein do not
! 13: // apply to combinations of this program with other product or programs, or
! 14: // any other product whatsoever. This program is distributed without any
! 15: // warranty that the program is delivered free of the rightful claim of any
! 16: // third person by way of infringement or the like. See the GNU General
! 17: // Public License for more details.
! 18: //
! 19: // You should have received a copy of the GNU General Public License along
! 20: // with this program; if not, write the Free Software Foundation, Inc., 59
! 21: // Temple Place - Suite 330, Boston MA 02111-1307, USA.
! 22:
! 23: #include "Pollster.h"
! 24:
! 25: #include "Interest.h"
! 26: #include "Log.h"
! 27: #include "Scheduler.h"
! 28: #include "ServerHost.h"
! 29:
! 30: bool Pollster::remote_polling_enabled = true;
! 31: timeval Pollster::pintvl; // set when main() calls interval()
! 32: Set<Interest *> Pollster::polled_interests;
! 33: Set<ServerHost *> Pollster::polled_hosts;
! 34: bool Pollster::polling = false;
! 35:
! 36: void
! 37: Pollster::watch(Interest *ip)
! 38: {
! 39: polled_interests.insert(ip);
! 40:
! 41: if (!polling)
! 42: start_polling();
! 43: }
! 44:
! 45: void
! 46: Pollster::forget(Interest *ip)
! 47: {
! 48: int old_size = polled_interests.size();
! 49: (void) polled_interests.remove(ip);
! 50: int new_size = polled_interests.size();
! 51:
! 52: if (old_size && !new_size && !polled_hosts.size())
! 53: stop_polling();
! 54: }
! 55:
! 56: void
! 57: Pollster::watch(ServerHost *ip)
! 58: {
! 59: if (remote_polling_enabled)
! 60: { polled_hosts.insert(ip);
! 61:
! 62: if (!polling)
! 63: start_polling();
! 64: }
! 65: }
! 66:
! 67: void
! 68: Pollster::forget(ServerHost *ip)
! 69: {
! 70: int old_size = polled_hosts.size();
! 71: (void) polled_hosts.remove(ip);
! 72: int new_size = polled_hosts.size();
! 73:
! 74: if (old_size && !new_size && !polled_interests.size())
! 75: stop_polling();
! 76: }
! 77:
! 78: //////////////////////////////////////////////////////////////////////////////
! 79:
! 80: void
! 81: Pollster::start_polling()
! 82: {
! 83: assert(!polling);
! 84: assert(polled_interests.size() || polled_hosts.size());
! 85: Log::debug("polling every %d seconds", pintvl.tv_sec);
! 86: (void) Scheduler::install_recurring_task(pintvl, polling_task, NULL);
! 87: polling = true;
! 88: }
! 89:
! 90: void
! 91: Pollster::stop_polling()
! 92: {
! 93: assert(polling);
! 94: assert(!polled_interests.size());
! 95: assert(!polled_hosts.size());
! 96: (void) Scheduler::remove_recurring_task(polling_task, NULL);
! 97: polling = false;
! 98: Log::debug("will stop polling");
! 99: }
! 100:
! 101: void
! 102: Pollster::polling_task(void *)
! 103: {
! 104: timeval t0, t1;
! 105: (void) gettimeofday(&t0, NULL);
! 106:
! 107: int ni = 0;
! 108: for (Interest *ip = polled_interests.first();
! 109: ip;
! 110: ip = polled_interests.next(ip))
! 111: {
! 112: ip->poll();
! 113: ni++;
! 114: }
! 115:
! 116: int nh = 0;
! 117: if (remote_polling_enabled)
! 118: for (ServerHost *hp = polled_hosts.first();
! 119: hp;
! 120: hp = polled_hosts.next(hp))
! 121: {
! 122: hp->poll();
! 123: nh++;
! 124: }
! 125:
! 126: if ((ni || nh) && (Log::get_level() == Log::DEBUG))
! 127: { (void) gettimeofday(&t1, NULL);
! 128: double t = t1.tv_sec - t0.tv_sec + (t1.tv_usec - t0.tv_usec)/1000000.0;
! 129: Log::debug("polled %d interest%s and %d host%s in %.3f seconds",
! 130: ni, ni == 1 ? "" : "s",
! 131: nh, nh == 1 ? "" : "s",
! 132: t);
! 133: }
! 134: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>