public void HandleConnectMsg(TextCommand cmd, ProtocolContext ctx) { var clientInfo = Util.GetClientInfo(cmd.device_id); if (clientInfo == null) { if (Util.RejectUnpairedDevices) { ctx.SetState(new UnconnectedState()); log4net.LogManager.GetLogger("pairing").Debug("send denied because reject-all is enabled, state = unconnected"); ctx.Send(new { action = "denied", reason = "Not allowed" }); ctx.Stop(WebSocketSharp.Frame.CloseStatusCode.POLICY_VIOLATION, "Not allowed"); } else { log4net.LogManager.GetLogger("pairing").Debug("send wait-for-pair, state = " + ctx.GetState().ToString()); ctx.Send(new { action = "wait-for-pair" }); ctx.SetState(new WaitForApproveState()); ctx.raiseOnPairingRequired(); } } else { ctx.device_folder_name = clientInfo.folder_name; ReplyAcceptMsgToDevice(ctx, clientInfo); } }
public void ReplyAcceptMsgToDevice(ProtocolContext ctx, Device clientInfo) { var summary = Util.GetDeviceSummary(clientInfo.device_id); var response = new { action = "accept", server_id = Util.GetServerId(), server_name = Settings.Default.LibraryName, backup_folder = Util.GetPhotoFolder(), backup_folder_free_space = Util.GetFreeSpace(Util.GetPhotoFolder()), backup_startdate = (summary != null) ? (DateTime?)summary.backup_range.start : null, backup_enddate = (summary != null) ? (DateTime?)summary.backup_range.end : null, photo_count = (summary != null) ? (long?)summary.photo_count : null, video_count = (summary != null) ? (long?)summary.video_count : null, audio_count = (summary != null) ? (long?)summary.audio_count : null }; ctx.SetState(new TransmitInitState()); ctx.raiseOnConnectAccepted(); log4net.LogManager.GetLogger("pairing").Debug("send accept, state is " + ctx.GetState().ToString()); ctx.Send(response); ctx.storage.setDeviceName(ctx.device_folder_name); }
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()); } }
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 handleDisapprove(ProtocolContext ctx) { var response = new { action = "denied", reason = "user rejected" }; log4net.LogManager.GetLogger("pairing").Debug("send denied"); ctx.Send(response); ctx.Stop(WebSocketSharp.Frame.CloseStatusCode.POLICY_VIOLATION, "User rejected"); ctx.SetState(new UnconnectedState()); }