/**
 * This sketch was written in order to developp and test the multi-client tcp server which will later be used in my project
 * Anatole SCHRAMM-HENRY 08/05/2019
 */
 
#include "TCPClient.h"
#include "TCPServer.h"
#include "WEBServer.h"
#include "WEBClient.h"
#include "FTPServer.h"
#include "FTPClient.h"

uint32_t lastFreeMem(0);
uint16_t lastClientCount(0);

//TCPServer<TCPClient> server(80, MAX_CLIENT, 5);
//WEBServer<WEBClient> webServer(8080);
FTPServer<FTPClient> ftpServer;//(21,"test","test");

WiFiEventHandler gotIpEventHandler, disconnectedEventHandler;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  
  gotIpEventHandler = WiFi.onStationModeGotIP(&(gotIp));
  disconnectedEventHandler = WiFi.onStationModeDisconnected(&(lostCon));
  
  WiFi.persistent(false);
  WiFi.begin("freebox_Henry","eustache1930");
}

void debugInfo()
{
  uint32_t freeMem;
  uint16_t biggestContigMemBlock;
  uint8_t frag;
  ESP.getHeapStats(&freeMem, &biggestContigMemBlock, &frag);
  if(lastFreeMem != freeMem)
  {
    Serial.print("Free MEM : ");Serial.println(freeMem);
    Serial.print("Heap Frag : ");Serial.println(frag);
    lastFreeMem = freeMem;
  }
  /*if(lastClientCount != server.getConnectedClientsCount())
  {
    lastClientCount = server.getConnectedClientsCount();
    Serial.print("Connected client(s) : ");Serial.println(lastClientCount);
  }*/
}

void loop() {
  // put your main code here, to run repeatedly:
  //server.runServer();
  //webServer.runServer();
  debugInfo();
  ftpServer.runServer();
}


void gotIp(const WiFiEventStationModeGotIP& event)
{
  Serial.print("Got IP : ");Serial.println(WiFi.localIP());
}

void lostCon(const WiFiEventStationModeDisconnected& event)
{
  Serial.println("Lost connection");
}
