Esempio n. 1
0
    // Menages the post queue.
    private IEnumerator ContactServer()
    {
        while (true)
        {
            if (postQueue.Count > 0)
            {
                Entry currentEntry = postQueue.Dequeue();
                // Debug.Log("I'm posting " + currentEntry.Label + "...");
                // Wait for the Data Manager to finish the current operation, if any.
                yield return(StartCoroutine(WaitForDataManager()));

                // Get the log count of the last log on the server.
                RemoteDataManager.Instance.GetLastEntry();
                yield return(StartCoroutine(WaitForDataManager()));

                JsonCompletionTracker jcp =
                    ExtractCompletionTracker(RemoteDataManager.Instance.Result);
                jcp.logsCount++;
                if (currentEntry.Comment == ConnectionSettings.KEEP_LOCAL_COMPLETION)
                {
                    jcp.studyCompletionTrackers = studyCompletionTrackers;
                }
                // Post the data.
                RemoteDataManager.Instance.SaveData(new Entry(currentEntry.Label, currentEntry.Data,
                                                              JsonUtility.ToJson(jcp)));
            }
            else
            {
                // Debug.Log("I'm doing nothing...");
                yield return(new WaitForSeconds(ConnectionSettings.SERVER_CONNECTION_PERIOD));
            }
        }
    }
Esempio n. 2
0
    // Extracts the completion tracker from a log.
    private JsonCompletionTracker ExtractCompletionTracker(string result)
    {
        string[] splittedResult = result.Split('|');

        if (splittedResult.Length == 6)
        {
            try {
                JsonCompletionTracker jct =
                    JsonUtility.FromJson <JsonCompletionTracker>(splittedResult[4]);
                if (jct != null && jct.studyCompletionTrackers.Count > 0)
                {
                    return(jct);
                }
            } catch {
            }
        }

        return(new JsonCompletionTracker(0, GetZeroTracker()));
    }
Esempio n. 3
0
    // Sets the completion (online).
    private IEnumerator SetCompletionOnline()
    {
        int connectionAttempts = 0;

        // Wait for the Connection Manager.
        while (connectionAttempts * ConnectionSettings.SERVER_CONNECTION_PERIOD <
               ConnectionSettings.SERVER_CONNECTION_TIMEOUT && postQueue.Count() > 0)
        {
            connectionAttempts++;
            yield return(new WaitForSeconds(ConnectionSettings.SERVER_CONNECTION_PERIOD));
        }

        // If the Connection Manager finished before the timeout I try to contact the server.
        if (connectionAttempts * ConnectionSettings.SERVER_CONNECTION_PERIOD <
            ConnectionSettings.SERVER_CONNECTION_TIMEOUT)
        {
            // Get the log count of the last log on the server.
            RemoteDataManager.Instance.GetLastEntry();
            yield return(StartCoroutine(WaitForDataManager(
                                            ConnectionSettings.SERVER_CONNECTION_TIMEOUT - connectionAttempts *
                                            ConnectionSettings.SERVER_CONNECTION_PERIOD)));

            if (RemoteDataManager.Instance.IsResultReady)
            {
                JsonCompletionTracker jcp =
                    ExtractCompletionTracker(RemoteDataManager.Instance.Result);
                studyCompletionTrackers = jcp.studyCompletionTrackers;
                postCompletion          = true;
            }
            else
            {
                studyCompletionTrackers = GetRandomTracker();
                postCompletion          = false;
            }
        }
        else
        {
            studyCompletionTrackers = GetRandomTracker();
            postCompletion          = false;
        }
    }