/*
 * Author:
 *         Kendy Kutzner <kutzner@ira.uka.de>
 *
 * (c) 2005 Research Group Dr. Fuhrmann, University of Karlsruhe, Germany
 * All rights reserved.
 *
 * $Id: callbacklist.h,v 1.7 2005/11/21 10:46:57 kutzner Exp $
 *
 */

#ifndef _CALLBACKLIST_H_
#define _CALLBACKLIST_H_

#include <map>
#include <sys/time.h>
#include "callback.h"
#include "connection.h"
#include "statistic.h"

/** \file
 * headers for cblentry and callbacklist
 */


/**
 * Internal structure for class callbacklist.
 * \bug This should be moved into the class.
 */

struct cblentry {
	callbackbase * cb;
	struct timeval timeout;
	int fd_r;
	int fd_w;
	int fd_e;
	int id;
};

/**
 * the class callbacklist is basically an interface to select()
 * objects can register their connections and timeouts and get called back
 * when the conn is ready or the timeout is due
 */


class callbacklist{
	public:
	callbacklist();
	~callbacklist();
	int reg(callbackbase * cb, double timeout);
	int reg_r(callbackbase * cb, conn * c, double timeout);
	int reg_w(callbackbase * cb, conn * c, double timeout);
	int reg_e(callbackbase * cb, conn * c, double timeout);
	int unreg(int handle);
	int rereg(int handle);
	int rereg(int handle, double timeout);

	int wait_and_execute();

	private:
	typedef map<int,struct cblentry*> list_t;
	list_t list;
	int next_id();
	int _next_id;
	cblentry * makeentry(callbackbase * cb, double timeout);
	int _entries;

	double add_jitter(double); /// try to prevent syncronizations of all kinds

	stat_average fd_count_stats;
	stat_average timer_count_stats;
	stat_average timer_val_stats;

};
	
#endif

