Пример #1
0
        private void ShutdownState()
        {
            TransferComponent transfer = Owner.Get <TransferComponent>();

            if (!transfer.Done && !Cancelled)
            {
                Status = DemonStatus.Error;
            }

            State = DemonState.None;

            Disconnect();
        }
Пример #2
0
        private void DownloadState()
        {
            try
            {
                // Command
                FileComponent          file    = Owner.Get <FileComponent>();
                DownloadRequestCommand command = new DownloadRequestCommand(Entity, Connection);
                HttpCode code = command.Execute(Id, file, ClientId);

                if (code == HttpCode.Ok)
                {
                    byte[] data = command.Data;

                    if (data != null)
                    {
                        // Data
                        JsonChunk jsonChunk = command.Chunk;
                        Debug.Assert(data.Length == jsonChunk.Size);

                        while (true)
                        {
                            try
                            {
                                FileUtil.Write(file.Path, data, jsonChunk.Offset);
                                break;
                            }
                            catch (IOException ex)
                            {
                                Console.WriteLine(ex.Message);
                                Status = DemonStatus.Warning;
                                Thread.Sleep(DemonTimeout.File);
                            }
                        }

                        TransferComponent transfer = Owner.Get <TransferComponent>();
                        transfer.Size += jsonChunk.Size;

                        if (transfer.Done)
                        {
                            State  = DemonState.Shutdown;
                            Status = DemonStatus.Success;
                        }
                    }
                    else
                    {
                        if (Id == null)
                        {
                            FileUtil.Create(file.Path, file.Size);

                            if (code == HttpCode.Ok)
                            {
                                // Data
                                Id = command.Id;
                                if (!Paused && !Cancelled)
                                {
                                    Status = DemonStatus.Success;
                                }
                            }
                        }
                        else
                        {
                            // TODO: Implement infinite time-out?
                            //
                            Sleep(DemonTimeout.Seconds);
                        }
                    }
                }
                else
                {
                    State = DemonState.Shutdown;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                State  = DemonState.Shutdown;
                Status = DemonStatus.Error;
            }
        }