#include "UDPServer/UDPServer.h"
#include "ReceiverThreads/ReceiverThreads.h"

#include <iostream>
#include <functional>
#include <string>
#include <thread>

void start(std::function<void(void)> func){
    std::thread([func]() {
        while (true)
        {
            func();
        }
    }).detach();
}

int main (int argc, char *argv[]){

   if(argc < 4){
      std::cerr << "Program usage: ./onlineDetectorSimulatorServer <address> <first_port> <num_ports>";
      return 0;
   }

   std::string address = argv[1];
  int firstPort = std::stoi(argv[2]);
  int numPorts = std::stoi(argv[3]);

//   int port = 4002;
//
//   UDPServer server = UDPServer(address, port);

   std::size_t length{32768};
   std::size_t lastIndex{0};

   std::vector<unsigned short> buf(16000);

   printf("Receving udp packets on ports %u - %u\n", firstPort, firstPort + numPorts);
   ReceiverThreads(address, 10, numPorts, firstPort);

//   for(auto i = 0; i < 27; i++){
//      std::function<void(void)> f = [=]() {
//         server.recv();
//      };
//      start();
//   }

//   while(true){
//     int bytes = server.recv((char*)buf.data(), length);
//     std::size_t index = *((std::size_t *)buf.data());
//     if(index%1000 == 99) printf("%lu\n", index);
//
//     lastIndex = index;
//
//   }

   return 0;

}