/**
* Namf_Communication
* AMF Communication Service © 2019, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TSDSI, TTA, TTC). All rights reserved. 
*
* The version of the OpenAPI document: 1.1.0.alpha-1
* 
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/


#include "pistache/endpoint.h"
#include "pistache/http.h"
#include "pistache/router.h"
#ifdef __linux__
#include <vector>
#include <signal.h>
#include <unistd.h>
#endif

#include "IndividualSubscriptionDocumentApiImpl.h"
#include "IndividualUeContextDocumentApiImpl.h"
#include "N1N2IndividualSubscriptionDocumentApiImpl.h"
#include "N1N2MessageCollectionDocumentApiImpl.h"
#include "N1N2SubscriptionsCollectionForIndividualUEContextsDocumentApiImpl.h"
#include "NonUEN2MessageNotificationIndividualSubscriptionDocumentApiImpl.h"
#include "NonUEN2MessagesCollectionDocumentApiImpl.h"
#include "NonUEN2MessagesSubscriptionsCollectionDocumentApiImpl.h"
#include "SubscriptionsCollectionDocumentApiImpl.h"

#define PISTACHE_SERVER_THREADS     2
#define PISTACHE_SERVER_MAX_PAYLOAD 32768

static Pistache::Http::Endpoint *httpEndpoint;
#ifdef __linux__
static void sigHandler(int sig){
    switch(sig){
        case SIGINT:
        case SIGQUIT:
        case SIGTERM:
        case SIGHUP:
        default:
            httpEndpoint->shutdown();
            break;
    }
    exit(0);
}

static void setUpUnixSignals(std::vector<int> quitSignals) {
    sigset_t blocking_mask;
    sigemptyset(&blocking_mask);
    for (auto sig : quitSignals)
        sigaddset(&blocking_mask, sig);

    struct sigaction sa;
    sa.sa_handler = sigHandler;
    sa.sa_mask    = blocking_mask;
    sa.sa_flags   = 0;

    for (auto sig : quitSignals)
        sigaction(sig, &sa, nullptr);
}
#endif

using namespace oai::amf::api;

int main(int argc, char* argv[]) {
#ifdef __linux__
    std::vector<int> sigs{SIGQUIT, SIGINT, SIGTERM, SIGHUP};
    setUpUnixSignals(sigs);
#endif
    std::string amf_ip_address;
    if ((argc != 1) && (argc != 3)) {
        std::cout << "Error: Usage is " <<std::endl;
        std::cout << "  " << argv[0] << " [ -i www.xxx.yy.zz ]" <<std::endl;
        return -1;
    }

    if (argc == 1) {
        amf_ip_address.append(std::string("192.168.28.3"));
    } else {
        int opt = 0;
        while ((opt = getopt(argc, argv, "i:")) != -1) {
            switch(opt) {
            case 'i':
                amf_ip_address.append(optarg);
                break;
            default:
                std::cout << "Error: Usage is " <<std::endl;
                std::cout << "  " << argv[0] << " [ -i www.xxx.yy.zz ]" <<std::endl;
                return -1;
                break;
            }
        }
    }

    Pistache::Address addr(amf_ip_address, Pistache::Port(80));

    httpEndpoint = new Pistache::Http::Endpoint((addr));
    auto router = std::make_shared<Pistache::Rest::Router>();

    auto opts = Pistache::Http::Endpoint::options()
        .threads(PISTACHE_SERVER_THREADS);
    opts.flags(Pistache::Tcp::Options::ReuseAddr);
    opts.maxPayload(PISTACHE_SERVER_MAX_PAYLOAD);
    httpEndpoint->init(opts);

    
    IndividualSubscriptionDocumentApiImpl IndividualSubscriptionDocumentApiserver(router);
    IndividualSubscriptionDocumentApiserver.init();
    IndividualUeContextDocumentApiImpl IndividualUeContextDocumentApiserver(router);
    IndividualUeContextDocumentApiserver.init();
    N1N2IndividualSubscriptionDocumentApiImpl N1N2IndividualSubscriptionDocumentApiserver(router);
    N1N2IndividualSubscriptionDocumentApiserver.init();
    N1N2MessageCollectionDocumentApiImpl N1N2MessageCollectionDocumentApiserver(router);
    N1N2MessageCollectionDocumentApiserver.init();
    N1N2SubscriptionsCollectionForIndividualUEContextsDocumentApiImpl N1N2SubscriptionsCollectionForIndividualUEContextsDocumentApiserver(router);
    N1N2SubscriptionsCollectionForIndividualUEContextsDocumentApiserver.init();
    NonUEN2MessageNotificationIndividualSubscriptionDocumentApiImpl NonUEN2MessageNotificationIndividualSubscriptionDocumentApiserver(router);
    NonUEN2MessageNotificationIndividualSubscriptionDocumentApiserver.init();
    NonUEN2MessagesCollectionDocumentApiImpl NonUEN2MessagesCollectionDocumentApiserver(router);
    NonUEN2MessagesCollectionDocumentApiserver.init();
    NonUEN2MessagesSubscriptionsCollectionDocumentApiImpl NonUEN2MessagesSubscriptionsCollectionDocumentApiserver(router);
    NonUEN2MessagesSubscriptionsCollectionDocumentApiserver.init();
    SubscriptionsCollectionDocumentApiImpl SubscriptionsCollectionDocumentApiserver(router);
    SubscriptionsCollectionDocumentApiserver.init();

    httpEndpoint->setHandler(router->handler());
    httpEndpoint->serve();

    httpEndpoint->shutdown();

}

