#include <stdint.h>
#include <signal.h>
#include <sys/time.h>
#include <string.h>
#include <pthread.h>
#include <stdlib.h>
#include <iostream>
#include "elmo.h"
#include "control_thread.h"
#include "user_interface_thread.h"
#include "logging_thread.h"
#include "motor_interogator.h"

using namespace std;

Elmo	winch(true);
Elmo	levelWind(false);

uint32_t SIGPIPE_count = 0;

int main(int argc, char** argv)
{
	// Turn off SIGPIPE signals to prevent the profiler application
	// from terminating if the connection to the ship through the Freewave
	// modems is broken.

	sigignore(SIGPIPE);

	if (argc != 6) {
		cout << "usage: profiler <Telnet Port #> <Labview Port #> <IP address of Motion Control Moxa>"
		cout << "<IP Address of Sensor Moxa> <IP Address of Labjack>" << endl;
		exit(1);
	}

	uint16_t				telnetPortNumber = atoi(argv[1]);
	uint16_t				labviewPortNumber = atoi(argv[2]);

	string					MotionControlMoxa_IP_Address(argv[3]);
	string					SensorMoxa_IP_Address
	sigset_t				signalsToBlock;
	ControlThread			espProfiler(winch, levelWind);
	UserInterfaceThread 	userInterface(telnetPortNumber, winch, levelWind, espProfiler);
	LoggingThread			logger(labviewPortNumber, espProfiler, winch, levelWind);
	PressureSensorThread	pressureSensor()

	// Connect to the Elmo Motion Controllers
	winch.Connect(MotionControlMoxa_IP_Address, 4001);
	levelWind.Connect(MotionControlMoxa_IP_Address, 4002);

	// Connect to Pressure Sensor
	pressureSensor.Connect(SensorMoxa_IP_Address, 4001);

	sigemptyset(&signalsToBlock);
	sigaddset(&signalsToBlock,SIGALRM);
	pthread_sigmask(SIG_BLOCK,&signalsToBlock, NULL);

	// Start the threads, logging, control and user interface
	logger.Start(NULL);
	espProfiler.Start((void *) argv[3]);
	userInterface.Start(NULL);

	// Wait until the user has had enough
	userInterface.Wait();
	cout << "That's All Folks" << endl;
}

