#ifndef DISPATCHERSERVER_H
#define DISPATCHERSERVER_H

#include "cloudserver.h"

class TileList;


class DispatcherServer: public PIObject {
	PIOBJECT(DispatcherServer)
public:
	DispatcherServer(PIEthernet::Address addr);
	~DispatcherServer();
	void start();

	void updateConnectionsTile(TileList * tl);
	void updateServersTile(TileList * tl, PISet<const DispatcherClient *> servers = PISet<const DispatcherClient *>());
	void updateClientsTile(TileList * tl, PISet<const DispatcherClient *> servers = PISet<const DispatcherClient *>());
	const DispatcherClient * getConnection(int index);
	const DispatcherClient * getServer(int index);
	PISet<const DispatcherClient *> getServers(PISet<int> ids);
	void setMaxConnections(uint max_count);
	uint maxConnections() const {return max_connections;}
	EVENT_HANDLER0(void, picoutStatus);

private:
	EVENT_HANDLER1(void, newConnection, PIEthernet * , cl);
	EVENT_HANDLER1(void, disconnectClient, DispatcherClient *, client);
	EVENT_HANDLER0(void, cleanClients);

	PIEthernet eth;
	PIVector<DispatcherClient*> clients;
	PIMap<PIByteArray, CloudServer *> c_servers;
	PIMap<const DispatcherClient *, CloudServer *> index_c_servers;
	PIMap<const DispatcherClient *, CloudServer *> index_c_clients;
	PIVector<DispatcherClient*> rm_clients;
	PIVector<DispatcherClient*> rmrf_clients;
	PITimer timeout_timer;
	PIMutex map_mutex;
	uint client_gid;
	uint max_connections;
};

#endif // DISPATCHERSERVER_H
