private void FileReceiverOnConnectionClosed(FileSendInfo fileInfo) { RemovePendingFileReceive(fileInfo.FileSendId); _appContext.NotificationManager.DeleteNotification(fileInfo.Notification); if (fileInfo.FileReceiver.FileReceiveResult) { var fileReceivedNotification = new FileReceivedNotification { AssociatedUsername = _engagement.SecondParty.Party.Username, FileInfo = fileInfo }; _appContext.NotificationManager.AddNotification(fileReceivedNotification); Chat.LogSystemMessage("Successfully received the file " + fileInfo.Filename); OnNewActivity(new FileSendActivity(_engagement, FileSendActivity.FILE_SEND_COMPLETE) { To = "_SELF", From = _engagement.SecondParty.Party.Username, FileInfo = fileInfo }); } else { if (fileInfo.State != FileSendState.ReceiveCancelled) { Chat.LogErrorMessage("There was a problem receiving the file " + fileInfo.Filename); } OnNewActivity(new FileSendActivity(_engagement, FileSendActivity.FILE_SEND_FAILED) { To = "_SELF", From = _engagement.SecondParty.Party.Username, FileInfo = fileInfo }); } }
// called if we are sending the file and it is complete private void FileSendComplete(FileSendInfo fileInfo, FileSendCompleteEventArgs args) { RemovePendingFileSend(fileInfo.FileSendId); _appContext.NotificationManager.DeleteNotification(fileInfo.Notification); if (args.Success) { fileInfo.State = FileSendState.SendComplete; Chat.LogSystemMessage("You successfully sent the file " + fileInfo.Filename); OnNewActivity(new FileSendActivity(_engagement, FileSendActivity.FILE_SEND_COMPLETE) { From = "_SELF", To = _engagement.SecondParty.Party.Username, FileInfo = fileInfo }); } else { if (fileInfo.State != FileSendState.SendCancelled) { Chat.LogSystemMessage("Sending of " + fileInfo.Filename + " failed."); OnNewActivity(new FileSendActivity(_engagement, FileSendActivity.FILE_SEND_FAILED) { From = "_SELF", To = _engagement.SecondParty.Party.Username, FileInfo = fileInfo }); } } }
// Called when this user accepts a file private void ProcessAcceptFile(FileSendInfo fileInfo) { Logger.Info("Accepted request from " + _engagement.SecondParty.Party.Username + " to send the file " + fileInfo.Filename); //Chat.LogSystemMessage("Accepted " + fileInfo.Filename + "."); FileSendRequestResponseRq request = new FileSendRequestResponseRq() { shortCode = _engagement.SecondParty.ActiveShortCode, username = _engagement.SecondParty.Party.Username, interactionId = _engagement.Interactions.CurrentOrNewInteraction.Id, fileSendId = fileInfo.FileSendId, accepted = true }; try { fileInfo.State = FileSendState.Receiving; fileInfo.FilePath = OsUtils.IsWinVistaOrHigher ? Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Downloads", fileInfo.Filename) : Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), fileInfo.Filename); fileInfo.FileReceiver = new FileSendListener(fileInfo); var notification = ShowFileProgressNotification(fileInfo); fileInfo.Notification = notification; notification.Cancelled += delegate { CancelFileReceive(fileInfo); }; fileInfo.FileReceiver.ServerConnectionClosed += (o, eventArgs) => FileReceiverOnConnectionClosed(fileInfo); fileInfo.FileReceiver.DataRead += delegate { notification.Progress = (int)((fileInfo.FileReceiver.DataReadSize * 100) / fileInfo.FileSize); }; fileInfo.FileReceiver.Listen(); // Now we have started our listener, we can send our filesendrequest response to the requester, to tell him to send. _appContext.ConnectionManager.Connection.RequestAsync <FileSendRequestResponseRq, FileSendRequestResponseRs>(request, (rq, rs, ex) => FileSendRequestResponseResponseHandler(fileInfo, rq, rs, ex)); } catch (Exception e) { Logger.Error("Failed to send a file acceptance request for file " + fileInfo.Filename + "[" + fileInfo.FileSendId + "] to " + _engagement.SecondParty.Party.Username); } }
// Called when the offering of the file is cancelled by this user private void CancelFileOffer(FileSendInfo fileInfo) { Chat.LogSystemMessage("You stopped offering " + fileInfo.Filename + " to " + _engagement.SecondParty.Party.Firstname); Logger.Debug("User cancelled the file send for " + fileInfo.Filename); RemovePendingFileSend(fileInfo.FileSendId); OnNewActivity(new FileSendActivity(_engagement, FileSendActivity.FILE_SEND_CANCEL_REQUEST) { From = "_SELF", To = _engagement.SecondParty.Party.Username, FileInfo = fileInfo }); }
private FileSendProgressNotification ShowFileProgressNotification(FileSendInfo fileInfo) { var notification = new FileSendProgressNotification() { AssociatedUsername = _engagement.SecondParty.Party.Username, FileInfo = fileInfo }; _appContext.NotificationManager.AddNotification(notification); return(notification); }
// called if we cancel the file send private void CancelFileSend(FileSendInfo fileInfo) { Chat.LogSystemMessage("You cancelled sending the file " + fileInfo.Filename); fileInfo.State = FileSendState.SendCancelled; Logger.Warn("Cancelling file send of " + fileInfo.Filename); RemovePendingFileReceive(fileInfo.FileSendId); fileInfo.FileSender.Close(); _appContext.NotificationManager.DeleteNotification(fileInfo.Notification); OnNewActivity(new FileSendActivity(_engagement, FileSendActivity.FILE_SEND_CANCEL) { From = "_SELF", To = _engagement.SecondParty.Party.Username, FileInfo = fileInfo }); }
// Async callback from our file send request response private void FileSendRequestResponseResponseHandler(FileSendInfo fileInfo, FileSendRequestResponseRq request, FileSendRequestResponseRs response, Exception e) { { if (e != null) { RemovePendingFileReceive(request.fileSendId); Logger.Error("Failed to send the FileSendRequestResponse for file " + request.fileSendId + " : " + e.Message, e); fileInfo.FileReceiver.Close(); _appContext.NotificationManager.DeleteNotification(fileInfo.Notification); Chat.LogSystemMessage("An error occured receiving the file"); } else { OnNewActivity(new FileSendActivity(_engagement, FileSendActivity.FILE_SEND_RESPONSE) { From = "_SELF", To = _engagement.SecondParty.Party.Username, FileInfo = fileInfo, Answer = true }); } } }
// We call this when we denies secondparty the request to send the file to us private void ProcessDenyFile(FileSendInfo fileInfo) { fileInfo.State = FileSendState.ReceiveCancelled; RemovePendingFileReceive(fileInfo.FileSendId); Logger.Info("Denied request from " + _engagement.SecondParty.Party.Name + " to send the file " + fileInfo.Filename); Chat.LogSystemMessage("You refused " + fileInfo.Filename + " from " + _engagement.SecondParty.Party.Firstname + "."); FileSendRequestResponseRq request = new FileSendRequestResponseRq() { shortCode = _engagement.SecondParty.ActiveShortCode, username = _engagement.SecondParty.Party.Username, fileSendId = fileInfo.FileSendId, accepted = false, interactionId = _engagement.Interactions.CurrentOrNewInteraction.Id }; try { _appContext.ConnectionManager.Connection.RequestAsync <FileSendRequestResponseRq, FileSendRequestResponseRs>(request, delegate(FileSendRequestResponseRq rq, FileSendRequestResponseRs rs, Exception e) { if (e != null) { Logger.Error( "Failed to send the FileSendRequestResponse for file " + request.fileSendId + " : " + e.Message, e); } else { OnNewActivity(new FileSendActivity(_engagement, FileSendActivity.FILE_SEND_RESPONSE) { From = "_SELF", To = _engagement.SecondParty.Party.Username, FileInfo = fileInfo, Answer = false }); } }); } catch (Exception e) { Logger.Error("Failed to send a file acceptance request for file " + fileInfo.Filename + "[" + fileInfo.FileSendId + "] to " + _engagement.SecondParty.Party.Username); } }
// Called when someone is requesting to send a file to us internal void ProcessIncomingFileSendRequest(string filename, string fileSendId, long fileSize) { Logger.Info(_engagement.SecondParty.Party.Username + " requests to send the file " + filename); var fileSendInfo = new FileSendInfo() { Filename = filename, FileSendId = fileSendId, FileSize = fileSize, Direction = FileSendDirection.Receive, State = FileSendState.PendingReceive }; IsActive = true; _pendingFileReceives.Add(fileSendId, fileSendInfo); FileSendRequestChatElement chatElement = LogFileSendRequest(_engagement.SecondParty.Party.Firstname + " offered you the file " + filename + ".", _engagement.SecondParty.Party.Username); chatElement.AnswerHandler.AnsweredTrue += (sender, args) => ProcessAcceptFile(fileSendInfo); chatElement.AnswerHandler.AnsweredFalse += (sender, args) => ProcessDenyFile(fileSendInfo); OnNewActivity(new FileSendActivity(_engagement, FileSendActivity.FILE_SEND_REQUEST) { To = "_SELF", From = _engagement.SecondParty.Party.Username, FileInfo = fileSendInfo }); }
private void CloseFileSend(FileSendInfo fileSend) { switch (fileSend.State) { case (FileSendState.PendingSend): CancelFileOffer(fileSend); break; case (FileSendState.Receiving): CancelFileReceive(fileSend); break; case (FileSendState.Sending): CancelFileSend(fileSend); break; default: if (fileSend.Notification != null) { _appContext.NotificationManager.DeleteNotification(fileSend.Notification); } break; } }
// Async callback on result of requesting to send a file private void ProcessFileSendRequestRs(FileSendRequestRq request, FileSendRequestRs res, Exception e, FileSendInfo fileInfo) { if (e != null) { Chat.LogSystemMessage("An error occured trying to send " + _engagement.SecondParty.Party.Username + " a request to send them " + fileInfo.Filename); Logger.Error("Failed to send file send request for " + fileInfo.Filename); RemovePendingFileSend(fileInfo.FileSendId); OnNewActivity(new FileSendActivity(_engagement, FileSendActivity.FILE_SEND_REQUEST_FAILED) { From = "_SELF", To = _engagement.SecondParty.Party.Username, FileInfo = fileInfo }); } else { } }
// Called when a response comes back to me from a file request I sent (either I am allowed to send or not) public void ProcessFileSendRequestResponse(bool accepted, string fileSendId) { if (fileSendId != null && _pendingFileSends.ContainsKey(fileSendId)) { FileSendInfo fileInfo = _pendingFileSends[fileSendId]; _appContext.NotificationManager.DeleteNotification(fileInfo.Notification); OnNewActivity(new FileSendActivity(_engagement, FileSendActivity.FILE_SEND_RESPONSE) { To = "_SELF", From = _engagement.SecondParty.Party.Username, FileInfo = fileInfo, Answer = accepted }); if (accepted) { fileInfo.State = FileSendState.Sending; Logger.Info("File send of file " + fileInfo.Filename + " accepted by " + _engagement.SecondParty.Party.Name); Chat.LogSystemMessage(_engagement.SecondParty.Party.Firstname + " accepted your request to send " + fileInfo.Filename); fileInfo.FileSender = new FileSendClient(_engagement.SecondParty, fileInfo); var notification = ShowFileProgressNotification(fileInfo); fileInfo.Notification = notification; notification.Cancelled += delegate { CancelFileSend(fileInfo); }; fileInfo.FileSender.DataWritten += delegate { notification.Progress = (int)((fileInfo.FileSender.DataWriteSize * 100) / fileInfo.FileSize); }; fileInfo.FileSender.SendFileComplete += (sender, args) => FileSendComplete(fileInfo, args); Thread fileSendThread = new Thread(() => fileInfo.FileSender.SendFile()) { IsBackground = true, Name = "fileSend[" + fileInfo.FileSendId + "]" }; fileSendThread.Start(); OnNewActivity(new FileSendActivity(_engagement, FileSendActivity.FILE_SEND_START) { From = "_SELF", To = _engagement.SecondParty.Party.Username, FileInfo = fileInfo }); /*} * else * { * try * { * BlitsMeClientAppContext.CurrentAppContext.RepeaterManager.InitRepeatedConnection( * _engagement.SecondParty.Person.Username, _engagement.SecondParty.ActiveShortCode, * _engagement.Interactions.CurrentOrNewInteraction.Id, fileSendId); * } * catch (Exception e) * { * Logger.Error("Failed to get a repeated connection to " + _engagement.SecondParty.Person.Username); * } * }*/ } else { Chat.LogSystemMessage(_engagement.SecondParty.Party.Firstname + " refused " + fileInfo.Filename); RemovePendingFileSend(fileInfo.FileSendId); Logger.Info("File send of file " + fileInfo.Filename + " rejected by " + _engagement.SecondParty.Party.Name); } } else { throw new Exception("Got a file send request response with an invalid id [" + fileSendId + "]"); } }
// We call this when we denies secondparty the request to send the file to us private void ProcessDenyFile(FileSendInfo fileInfo) { fileInfo.State = FileSendState.ReceiveCancelled; RemovePendingFileReceive(fileInfo.FileSendId); Logger.Info("Denied request from " + _engagement.SecondParty.Party.Name + " to send the file " + fileInfo.Filename); Chat.LogSystemMessage("You refused " + fileInfo.Filename + " from " + _engagement.SecondParty.Party.Firstname + "."); FileSendRequestResponseRq request = new FileSendRequestResponseRq() { shortCode = _engagement.SecondParty.ActiveShortCode, username = _engagement.SecondParty.Party.Username, fileSendId = fileInfo.FileSendId, accepted = false, interactionId = _engagement.Interactions.CurrentOrNewInteraction.Id }; try { _appContext.ConnectionManager.Connection.RequestAsync<FileSendRequestResponseRq, FileSendRequestResponseRs>(request, delegate(FileSendRequestResponseRq rq, FileSendRequestResponseRs rs, Exception e) { if (e != null) { Logger.Error( "Failed to send the FileSendRequestResponse for file " + request.fileSendId + " : " + e.Message, e); } else { OnNewActivity(new FileSendActivity(_engagement, FileSendActivity.FILE_SEND_RESPONSE) { From = "_SELF", To = _engagement.SecondParty.Party.Username, FileInfo = fileInfo, Answer = false }); } }); } catch (Exception e) { Logger.Error("Failed to send a file acceptance request for file " + fileInfo.Filename + "[" + fileInfo.FileSendId + "] to " + _engagement.SecondParty.Party.Username); } }
// Called when this user accepts a file private void ProcessAcceptFile(FileSendInfo fileInfo) { Logger.Info("Accepted request from " + _engagement.SecondParty.Party.Username + " to send the file " + fileInfo.Filename); //Chat.LogSystemMessage("Accepted " + fileInfo.Filename + "."); FileSendRequestResponseRq request = new FileSendRequestResponseRq() { shortCode = _engagement.SecondParty.ActiveShortCode, username = _engagement.SecondParty.Party.Username, interactionId = _engagement.Interactions.CurrentOrNewInteraction.Id, fileSendId = fileInfo.FileSendId, accepted = true }; try { fileInfo.State = FileSendState.Receiving; fileInfo.FilePath = OsUtils.IsWinVistaOrHigher ? Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Downloads", fileInfo.Filename) : Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), fileInfo.Filename); fileInfo.FileReceiver = new FileSendListener(fileInfo); var notification = ShowFileProgressNotification(fileInfo); fileInfo.Notification = notification; notification.Cancelled += delegate { CancelFileReceive(fileInfo); }; fileInfo.FileReceiver.ServerConnectionClosed += (o, eventArgs) => FileReceiverOnConnectionClosed(fileInfo); fileInfo.FileReceiver.DataRead += delegate { notification.Progress = (int)((fileInfo.FileReceiver.DataReadSize * 100) / fileInfo.FileSize); }; fileInfo.FileReceiver.Listen(); // Now we have started our listener, we can send our filesendrequest response to the requester, to tell him to send. _appContext.ConnectionManager.Connection.RequestAsync<FileSendRequestResponseRq, FileSendRequestResponseRs>(request, (rq, rs, ex) => FileSendRequestResponseResponseHandler(fileInfo, rq, rs, ex)); } catch (Exception e) { Logger.Error("Failed to send a file acceptance request for file " + fileInfo.Filename + "[" + fileInfo.FileSendId + "] to " + _engagement.SecondParty.Party.Username); } }
private void FileReceiverOnConnectionClosed(FileSendInfo fileInfo) { RemovePendingFileReceive(fileInfo.FileSendId); _appContext.NotificationManager.DeleteNotification(fileInfo.Notification); if (fileInfo.FileReceiver.FileReceiveResult) { var fileReceivedNotification = new FileReceivedNotification { AssociatedUsername = _engagement.SecondParty.Party.Username, FileInfo = fileInfo }; _appContext.NotificationManager.AddNotification(fileReceivedNotification); Chat.LogSystemMessage("Successfully received the file " + fileInfo.Filename); OnNewActivity(new FileSendActivity(_engagement, FileSendActivity.FILE_SEND_COMPLETE) { To = "_SELF", From = _engagement.SecondParty.Party.Username, FileInfo = fileInfo }); } else { if (fileInfo.State != FileSendState.ReceiveCancelled) Chat.LogErrorMessage("There was a problem receiving the file " + fileInfo.Filename); OnNewActivity(new FileSendActivity(_engagement, FileSendActivity.FILE_SEND_FAILED) { To = "_SELF", From = _engagement.SecondParty.Party.Username, FileInfo = fileInfo }); } }
// Request to send a file to user internal void RequestFileSend(String filepath) { String filename = Path.GetFileName(filepath); try { FileSendInfo fileInfo = new FileSendInfo() { Filename = filename, FileSize = new FileInfo(filepath).Length, FileSendId = Util.getSingleton().generateString(8), FilePath = filepath, Direction = FileSendDirection.Send }; FileSendRequestRq request = new FileSendRequestRq() { shortCode = _engagement.SecondParty.ActiveShortCode, username = _engagement.SecondParty.Party.Username, filename = fileInfo.Filename, fileSize = fileInfo.FileSize, fileSendId = fileInfo.FileSendId, interactionId = _engagement.Interactions.CurrentOrNewInteraction.Id }; try { Chat.LogSystemMessage(string.Format("Sending {0} the file {1}, waiting for acceptance.", _engagement.SecondParty.Party.Firstname, filename)); _appContext.ConnectionManager.Connection.RequestAsync<FileSendRequestRq, FileSendRequestRs>(request, (req, res, ex) => ProcessFileSendRequestRs(req, res, ex, fileInfo)); Logger.Info("Requested to send " + fileInfo.Filename + " to " + _engagement.SecondParty.Party.Username); fileInfo.State = FileSendState.PendingSend; _pendingFileSends.Add(request.fileSendId, fileInfo); IsActive = true; var notification = new CancellableNotification() { AssociatedUsername = _engagement.SecondParty.Party.Username, Message = "Offering " + _engagement.SecondParty.Party.Firstname + " " + fileInfo.Filename, CancelTooltip = "Cancel File Send", Id = fileInfo.FileSendId }; notification.Cancelled += (sender, args) => CancelFileOffer(fileInfo); fileInfo.Notification = notification; _appContext.NotificationManager.AddNotification(notification); OnNewActivity(new FileSendActivity(_engagement, FileSendActivity.FILE_SEND_REQUEST) { From = "_SELF", To = _engagement.SecondParty.Party.Username, FileInfo = fileInfo }); } catch (Exception ex) { Logger.Error("Error during request for File Send : " + ex.Message, ex); Chat.LogErrorMessage("An error occured trying to send " + _engagement.SecondParty.Party.Firstname + " a request to send them a file."); } } catch (Exception ex) { Logger.Error("Error setting up request for file send : " + ex.Message, ex); Chat.LogErrorMessage("An error occured trying to send " + _engagement.SecondParty.Party.Firstname + " a request to send them a file."); } }
private FileSendProgressNotification ShowFileProgressNotification(FileSendInfo fileInfo) { var notification = new FileSendProgressNotification() { AssociatedUsername = _engagement.SecondParty.Party.Username, FileInfo = fileInfo }; _appContext.NotificationManager.AddNotification(notification); return notification; }
internal FileSendClient(Attendance secondParty, FileSendInfo fileInfo) : base(secondParty) { _fileInfo = fileInfo; }
// Request to send a file to user internal void RequestFileSend(String filepath) { String filename = Path.GetFileName(filepath); try { FileSendInfo fileInfo = new FileSendInfo() { Filename = filename, FileSize = new FileInfo(filepath).Length, FileSendId = Util.getSingleton().generateString(8), FilePath = filepath, Direction = FileSendDirection.Send }; FileSendRequestRq request = new FileSendRequestRq() { shortCode = _engagement.SecondParty.ActiveShortCode, username = _engagement.SecondParty.Party.Username, filename = fileInfo.Filename, fileSize = fileInfo.FileSize, fileSendId = fileInfo.FileSendId, interactionId = _engagement.Interactions.CurrentOrNewInteraction.Id }; try { Chat.LogSystemMessage(string.Format("Sending {0} the file {1}, waiting for acceptance.", _engagement.SecondParty.Party.Firstname, filename)); _appContext.ConnectionManager.Connection.RequestAsync <FileSendRequestRq, FileSendRequestRs>(request, (req, res, ex) => ProcessFileSendRequestRs(req, res, ex, fileInfo)); Logger.Info("Requested to send " + fileInfo.Filename + " to " + _engagement.SecondParty.Party.Username); fileInfo.State = FileSendState.PendingSend; _pendingFileSends.Add(request.fileSendId, fileInfo); IsActive = true; var notification = new CancellableNotification() { AssociatedUsername = _engagement.SecondParty.Party.Username, Message = "Offering " + _engagement.SecondParty.Party.Firstname + " " + fileInfo.Filename, CancelTooltip = "Cancel File Send", Id = fileInfo.FileSendId }; notification.Cancelled += (sender, args) => CancelFileOffer(fileInfo); fileInfo.Notification = notification; _appContext.NotificationManager.AddNotification(notification); OnNewActivity(new FileSendActivity(_engagement, FileSendActivity.FILE_SEND_REQUEST) { From = "_SELF", To = _engagement.SecondParty.Party.Username, FileInfo = fileInfo }); } catch (Exception ex) { Logger.Error("Error during request for File Send : " + ex.Message, ex); Chat.LogErrorMessage("An error occured trying to send " + _engagement.SecondParty.Party.Firstname + " a request to send them a file."); } } catch (Exception ex) { Logger.Error("Error setting up request for file send : " + ex.Message, ex); Chat.LogErrorMessage("An error occured trying to send " + _engagement.SecondParty.Party.Firstname + " a request to send them a file."); } }
internal FileSendListener(FileSendInfo fileInfo) { _fileInfo = fileInfo; }