Exemple #1
0
        //       public async Task<string> MyMainAsync(int i, string h, string u, string p, string f, CancellationToken cancel, IProgress<string> progress)
        public async Task <string> MyMainAsync(int i, FTPConfiguration ftpconfiguration, CancellationToken cancel, IProgress <string> progress)
        {
            using (WebClient client = new WebClient())
            {
                try
                {
                    //--- Record the start time---
                    startTime = DateTime.Now;
                    // First create a Stopwatch instance
                    stopwatch = new Stopwatch();
                    ctime1    = ctime2 = ctime3 = ctime4 = ctime5 = ptime1 = ptime2 = ptime3 = ptime4 = ptime5 = 0; // zero out each run
                    // Begin the stopwatch
                    stopwatch.Start();
                    cancel.ThrowIfCancellationRequested();
                    if (cancel.IsCancellationRequested)
                    {
                        client.CancelAsync();
                    }
                    // Get the object used to communicate with the server.
                    //                WebClient client = new WebClient();
                    client.Proxy       = null;
                    client.BaseAddress = "ftp://" + ftpconfiguration.Host;
// TODO: put back in /demo/ when doing hardwired for real
//                    client.BaseAddress = "ftp://" + ftpconfiguration.Host + "/demo/";

                    CredentialCache   cache      = new CredentialCache();
                    NetworkCredential credential = new NetworkCredential(ftpconfiguration.Username, ftpconfiguration.Password);

                    client.Credentials = credential;

                    if (i == 0)
                    {
                        client.UploadProgressChanged += new UploadProgressChangedEventHandler(client_UploadProgressChanged1);
                    }
                    else if (i == 1)
                    {
                        client.UploadProgressChanged += new UploadProgressChangedEventHandler(client_UploadProgressChanged2);
                    }
                    else if (i == 2)
                    {
                        client.UploadProgressChanged += new UploadProgressChangedEventHandler(client_UploadProgressChanged3);
                    }
                    else if (i == 3)
                    {
                        client.UploadProgressChanged += new UploadProgressChangedEventHandler(client_UploadProgressChanged4);
                    }
                    else if (i == 4)
                    {
                        client.UploadProgressChanged += new UploadProgressChangedEventHandler(client_UploadProgressChanged5);
                    }

                    //client.UploadProgressChanged += new UploadProgressChangedEventHandler(client_UploadProgressChanged);
                    //client.UploadFileCompleted += new UploadFileCompletedEventHandler(client_UploadFileCompleted);
                    Uri remoteFile;
                    Uri.TryCreate(ftpconfiguration.Filename, System.UriKind.Relative, out remoteFile);

                    FileInfo fi = new FileInfo(@demoDir + ftpconfiguration.Filename);
                    //fileSize = fi.Length;
                    //                await client.DownloadFileTaskAsync(remoteFile, "/temp/demo/RRVideo.mp4");

                    Task uploadTask = client.UploadFileTaskAsync(remoteFile, @demoDir + ftpconfiguration.Filename);
                    //string sNull = null;
                    //client.UploadFileAsync(remoteFile, sNull, @demoDir + f, i.ToString());
                    //client.UploadFile(remoteFile, @demoDir + f);

                    //if (i == 3)
                    //{
                    //   await MyDelayAsync(i);
                    //}
                    await uploadTask;
                    ////update the progress on UI
//                  label1.Invoke((Action)delegate { ReportProgress2(1, i, Convert.ToString(fi.Length) + " bytes sent", 0); });

                    if (i == 0)     // stop Elapsed timer when last Task done.
                    {
                        label1.Invoke((Action) delegate { timer1.Change(Timeout.Infinite, Timeout.Infinite); });
                    }


                    if (cancel.IsCancellationRequested)
                    {
                        client.CancelAsync();
                    }
                    //showTime();
                }
                catch (OperationCanceledException oce)
                {
                    client.CancelAsync();
                    //label1.Text = "Cancelled";
                }
                catch (Exception e)
                {
                    ReportError(e.Message, i);
                }
                return("end");
            }
        }
