示例#1
0
// Leonardo Live Tracker (www.livetrack24.com) data exchange thread
static void LiveTrackerThread()
{
  int tracker_fsm = 0;
  livetracker_point_t sendpoint = {0};
  bool sendpoint_valid = false;
  bool sendpoint_processed = false;
  bool sendpoint_processed_old = false;
  // Session variables
  unsigned int packet_id = 0;
  unsigned int session_id = 0;               
  int userid = -1;
  
  _t_end = false;
  _t_run = true;

  srand(MonotonicClockMS());

  do {
    if (NewDataEvent.tryWait(5000)) NewDataEvent.reset();
    if (!_t_run) break;
    do {
      if (1) {
        sendpoint_valid = false;
        ScopeLock guard(_t_mutex);
        if (!_t_points.empty()) {
          sendpoint = _t_points.front();
          sendpoint_valid = true;
        }
      } //mutex
        if (sendpoint_valid) {
          sendpoint_processed = false;
          do {
            switch (tracker_fsm) {
              default:
              case 0:   // Wait for flying
                if (!sendpoint.flying) {
                  sendpoint_processed = true;
                  break;
                }
                tracker_fsm++;
                break;
                  
              case 1:
                // Get User ID
                userid = GetUserIDFromServer();
                sendpoint_processed = false;
                if (userid>=0) tracker_fsm++;
                break;
                
              case 2:
                //Start of track packet
                sendpoint_processed = SendStartOfTrackPacket(&packet_id, &session_id, userid);
                if (sendpoint_processed) {
                  StartupStore(TEXT(". Livetracker new track started.%s"),NEWLINE);
                  sendpoint_processed_old = true;
                  tracker_fsm++;
                }
                break;

              case 3:
                //Gps point packet
                sendpoint_processed = SendGPSPointPacket(&packet_id, &session_id, &sendpoint);
                
                //Connection lost to server
                if (sendpoint_processed_old && !sendpoint_processed) {
                  StartupStore(TEXT(". Livetracker connection to server lost.%s"), NEWLINE);
                }
                //Connection established to server
                if (!sendpoint_processed_old && sendpoint_processed) {
                  ScopeLock guard(_t_mutex);
                  int queue_size = _t_points.size();
                  StartupStore(TEXT(". Livetracker connection to server established, start sending %d queued packets.%s"), queue_size, NEWLINE);
                }
                sendpoint_processed_old = sendpoint_processed;
                
                if (!sendpoint.flying) {
                  tracker_fsm++;
                }
                break;

              case 4:
                //End of track packet
                sendpoint_processed = SendEndOfTrackPacket(&packet_id, &session_id);
                if (sendpoint_processed) {
                  StartupStore(TEXT(". Livetracker track finished, sent %d points.%s"), packet_id, NEWLINE);
                  tracker_fsm=0;
                }
                break;
            }// sw
            
            if (sendpoint_processed) {
              ScopeLock guard(_t_mutex);
              _t_points.pop_front();
            } else InterruptibleSleep(2500);
            sendpoint_processed_old = sendpoint_processed;
          } while (!sendpoint_processed && _t_run);
      }
    } while (sendpoint_valid && _t_run);
  } while (_t_run);
  
  _t_end = true;
}
示例#2
0
// Leonardo Live Tracker (www.livetrack24.com) data exchange thread
static DWORD WINAPI LiveTrackerThread (LPVOID lpvoid)
{
    int tracker_fsm = 0;
    livetracker_point_t sendpoint = {0};
    bool sendpoint_valid = false;
    bool sendpoint_processed = false;
    bool sendpoint_processed_old = false;
    // Session variables
    unsigned int packet_id = 0;
    unsigned int session_id = 0;
    int userid = -1;

    _t_end = false;
    _t_run = true;

    srand(GetTickCount());

    do {
        if (WaitForSingleObject(_hNewDataEvent, 5000) == WAIT_OBJECT_0) ResetEvent(_hNewDataEvent);
        if (!_t_run) break;
        do {
            if (1) {
                sendpoint_valid = false;
                CCriticalSection::CGuard guard(_t_mutex);
                if (!_t_points.empty()) {
                    sendpoint = _t_points.front();
                    sendpoint_valid = true;
                }
            } //mutex
            if (sendpoint_valid) {
                sendpoint_processed = false;
                do {
                    switch (tracker_fsm) {
                    default:
                    case 0:   // Wait for flying
                        if (!sendpoint.flying) {
                            sendpoint_processed = true;
                            break;
                        }
                        tracker_fsm++;
                        break;

                    case 1:
                        // Get User ID
                        userid = GetUserIDFromServer();
                        sendpoint_processed = false;
                        if (userid>=0) tracker_fsm++;
                        break;

                    case 2:
                        //Start of track packet
                        sendpoint_processed = SendStartOfTrackPacket(&packet_id, &session_id, userid);
                        if (sendpoint_processed) {
                            StartupStore(TEXT(". Livetracker new track started.%s"),NEWLINE);
                            sendpoint_processed_old = true;
                            tracker_fsm++;
                        }
                        break;

                    case 3:
                        //Gps point packet
                        sendpoint_processed = SendGPSPointPacket(&packet_id, &session_id, &sendpoint);

                        //Connection lost to server
                        if (sendpoint_processed_old && !sendpoint_processed) {
                            StartupStore(TEXT(". Livetracker connection to server lost.%s"), NEWLINE);
                        }
                        //Connection established to server
                        if (!sendpoint_processed_old && sendpoint_processed) {
                            CCriticalSection::CGuard guard(_t_mutex);
                            int queue_size = _t_points.size();
                            StartupStore(TEXT(". Livetracker connection to server established, start sending %d queued packets.%s"), queue_size, NEWLINE);
                        }
                        sendpoint_processed_old = sendpoint_processed;

                        if (!sendpoint.flying) {
                            tracker_fsm++;
                        }
                        break;

                    case 4:
                        //End of track packet
                        sendpoint_processed = SendEndOfTrackPacket(&packet_id, &session_id);
                        if (sendpoint_processed) {
                            StartupStore(TEXT(". Livetracker track finished, sent %d points.%s"), packet_id, NEWLINE);
                            tracker_fsm=0;
                        }
                        break;
                    }// sw

                    if (sendpoint_processed) {
                        CCriticalSection::CGuard guard(_t_mutex);
                        _t_points.pop_front();
                    } else InterruptibleSleep(2500);
                    sendpoint_processed_old = sendpoint_processed;
                } while (!sendpoint_processed && _t_run);
            }
        } while (sendpoint_valid && _t_run);
    } while (_t_run);

    _t_end = true;
    return 0;
}