Exemple #1
0
 public SshRequestAsyncHandle ExecuteAsync <T>(ISshRequest request,
                                               Action <ISshApiResponse <T>, SshRequestAsyncHandle> callback)
 {
     return(ExecuteAsync(request,
                         (response, asyncHandle) =>
                         DeserializeResponse(request, callback, response, asyncHandle)));
 }
Exemple #2
0
        private ISshApiResponse <T> Deserialize <T>(ISshRequest request, ISshApiResponse raw)
        {
            //request.OnBeforeDeserialization(raw);
            var restResponse = (ISshApiResponse <T>) new SshApiResponse <T>();

            try
            {
                restResponse         = raw.ToAsyncResponse <T>();
                restResponse.Request = request;
                if (restResponse.ErrorException == null)
                {
                    var handler = request.Handler;
                    if (handler != null)
                    {
                        restResponse.Data = handler.Deserialize <T>(raw);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                restResponse.ResponseStatus = ResponseStatus.Error;
                restResponse.ErrorMessage   = ex.Message;
                restResponse.ErrorException = ex;
            }

            return(restResponse);
        }
Exemple #3
0
        public async Task <T> RunCmd <T>(ISshRequest request)
        {
            var isRunningPidResult = await ExecuteTaskAsync <T>(request).ConfigureAwait(false);

            LogError(isRunningPidResult);
            return(isRunningPidResult.Data);
        }
        private async Task <ISshApiResponse <int> > RunInNoHup(ISshRequest request)
        {
            var noUpREquest = new NoHupRequest(request);
            var pid         = await ExecuteTaskAsync <int>(noUpREquest).ConfigureAwait(false);

            return(pid);
        }
Exemple #5
0
        private void ProcessResponse(ISshRequest request, SshApiResponse sshApiResponse,
                                     SshRequestAsyncHandle asyncHandle, Action <ISshApiResponse, SshRequestAsyncHandle> callback)
        {
            // SshApiResponse restResponse = ConvertToRestResponse(request, httpResponse);
            //var sshApiResponse = httpResponse;
            sshApiResponse.Request = request;

            callback(sshApiResponse, asyncHandle);
        }
Exemple #6
0
        //SshCommand cmd,
        private SshCommand DoAsGetAsync(SshClient ssh, ISshRequest request, Action <SshApiResponse> responseCb)
        {
            var cmd = ssh.CreateCommand(request.CommandText);

            var asyncResult =
                cmd.BeginExecute(result => { ResponseCallback(result, responseCb, cmd); },
                                 cmd);
            var b = asyncResult.IsCompleted;

            return(cmd);
        }
Exemple #7
0
        private SshRequestAsyncHandle ExecuteAsync(ISshRequest request,
                                                   Action <ISshApiResponse, SshRequestAsyncHandle> callback,
                                                   Func <SshClient, ISshRequest, Action <SshApiResponse>, SshCommand> getSshRequest)
        {
            //ISsh ssh= this.ConfigureSsh(request);
            var asyncHandle = new SshRequestAsyncHandle();
            var action      =
                (Action <SshApiResponse>)(r => ProcessResponse(request, r, asyncHandle, callback));

            //if (this.UseSynchronizationContext && SynchronizationContext.Current != null)
            //{
            //    SynchronizationContext ctx = SynchronizationContext.Current;
            //    Action<HttpResponse> cb = action;
            //    action = (Action<HttpResponse>)(resp => ctx.Post((SendOrPostCallback)(s => cb(resp)), (object)null));
            //}
            asyncHandle.SshRequest = getSshRequest(Client, request, action);
            return(asyncHandle);
        }
Exemple #8
0
        public Task <ISshApiResponse <T> > ExecuteTaskAsync <T>(ISshRequest request, CancellationToken token)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }
            var taskCompletionSource = new TaskCompletionSource <ISshApiResponse <T> >();

            try
            {
                var async = ExecuteAsync(request,
                                         (Action <ISshApiResponse <T>, SshRequestAsyncHandle>)((response, _) =>
                {
                    if (token.IsCancellationRequested)
                    {
                        taskCompletionSource.TrySetCanceled();
                    }
                    else
                    {
                        taskCompletionSource.TrySetResult(response);
                    }
                }));
                var registration = token.Register(() =>
                {
                    async.Abort();
                    taskCompletionSource.TrySetCanceled();
                });
                taskCompletionSource.Task.ContinueWith(t => registration.Dispose(),
                                                       token);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                taskCompletionSource.TrySetException(ex);
            }

            return(taskCompletionSource.Task);
        }
Exemple #9
0
        private void DeserializeResponse <T>(ISshRequest request,
                                             Action <ISshApiResponse <T>, SshRequestAsyncHandle> callback, ISshApiResponse apiResponse,
                                             SshRequestAsyncHandle asyncHandle)
        {
            ISshApiResponse <T> sshApiResponse1;

            try
            {
                sshApiResponse1 = Deserialize <T>(request, apiResponse);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                var restResponse2 = new SshApiResponse <T>();
                restResponse2.Request        = request;
                restResponse2.ResponseStatus = ResponseStatus.Error;
                restResponse2.ErrorMessage   = ex.Message;
                restResponse2.ErrorException = ex;
                sshApiResponse1 = restResponse2;
            }

            callback(sshApiResponse1, asyncHandle);
        }
Exemple #10
0
 public NoHupRequest(ISshRequest request) : this(request.CommandText)
 {
 }
 public Task <T> RunCmd <T>(ISshRequest request)
 {
     throw new NotImplementedException();
 }
Exemple #12
0
 public SshRequestAsyncHandle ExecuteAsync(ISshRequest request,
                                           Action <ISshApiResponse, SshRequestAsyncHandle> callback)
 {
     return(ExecuteAsync(request, callback,
                         DoAsGetAsync));
 }
Exemple #13
0
 public Task <ISshApiResponse <T> > ExecuteTaskAsync <T>(ISshRequest request)
 {
     return(ExecuteTaskAsync <T>(request, CancellationToken.None));
 }