Esempio n. 1
0
        public IActionResult AllNode()
        {
            try
            {
                Console.WriteLine("Get: all node...");

                var requestData = new PipeRequest
                {
                    Type  = "allNode",
                    Value = string.Empty
                };

                var pipeResult = service.RunClientPipe(JsonConvert.SerializeObject(requestData));
                var allNode    = JsonConvert.DeserializeObject <List <Node> >(pipeResult);

                if (allNode != null)
                {
                    Response.StatusCode = 200;
                    return(Json(allNode));
                }
                else
                {
                    Response.StatusCode = 400;
                    return(Json($"Error: Please supply a valid list of nodes"));
                }
            }
            catch (Exception ex)
            {
                Response.StatusCode = 500;
                return(Json($"ERROR: {ex.Message}"));
            }
        }
Esempio n. 2
0
        public IActionResult New(string sender, string recipient, double amount)
        {
            try
            {
                Console.WriteLine("Post: New Transaction...");
                if (string.IsNullOrEmpty(sender) || string.IsNullOrEmpty(recipient))
                {
                    Response.StatusCode = 400;
                    return(Json("Missing values"));
                }
                else
                {
                    var requestData = new PipeRequest
                    {
                        Type  = "newTransaction",
                        Value = JsonConvert.SerializeObject(new Transaction {
                            Sender    = sender,
                            Recipient = recipient,
                            Amount    = amount
                        })
                    };

                    Response.StatusCode = 201;
                    return(Json(service.RunClientPipe(JsonConvert.SerializeObject(requestData))));
                }
            }
            catch (Exception ex)
            {
                Response.StatusCode = 500;
                return(Json($"ERROR: {ex.Message}"));
            }
        }
Esempio n. 3
0
 protected void ImageRead(PipeRequest request, Pipe pipe)
 {
     if (Active)
     {
         try
         {
             _imageRequest = null;
             try
             {
                 if (request.Result.IsNative)
                 {
                     byte[] resultBytes = request.Result.AsByteArray;
                     _internalImage = System.Drawing.Image.FromStream(new MemoryStream(resultBytes, 0, resultBytes.Length, false, true));
                 }
                 else
                 {
                     using (Stream stream = request.Result.OpenStream())
                     {
                         MemoryStream copyStream = new MemoryStream();
                         StreamUtility.CopyStream(stream, copyStream);
                         _internalImage = System.Drawing.Image.FromStream(copyStream);
                     }
                 }
             }
             catch
             {
                 _internalImage = ImageUtility.GetErrorImage();
             }
         }
         finally
         {
             UpdateColumn();
         }
     }
 }
        /// <summary>
        ///     Begin writing to a network stream
        /// </summary>
        private Task WriteAsync <T, U>(PipeTaskState <T> state, PipeRequest <U> request, CancellationToken cancellationToken)
        {
            try
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    cancellationToken.ThrowIfCancellationRequested();
                }

                if (!_pipeClient.IsConnected)
                {
                    throw new IOException("Unable to send message, no connection established.");
                }

                if (_pipeClient.CanWrite)
                {
                    return(_pipeClient.WriteAsync(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(request)), cancellationToken).AsTask());
                }
            }
            catch (Exception ex)
            {
                _logger.Error($"{nameof(WriteAsync)}-{ex.Message}");
                state.Response = PipeResponse <T> .SetFail(ex.Message);
            }

            return(Task.CompletedTask);
        }
Esempio n. 5
0
 private void CancelImageRequest()
 {
     if (_imageRequest != null)
     {
         HostNode.Pipe.CancelRequest(_imageRequest);
         _imageRequest = null;
     }
 }
Esempio n. 6
0
 protected void ImageError(PipeRequest request, Pipe pipe, Exception exception)
 {
     // TODO: On image error, somehow update the error (warnings) list for the form
     _imageRequest = null;
     if (_internalImage != null)
     {
         _internalImage = ImageUtility.GetErrorImage();
     }
     UpdateColumn();
 }
Esempio n. 7
0
 protected void ImageError(PipeRequest request, Pipe pipe, Exception exception)
 {
     // TODO: On image error, somehow update the error (warnings) list for the form
     _imageRequest = null;
     if (_imageAspect != null)
     {
         _imageAspect.Image = ImageUtility.GetErrorImage();
     }
     if ((ImageWidth < 0) || (ImageHeight < 0))
     {
         UpdateLayout();
     }
 }
Esempio n. 8
0
 private void UpdateLoadedImage()
 {
     CancelImageRequest();
     if (_image == String.Empty || !Active)
     {
         LoadedImage = null;
     }
     else
     {
         // Queue up an asynchronous request
         _imageRequest = new PipeRequest(_image, new PipeResponseHandler(ImageRead), new PipeErrorHandler(ImageError));
         HostNode.Pipe.QueueRequest(_imageRequest);
     }
 }
        /// <summary>
        ///     Send message to server and return the response
        /// </summary>
        public async Task <PipeResponse <T> > SendMessage <T, U>(PipeRequest <U> request, CancellationToken cancellationToken)
        {
            var state = new PipeTaskState <T>();

            await WriteAsync <T, U>(state, request, cancellationToken);

            WaitForPipeDrain <T>(state);

            await ReadAsync <T>(state, cancellationToken);

            ConvertResponse <T>(state);

            return(state.Response);
        }