Exemple #2
0
        private void getData()
        {
            //if (!Directory.Exists(demoDir))
            //{
            Directory.CreateDirectory(demoDir);
            //}

            bool allSimMode = true;     // if all FTPConfiguratios at set to Simulate filesize from Admin screen (not any actual filesize)

            // -------------------- Connecting To DB --------------------
            using (mConn = new SQLiteConnection("Data Source=" + mDbPath))
            {
                // ----------------- Opening The Connection -----------------
                mConn.Open();
                using (SQLiteCommand fmd = mConn.CreateCommand())
                {
                    try
                    {
                        // Get FTPConfiguration data
                        fmd.CommandText = @"SELECT ID, Name, Description, Host, Username, Password, Duration, Bandwidth, Simulate, Filename FROM [FTPConfiguration]";
                        fmd.CommandType = CommandType.Text;
                        SQLiteDataReader r = fmd.ExecuteReader();
                        while (r.Read())
                        {
                            FTPConfiguration f = new FTPConfiguration(Convert.ToInt16(r["ID"]),
                                                                      Convert.ToString(r["Name"]),
                                                                      Convert.ToString(r["Description"]),
                                                                      Convert.ToString(r["Host"]),
                                                                      Convert.ToString(r["Username"]),
                                                                      Convert.ToString(r["Password"]),
                                                                      r["Duration"] == DBNull.Value ? 0 : Convert.ToInt64(r["Duration"]),
                                                                      Convert.ToString(r["Bandwidth"]),
                                                                      r["Simulate"] == DBNull.Value ? false : Convert.ToBoolean(r["Simulate"]),
                                                                      Convert.ToString(r["Filename"]));

                            ftpconfigurations.Add(f);      // add to list
                            if (f.Simulate == false)
                            {
                                allSimMode = false;
                            }
                        }

                        r.Close();
                        fmd.Cancel();

                        // Get FTPDemo data
                        fmd.CommandText = @"SELECT ID, Name, Description, Filesize, OverheadPct, AutoRestart, DelayStart, ThemeCode, IndustryCode, ChangeIndustryOnRestart FROM [FTPDemo]";
                        fmd.CommandType = CommandType.Text;
                        r = fmd.ExecuteReader();
                        //SQLiteDataReader r = fmd.ExecuteReader();
                        while (r.Read())
                        {
                            ftpdemo = new FTPDemo(Convert.ToInt16(r["ID"]),
                                                  Convert.ToString(r["Name"]),
                                                  Convert.ToString(r["Description"]),
                                                  r["Filesize"] == DBNull.Value ? 0 : Convert.ToInt64(r["Filesize"]),
                                                  r["OverheadPct"] == DBNull.Value ? 0 : Convert.ToInt16(r["OverheadPct"]),
                                                  r["AutoRestart"] == DBNull.Value ? 0 : Convert.ToInt16(r["AutoRestart"]),
                                                  r["DelayStart"] == DBNull.Value ? 0 : Convert.ToInt16(r["DelayStart"]),
                                                  r["ThemeCode"] == DBNull.Value ? 0 : Convert.ToInt16(r["ThemeCode"]),
                                                  r["IndustryCode"] == DBNull.Value ? 0 : Convert.ToInt16(r["IndustryCode"]),
                                                  r["ChangeIndustryOnRestart"] == DBNull.Value ? false : Convert.ToBoolean(r["ChangeIndustryOnRestart"])
                                                  );
                        }

                        r.Close();
                        fmd.Cancel();

                        // Get Industry data
                        fmd.CommandText = @"SELECT ID, Name, Description, Occupation, Image, HourlyRate, PeopleCount FROM [Industry] Where [ID] = @IndustryCode";
                        fmd.CommandType = CommandType.Text;
                        fmd.Parameters.Add(new SQLiteParameter("@IndustryCode", ftpdemo.IndustryCode));
                        //                        fmd.Parameters.Add(new SQLiteParameter("@IndustryCode", SqlDbType.Int) { Value = ftpdemo.IndustryCode });
                        r = fmd.ExecuteReader();
                        //SQLiteDataReader r = fmd.ExecuteReader();
                        while (r.Read())
                        {
                            industry = new Industry(Convert.ToInt16(r["ID"]),
                                                    Convert.ToString(r["Name"]),
                                                    Convert.ToString(r["Description"]),
                                                    Convert.ToString(r["Occupation"]),
                                                    Convert.ToString(r["Image"]),
                                                    r["HourlyRate"] == DBNull.Value ? 0 : Convert.ToInt16(r["HourlyRate"]),
                                                    r["PeopleCount"] == DBNull.Value ? 0 : Convert.ToInt16(r["PeopleCount"])
                                                    );

                            byte[] imgdata = (byte[])r["Image"];
                            if (imgdata != null)
                            {
                                File.WriteAllBytes(demoDir + "FTPIndustry.jpg", imgdata);
                            }

                            ////                          pictureBox2.Image = ByteToImage((byte[])r["Image"]);

                            //// Converting An Image To Array Of Bytes
                            //ImageConverter converter = new ImageConverter();
                            //pictureBox2.Image = (Bitmap)converter.ConvertTo((byte[])r["Image"], typeof(Bitmap));
                        }
                        r.Close();
                        fmd.Cancel();

                        //                        // Get Industry data
                        //                        fmd.CommandText = @"SELECT Image FROM [Industry] Where [ID] = @IndustryCode";
                        //                        fmd.CommandType = CommandType.Text;
                        //                        fmd.Parameters.Add(new SQLiteParameter("@IndustryCode", ftpdemo.IndustryCode));
                        //                        //                        fmd.Parameters.Add(new SQLiteParameter("@IndustryCode", SqlDbType.Int) { Value = ftpdemo.IndustryCode });
                        //                        r = fmd.ExecuteReader();
                        //                        //SQLiteDataReader r = fmd.ExecuteReader();
                        //                        while (r.Read())
                        //                        {
                        //                            pictureBox2.Image = ByteToImage(GetBytes(r));
                        //                        }
                        //                        r.Close();
                        //                        fmd.Cancel();
                        // Get Theme data
                        fmd.CommandText = @"SELECT ID, Name, Description, Logo, BackgroundColor, ProgressbarColor, TextColor FROM [Theme] Where [ID] = @ThemeCode";
                        fmd.CommandType = CommandType.Text;
                        fmd.Parameters.Add(new SQLiteParameter("@ThemeCode", ftpdemo.ThemeCode));
                        r = fmd.ExecuteReader();


                        //SQLiteDataReader r = fmd.ExecuteReader();
                        while (r.Read())
                        {
                            theme = new Theme(Convert.ToInt16(r["ID"]),
                                              Convert.ToString(r["Name"]),
                                              Convert.ToString(r["Description"]),
                                              Convert.ToString(r["Logo"]),
                                              Convert.ToString(r["BackgroundColor"]),
                                              Convert.ToString(r["ProgressbarColor"]),
                                              Convert.ToString(r["TextColor"])
                                              );
                            pictureLogo.Image = ByteToImage((byte[])r["Logo"]);
                            //pictureLogo.ImageLocation = @"C:\\demo\\image.png";
                        }
                        r.Close();
                        fmd.Cancel();
                    }
                    catch (Exception ex)
                    {
                        ReportError(ex.Message, 0);
                    }
                } // end of using command
            }     // end of using connection

            FileInfo myFile = new FileInfo(@demoDir + ftpconfigurations[0].Filename);

            // if file does not exist or AllSimMode use file size from Admin screen (FTPDemo)
            if (!myFile.Exists || allSimMode)
            {
                if (ftpdemo.Description.Length > 0)
                {
                    lFileSize.Text = ftpdemo.Description + " " + ftpdemo.Filesize.ToString() + "MB";
                }
                else
                {
                    lFileSize.Text = "This Demonstration shows five file transfers at five different speeds. The file size is " + ftpdemo.Filesize.ToString() + "MB";
                }
            }
            else
            {
                if (ftpdemo.Description.Length > 0)
                {
                    lFileSize.Text = ftpdemo.Description + Utils.BytesToString(myFile.Length);
                }
                else
                {
                    lFileSize.Text = "This Demonstration shows five file transfers at five different speeds. The file size is " + Utils.BytesToString(myFile.Length);
                }
            }

            //pictureBox1.Image = theme.Logo;
        }