/* Web Polygraph       http://www.web-polygraph.org/
 * (C) 2003-2006 The Measurement Factory
 * Licensed under the Apache License, Version 2.0 */

#ifndef POLYGRAPH__PGL_HOSTSBASEDITER_H
#define POLYGRAPH__PGL_HOSTSBASEDITER_H

#include "xstd/Array.h"
#include "xstd/NetAddr.h"

class HostsBasedSym;
class NetAddrSym;

// a configuration of an object often contains 'hosts' or 'addresses' array
// the addresses in hosts array should be filtered against "local" ones
// and then coresponding objects should be created based on
// one-for-many configuration and a specific host address
//
// host based iterator hides the complexity of the above process
// the iterator returns (object configuration symbol, host) pairs
class HostsBasedIter {
	public:
		typedef Array<HostsBasedSym*> CfgSyms;
		typedef Array<InAddress> Addrs;

	public:
		HostsBasedIter();
		HostsBasedIter(const Addrs *aLocalAddrs);
		virtual ~HostsBasedIter();

		HostsBasedIter &start(const Addrs *localAddrs);
		HostsBasedIter &restart();

		bool eof() const;
		operator void *() const;
		HostsBasedIter &operator ++();
		HostsBasedIter &nextSym();

		const NetAddr &host();   // current host
		const NetAddrSym *hostSym();
		int hostIdx() const { return theHostIdx; }
		int cfgIdx() const { return theCfgIdx; }

	protected:
		void sync();
		virtual bool belongs(const HostsBasedSym *cfgSym, const NetAddr &host) const;

		virtual HostsBasedSym *cfgSym(int idx) const = 0;
		virtual int cfgCount() const = 0;    // number of cfg symbols

	private:
		NetAddr host(int cidx, int hidx) const;

	protected:
		const Addrs *theLocalAddrs;  // local addresses

		int theCfgIdx;  // sequential cfgSym idx
		int theHostIdx; // sequential host idx within cfgSym

	private:
		NetAddr theHost; // cached value
};

#endif


syntax highlighted by Code2HTML, v. 0.9.1