Beispiel #1
0
        static private void GetEvents(Journal.OptionsRow or, ILJServer iLJ, ref SyncItemCollection sic,
                                      ref SyncItemCollection deletedsic, ref EventCollection ec)
        {
            // for an explanation of this algorithm, see
            // http://www.livejournal.com/community/lj_clients/143312.html
            // note that this is a very painful algorithm.  it will loop an extra time for each
            // deleted syncitem that getevents doesn't return an event for.  if LJ decides to revise
            // how they return syncitems, this algorithm can be made more efficient.
            int total = sic.Count;

            while (sic.Count > 0)
            {
                SyncItem             oldest     = sic.GetOldest();
                DateTime             oldestTime = DateTime.Parse(oldest.time, CultureInfo.InvariantCulture).AddSeconds(-1);
                GetChallengeResponse gcr        = iLJ.GetChallenge();
                string          auth_response   = MD5Hasher.Compute(gcr.challenge + or.HPassword);
                GetEventsParams gep             = new GetEventsParams(or.UserName, "challenge", gcr.challenge,
                                                                      auth_response, 1, 0, 0, 0, "syncitems", oldestTime.ToString(_datetimeformat), 0, 0, 0, 0,
                                                                      string.Empty, 0, "unix", (or.IsUseJournalNull() ? string.Empty : or.UseJournal));
                GetEventsResponse ger;
                socb(new SyncOperationEventArgs(SyncOperation.GetEvents, total - sic.Count, total));
                ger = iLJ.GetEvents(gep);
                // remove this item in case it isn't returned by getevents
                // this signifies that the item has been deleted
                // this also ensures we don't get stuck in an endless loop
                sic.Remove(oldest);
                sic.RemoveDownloaded(ger.events);
                deletedsic.RemoveDownloaded(ger.events);
                ec.AddRange(ger.events);
            }
        }
Beispiel #2
0
        static private void ThreadStart()
        {
            // The main threaded execution body for performing a sync.
            // This method is chopped up into smaller methods for clarity and structure.
            ILJServer iLJ;

            Journal.OptionsRow      or = null;
            SyncItemCollection      sic = null, deletedsic = null;
            EventCollection         ec = null;
            CommentCollection       ccbody = null, ccmeta = null;
            UserMapCollection       umc             = null;
            LoginResponse           lr              = new LoginResponse();
            string                  communityPicURL = null;
            DateTime                lastSync        = DateTime.MinValue;
            SessionGenerateResponse sgr;
            int serverMaxID, localMaxID;

            try
            {
                // STEP 1: Initialize
                socb(new SyncOperationEventArgs(SyncOperation.Initialize, 0, 0));
                syncException = null;
                or            = j.Options;
                iLJ           = LJServerFactory.Create(or.ServerURL);
                sic           = new SyncItemCollection();
                deletedsic    = new SyncItemCollection();
                ec            = new EventCollection();
                ccmeta        = new CommentCollection();
                ccbody        = new CommentCollection();
                umc           = new UserMapCollection();

                // STEP 2: Login
                socb(new SyncOperationEventArgs(SyncOperation.Login, 0, 0));
                lr = new LoginResponse();
                Login(or, iLJ, ref lr, ref communityPicURL);

                // STEP 3: SyncItems
                socb(new SyncOperationEventArgs(SyncOperation.SyncItems, 0, 0));
                lastSync = DateTime.MinValue;
                SyncItems(or, iLJ, ref sic, ref deletedsic, ref lastSync);

                // STEP 4: GetEvents
                socb(new SyncOperationEventArgs(SyncOperation.GetEvents, 0, 0));
                GetEvents(or, iLJ, ref sic, ref deletedsic, ref ec);

                if (or.GetComments)
                {
                    // STEP 5: SessionGenerate
                    socb(new SyncOperationEventArgs(SyncOperation.SessionGenerate, 0, 0));
                    sgr = new SessionGenerateResponse();
                    SessionGenerate(or, iLJ, ref sgr);

                    // STEP 6: ExportCommentsMeta
                    socb(new SyncOperationEventArgs(SyncOperation.ExportCommentsMeta, 0, 0));
                    localMaxID = serverMaxID = j.GetMaxCommentID();
                    ExportCommentsMeta(or, iLJ, sgr, ref serverMaxID, localMaxID, umc, ccmeta);

                    // STEP 7: ExportCommentsBody
                    socb(new SyncOperationEventArgs(SyncOperation.ExportCommentsBody, 0, 0));
                    ExportCommentsBody(or, iLJ, sgr, serverMaxID, localMaxID, ccbody);
                }
            }
            catch (Exception ex)
            {
                ParseException(ex, ref syncException);
                if (ex.GetType() == typeof(ThreadAbortException))
                {
                    socb(new SyncOperationEventArgs(SyncOperation.Failure, 0, 0));                     // do this before thread terminates
                    return;
                }
            }

            // STEP 8: Merge
            try
            {
                if (syncException == null)
                {
                    socb(new SyncOperationEventArgs(SyncOperation.Merge, 0, 0));
                    Merge(j, ec, ccmeta, ccbody, umc, deletedsic, lr, communityPicURL, lastSync);
                    socb(new SyncOperationEventArgs(SyncOperation.Success, ec.Count, ccbody.Count));
                }
                else if (syncException.GetType() == typeof(ExpectedSyncException) &&
                         (((ExpectedSyncException)syncException).ExpectedError == ExpectedError.ServerNotResponding ||
                          ((ExpectedSyncException)syncException).ExpectedError ==
                          ExpectedError.ExportCommentsNotSupported ||
                          ((ExpectedSyncException)syncException).ExpectedError ==
                          ExpectedError.CommunityAccessDenied) &&
                         lr.moods != null)
                {
                    socb(new SyncOperationEventArgs(SyncOperation.Merge, 0, 0));
                    if (sic.Count > 0)
                    {
                        lastSync = DateTime.Parse(sic.GetOldest().time, CultureInfo.InvariantCulture).AddSeconds(-1);
                    }
                    Merge(j, ec, ccmeta, ccbody, umc, deletedsic, lr, communityPicURL, lastSync);
                    socb(new SyncOperationEventArgs(SyncOperation.PartialSync, ec.Count, ccbody.Count));
                }
                else
                {
                    socb(new SyncOperationEventArgs(SyncOperation.Failure, 0, 0));
                }
            }
            catch (Exception ex)
            {
                syncException = ex;
                socb(new SyncOperationEventArgs(SyncOperation.Failure, 0, 0));
            }
        }