// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %rsi, %r15
movq 0x18(%rsi), %rdi
callq 0x2290
movl %eax, %ebx
movq 0x20(%r15), %rdi
callq 0x2290
movl %eax, %r14d
movq 0x10(%r15), %rdi
callq 0x2290
movl %eax, 0x3d03(%rip)      # 0x624c
movq 0x8(%r15), %rax
movq %rax, 0x3cfc(%rip)      # 0x6250
movq 0x3a75(%rip), %rdi      # 0x5fd0
leaq 0x1ad4(%rip), %rsi      # 0x4036
callq 0x2190
movl 0x3cdf(%rip), %esi      # 0x624c
movq %rax, %rdi
callq 0x22b0
leaq 0x1ac1(%rip), %rsi      # 0x403d
movq %rax, %rdi
callq 0x2190
movq 0x3cc5(%rip), %rsi      # 0x6250
movq %rax, %rdi
callq 0x2190
movq %rax, %rdi
callq 0x20d0
leaq 0x1aa1(%rip), %rdi      # 0x4043
leaq 0x1aa5(%rip), %rsi      # 0x404e
callq 0x21d0
movq %rax, -0x38(%rbp)
leaq 0x3b9(%rip), %rax       # 0x2972
leaq -0xd0(%rbp), %rdi
movq %rax, -0x8(%rdi)
callq 0x2090
testl %eax, %eax
jns 0x25e8
movq 0x39fc(%rip), %rdi      # 0x5fd0
leaq 0x1a75(%rip), %rsi      # 0x4050
callq 0x2190
movq %rax, %rdi
callq 0x20d0
leaq -0xd8(%rbp), %rsi
andl $0x0, 0x88(%rsi)
pushq $0xe
popq %rdi
xorl %edx, %edx
callq 0x2200
testl %eax, %eax
jns 0x261f
movq 0x39c5(%rip), %rdi      # 0x5fd0
leaq 0x1a52(%rip), %rsi      # 0x4064
callq 0x2190
movq %rax, %rdi
callq 0x20d0
movl %ebx, %edi
movl %r14d, %esi
callq 0x2978
callq 0x2d1b
cvtss2sd 0xf7e8a(%rip), %xmm0    # 0xfa4c0
cvtss2sd 0xf7e86(%rip), %xmm1    # 0xfa4c4
leaq 0x1a32(%rip), %rdi      # 0x4077
movl %ebx, %esi
movl %r14d, %edx
movb $0x2, %al
callq 0x2040
cvtsi2sd %ebx, %xmm0
divsd 0x19ab(%rip), %xmm0     # 0x4008
mulsd 0x19ab(%rip), %xmm0     # 0x4010
cvttsd2si %xmm0, %rax
movq %rax, -0x30(%rbp)
movq 0x3b73(%rip), %rax      # 0x61e8
xorl %r15d, %r15d
leaq 0x3b69(%rip), %r14      # 0x61e8
movq 0x394a(%rip), %r13      # 0x5fd0
cmpq 0x3b63(%rip), %rax      # 0x61f0
je 0x27f2
movq %rax, -0x40(%rbp)
movl $0xf4240, %edx          # imm = 0xF4240
leaq 0x3bbd(%rip), %rdi      # 0x6260
xorl %esi, %esi
callq 0x2100
movl $0x10, 0x3b94(%rip)     # 0x6248
movl 0x3b5e(%rip), %ebx      # 0x6218
movq %r14, %rdi
movq %r15, %rsi
callq 0x32b2
movq (%rax), %r12
movq %r14, %rdi
movq %r15, %rsi
callq 0x32b2
movq 0x8(%rax), %rdx
movl %ebx, %edi
movq %r12, %rsi
xorl %ecx, %ecx
leaq 0x3b37(%rip), %r8       # 0x621c
pushq $0x10
popq %r9
callq 0x2050
movq %rax, %rbx
movq %r14, %rdi
movq %r15, %rsi
callq 0x32b2
cmpq 0x8(%rax), %rbx
je 0x2719
movq %r13, %rdi
leaq 0x199e(%rip), %rsi      # 0x40aa
callq 0x2190
movq %rax, %rdi
callq 0x20d0
pushq $0x5
popq %rdi
callq 0x22e0
callq 0x2d7d
movq %r13, %rdi
leaq 0x3a98(%rip), %rsi      # 0x61c8
callq 0x2180
movq %rax, %rdi
callq 0x20d0
movq %rax, %rdi
callq 0x20d0
movq %r14, %rdi
movq %r15, %rsi
callq 0x32b2
movq %rax, %rdi
leaq 0x1960(%rip), %rsi      # 0x40ba
xorl %edx, %edx
callq 0x2310
cmpq $-0x1, %rax
je 0x2787
movq %r13, %rdi
leaq 0x194e(%rip), %rsi      # 0x40bf
callq 0x2190
movq %rax, %rdi
callq 0x20d0
movq -0x30(%rbp), %rdi
callq 0x2240
movq %r14, %rdi
movq %r15, %rsi
callq 0x32b2
movq %rax, %rdi
leaq 0x193c(%rip), %rsi      # 0x40d8
xorl %edx, %edx
callq 0x2310
cmpq $-0x1, %rax
je 0x27e2
movq %r13, %rdi
leaq 0x192a(%rip), %rsi      # 0x40dd
callq 0x2190
movq %rax, %rdi
callq 0x20d0
cvtss2sd 0xf7cf8(%rip), %xmm0    # 0xfa4c0
divsd 0x1838(%rip), %xmm0     # 0x4008
mulsd 0x1838(%rip), %xmm0     # 0x4010
cvttsd2si %xmm0, %rdi
callq 0x2240
incq %r15
movq -0x40(%rbp), %rax
addq $0x20, %rax
jmp 0x2686
movq 0x3a07(%rip), %r15      # 0x6200
xorl %ebx, %ebx
leaq 0x39fe(%rip), %r12      # 0x6200
cmpq 0x39ff(%rip), %r15      # 0x6208
je 0x2955
movl $0xf4240, %edx          # imm = 0xF4240
leaq 0x3a45(%rip), %rdi      # 0x6260
xorl %esi, %esi
callq 0x2100
movl $0x10, 0x3a1c(%rip)     # 0x6248
movl 0x39e5(%rip), %r14d     # 0x6218
movq %r12, %rdi
movq %rbx, %rsi
callq 0x32b2
movq (%rax), %r13
movq %r12, %rdi
movq %rbx, %rsi
callq 0x32b2
movq 0x8(%rax), %rdx
movl %r14d, %edi
movq %r13, %rsi
xorl %ecx, %ecx
leaq 0x39bd(%rip), %r8       # 0x621c
pushq $0x10
popq %r9
callq 0x2050
movq %rax, %r14
movq %r12, %rdi
movq %rbx, %rsi
callq 0x32b2
cmpq 0x8(%rax), %r14
je 0x2897
movq 0x374d(%rip), %rdi      # 0x5fd0
leaq 0x1820(%rip), %rsi      # 0x40aa
callq 0x2190
movq %rax, %rdi
callq 0x20d0
pushq $0x5
popq %rdi
callq 0x22e0
callq 0x2d7d
movq %r12, %rdi
movq %rbx, %rsi
callq 0x32b2
movq %rax, %rdi
leaq 0x1801(%rip), %rsi      # 0x40ba
xorl %edx, %edx
callq 0x2310
cmpq $-0x1, %rax
je 0x28ea
movq 0x3703(%rip), %rdi      # 0x5fd0
leaq 0x1822(%rip), %rsi      # 0x40f6
callq 0x2190
movq %rax, %rdi
callq 0x20d0
movq -0x30(%rbp), %rdi
callq 0x2240
movq %r12, %rdi
movq %rbx, %rsi
callq 0x32b2
movq %rax, %rdi
leaq 0x17d9(%rip), %rsi      # 0x40d8
xorl %edx, %edx
callq 0x2310
cmpq $-0x1, %rax
je 0x2949
movq 0x36bd(%rip), %rdi      # 0x5fd0
leaq 0x17f5(%rip), %rsi      # 0x410f
callq 0x2190
movq %rax, %rdi
callq 0x20d0
cvtss2sd 0xf7b95(%rip), %xmm0    # 0xfa4c4
divsd 0x16d1(%rip), %xmm0     # 0x4008
mulsd 0x16d1(%rip), %xmm0     # 0x4010
cvttsd2si %xmm0, %rdi
callq 0x2240
incq %rbx
addq $0x20, %r15
jmp 0x2802
movq -0x38(%rbp), %rdi
callq 0x21a0
xorl %eax, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq