/*
 * encapsulated poll
 */

#ifndef _VDRMON_SELECT
#define _VDRMON_SELECT

#include <string>
#include <sys/select.h>
#include "sock.h"
#include "handler.h"

struct node;
class cSelect
{
private:
  cVdrmonClientSocket * readeventpipe;
  cVdrmonClientSocket * writeeventpipe;
  node * clientsockets;
  int clientsocketcount;
  cVdrmonServerSocket * serversocket;
  cHandler * handler;
  struct pollfd * pollfds;
  bool stopped;
  time_t nexttimer;
public:
  cSelect();
  virtual ~cSelect();
  void DispatchVdrEvent(string event);
  void SetEventPipe(int pipe[2]);
  void SetServerSocket(cVdrmonServerSocket * sock);
  void AddClientSocket(cVdrmonClientSocket * sock);
  void RemoveClientSocket(cVdrmonClientSocket * sock);
  bool Action();
  bool Stop();
private:
  void CreatePollfds();
  cVdrmonClientSocket * GetClientSocket(int fd);
  bool Poll();
  void NotifyClients(string event);
};

#endif
