Ejemplo n.º 1
0
        internal void InitialiseStateMachine()
        {
            if (_restClient.MobileDevice != null)
            {
                StateMachine = new ActivationStateMachine(_restClient, _logger);

                var hasState = StateMachine.LoadPersistedState();

                // if we are not in the initial state
                if (hasState && (StateMachine.CurrentState is ActivationStateMachine.NotActivated == false))
                {
                    var device = _restClient.Device;
                    if (device.Id.IsEmpty() || device.DeviceSecret.IsEmpty())
                    {
                        _logger.Warning("State machine has loaded state but failed to load Local device. Resetting local device.");
                        LocalDevice.ResetDevice(_restClient.MobileDevice);

                        StateMachine.ClearPersistedState();
                        StateMachine.ResetStateMachine();
                    }
                }

                if (_restClient.Device != null)
                {
                    _restClient.Device.ClientIdUpdated = ClientIdUpdated;
                }
            }
        }
Ejemplo n.º 2
0
            public override async Task <(State, Func <Task <Event> >)> Transition(Event @event)
            {
                switch (@event)
                {
                case CalledActivate _:
                    return(this, EmptyNextEventFunc);

                case CalledDeactivate _:
                    Machine.TriggerDeactivatedCallback();
                    return(new NotActivated(Machine), EmptyNextEventFunc);

                case GettingPushDeviceDetailsFailed failedEvent:
                    Machine.TriggerActivatedCallback(failedEvent.Reason);
                    return(new NotActivated(Machine), EmptyNextEventFunc);

                case GotPushDeviceDetails _:
                    return(new WaitingForDeviceRegistration(Machine), ToNextEventFunc(RegisterDevice));

                default:
                    throw new AblyException($"WaitingForPushDeviceDetails cannot handle {@event.GetType().Name} event.", ErrorCodes.InternalError);
                }

                async Task <Event> RegisterDevice()
                {
                    LocalDevice device = Machine.LocalDevice;

                    var ably = Machine._restClient; // TODO: Check if there is an instance when Ably is not set. In which case Java set queues GettingDeviceRegistrationFailed

                    try
                    {
                        var registeredDevice = await ably.Push.Admin.RegisterDevice(device);

                        var deviceIdentityToken = registeredDevice.DeviceIdentityToken;
                        if (deviceIdentityToken.IsEmpty())
                        {
                            return(new GettingDeviceRegistrationFailed(new ErrorInfo(
                                                                           "Invalid deviceIdentityToken in response", ErrorCodes.BadRequest, HttpStatusCode.BadRequest)));
                        }

                        if (registeredDevice.ClientId.IsNotEmpty())
                        {
                            device.UpdateClientId(registeredDevice.ClientId, Machine.MobileDevice);
                        }

                        return(new GotDeviceRegistration(deviceIdentityToken));
                    }
                    catch (AblyException e)
                    {
                        return(new GettingDeviceRegistrationFailed(e.ErrorInfo));
                    }
                }
            }
Ejemplo n.º 3
0
        internal static void AddDeviceAuthenticationToRequest(AblyRequest request, LocalDevice device)
        {
            if (device is null)
            {
                return;
            }

            if (device.DeviceIdentityToken.IsNotEmpty())
            {
                request.Headers.Add(Defaults.DeviceIdentityTokenHeader, device.DeviceIdentityToken);
            }
            else if (device.DeviceSecret.IsNotEmpty())
            {
                request.Headers.Add(Defaults.DeviceSecretHeader, device.DeviceSecret);
            }
        }