/*
 +----------------------------------------------------------------------+
 | Zan                                                                  |
 +----------------------------------------------------------------------+
 | Copyright (c) 2012-2016 Swoole Team <http://github.com/swoole>       |
 +----------------------------------------------------------------------+
 | This source file is subject to version 2.0 of the Apache license,    |
 | that is bundled with this package in the file LICENSE, and is        |
 | available through the world-wide-web at the following url:           |
 | http://www.apache.org/licenses/LICENSE-2.0.html                      |
 | If you did not receive a copy of the Apache2.0 license and are unable|
 | to obtain it through the world-wide-web, please send a note to       |
 | license@swoole.com so we can mail you a copy immediately.            |
 +----------------------------------------------------------------------+
 | Author: Tianfeng Han  <mikan.tenny@gmail.com>                        |
 +----------------------------------------------------------------------+
 */


#include "swoole.h"
#include "swReactor.h"
#include "swError.h"
#include <string.h>

#include "zanLog.h"

#ifdef IDE_HELPER
#ifdef HAVE_KQUEUE
#include <sys/event.h>
#else
#include "helper/kqueue.h"
#define HAVE_KQUEUE
#endif
#else
#ifdef HAVE_KQUEUE
#include <sys/event.h>
#endif
#endif

#ifdef HAVE_KQUEUE


typedef struct swReactorKqueue_s swReactorKqueue;
typedef struct _swFd
{
    uint32_t fd;
    uint32_t fdtype;
} swFd;

static int swReactorKqueue_add(swReactor *reactor, int fd, int fdtype);
static int swReactorKqueue_set(swReactor *reactor, int fd, int fdtype);
static int swReactorKqueue_del(swReactor *reactor, int fd);
static int swReactorKqueue_wait(swReactor *reactor, struct timeval *timeo);
static void swReactorKqueue_free(swReactor *reactor);

struct swReactorKqueue_s
{
    int epfd;
    int event_max;
    struct kevent *events;
};

int swReactorKqueue_create(swReactor *reactor, int max_event_num)
{
    //create reactor object
    swReactorKqueue *reactor_object = sw_malloc(sizeof(swReactorKqueue));
    if (reactor_object == NULL)
    {
        zanTrace("[swReactorKqueueCreate] malloc[0] fail\n");
        return ZAN_ERR;
    }
    bzero(reactor_object, sizeof(swReactorKqueue));

    reactor->object = reactor_object;
    reactor->max_event_num = max_event_num;
    reactor_object->events = sw_calloc(max_event_num, sizeof(struct kevent));

    if (reactor_object->events == NULL)
    {
        zanTrace("[swReactorKqueueCreate] malloc[1] fail\n");
        sw_free(reactor_object);
        return ZAN_ERR;
    }
    //kqueue create
    reactor_object->event_max = max_event_num;
    reactor_object->epfd = kqueue();
    if (reactor_object->epfd < 0)
    {
        zanTrace("[swReactorKqueueCreate] kqueue_create[0] fail\n");
        sw_free(reactor_object->events);
        sw_free(reactor_object);
        return ZAN_ERR;
    }

    //binding method
    reactor->add = swReactorKqueue_add;
    reactor->set = swReactorKqueue_set;
    reactor->del = swReactorKqueue_del;
    reactor->wait = swReactorKqueue_wait;
    reactor->free = swReactorKqueue_free;

    return ZAN_OK;
}

static void swReactorKqueue_free(swReactor *reactor)
{
    swReactorKqueue *this = reactor->object;
    close(this->epfd);
    sw_free(this->events);
    sw_free(this);
}

