#include "MessageDim.h"

#include "tools.h"
#include "Time.h"
#include "dic.hxx"
#include <iostream>

using namespace std;
struct testType {
	long v1;
	long v2;
	long v3;
	long v4;
	long v5;
	long v6;
	long v7;
	long v8;
	long v9;
	long v10;
};

struct DataLoggerStats {
    long sizeWritten;
    long freeSpace;
    long writingRate;
};

class StatSub : public DimClient
{
    DimStampedInfo* info;
    DimStampedInfo* info2;

public:
    StatSub()
    {
        info = new DimStampedInfo("FAD_CONTROL/START_RUN", const_cast<char*>(""), this);
        info2 = new DimStampedInfo("TIME/EMPTY", const_cast<char*>(""), this);
    }
    void infoHandler()
    {
        return;
        DimInfo* I = getInfo();
        if (I == info2)
        {
            cout << "EMPTY SERVICE UPDATED. " << "Format: " << I->getFormat() << endl;
        }
        if (I != info)
            return;
        DataLoggerStats* data = reinterpret_cast<DataLoggerStats*>(I->getData());
        std::cout << "size written: " << data->sizeWritten << " free space: " << data->freeSpace << " writing rate" << data->writingRate << std::endl;
    }
};

DimService * myService;

int main(int, const char **)
{
   // We could use putenv to make the Configure class change the value...
    setenv("DIM_DNS_NODE", "localhost", 0);

    cout << "This is the new version of the service 21 July 2011" << endl;

    // Start a DimServer called TIME
    DimServer::start("FAD_CONTROL");

    // Some info on the console
    std::cout << "Offering TIME/MESSAGE...\n" << std::endl;
    long emptyFormatVariable[2];
    emptyFormatVariable[0] = 1;
    emptyFormatVariable[1] = 2;
    const char* format = "X:1;X:1";
    DimService* emptyFormatService = new DimService("FAD_CONTROL/START_RUN", "X:1;X:1", emptyFormatVariable, 2*sizeof(long));
    testType variable;
    variable.v1 = 0;
    variable.v2 = 1;
    variable.v3 = 2;
    variable.v4 = 3;
    variable.v5 = 4;
    variable.v6 = 5;
    variable.v7 = 6;
    variable.v8 = 7;
    variable.v9 = 8;
    variable.v10 = 9;
 //   DimDescribedService srvc("TIME/ETIENNE", "X:1;X:1;X:1;X:1;X:1;X:1;X:1;X:1;X:1;X:1", variable, "Test Service");

    StatSub stSub;
//    delete emptyFormatService;
 //   usleep(1000000);
//    emptyFormatService = new DimService("TIME/EMPTY", "X:1", &emptyFormatVariable, sizeof(long));

    cout << "Test service created" << endl;
    // Setup a DimService called TIME/MESSAGE
    MessageDimTX msg("TIME");
    while (1)
    {
//        if (variable.v1 %10 == 0)
//        {
//            delete emptyFormatService;
//            cout << "Service deleted" << endl;
//            usleep(1000000);
//            emptyFormatService = new DimService("TIME/EMPTY", "X:1", &emptyFormatVariable, sizeof(long));
//            cout << "Service re-created" << endl;
//        }

//        srvc.updateService();
        emptyFormatService->updateService();
        emptyFormatVariable[0]++;
        emptyFormatVariable[1]++;
		variable.v1++;
		variable.v2++;
		variable.v3++;
		variable.v4++;
		variable.v5++;
		variable.v6++;
		variable.v7++;
		variable.v8++;
		variable.v9++;
		variable.v10++;
        // wait approximately one second
        usleep(1000000);
    }

    return 0;
}

// **************************************************************************
/** @example logtime.cc

This is a simple example how to log messages through the Dim network
using MessageDimTX. Here we are offering the time once a second.

The program is stopped by CTRL-C

*/
// **************************************************************************
