text: #include "Poller.h"
  
  #include "Channel.h"
  #include <iostream>
  
  #include <assert.h>
  #include <poll.h>
  
  using namespace net;
  
  Poller::Poller(EventLoop* loop)
    : ownerLoop_(loop)
  {
  }
  
  Poller::~Poller()
  {
  }
  
  Timestamp Poller::poll(int timeoutMs, ChannelList* activeChannels) {
    int numEvents = ::poll(&*pollfds_.begin(), pollfds_.size(), timeoutMs);
    Timestamp now(Timestamp::now());
    if (numEvents > 0) {
      std::cout << numEvents << " events happended" << std::endl;
      fillActiveChannels(numEvents, activeChannels);
    } else if (numEvents == 0) {
      std::cout << "nothing happended" << std::endl;
    } else {
      std::cout << "Poller::poll()" << std::endl;
    }
    return now;
  }
  
  void Poller::fillActiveChannels(int numEvents,
                                  ChannelList* activeChannels) const
  {
    for (auto pfd = pollfds_.begin(); 
         pfd != pollfds_.end() && numEvents > 0; ++pfd)
    {
      if (pfd->revents > 0) {
        --numEvents;
        auto ch = channels_.find(pfd->fd);
        assert(ch != channels_.end());
        Channel* channel = ch->second;
        assert(channel->fd() == pfd->fd);
        channel->set_revents(pfd->revents);
        activeChannels->push_back(channel);
      }
    }
  }
  
  void Poller::updateChannel(Channel* channel)
  {
    assertInLoopThread();
    std::cout << "fd = " << channel->fd() << " events = " << channel->events() << std::endl;
    if (channel->index() < 0) {
      assert(channels_.find(channel->fd()) == channels_.end());
      struct pollfd pfd;
      pfd.fd = channel->fd();
      pfd.events = static_cast<short>(channel->events());
      pfd.revents = 0;
      pollfds_.push_back(pfd);
      int idx = static_cast<int>(pollfds_.size()) - 1;
      channel->set_index(idx);
      channels_[pfd.fd] = channel;
    } else {
      assert(channels_.find(channel->fd()) != channels_.end());
      assert(channels_[channel->fd()] == channel);
      int idx = channel->index();
      assert(0 <= idx && idx < static_cast<int>(pollfds_.size()));
      struct pollfd& pfd = pollfds_[idx];
      assert(pfd.fd == channel->fd() || pfd.fd == -1);
      pfd.events = static_cast<short>(channel->events());
      pfd.revents = 0;
      if (channel->isNoneEvent()) {
        pfd.fd = -1;
      }
    }
  }