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

#ifndef POLYGRAPH__RUNTIME_REPSIZE_H
#define POLYGRAPH__RUNTIME_REPSIZE_H

#include "xstd/Size.h"

class OLog;
class ILog;

// maintains some of the logic of HTTP response sizes such as
// missing or incorrect Content-Length headers
class RepSize {
	public:
		RepSize();

		void reset();

		Size expected() const { return theExpSize; }
		Size actual() const { return theActSize; }
		bool gotAll() const { return !expectToGetMore(); }
		bool expectToGetMore() const { return theActSize < theExpSize; }
		bool expectToGetLess() const { return theActSize > theExpSize; }
		Size expectToGet(Size availSize) const;

		bool expectingWhatParsed() const { return isExpectingWhatParsed; }
		void expectingWhatParsed(bool be) { isExpectingWhatParsed = be; }

		void expect(Size sz) { theExpSize = sz; }
		void got(Size sz) { theActSize += sz; }

		void store(OLog &ol) const;
		void load(ILog &il);

	protected:
		Size theExpSize;  // expected (e.g., Content-Length header)
		Size theActSize;  // incremented as response is received or sent
		bool isExpectingWhatParsed; // body parser will determine ExpSize
};

inline
OLog &operator <<(OLog &ol, const RepSize &s) {
	s.store(ol);
	return ol;
}

inline
ILog &operator >>(ILog &il, RepSize &s) {
	s.load(il);
	return il;
}


#endif


syntax highlighted by Code2HTML, v. 0.9.1