#ifndef _BALANCER_
#define _BALANCER_

#include "sys/sys"
#include "memory/memory"

#include "backend/backend"
#include "backenddef/backenddef"
#include "config/config"
#include "fdset/fdset"
#include "error/error"

// Check ups of back ends and the web interface
#include "Checkers/wakeupthread/wakeupthread"
#include "Checkers/checkupthread/checkupthread"
#include "webinterface/webinterface"

using namespace std;

class Balancer {
public:
    Balancer ();
    void init();
    void addbackend(BackendDef const &b);
    void addbackend(Backend const &b,
		    bool is_up = true, bool is_live = true,
		    bool do_check = true);
    void deletebackend(unsigned i);
    void serve();
    
    unsigned nbackends()  		{ return backends.size(); }
    Backend &backend (unsigned i)	{ return backends[i]; }
    bool terminate() const 		{ return term; }
    void terminate (bool t)		{ term = t; }
    bool report() const			{ return rep; }
    void report (bool r)		{ rep = r; }
    void restart(bool t)		{ rest = t; }
    bool restart() const		{ return rest; }
    long requestnr() const		{ return request_nr; }

    unsigned connections();

private:
    void init_tcp ();
    void init_fd  ();

    int server_fd;
    long request_nr;
    vector<Backend> backends;
    bool term;
    bool rep;
    bool rest;
    Webinterface *webinterface;
};

extern Balancer balancer;

#endif
