#include <iostream>
#include "stdlib.h"
#include <fstream>
#include <string>
#include "unistd.h"
#include "time.h"
#include "math.h"
#include <pthread.h>
#include <csignal>

#include <rte_common.h>
#include <rte_eal.h>
#include <rte_ring.h>
#include <rte_ring_core.h>


using namespace std;
int keepRunning=1;

void signal_handler(int signum) 
{
    std::cout << "\nSignal (" << signum << ") received. Exiting..." << std::endl;
    keepRunning = 0;
}

void init_dpdk()
{
    /* DPDK initialization */
    int instId = 12;
    char procName[50] = {0} ;
    char filePrefix[50] = {0} ;
    sprintf(procName, "ipp-dpdk-opt-%d", instId) ;
    sprintf(filePrefix, "--file-prefix=ipp-inst-%d", instId) ;
    int dpdk_argc = 8; char *dpdk_argv[] = { procName, "-c", "0x2", "--no-huge", filePrefix ,"-m","64" , "--log-level=eal,8"};

    int ret = rte_eal_init(dpdk_argc, dpdk_argv);
	if(ret < 0)
    {
        cout << "DPDK initialization: rte_eal_init failed with code "<< ret << endl;
        return  ;
    }
	cout << "\n\n\n";

}

void init_ring()
{
	for (int i=0; i<25; i++)
	{
		int size = pow(2,i) ;
		cout << i+1 << " " << size -1 << endl; //" "<< rte_ring_get_memsize(size) << endl ;
		char name [50];
		sprintf (name,"test_ring-%d",i);
		rte_ring_create(name, size-1, 0, RING_F_EXACT_SZ);
	}
}

static void* thread_func1(void *arg)
{
	while(keepRunning)
	{
		printf("this is thread1\n");
		sleep(100);
	}
	return NULL;
}

static void* thread_func2(void *arg)
{
	while(keepRunning)
	{
		printf("this is thread2\n");
		sleep(100);
	}
	return NULL;
}

int main() {
	std::cout << "##################### Starting the test ################ " << std::endl;

    struct sigaction sig_action;
    sig_action.sa_handler = signal_handler;
    sigemptyset(&sig_action.sa_mask);
    sig_action.sa_flags = 0;
    sigaction(SIGINT, &sig_action, NULL);

	init_dpdk();

	// worker threads initializtion
    pthread_t thread_id1, thread_id2;
    pthread_attr_t thread_attr;
    pthread_attr_init(&thread_attr);

    pthread_create(&thread_id1, &thread_attr, &thread_func1,NULL);
    pthread_setname_np(thread_id1, "WorkerThread1");
    pthread_create(&thread_id2, &thread_attr, &thread_func2,NULL);
    pthread_setname_np(thread_id2, "WorkerThread2");

    while (keepRunning) {
        pause();  
    }

    std::cout << "##################### Edning the test ################ " << std::endl;
    pthread_join(thread_id1, NULL);
    pthread_join(thread_id2, NULL);


    return 0;
}

