// File: awbrenn[P]robot-controller/Client.cpp
// Function: main
int main (int argc, char *argv[])
{

   int len = atoi(argv[3]);
   int num = atoi(argv[4]);
   port = atoi(argv[2]);
   ip = argv[1];
   double moveSpeed = 0.2;
   double turnSpeed = 0.2;
   double moveSleep, turnSleep;
   size_t found;
   
   unsigned int TIMEOUT_SECONDS = 5;
   struct sigaction timeoutAction;
  
   cout << "Port: " << port << " IP: " << ip << endl;
   
   //Open file for output
   FILE *output;
   output = fopen("output.txt", "w");
   
   //Set timeout
   timeoutAction.sa_handler = catchAlarm;
   if (sigfillset(&timeoutAction.sa_mask) < 0)
      cout << "Sigfillset() failed" << endl;
   timeoutAction.sa_flags = 0;
   if (sigaction(SIGALRM, &timeoutAction, 0) < 0)
      cout << "Sigaction() failed" << endl;
   
   //Call function to populate the vector
   createVector(len, num);
     
   //Call function to start connection
   initalizeMiddleware();   
   
   //Call function to add header to vector components
//   shape1 = addHeaderToCommands(shape1);
//   shape2 = addHeaderToCommands(shape2);
   
   printf("Len: %d, Num: %d, AngleFirst: %f, AngleSecond: %f\n", len, 
      num, angleFirst, angleSecond);
      
   //Print content of vector
   vector<string>::iterator it;
   // for (it = v.begin(); it != v.end(); it++)
   //    cout << *it << endl;

   int i = 0;
   //Run loop until all of shape1 has been sent 
   for (it = shape1.begin(); it != shape1.end(); it++)
   {
      memset (recvBuffer, 0, sizeof(recvBuffer));
      clntAddrLen = sizeof(clntAddr);
       
      //Send command
      if (sendto(sock, shape1.at(i).c_str(), shape1.at(i).length(), 0, 
         (struct sockaddr *) &servAddr, sizeof(servAddr)) != shape1.at(i).length())
	      cout << "Error on sendto" << endl; 

      alarm(TIMEOUT_SECONDS);

      //Recv ack
      recieveAckFromMiddleware();

      cout << PROXY_RESPONSE <<  endl << endl;


      //Shape1 sleep for move
      found = shape1.at(i).find("MOVE");
      if (found != std::string::npos)
      { 
         cout << "Sleeping for shape1 move" << endl;
         moveSleep = len / moveSpeed * 1000000;
         usleep(moveSleep);
      }
      
      //Shape1 sleep for turn
      found = shape1.at(i).find("TURN");
      if (found != std::string::npos)
      {
         cout << "Sleeping for shape1 turn" << endl;
         turnSleep = angleFirst / turnSpeed * 1000000;
         usleep(turnSleep);
      }
      
      i++;
   }

   // test

  // string get_lasers("GET LASERS");

  // if (sendto(sock, get_lasers.c_str(), get_lasers.length(), 0, 
  //       (struct sockaddr *) &servAddr, sizeof(servAddr)) != get_lasers.length())
  //       cout << "Error on sendto" << endl;

  // //Recv ack
  // recieveAckFromMiddleware();

  // cout << PROXY_RESPONSE << endl;
   
   i = 0;
   //Run loop until all of shape2 has been sent 
   for (it = shape2.begin(); it != shape2.end(); it++)
   {
      memset (recvBuffer, 0, sizeof(recvBuffer));
      clntAddrLen = sizeof(clntAddr);
       
      //Send command
      if (sendto(sock, shape2.at(i).c_str(), shape2.at(i).length(), 0, 
         (struct sockaddr *) &servAddr, sizeof(servAddr)) != shape2.at(i).length())
	      cout << "Error on sendto" << endl; 
         
      alarm(TIMEOUT_SECONDS);

      //Recv ack
      recieveAckFromMiddleware();
      
      //Shape2 sleep for move
      found = shape2.at(i).find("MOVE");
      if (found != std::string::npos)
      { 
         cout << "Sleeping for shape2 move" << endl;
         moveSleep = len / moveSpeed * 1000000;
         usleep(moveSleep);
      }
      //Shape2 sleep for turn
      found = shape2.at(i).find("TURN");
      if (found != std::string::npos)
      {
         cout << "Sleeping for shape2 turn" << endl;
         turnSleep = angleSecond / turnSpeed * 1000000;
         usleep(turnSleep);
      }
      
      i++;
   }
   
   fclose(output);
   return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x160, %rsp            # imm = 0x160
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x23a0
movl %eax, -0x14(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x23a0
movl %eax, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x23a0
movl %eax, 0x6ba0(%rip)      # 0x92dc
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x6b95(%rip)      # 0x92e0
movsd 0x38b5(%rip), %xmm0     # 0x6008
movsd %xmm0, -0x20(%rbp)
movsd 0x38a8(%rip), %xmm0     # 0x6008
movsd %xmm0, -0x28(%rbp)
movl $0x5, -0x44(%rbp)
movq 0x685d(%rip), %rdi      # 0x8fd0
leaq 0x38ac(%rip), %rsi      # 0x6026
callq 0x21f0
movq %rax, %rdi
movl 0x6b54(%rip), %esi      # 0x92dc
callq 0x23c0
movq %rax, %rdi
leaq 0x3896(%rip), %rsi      # 0x602d
callq 0x21f0
movq %rax, %rdi
movq 0x6b3a(%rip), %rsi      # 0x92e0
callq 0x21f0
movq %rax, %rdi
movq 0x6803(%rip), %rsi      # 0x8fb8
callq 0x2230
leaq 0x3872(%rip), %rdi      # 0x6033
leaq 0x3876(%rip), %rsi      # 0x603e
callq 0x2290
movq %rax, -0xe8(%rbp)
leaq 0x5f5(%rip), %rax       # 0x2dd0
movq %rax, -0xe0(%rbp)
leaq -0xe0(%rbp), %rdi
addq $0x8, %rdi
callq 0x20c0
cmpl $0x0, %eax
jge 0x2819
movq 0x67d2(%rip), %rdi      # 0x8fd0
leaq 0x383b(%rip), %rsi      # 0x6040
callq 0x21f0
movq %rax, %rdi
movq 0x67a4(%rip), %rsi      # 0x8fb8
callq 0x2230
movl $0x0, -0x58(%rbp)
movl $0xe, %edi
leaq -0xe0(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x22d0
cmpl $0x0, %eax
jge 0x285c
movq 0x678f(%rip), %rdi      # 0x8fd0
leaq 0x380c(%rip), %rsi      # 0x6054
callq 0x21f0
movq %rax, %rdi
movq 0x6761(%rip), %rsi      # 0x8fb8
callq 0x2230
movl -0x14(%rbp), %edi
movl -0x18(%rbp), %esi
callq 0x2de0
callq 0x3740
movl -0x14(%rbp), %esi
movl -0x18(%rbp), %edx
movss 0xfacd6(%rip), %xmm0    # 0xfd550
cvtss2sd %xmm0, %xmm0
movss 0xfacce(%rip), %xmm1    # 0xfd554
cvtss2sd %xmm1, %xmm1
leaq 0x37d6(%rip), %rdi      # 0x6067
movb $0x2, %al
callq 0x2050
leaq -0xf0(%rbp), %rdi
callq 0x4020
movl $0x0, -0xf4(%rbp)
leaq 0x69c3(%rip), %rdi      # 0x9278
callq 0x4040
movq %rax, -0x100(%rbp)
movq -0x100(%rbp), %rax
movq %rax, -0xf0(%rbp)
leaq 0x69a2(%rip), %rdi      # 0x9278
callq 0x40b0
movq %rax, -0x108(%rbp)
leaq -0xf0(%rbp), %rdi
leaq -0x108(%rbp), %rsi
callq 0x4070
testb $0x1, %al
jne 0x28fe
jmp 0x2b47
leaq 0x69eb(%rip), %rdi      # 0x92f0
xorl %esi, %esi
movl $0xf4240, %edx          # imm = 0xF4240
callq 0x2130
movl $0x10, 0x69bd(%rip)     # 0x92d8
movl 0x6987(%rip), %eax      # 0x92a8
movl %eax, -0x13c(%rbp)
movslq -0xf4(%rbp), %rsi
leaq 0x6943(%rip), %rdi      # 0x9278
callq 0x40e0
movq %rax, %rdi
callq 0x20f0
movq %rax, -0x138(%rbp)
movslq -0xf4(%rbp), %rsi
leaq 0x6921(%rip), %rdi      # 0x9278
callq 0x40e0
movq %rax, %rdi
callq 0x2370
movl -0x13c(%rbp), %edi
movq -0x138(%rbp), %rsi
movq %rax, %rdx
xorl %ecx, %ecx
leaq 0x692f(%rip), %r8       # 0x92ac
movl $0x10, %r9d
callq 0x2070
movq %rax, -0x130(%rbp)
movslq -0xf4(%rbp), %rsi
leaq 0x68db(%rip), %rdi      # 0x9278
callq 0x40e0
movq %rax, %rdi
callq 0x2370
movq %rax, %rcx
movq -0x130(%rbp), %rax
cmpq %rcx, %rax
je 0x29db
movq 0x6610(%rip), %rdi      # 0x8fd0
leaq 0x36d3(%rip), %rsi      # 0x609a
callq 0x21f0
movq %rax, %rdi
movq 0x65e2(%rip), %rsi      # 0x8fb8
callq 0x2230
movl -0x44(%rbp), %edi
callq 0x2400
callq 0x3800
movq 0x65e1(%rip), %rdi      # 0x8fd0
leaq 0x6862(%rip), %rsi      # 0x9258
callq 0x21e0
movq %rax, %rdi
movq 0x65b3(%rip), %rsi      # 0x8fb8
callq 0x2230
movq %rax, %rdi
movq 0x65a4(%rip), %rsi      # 0x8fb8
callq 0x2230
movslq -0xf4(%rbp), %rsi
leaq 0x6851(%rip), %rdi      # 0x9278
callq 0x40e0
movq %rax, %rdi
leaq 0x3674(%rip), %rsi      # 0x60aa
xorl %eax, %eax
movl %eax, %edx
callq 0x2430
movq %rax, -0x40(%rbp)
cmpq $-0x1, -0x40(%rbp)
je 0x2a98
movq 0x657f(%rip), %rdi      # 0x8fd0
leaq 0x3657(%rip), %rsi      # 0x60af
callq 0x21f0
movq %rax, %rdi
movq 0x6551(%rip), %rsi      # 0x8fb8
callq 0x2230
cvtsi2sdl -0x14(%rbp), %xmm0
movsd -0x20(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd 0x358e(%rip), %xmm1     # 0x6010
mulsd %xmm1, %xmm0
movsd %xmm0, -0x30(%rbp)
cvttsd2si -0x30(%rbp), %rax
movl %eax, %edi
callq 0x2340
movslq -0xf4(%rbp), %rsi
leaq 0x67d2(%rip), %rdi      # 0x9278
callq 0x40e0
movq %rax, %rdi
leaq 0x3613(%rip), %rsi      # 0x60c8
xorl %eax, %eax
movl %eax, %edx
callq 0x2430
movq %rax, -0x40(%rbp)
cmpq $-0x1, -0x40(%rbp)
je 0x2b1e
movq 0x6500(%rip), %rdi      # 0x8fd0
leaq 0x35f6(%rip), %rsi      # 0x60cd
callq 0x21f0
movq %rax, %rdi
movq 0x64d2(%rip), %rsi      # 0x8fb8
callq 0x2230
movss 0xfaa5d(%rip), %xmm0    # 0xfd550
cvtss2sd %xmm0, %xmm0
movsd -0x28(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd 0x3508(%rip), %xmm1     # 0x6010
mulsd %xmm1, %xmm0
movsd %xmm0, -0x38(%rbp)
cvttsd2si -0x38(%rbp), %rax
movl %eax, %edi
callq 0x2340
movl -0xf4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xf4(%rbp)
leaq -0xf0(%rbp), %rdi
xorl %esi, %esi
callq 0x4120
movq %rax, -0x110(%rbp)
jmp 0x28cf
movl $0x0, -0xf4(%rbp)
leaq 0x6738(%rip), %rdi      # 0x9290
callq 0x4040
movq %rax, -0x118(%rbp)
movq -0x118(%rbp), %rax
movq %rax, -0xf0(%rbp)
leaq 0x6717(%rip), %rdi      # 0x9290
callq 0x40b0
movq %rax, -0x120(%rbp)
leaq -0xf0(%rbp), %rdi
leaq -0x120(%rbp), %rsi
callq 0x4070
testb $0x1, %al
jne 0x2ba1
jmp 0x2db9
leaq 0x6748(%rip), %rdi      # 0x92f0
xorl %esi, %esi
movl $0xf4240, %edx          # imm = 0xF4240
callq 0x2130
movl $0x10, 0x671a(%rip)     # 0x92d8
movl 0x66e4(%rip), %eax      # 0x92a8
movl %eax, -0x154(%rbp)
movslq -0xf4(%rbp), %rsi
leaq 0x66b8(%rip), %rdi      # 0x9290
callq 0x40e0
movq %rax, %rdi
callq 0x20f0
movq %rax, -0x150(%rbp)
movslq -0xf4(%rbp), %rsi
leaq 0x6696(%rip), %rdi      # 0x9290
callq 0x40e0
movq %rax, %rdi
callq 0x2370
movl -0x154(%rbp), %edi
movq -0x150(%rbp), %rsi
movq %rax, %rdx
xorl %ecx, %ecx
leaq 0x668c(%rip), %r8       # 0x92ac
movl $0x10, %r9d
callq 0x2070
movq %rax, -0x148(%rbp)
movslq -0xf4(%rbp), %rsi
leaq 0x6650(%rip), %rdi      # 0x9290
callq 0x40e0
movq %rax, %rdi
callq 0x2370
movq %rax, %rcx
movq -0x148(%rbp), %rax
cmpq %rcx, %rax
je 0x2c7e
movq 0x636d(%rip), %rdi      # 0x8fd0
leaq 0x3430(%rip), %rsi      # 0x609a
callq 0x21f0
movq %rax, %rdi
movq 0x633f(%rip), %rsi      # 0x8fb8
callq 0x2230
movl -0x44(%rbp), %edi
callq 0x2400
callq 0x3800
movslq -0xf4(%rbp), %rsi
leaq 0x65f7(%rip), %rdi      # 0x9290
callq 0x40e0
movq %rax, %rdi
leaq 0x3402(%rip), %rsi      # 0x60aa
xorl %eax, %eax
movl %eax, %edx
callq 0x2430
movq %rax, -0x40(%rbp)
cmpq $-0x1, -0x40(%rbp)
je 0x2d0a
movq 0x630d(%rip), %rdi      # 0x8fd0
leaq 0x341c(%rip), %rsi      # 0x60e6
callq 0x21f0
movq %rax, %rdi
movq 0x62df(%rip), %rsi      # 0x8fb8
callq 0x2230
cvtsi2sdl -0x14(%rbp), %xmm0
movsd -0x20(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd 0x331c(%rip), %xmm1     # 0x6010
mulsd %xmm1, %xmm0
movsd %xmm0, -0x30(%rbp)
cvttsd2si -0x30(%rbp), %rax
movl %eax, %edi
callq 0x2340
movslq -0xf4(%rbp), %rsi
leaq 0x6578(%rip), %rdi      # 0x9290
callq 0x40e0
movq %rax, %rdi
leaq 0x33a1(%rip), %rsi      # 0x60c8
xorl %eax, %eax
movl %eax, %edx
callq 0x2430
movq %rax, -0x40(%rbp)
cmpq $-0x1, -0x40(%rbp)
je 0x2d90
movq 0x628e(%rip), %rdi      # 0x8fd0
leaq 0x33b6(%rip), %rsi      # 0x60ff
callq 0x21f0
movq %rax, %rdi
movq 0x6260(%rip), %rsi      # 0x8fb8
callq 0x2230
movss 0xfa7ef(%rip), %xmm0    # 0xfd554
cvtss2sd %xmm0, %xmm0
movsd -0x28(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd 0x3296(%rip), %xmm1     # 0x6010
mulsd %xmm1, %xmm0
movsd %xmm0, -0x38(%rbp)
cvttsd2si -0x38(%rbp), %rax
movl %eax, %edi
callq 0x2340
movl -0xf4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xf4(%rbp)
leaq -0xf0(%rbp), %rdi
xorl %esi, %esi
callq 0x4120
movq %rax, -0x128(%rbp)
jmp 0x2b72
movq -0xe8(%rbp), %rdi
callq 0x2200
xorl %eax, %eax
addq $0x160, %rsp            # imm = 0x160
popq %rbp
retq