static int swReactorKqueue_add(swReactor *reactor, int fd, int fdtype)
{
    if (swReactor_add(reactor, fd, fdtype) < 0)
    {
        return ZAN_ERR;
    }

    swReactorKqueue *this = reactor->object;
    struct kevent e;
    swFd fd_;
    int ret;
    bzero(&e, sizeof(e));

    int fflags = 0;
    fd_.fd = fd;
    fd_.fdtype = swReactor_fdtype(fdtype);

    if (swReactor_event_read(fdtype))
    {
#ifdef NOTE_EOF
        fflags = NOTE_EOF;
#endif
        EV_SET(&e, fd, EVFILT_READ, EV_ADD, fflags, 0, NULL);
        memcpy(&e.udata, &fd_, sizeof(swFd));
        ret = kevent(this->epfd, &e, 1, NULL, 0, NULL);
        if (ret < 0)
        {
            zanWarn("add events[fd=%d#%d, type=%d, events=read] failed.", fd, reactor->id, fd_.fdtype);
            return ZAN_ERR;
        }
    }

    if (swReactor_event_write(fdtype))
    {
        EV_SET(&e, fd, EVFILT_WRITE, EV_ADD, 0, 0, NULL);
        memcpy(&e.udata, &fd_, sizeof(swFd));
        ret = kevent(this->epfd, &e, 1, NULL, 0, NULL);
        if (ret < 0)
        {
            zanWarn("add events[fd=%d#%d, type=%d, events=write] failed.", fd, reactor->id, fd_.fdtype);
            return ZAN_ERR;
        }
    }

    memcpy(&e.udata, &fd_, sizeof(swFd));
    zanTrace("[THREAD #%ld]EP=%d|FD=%d\n", (long)pthread_self(), this->epfd, fd);
    reactor->event_num++;
    return ZAN_OK;
}

