Exemplo n.º 1
0
        private void Initialize()
        {
            _bindingSource = new BindingSource();

            // Initializing service proxy
            var cloudProxy = new CloudServiceClient();

            try
            {
                // Getting file names from cloud server
                _fileList = new List <string>(cloudProxy.GetFileList().Select(Path.GetFileName));

                // Logging
                Log("Connected to cloud, " + _fileList.Count + " files available");

                // Setting data source for the listbox control
                _bindingSource.DataSource = _fileList;
                fileListBox.DataSource    = _bindingSource;
            }
            catch (Exception e)
            {
                Log(e.Message);
            }
            finally
            {
                // Close service proxy
                cloudProxy.Close();
            }
        }
Exemplo n.º 2
0
        private void Delete(string cloudFileName)
        {
            // Initializing service proxy
            var cloudProxy = new CloudServiceClient();

            try
            {
                if (cloudProxy.DeleteFile(cloudFileName))
                {
                    Log("File " + cloudFileName + " deleted");
                    _fileList.RemoveAt(fileListBox.SelectedIndex);
                    _bindingSource.ResetBindings(false);
                }
                else
                {
                    Log("Error deleting the file");
                }
            }
            catch (Exception exception)
            {
                Log(exception.Message);
            }
            finally
            {
                // Close service proxy
                cloudProxy.Close();
            }
        }
Exemplo n.º 3
0
        static void Main()
        {
            // Start up the Cloud Gateway!
            using (var cloud = new CloudServiceClient())
            {
                // Formulate our Request
                var request = new Request
                {
                    Argument = 15,
                    Services = new[]
                    {
                        new ServiceCall
                        {
                            Name = "Decrementer",
                            Services = new[]
                            {
                                new ServiceCall { Name = "Incrementer" },
                                new ServiceCall { Name = "Incrementer" },
                                new ServiceCall { Name = "Incrementer" },
                                new ServiceCall { Name = "Incrementer" },
                                new ServiceCall { Name = "Incrementer" },
                                new ServiceCall { Name = "Incrementer" },
                                new ServiceCall { Name = "Incrementer" },
                            },
                        },
                        new ServiceCall { Name = "Decrementer" },
                        new ServiceCall { Name = "Decrementer" },
                        new ServiceCall { Name = "Decrementer" },
                    },
                };

                // Assign the Endpoint Addresses (avoids redundancy -- this could easily be its own Service)
                request.Services.ToList().ForEach(AssignAddress);

                // Get a Response from the Gateway
                var response = cloud.Execute(request);

                if (response == null)
                {
                    Console.WriteLine("Response is NULL!!!");
                    Console.ReadLine();
                    return;
                }

                Console.WriteLine("ReturnObject is {0}", response.ReturnObject);

                // Find out which services ran
                if (response.ServicesRan != null && response.ServicesRan.Length > 0)
                {
                    response.ServicesRan.ToList().ForEach(s => Console.WriteLine("Ran on Service: {0}", s));
                }

                cloud.Close();
            }

            Console.ReadLine();
        }
Exemplo n.º 4
0
        private void Upload(System.ComponentModel.DoWorkEventArgs e)
        {
            // Initializing service proxy
            var cloudProxy = new CloudServiceClient();

            try
            {
                // Creating fileInfo and fileName
                var fileInfo = new FileInfo(_localFilePath);
                var fileName = fileInfo.Name;

                // Uploading file
                using (var stream = new FileStream(_localFilePath, FileMode.Open, FileAccess.Read))
                {
                    using (var uploadStream = new ProgressStream(stream))
                    {
                        // Adding event handler to uploadStream
                        uploadStream.ProgressChanged += uploadStream_ProgressChanged;

                        // Invoking service method
                        cloudProxy.UploadFile(ref fileName, fileInfo.Length, uploadStream);

                        // Check for cancellation
                        if (backgroundWorker.CancellationPending)
                        {
                            e.Cancel       = true;
                            _cloudFileName = fileName;
                            return;
                        }

                        // Adding file name to list instead of invoking GetFileList
                        _fileList.Add(fileName);
                        _bindingSource.ResetBindings(false);
                    }
                }
            }
            catch (Exception exception)
            {
                throw new ArgumentException(exception.Message);
            }
            finally
            {
                // Closing service proxy
                cloudProxy.Close();
            }
        }
Exemplo n.º 5
0
        private void backgroundWorker_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
        {
            // Operation was cancelled
            if (e.Cancelled)
            {
                // If uploading delete file server-side
                if (_upload)
                {
                    // Initializing service proxy
                    var cloudProxy = new CloudServiceClient();

                    // Deleting file server-side
                    cloudProxy.DeleteFile(_cloudFileName);

                    // Closing service proxy
                    cloudProxy.Close();
                }
                // If downloading delete file locally
                else
                {
                    if (File.Exists(_localFilePath))
                    {
                        File.Delete(_localFilePath);
                    }
                }
            }
            // Error
            else if (e.Error != null)
            {
                Close();
                MessageBox.Show("There has been an error in the process: " + e.Error.Message,
                                "Error", MessageBoxButtons.OK);
            }
            // Operation completed
            else
            {
                // Close form when work completes
                Close();
            }
        }
Exemplo n.º 6
0
        private void Download(System.ComponentModel.DoWorkEventArgs e)
        {
            // Initializing service proxy
            var cloudProxy = new CloudServiceClient();

            try
            {
                // Initializing client-side decryption
                var clientCypher = new XTEA();

                // Getting decryption key
                clientCypher.SetKey(Encoding.ASCII.GetBytes(cloudProxy.GetKey()));

                // Initializing stream from service
                var length = cloudProxy.DownloadFile(ref _cloudFileName, out var inputStream);

                // Write stream to disk
                using (var writeStream = new FileStream(_localFilePath, FileMode.CreateNew, FileAccess.Write))
                {
                    // Initializing buffer
                    var buffer = new byte[ChunkSize];

                    // Initializing boolean determining whether it's the last chunk
                    var lastChunk = false;

                    do
                    {
                        // Read bytes from input stream
                        var bytesRead = inputStream.Read(buffer, 0, ChunkSize);
                        if (bytesRead == 0)
                        {
                            break;
                        }

                        // Check if it is the last chunk
                        if (bytesRead < ChunkSize)
                        {
                            var temp = new byte[bytesRead];
                            Array.Copy(buffer, temp, bytesRead);
                            buffer    = temp;
                            lastChunk = true;
                        }

                        // Decrypt data from the buffer
                        var decryptedBuffer = clientCypher.Decrypt(buffer);

                        // Write bytes to output stream
                        writeStream.Write(decryptedBuffer, 0, decryptedBuffer.Length);

                        // Update progress bar
                        if (lastChunk)
                        {
                            backgroundWorker.ReportProgress(100);
                        }
                        else
                        {
                            backgroundWorker.ReportProgress((int)(writeStream.Position * 100 / length));
                        }

                        // Check for cancellation
                        if (!backgroundWorker.CancellationPending)
                        {
                            continue;
                        }

                        e.Cancel = true;
                        inputStream.Dispose();
                        return;
                    } while (!lastChunk);

                    writeStream.Close();
                }

                // Deallocate stream
                inputStream.Dispose();
            }
            catch (Exception exception)
            {
                throw new ArgumentException(exception.Message);
            }
            finally
            {
                // Close service proxy
                cloudProxy.Close();
            }
        }