/// <summary> /// Coloca un manejador en la cola /// </summary> /// <param name="fileMessageHandler">El manejador a colocar en la cola</param> public bool put(FileMessageHandler fileMessageHandler) { lock (thisLock) { if (!contains(fileMessageHandler.Id)) { queue.Enqueue(fileMessageHandler); return(true); } return(false); } }
/// <summary> /// Coloca un manejador en la cola /// </summary> /// <param name="fileMessageHandler">El manejador a colocar en la cola</param> public bool put(FileMessageHandler fileMessageHandler) { lock (thisLock) { if (!contains(fileMessageHandler.Id)) { queue.Enqueue(fileMessageHandler); return true; } return false; } }
/// <summary> /// Procesa el archivos en proceso de descarga /// </summary> private void processFiles() { if (Interlocked.CompareExchange(ref fileMessageHandlerPoint, 1, 0) == 0) { lock (fileMessageHandlerLock) { FileMessageHandler[] handlers = new FileMessageHandler[activeDownloads.Count]; IDictionaryEnumerator en = activeDownloads.GetEnumerator(); int i = 0; while (en.MoveNext()) { handlers[i] = (FileMessageHandler)en.Value; i++; } foreach (FileMessageHandler fileMessageHandler in handlers) { switch (fileMessageHandler.State) { case FileMessageHandlerState.WAITING: { fileMessageHandler.open(); controlFileHandler.downloadFileOpened(fileMessageHandler.Id.ToString()); break; } case FileMessageHandlerState.OPEN: { fileMessageHandler.TimeOut--; if (fileMessageHandler.TimeOut <= 0) { fileMessageHandler.resetTimeOut(); sendMessageEvent(new FileWaitMessage(fileMessageHandler.RemoteNetUser, fileMessageHandler.Id)); } break; } case FileMessageHandlerState.ACTIVE: { fileMessageHandler.execute(); controlFileHandler.downloadFileTransfer(fileMessageHandler.Id.ToString(), fileMessageHandler.completed()); fileMessageHandler.TimeOut--; if (fileMessageHandler.TimeOut <= 0) { fileMessageHandler.resetTimeOut(); sendMessageEvent(new FileWaitMessage(fileMessageHandler.RemoteNetUser, fileMessageHandler.Id)); } break; } case FileMessageHandlerState.ERROR: { controlFileHandler.downloadFileFailed(fileMessageHandler.Id.ToString()); fileMessageHandler.close(); activeDownloads.Remove(fileMessageHandler.Id); break; } case FileMessageHandlerState.COMPLETED: { controlFileHandler.downloadFileComplete(fileMessageHandler.Id.ToString(), fileMessageHandler.FileName); fileMessageHandler.close(); activeDownloads.Remove(fileMessageHandler.Id); break; } } } } lock (fileMessageHandlerLock) { FileMessageHandler[] handlers = new FileMessageHandler[activeUploads.Count]; IDictionaryEnumerator en = activeUploads.GetEnumerator(); int i = 0; while (en.MoveNext()) { handlers[i] = (FileMessageHandler)en.Value; i++; } foreach (FileMessageHandler fileMessageHandler in handlers) { switch (fileMessageHandler.State) { case FileMessageHandlerState.WAITING: { fileMessageHandler.open(); controlFileHandler.uploadFileOpened(fileMessageHandler.Id.ToString()); break; } case FileMessageHandlerState.OPEN: { fileMessageHandler.TimeOut--; if (fileMessageHandler.TimeOut <= 0) { fileMessageHandler.resetTimeOut(); sendMessageEvent(new FileWaitMessage(fileMessageHandler.RemoteNetUser, fileMessageHandler.Id)); } break; } case FileMessageHandlerState.ACTIVE: { fileMessageHandler.execute(); controlFileHandler.uploadFileTransfer(fileMessageHandler.Id.ToString(), fileMessageHandler.completed()); break; } case FileMessageHandlerState.ERROR: { controlFileHandler.uploadFileFailed(fileMessageHandler.Id.ToString()); fileMessageHandler.close(); activeUploads.Remove(fileMessageHandler.Id); break; } case FileMessageHandlerState.COMPLETED: { controlFileHandler.uploadFileComplete(fileMessageHandler.Id.ToString()); fileMessageHandler.close(); activeUploads.Remove(fileMessageHandler.Id); break; } } } } lock (fileMessageHandlerLock) { if (activeDownloads.Count < fileData.SimulteneusDownload) { FileMessageHandler fileMessageHandler = fileMessageDownloadQueue.draw(); if (fileMessageHandler != null) { activeDownloads.Add(fileMessageHandler.Id, fileMessageHandler); } } } lock (fileMessageHandlerLock) { if (activeUploads.Count < fileData.SimulteneusUpload) { FileMessageHandler fileMessageHandler = fileMessageUploadQueue.draw(); if (fileMessageHandler != null) { activeUploads.Add(fileMessageHandler.Id, fileMessageHandler); } } } fileMessageHandlerPoint = 0; } }
/// <summary> /// Se gatilla cuando no es posible entregar un mensaje /// </summary> /// <param name="message">El mensaje no entregado</param> public void errorMessage(Message message) { switch (message.Type) { case SubProtocol.FileTransfer.Types.FILEREQUESTMESSAGE: { FileRequestMessage fileRequestMessage = (FileRequestMessage)message; lock (fileMessageHandlerLock) { if (activeDownloads.Contains(fileRequestMessage.FileHandlerId)) { FileMessageHandler fileMessageHandler = (FileMessageHandler)activeDownloads[fileRequestMessage.FileHandlerId]; controlFileHandler.downloadFileFailed(fileMessageHandler.Id.ToString()); fileMessageHandler.close(); activeDownloads.Remove(fileMessageHandler.Id); } } break; } case SubProtocol.FileTransfer.Types.FILEERRORMESSAGES: { break; } case SubProtocol.FileTransfer.Types.FILEPARTMESSAGE: { FilePartMessage filePartMessage = (FilePartMessage)message; lock (fileMessageHandlerLock) { if (activeUploads.Contains(filePartMessage.FileHandlerId)) { FileMessageHandler fileMessageHandler = (FileMessageHandler)activeUploads[filePartMessage.FileHandlerId]; controlFileHandler.uploadFileFailed(fileMessageHandler.Id.ToString()); fileMessageHandler.close(); activeUploads.Remove(fileMessageHandler.Id); } } break; } case SubProtocol.FileTransfer.Types.FILEWAITMESSAGE: { FileWaitMessage fileWaitMessage = (FileWaitMessage)message; lock (fileMessageHandlerLock) { if (activeDownloads.Contains(fileWaitMessage.FileHandlerId)) { FileMessageHandler fileMessageHandler = (FileMessageHandler)activeDownloads[fileWaitMessage.FileHandlerId]; controlFileHandler.downloadFileFailed(fileMessageHandler.Id.ToString()); fileMessageHandler.close(); activeDownloads.Remove(fileMessageHandler.Id); } else if (activeUploads.Contains(fileWaitMessage.FileHandlerId)) { FileMessageHandler fileMessageHandler = (FileMessageHandler)activeUploads[fileWaitMessage.FileHandlerId]; controlFileHandler.uploadFileFailed(fileMessageHandler.Id.ToString()); fileMessageHandler.close(); activeUploads.Remove(fileMessageHandler.Id); } } break; } case SubProtocol.FileTransfer.Types.FILECOMPLETEMESSAGE: { break; } case SubProtocol.FileTransfer.Types.FILELISTREQUESTMESSAGE: { break; } case SubProtocol.FileTransfer.Types.FILELISTMESSAGE: { break; } } }
/// <summary> /// Se gatilla cuando se recibe un mensaje /// </summary> /// <param name="message">El mensaje recibido</param> public void proccessMessage(Message message) { switch (message.Type) { case SubProtocol.FileTransfer.Types.FILEREQUESTMESSAGE: { FileRequestMessage fileRequestMessage = (FileRequestMessage)message; FileInformation fileInformation = fileData.FileList.getFileInformation(fileRequestMessage.FileId); if (fileInformation != null) { FileMessageSender fileMessageSender = new FileMessageSender(fileRequestMessage.SenderNetUser, fileRequestMessage.FileHandlerId, sendMessageDelegate, fileInformation, fileData); lock (fileMessageHandlerLock) { if (!activeUploads.Contains(fileMessageSender.Id)) { if (fileMessageUploadQueue.put(fileMessageSender)) { controlFileHandler.uploadFileQueued(fileRequestMessage.SenderNetUser, fileMessageSender.Id.ToString(), fileInformation.Name); } } } } else { FileErrorMessage fileErrorMessage = new FileErrorMessage(fileRequestMessage.SenderNetUser, fileRequestMessage.FileHandlerId); sendMessageEvent(fileErrorMessage); } break; } case SubProtocol.FileTransfer.Types.FILEERRORMESSAGES: { FileErrorMessage fileErrorMessage = (FileErrorMessage)message; lock (fileMessageHandlerLock) { if (activeUploads.Contains(fileErrorMessage.FileHandlerId)) { FileMessageHandler fileMessageHandler = (FileMessageHandler)activeUploads[fileErrorMessage.FileHandlerId]; controlFileHandler.uploadFileFailed(fileMessageHandler.Id.ToString()); fileMessageHandler.close(); activeUploads.Remove(fileMessageHandler.Id); } else if (activeDownloads.Contains(fileErrorMessage.FileHandlerId)) { FileMessageHandler fileMessageHandler = (FileMessageHandler)activeDownloads[fileErrorMessage.FileHandlerId]; controlFileHandler.downloadFileFailed(fileMessageHandler.Id.ToString()); fileMessageHandler.close(); activeDownloads.Remove(fileMessageHandler.Id); } } break; } case SubProtocol.FileTransfer.Types.FILEPARTMESSAGE: { FilePartMessage filePartMessage = (FilePartMessage)message; lock (fileMessageHandlerLock) { if (activeDownloads.Contains(filePartMessage.FileHandlerId)) { FileMessageHandler fileMessageHandler = (FileMessageHandler)activeDownloads[filePartMessage.FileHandlerId]; fileMessageHandler.attendMessage(filePartMessage); fileMessageHandler.waitUp(fileData.FileRiseUp); } else { FileErrorMessage fileErrorMessage = new FileErrorMessage(filePartMessage.SenderNetUser, filePartMessage.FileHandlerId); sendMessageEvent(fileErrorMessage); } } break; } case SubProtocol.FileTransfer.Types.FILEWAITMESSAGE: { FileWaitMessage fileWaitMessage = (FileWaitMessage)message; lock (fileMessageHandlerLock) { if (activeDownloads.Contains(fileWaitMessage.FileHandlerId)) { FileMessageHandler fileMessageHandler = (FileMessageHandler)activeDownloads[fileWaitMessage.FileHandlerId]; fileMessageHandler.waitUp(fileData.FileRiseUp); } else if (activeUploads.Contains(fileWaitMessage.FileHandlerId)) { FileMessageHandler fileMessageHandler = (FileMessageHandler)activeUploads[fileWaitMessage.FileHandlerId]; fileMessageHandler.waitUp(fileData.FileRiseUp); } else { if (!fileMessageDownloadQueue.contains(fileWaitMessage.FileHandlerId) && !fileMessageUploadQueue.contains(fileWaitMessage.FileHandlerId)) { FileErrorMessage fileErrorMessage = new FileErrorMessage(fileWaitMessage.SenderNetUser, fileWaitMessage.FileHandlerId); sendMessageEvent(fileErrorMessage); } } } break; } case SubProtocol.FileTransfer.Types.FILECOMPLETEMESSAGE: { FileCompleteMessage fileCompleteMessage = (FileCompleteMessage)message; lock (fileMessageHandlerLock) { if (activeUploads.Contains(fileCompleteMessage.FileHandlerId)) { FileMessageHandler fileMessageHandler = (FileMessageHandler)activeUploads[fileCompleteMessage.FileHandlerId]; fileMessageHandler.State = FileMessageHandlerState.COMPLETED; } } break; } case SubProtocol.FileTransfer.Types.FILELISTREQUESTMESSAGE: { FileListRequestMessage fileListRequestMessage = (FileListRequestMessage)message; FileListMessage fileListMessage = new FileListMessage(fileListRequestMessage.SenderNetUser, fileData.FileList); sendMessageEvent(fileListMessage); break; } case SubProtocol.FileTransfer.Types.FILELISTMESSAGE: { FileListMessage fileListMessage = (FileListMessage)message; controlFileListHandler.addFileList(fileListMessage.SenderNetUser, fileListMessage.FileList); break; } } }