static int swReactorKqueue_set(swReactor *reactor, int fd, int fdtype)
{
    swReactorKqueue *this = reactor->object;
    struct kevent e;
    swFd fd_;
    int ret;
    bzero(&e, sizeof(e));

    int fflags = 0;
    fd_.fd = fd;
    fd_.fdtype = swReactor_fdtype(fdtype);

    swConnection *socket = swReactor_get(reactor, fd);
    if (swReactor_event_read(fdtype))
    {
#ifdef NOTE_EOF
        fflags = NOTE_EOF;
#endif

        EV_SET(&e, fd, EVFILT_READ, EV_ADD, fflags, 0, NULL);
        memcpy(&e.udata, &fd_, sizeof(swFd));
        ret = kevent(this->epfd, &e, 1, NULL, 0, NULL);
        if (ret < 0)
        {
            zanWarn("kqueue->set(%d, SW_EVENT_READ) failed.", fd);
            return ZAN_ERR;
        }
    }
    else if (socket->events & SW_EVENT_READ)
    {
        EV_SET(&e, fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
        memcpy(&e.udata, &fd_, sizeof(swFd));
        ret = kevent(this->epfd, &e, 1, NULL, 0, NULL);
        if (ret < 0)
        {
            zanWarn("kqueue->del(%d, SW_EVENT_READ) failed.", fd);
            return ZAN_ERR;
        }
    }

    if (swReactor_event_write(fdtype))
    {
        EV_SET(&e, fd, EVFILT_WRITE, EV_ADD, 0, 0, NULL);
        memcpy(&e.udata, &fd_, sizeof(swFd));
        ret = kevent(this->epfd, &e, 1, NULL, 0, NULL);
        if (ret < 0)
        {
            zanWarn("kqueue->set(%d, SW_EVENT_WRITE) failed.", fd);
            return ZAN_ERR;
        }
    }
    else if (socket->events & SW_EVENT_WRITE)
    {
        EV_SET(&e, fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
        memcpy(&e.udata, &fd_, sizeof(swFd));
        ret = kevent(this->epfd, &e, 1, NULL, 0, NULL);
        if (ret < 0)
        {
            zanWarn("kqueue->del(%d, SW_EVENT_WRITE) failed.", fd);
            return ZAN_ERR;
        }
    }

    //execute parent method
    swReactor_set(reactor, fd, fdtype);
    return ZAN_OK;
}

static int swReactorKqueue_del(swReactor *reactor, int fd)
{
    swReactorKqueue *this = reactor->object;
    struct kevent e;
    int ret;

    swConnection *socket = swReactor_get(reactor, fd);

    if (socket->events & SW_EVENT_READ)
    {
        EV_SET(&e, fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
        ret = kevent(this->epfd, &e, 1, NULL, 0, NULL);
        if (ret < 0)
        {
            zanWarn("kqueue->del(%d, SW_EVENT_READ) failed, ret=%d, errno=%d:%s", fd, ret, errno, strerror(errno));
            return ZAN_ERR;
        }
    }

    if (socket->events & SW_EVENT_WRITE)
    {
        EV_SET(&e, fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
        ret = kevent(this->epfd, &e, 1, NULL, 0, NULL);
        if (ret < 0)
        {
            zanWarn("kqueue->del(%d, SW_EVENT_WRITE) failed.", fd);
            return ZAN_ERR;
        }
    }

    if (swReactor_del(reactor, fd) < 0)
    {
        return ZAN_ERR;
    }
    reactor->event_num = reactor->event_num <= 0 ? 0 : reactor->event_num - 1;
    return ZAN_OK;
}

static int swReactorKqueue_wait(swReactor *reactor, struct timeval *timeo)
{
    swEvent event;
    swFd fd_;
    swReactorKqueue *object = reactor->object;
    swReactor_handle handle;

    int i, n, ret;
    struct timespec t;
    struct timespec *t_ptr;
    bzero(&t, sizeof(t));

    if (reactor->timeout_msec == 0)
    {
        reactor->timeout_msec = (timeo == NULL)? -1:timeo->tv_sec * 1000 + timeo->tv_usec / 1000;
    }

    while (reactor->running > 0)
    {
        if (reactor->timeout_msec > 0 || reactor->defer_callback_list)
        {
            int32_t timeout_msec = reactor->defer_callback_list? 1:reactor->timeout_msec;
            t.tv_sec = timeout_msec / 1000;
            t.tv_nsec = (timeout_msec - t.tv_sec * 1000) * 1000 *1000;
            t_ptr = &t;
        }
        else
        {
            t_ptr = NULL;
        }

        n = kevent(object->epfd, NULL, 0, object->events, object->event_max, t_ptr);
        if (n < 0)
        {
            if (swReactor_error(reactor) < 0)
            {
                zanWarn("Kqueue[#%d].", reactor->id);
                return ZAN_ERR;
            }
            else
            {
                continue;
            }
        }
        else if (n == 0)
        {
            if (reactor->onTimeout != NULL)
            {
                reactor->onTimeout(reactor);
            }
        //    continue;
        }

        for (i = 0; i < n; i++)
        {
            if (object->events[i].udata)
            {
                memcpy(&fd_, &(object->events[i].udata), sizeof(fd_));
                event.fd = fd_.fd;
                event.from_id = reactor->id;
                event.type = fd_.fdtype;
                event.socket = swReactor_get(reactor, event.fd);

                //read
                if (object->events[i].filter == EVFILT_READ )
                {
                    if(!event.socket->removed)
                    {
                        handle = swReactor_getHandle(reactor, SW_EVENT_READ, event.type);
                        ret = handle(reactor, &event);
                        if (ret < 0)
                        {
                            zanWarn("kqueue event read socket#%d handler failed.", event.fd);
                        }
                    }
                }
                //write
                else if (object->events[i].filter == EVFILT_WRITE)
                {
                    if(!event.socket->removed)
                    {
                        handle = swReactor_getHandle(reactor, SW_EVENT_WRITE, event.type);
                        ret = handle(reactor, &event);
                        if (ret < 0)
                        {
                            zanWarn("kqueue event write socket#%d handler failed.", event.fd);
                        }
                    }
                }
                else
                {
                    zanWarn("kqueue event unknow filter=%d", object->events[i].filter);
                }
            }
        }

        if (reactor->onFinish != NULL)
        {
            reactor->onFinish(reactor);
        }
    }
    return 0;
}
#endif
