public IMobeelizerAuthenticateResponse Authenticate(string user, string password, String notificationChanelUri) { WebRequest request = WebRequest.Create(GetUrl("/authenticate?cache=" + Guid.NewGuid().ToString())); request.Method = "GET"; SetHeaders(request, false, false); request.Headers["mas-user-name"] = user; request.Headers["mas-user-password"] = password; try { MobeelizerResponse result = new Synchronizer().GetResponse(request); if (result.StatusCode == HttpStatusCode.OK) { JObject jObject = (result as MobeelizerJsonResponse).Json; return(new MobeelizerAuthenticateResponse((String)jObject["instanceGuid"], (String)jObject["role"])); } else if (result.StatusCode == HttpStatusCode.InternalServerError) { return(new MobeelizerAuthenticateResponse(MobeelizerOperationError.ServerError((result as MobeelizerJsonResponse).Json))); } else { throw new IOException("Http connection status code: " + result.StatusCode.ToString()); } } catch (WebException e) { return(new MobeelizerAuthenticateResponse(MobeelizerOperationError.ConnectionError(e.Message))); } catch (JsonException e) { return(new MobeelizerAuthenticateResponse(MobeelizerOperationError.Other(e.Message))); } }
public MobeelizerOperationError WaitUntilSyncRequestComplete(string ticket) { MobeelizerSynchronizationStatus sycStatus = MobeelizerSynchronizationStatus.REJECTED; for (int i = 0; i < 240; i++) { WebRequest request = WebRequest.Create(GetUrl(String.Format("/checkStatus?ticket={0}&aaa={1}", ticket, DateTime.Now.Ticks))); request.Method = "GET"; SetHeaders(request, false, true); MobeelizerResponse result = new Synchronizer().GetResponse(request); if (result.StatusCode == HttpStatusCode.OK) { String strSyncStatus = (String)(result as MobeelizerJsonResponse).Json["status"]; sycStatus = (MobeelizerSynchronizationStatus)Enum.Parse(typeof(MobeelizerSynchronizationStatus), strSyncStatus, true); } else { throw new IOException(result.StatusCode.ToString() + ": " + (result as MobeelizerJsonResponse).Json.ToString()); } if (sycStatus == MobeelizerSynchronizationStatus.REJECTED || sycStatus == MobeelizerSynchronizationStatus.CONFIRMED) { return(MobeelizerOperationError.SyncRejected((String)(result as MobeelizerJsonResponse).Json["message"], (String)(result as MobeelizerJsonResponse).Json["result"])); } else if (sycStatus == MobeelizerSynchronizationStatus.FINISHED) { return(null); } try { Thread.Sleep(100 * i + 500); } catch (Exception e) { throw new IOException(e.Message, e); } } return(MobeelizerOperationError.Other("Sync timeout."));; }
internal MobeelizerOperationError Sync() { if (application.CheckSyncStatus() != MobeelizerSyncStatus.STARTED) { Log.i(TAG, "Send is already running - skipping."); return(null); } MobeelizerDatabase database = (MobeelizerDatabase)application.GetDatabase(); IMobeelizerConnectionManager connectionManager = application.GetConnectionManager(); String ticket = String.Empty; bool success = false; Others.File outputFile = null; Others.File inputFile = null; try { database.LockModifiedFlag(); if (isAllSynchronization) { Log.i(TAG, "Send sync all request."); MobeelizerSyncResponse response = connectionManager.SendSyncAllRequest(); if (response.Error == null) { ticket = response.Ticket; } else { return(response.Error); } } else { outputFile = GetOuptutFile(); outputFile.Create(); MobeelizerOperationError prepareFileError = dataFileService.PrepareOutputFile(outputFile); if (prepareFileError != null) { Log.i(TAG, "Send file haven't been created."); return(prepareFileError); } else { ChangeStatus(MobeelizerSyncStatus.FILE_CREATED, ticket); Log.i(TAG, "Send sync request."); MobeelizerSyncResponse response = connectionManager.SendSyncDiffRequest(outputFile); if (response.Error == null) { ticket = response.Ticket; } else { return(response.Error); } } } Log.i(TAG, "Sync request completed: " + ticket + "."); ChangeStatus(MobeelizerSyncStatus.TASK_CREATED, ticket); MobeelizerOperationError waitError = connectionManager.WaitUntilSyncRequestComplete(ticket); if (waitError != null) { return(waitError); } else { Log.i(TAG, "Sync process complete with success."); ChangeStatus(MobeelizerSyncStatus.TASK_PERFORMED, ticket); MobeelizerGetSyncDataOperationResult getDataResult = connectionManager.GetSyncData(ticket); if (getDataResult.Error == null) { inputFile = getDataResult.InputFile; } else { return(getDataResult.Error); } ChangeStatus(MobeelizerSyncStatus.FILE_RECEIVED, ticket); MobeelizerOperationError processError = dataFileService.ProcessInputFile(inputFile, isAllSynchronization); if (processError != null) { return(processError); } else { success = true; } connectionManager.ConfirmTask(ticket); database.ClearModifiedFlag(); application.InternalDatabase.SetInitialSyncAsNotRequired(application.Instance, application.User); } } catch (IOException e) { Log.i(TAG, e.Message); return(MobeelizerOperationError.Exception(e)); } catch (InvalidOperationException e) { Log.i(TAG, e.Message); return(MobeelizerOperationError.Exception(e)); } finally { if (inputFile != null) { inputFile.Delete(); } if (outputFile != null) { outputFile.Delete(); } database.UnlockModifiedFlag(); if (success) { ChangeStatus(MobeelizerSyncStatus.FINISHED_WITH_SUCCESS, ticket); } else { Log.i(TAG, "Sync process complete with failure."); ChangeStatus(MobeelizerSyncStatus.FINISHED_WITH_FAILURE, ticket); } } return(success ? null : MobeelizerOperationError.Other("Sync process complete with failure.")); }