public TnSClient(Socket socket, Protocol protocol, SendingJob job) : base(socket, protocol)
 {
     this.job   = job;
     job.Status = Job.JobStatus.Preparing;
     try {
         JobZipStorageModule module = new JobZipStorageModule();
         iterator      = module.prepareJob(job);
         network       = new TransferNetworkModule();
         transferBlock = new byte[((JobZipStorageModule.JobFileIterator)iterator).READ_BLOCK_SIZE];
     } catch (Exception e) {
         if (iterator != null)
         {
             iterator.close();
         }
         throw e;
     }
 }
Example #2
0
        ///<summary>
        /// Prepares a Job performing the operations needed to execute the transfer.
        /// The file relative to the Job is zipped (but only if necessary) and a
        /// JobFileIterator is created and returned in order to allow the caller to
        /// access to the temporary file to trasnfer.
        ///</summary>
        public FileIterator prepareJob(SendingJob j)
        {
            //DateTime lastModify = j.Task.RequestTimestamp;  // The timestamp

            string zipName = null;

            JobFileIterator jobIterator = null;

            try {
                // Check zipped file existence
                //if (!File.Exists(zipName)) {  // NO MORE NEEDED
                ManualResetEvent zippedEvent;
                if (j.FilePaths.Count == 1 /*&& j.Task.Info[0].Type == FileTransfer.FileInfo.FType.DIRECTORY*/)
                {
                    FileAttributes attr = 0;
                    attr = File.GetAttributes(j.FilePaths.Last());

                    bool toZip = true;
                    //DateTime lastModify;
                    string uniquetoken;

                    if ((attr & FileAttributes.Directory) == FileAttributes.Directory)
                    {
                        var directory = new DirectoryInfo(j.FilePaths[0]);
                        //lastModify = RealLastModify(directory);
                        uniquetoken = UniqueTokenFromDirectory(directory);
                    }
                    else
                    {
                        DateTime lastModify = new System.IO.FileInfo(j.FilePaths[0]).LastWriteTimeUtc;
                        uniquetoken = "_" + lastModify.Year + lastModify.Month + lastModify.Day + lastModify.Hour + lastModify.Minute + lastModify.Second + lastModify.Millisecond;
                    }

                    zipName = Path.Combine(zipTempFolder, j.Task.Info[0].Name + uniquetoken);
                    lock (dictionaryLock) {
                        if (File.Exists(zipName))
                        {
                            toZip = false;
                            events.TryGetValue(zipName, out zippedEvent);
                        }
                        else
                        {
                            zippedEvent = new ManualResetEvent(false);
                            events.TryAdd(zipName, zippedEvent);
                        }
                    }
                    if (toZip)
                    {
                        if ((attr & FileAttributes.Directory) == FileAttributes.Directory)
                        {
                            // zip directory (including base directory)
                            ZipFile.CreateFromDirectory(j.FilePaths.Last(), zipName, CompressionLevel.NoCompression, false);
                        }
                        else      // otherwise
                                  // Note: if it's a file, ZipArchive has to work on a new file in order to zip
                        {
                            ZipArchive newFile = null;
                            try {
                                newFile = ZipFile.Open(zipName, ZipArchiveMode.Create);
                                //string p = Path.GetDirectoryName(filePath);
                                newFile.CreateEntryFromFile(j.FilePaths.Last(), Path.GetFileName(j.FilePaths.Last()), CompressionLevel.NoCompression);
                            } finally {
                                if (newFile != null)
                                {
                                    newFile.Dispose();
                                }
                            }
                        }
                        zippedEvent.Set();
                        lock (dictionaryLock) {
                            events.TryRemove(zipName, out zippedEvent);
                            zippedEvent = null;
                        }
                    }
                    else
                    {
                        //TODO
                        //WAIT UNITL FILE IS READY;
                        if (zippedEvent != null)
                        {
                            zippedEvent.WaitOne();
                        }
                    }
                }
                else
                {
                    ZipArchive newFile = null;

                    try {
                        // Constructs the zip file name
                        Directory.CreateDirectory(Path.Combine(zipTempFolder, j.Id));
                        zipName = Path.Combine(zipTempFolder, j.Id, j.Id + ".tmp");// + @"\" + Path.GetFileNameWithoutExtension(filePath) + date + ".zip";

                        newFile = ZipFile.Open(zipName, ZipArchiveMode.Create);

                        foreach (string filePath in j.FilePaths)
                        {
                            /* Retrieves the file attributes to check if the file is a directory or not
                             *  because the way to zip is different in the two cases. */
                            FileAttributes attr = 0;
                            attr = File.GetAttributes(filePath);

                            if ((attr & FileAttributes.Directory) == FileAttributes.Directory)
                            {
                                string tmpArchivePath = Path.Combine(Path.GetDirectoryName(zipName), Path.GetFileName(filePath) + ".zip");
                                try {
                                    ZipFile.CreateFromDirectory(filePath, tmpArchivePath, CompressionLevel.NoCompression, false);
                                    newFile.CreateEntryFromFile(tmpArchivePath, Path.GetFileName(filePath), CompressionLevel.NoCompression);
                                } finally {
                                    File.Delete(tmpArchivePath);
                                }
                            }
                            else
                            {
                                newFile.CreateEntryFromFile(filePath, Path.GetFileName(filePath), CompressionLevel.NoCompression);
                            }
                        }
                        //}
                    } finally {
                        newFile.Dispose();
                    }
                }
                // Intializes the remaining info in the task (SentName and Size)
                j.Task.SentName = Path.GetFileName(zipName);              // name of the zipped file
                j.Task.Size     = new System.IO.FileInfo(zipName).Length; // size of the zipped file

                // Returns an iterator to the file
                jobIterator     = (JobFileIterator)getIterator(zipName);
                jobIterator.Job = j;

                //}
            } catch (IOException e) {
                if (zipName != null && File.Exists(zipName))
                {
                    File.Delete(zipName);
                }
                if (Directory.Exists(Path.Combine(zipTempFolder, j.Id)))
                {
                    Directory.Delete(Path.Combine(zipTempFolder, j.Id));
                }
                throw e;
            }

            return(jobIterator);
            // File not found
            //return null;
        }
 public void Execute(IJobExecutionContext context)
 {
     SendingJob.Execute <StringDealerPartsMaster, DealerPartsMaster>(context);
 }
Example #4
0
 public void Execute(IJobExecutionContext context)
 {
     SendingJob.Execute <StringTransactionalDemand, TransactionalDemand>(context);
 }
Example #5
0
 public void Execute(IJobExecutionContext context)
 {
     SendingJob.Execute <StringOpenPurchaseOrders, OpenPurchaseOrders>(context);
 }