#ifndef _SESSION_MANAGER_H
#define _SESSION_MANAGER_H

#ifdef __cplusplus
extern "C"
{
#endif

#include "session_priv.h"
#include "log.h"

#define SESSION_LOG_ERROR(format, ...) LOG_ERROR("session", format, ##__VA_ARGS__)
#define SESSION_LOG_DEBUG(format, ...) LOG_DEBUG("session", format, ##__VA_ARGS__)

struct session_manager_options
{
    // max session number
    uint64_t max_tcp_session_num;
    uint64_t max_udp_session_num;

    // session overload
    uint8_t tcp_overload_evict_old_sess; // 1: evict old session, 0: bypass new session
    uint8_t udp_overload_evict_old_sess; // 1: evict old session, 0: bypass new session

    // TCP timeout
    uint64_t tcp_init_timeout;           // range: [1, 60000] (ms)
    uint64_t tcp_handshake_timeout;      // range: [1, 60000] (ms)
    uint64_t tcp_data_timeout;           // range: [1, 15999999000] (ms)
    uint64_t tcp_half_closed_timeout;    // range: [1, 604800000] (ms)
    uint64_t tcp_time_wait_timeout;      // range: [1, 600000] (ms)
    uint64_t tcp_discard_timeout;        // range: [1, 15999999000] (ms)
    uint64_t tcp_unverified_rst_timeout; // range: [1, 600000] (ms)
    // UDP timeout
    uint64_t udp_data_timeout;    // range: [1, 15999999000] (ms)
    uint64_t udp_discard_timeout; // range: [1, 15999999000] (ms)

    // duplicate packet filter
    uint8_t duplicated_packet_filter_enable;
    uint32_t duplicated_packet_filter_capacity; // range: [1, 4294967295]
    uint32_t duplicated_packet_filter_timeout;  // range: [1, 60000] (ms)
    double duplicated_packet_filter_error_rate; // range: [0.0, 1.0]

    // evicted session filter
    uint8_t evicted_session_filter_enable;
    uint32_t evicted_session_filter_capacity; // range: [1, 4294967295]
    uint32_t evicted_session_filter_timeout;  // range: [1, 60000] (ms)
    double evicted_session_filter_error_rate; // range: [0.0, 1.0]

    // TCP reassembly
    uint8_t tcp_reassembly_enable;
    uint32_t tcp_reassembly_max_timeout;  // range: [1, 60000] (ms)
    uint32_t tcp_reassembly_max_segments; // range: [2, 512]
};

struct session_manager_stat
{
    // TCP session
    uint64_t total_nr_tcp_sess_used;
    uint64_t curr_nr_tcp_sess_used;
    uint64_t curr_nr_tcp_sess_opening;
    uint64_t curr_nr_tcp_sess_active;
    uint64_t curr_nr_tcp_sess_closing;
    uint64_t curr_nr_tcp_sess_discard;
    uint64_t curr_nr_tcp_sess_closed;

    // UDP session
    uint64_t total_nr_udp_sess_used;
    uint64_t curr_nr_udp_sess_used;
    uint64_t curr_nr_udp_sess_opening;
    uint64_t curr_nr_udp_sess_active;
    uint64_t curr_nr_udp_sess_closing;
    uint64_t curr_nr_udp_sess_discard;
    uint64_t curr_nr_udp_sess_closed;

    // Evicted session
    uint64_t nr_tcp_sess_evicted; // sum
    uint64_t nr_udp_sess_evicted; // sum

    // Packet
    uint64_t nr_udp_pkts_nospace_bypass; // sum
    uint64_t nr_tcp_pkts_nospace_bypass; // sum
    uint64_t nr_tcp_pkts_nosess_bypass;  // sum
    uint64_t nr_tcp_pkts_duped_bypass;   // sum
    uint64_t nr_udp_pkts_duped_bypass;   // sum
    uint64_t nr_udp_pkts_evctd_bypass;   // sum

    // TCP segments
    uint64_t nr_tcp_seg_received;   // sum
    uint64_t nr_tcp_seg_expired;    // sum
    uint64_t nr_tcp_seg_retransmit; // sum
    uint64_t nr_tcp_seg_overlap;    // sum
    uint64_t nr_tcp_seg_no_space;   // sum
    uint64_t nr_tcp_seg_inorder;    // sum
    uint64_t nr_tcp_seg_reorded;    // sum
    uint64_t nr_tcp_seg_buffered;   // sum
    uint64_t nr_tcp_seg_released;   // sum
};

struct session_manager;
struct session_manager *session_manager_new(struct session_manager_options *opts, uint64_t now);
void session_manager_free(struct session_manager *mgr);
void session_manager_record_duplicated_packet(struct session_manager *mgr, const struct packet *pkt, uint64_t now);

struct session *session_manager_new_session(struct session_manager *mgr, const struct packet *pkt, uint64_t now);
void session_manager_free_session(struct session_manager *mgr, struct session *sess);

struct session *session_manager_lookup_session(struct session_manager *mgr, const struct packet *pkt, uint64_t now);
int session_manager_update_session(struct session_manager *mgr, struct session *sess, const struct packet *pkt, uint64_t now);

// return session need free by session_manager_free_session()
struct session *session_manager_get_expired_session(struct session_manager *mgr, uint64_t now);
struct session *session_manager_get_evicted_session(struct session_manager *mgr);

// return  0: have already timeout session
// return >0: next expire interval
uint64_t session_manager_get_expire_interval(struct session_manager *mgr);

struct session_manager_stat *session_manager_stat(struct session_manager *mgr);

#ifdef __cplusplus
}
#endif

#endif