Esempio n. 10
0
 private void UpdateImageAspect()
 {
     CancelImageRequest();
     if (_image == String.Empty)
     {
         ClearImageAspectImage();
     }
     else
     {
         // Queue up an asynchronous request
         _imageRequest = new PipeRequest(_image, new PipeResponseHandler(ImageRead), new PipeErrorHandler(ImageError));
         HostNode.Pipe.QueueRequest(_imageRequest);
     }
 }
Esempio n. 11
0
        protected void ImageRead(PipeRequest request, Pipe pipe)
        {
            Size oldSize;

            if (Active)
            {
                if (_imageAspect.Image != null)
                {
                    oldSize = _imageAspect.Image.Size;
                }
                else
                {
                    oldSize = Size.Empty;
                }

                _imageRequest = null;
                try
                {
                    if (request.Result.IsNative)
                    {
                        byte[] resultBytes = request.Result.AsByteArray;
                        _imageAspect.Image = System.Drawing.Image.FromStream(new MemoryStream(resultBytes, 0, resultBytes.Length, false, true));
                    }
                    else
                    {
                        using (Stream stream = request.Result.OpenStream())
                        {
                            MemoryStream copyStream = new MemoryStream();
                            StreamUtility.CopyStream(stream, copyStream);
                            _imageAspect.Image = System.Drawing.Image.FromStream(copyStream);
                        }
                    }
                }
                catch
                {
                    _imageAspect.Image = ImageUtility.GetErrorImage();
                }
                if ((ImageWidth < 0) || (ImageHeight < 0) || !(_imageAspect.Image.Size != oldSize))
                {
                    UpdateLayout();
                }
            }
        }
Esempio n. 12
0
        public IActionResult Chain()
        {
            try
            {
                Console.WriteLine("Get: Full Chain...");
                var requestData = new PipeRequest
                {
                    Type  = "fullChain",
                    Value = string.Empty
                };

                Response.StatusCode = 200;
                var pipeResult = service.RunClientPipe(JsonConvert.SerializeObject(requestData));
                var result     = JsonConvert.DeserializeObject <Chain>(pipeResult);
                return(Json(result));
            }
            catch (Exception ex)
            {
                Response.StatusCode = 500;
                return(Json($"ERROR: {ex.Message}"));
            }
        }
Esempio n. 13
0
 protected void ImageRead(PipeRequest request, Pipe pipe)
 {
     if (Active)
     {
         _imageRequest = null;
         try
         {
             if (request.Result.IsNative)
             {
                 LoadedImage = ImageUtility.BitmapImageFromBytes(request.Result.AsByteArray);
             }
             else
             {
                 using (Stream stream = request.Result.OpenStream())
                     LoadedImage = ImageUtility.BitmapImageFromStream(stream);
             }
         }
         catch
         {
             LoadedImage = ImageUtility.GetErrorImage();
         }
     }
 }
Esempio n. 14
0
        static async Task Main(string[] args)
        {
            Console.WriteLine("Hello World!You are client!");

            var logger = new logger();
            var client = new PipeClient("listener", logger);

            try
            {
                var token = new CancellationToken();

                await client.Start(token);

                var request = new PipeRequest <string>()
                {
                    RequestName = "WriteCall",
                    Parameter   = "Kabooom!"
                };

                var result = await client.SendMessage <int, string>(request, token);

                Console.WriteLine("Result:");
                Console.WriteLine($"\t IS succes? {result.IsSuccess}");
                Console.WriteLine($"\t Message: {result.ErrorMessage}");
                Console.WriteLine($"\t Data: {result.Data}");

                client.Stop();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"FATAL ERROR : {ex.Message}");
                Console.WriteLine(ex.StackTrace);
            }

            Console.WriteLine("...");
            Console.ReadKey();
        }
Esempio n. 15
0
        public IActionResult Register(string address, string identifier)
        {
            try
            {
                if (!string.IsNullOrEmpty(address) && !string.IsNullOrEmpty(identifier))
                {
                    Console.WriteLine("Post: add new Node...");

                    var newNode = new Node
                    {
                        Address    = address,
                        Identifier = identifier
                    };

                    var requestData = new PipeRequest
                    {
                        Type  = "registerNode",
                        Value = JsonConvert.SerializeObject(newNode)
                    };

                    Response.StatusCode = 201;
                    var result = service.RunClientPipe(JsonConvert.SerializeObject(requestData));
                    return(Json(JsonConvert.DeserializeObject <Node>(result)));
                }
                else
                {
                    Response.StatusCode = 400;
                    return(Json("Missing values"));
                }
            }
            catch (Exception ex)
            {
                Response.StatusCode = 500;
                return(Json($"ERROR: {ex.Message}"));
            }
        }
Esempio n. 16
0
 protected void ImageError(PipeRequest request, Pipe pipe, Exception exception)
 {
     _imageRequest = null;
     LoadedImage   = ImageUtility.GetErrorImage();
     this.HandleException(exception);
 }