#include <cstdlib>
#include <iostream>
#include <string>
#include <conio.h>
#include "nxt.h"
using namespace std;

//set up the NXT
Connection *connection = new Nxt_network();
Sensor *sensor1 = new Touch(IN_1, connection);
int main()
{
  Server_settings settings;
  try{
    cout << "Try to connect to the NXT" << endl;
    connection->connect(1000, "127.0.0.1", settings);//connect to localhost
    cout << "Connected" << endl;
    cout << "Client timeout" << settings.timeout <<endl;
    if(settings.mode == CLOSE_DOWN){
      cout << "Server will close down if connection is lost" << endl;
    }
    else{
      cout << "Server will reconnect if connection is lost" << endl;
    }
    cout << "Read the touch sensor - hit any key to end" << endl;
    while(!_kbhit()){//hit a key to end
      cout << sensor1->print() << endl;
    }
    connection->disconnect();
  }
  catch (Nxt_exception& e){
    //some error occurred - print it out
    cout << e.what() << endl;
    cout << "error code: " << e.error_code() << endl;
    cout << "error type: " << e.error_type() << endl;
    cout << e.who() << endl;
    connection->disconnect();
  }
  return 0;
}