public override void handleFileStartCmd(ProtocolContext ctx, TextCommand cmd)
        {
            if (string.IsNullOrEmpty(cmd.type))
            {
                throw new ProtocolErrorException("missing fied: type");
            }
            if (string.IsNullOrEmpty(cmd.file_name))
            {
                throw new ProtocolErrorException("missing fied: file_name");
            }
            if (string.IsNullOrEmpty(cmd.folder))
            {
                throw new ProtocolErrorException("missing fied: folder");
            }

            FileAssetType type;

            if (!Enum.TryParse <FileAssetType>(cmd.type, true, out type))
            {
                throw new ProtocolErrorException("unknown type: " + cmd.type);
            }

            ctx.backup_count = cmd.backuped_count;
            ctx.total_count  = cmd.total_count;

            var fileCtx = new FileContext
            {
                file_name = cmd.file_name,
                file_size = cmd.file_size,
                folder    = cmd.folder,

                datetime = cmd.datetime,
                type     = type
            };

            var hasDup = util.HasDuplicateFile(fileCtx, ctx.device_id);

            if (hasDup)
            {
                ctx.fileCtx = null;
                ctx.raiseOnFileReceiving();
                ctx.Send(new TextCommand {
                    action = "file-exist", file_name = cmd.file_name
                });
                log4net.LogManager.GetLogger("wsproto").Debug("file duplicate! send back file-exist");
            }
            else
            {
                ctx.fileCtx = fileCtx;
                ctx.raiseOnFileReceiving();
                ctx.temp_file = ctx.factory.CreateTempFile();
                ctx.Send(new TextCommand {
                    action = "file-go", file_name = cmd.file_name
                });
                ctx.SetState(new TransmitStartedState());
            }
        }
Exemple #2
0
        public override void handleFileEndCmd(ProtocolContext ctx, TextCommand cmd)
        {
            ctx.temp_file.EndWrite();

            ctx.raiseOnFileEnding();

            if (!Util.HasDuplicateFile(ctx.fileCtx, ctx.device_id))
            {
                SavedPath saved = null;
                try
                {
                    saved = ctx.storage.MoveToStorage(ctx.temp_file.Path, ctx.fileCtx);
                }
                catch (Exception e)
                {
                    throw new IOException("Unable to move temp file to storage. temp_file:" + ctx.temp_file.Path + ", file_name: " + ctx.fileCtx.file_name, e);
                }

                var fileAsset = new FileAsset
                {
                    device_id     = ctx.device_id,
                    event_time    = ctx.fileCtx.datetime,
                    file_id       = Guid.NewGuid(),
                    file_name     = ctx.fileCtx.file_name,
                    file_path     = Path.Combine(ctx.fileCtx.folder, ctx.fileCtx.file_name),
                    file_size     = ctx.fileCtx.file_size,
                    type          = (int)ctx.fileCtx.type,
                    saved_path    = saved.relative_file_path,
                    parent_folder = Path.GetDirectoryName(saved.relative_file_path),
                    seq           = Util.GetNextSeq()
                };
                Util.SaveFileRecord(fileAsset);


                if (ctx.fileCtx.file_size != ctx.temp_file.BytesWritten)
                {
                    log4net.LogManager.GetLogger(typeof(TransmitStartedState)).WarnFormat("{0} is expected to have {1} bytes but {2} bytes received.", ctx.fileCtx.file_name, ctx.fileCtx.file_size, ctx.temp_file.BytesWritten);
                }

                ctx.fileCtx.file_id = fileAsset.file_id;
                ctx.raiseOnFileReceived();
            }
            else
            {
                ctx.temp_file.Delete();
            }

            log4net.LogManager.GetLogger("wsproto").Debug("send back file-exist for file recv success");
            ctx.Send(new TextCommand {
                action = "file-exist", file_name = ctx.fileCtx.file_name
            });

            ctx.recved_files++;
            ctx.SetState(new TransmitInitState());
        }
 public override void handleUpdateCountCmd(ProtocolContext ctx, TextCommand cmd)
 {
     ctx.total_count  = cmd.transfer_count;
     ctx.backup_count = cmd.backuped_count;
     ctx.raiseOnTotalCountUpdated();
 }
 public void handleConnectCmd(TextCommand cmd)
 {
     state.handleConnectCmd(this, cmd);
 }
 public void handleFileEndCmd(TextCommand cmd)
 {
     state.handleFileEndCmd(this, cmd);
 }
 public void handleUpdateCountCmd(TextCommand cmd)
 {
     state.handleUpdateCountCmd(this, cmd);
 }