Ejemplo n.º 1
0
        public Task <T> Invoke <T>(ServiceMetaData metaData)
        {
            if (string.IsNullOrEmpty(this._baseurl))
            {
                throw new Exception("No Base Url Setup");
            }
            Url    fUrl = null;
            string url  = string.Empty;

            url = this._baseurl;

            if (metaData.PathSegment != null)
            {
                //fUrl = this._baseurl
                //.AppendPathSegment(metaData.PathSegment);
                url = string.Format("{0}/{1}", this._baseurl, metaData.PathSegment.ToString());
            }

            if (metaData.PathSegments != null)
            {
                foreach (var seg in metaData.PathSegments)
                {
                    var token = string.Empty;
                    if (seg is DateTime)
                    {
                        token = System.Net.WebUtility.UrlEncode(((DateTime)seg).ToString("yyyy-MM-dd"));
                    }
                    else
                    {
                        token = System.Net.WebUtility.UrlEncode(seg.ToString());
                    }
                    url = string.Format("{0}/{1}", url, token);
                }
                //.AppendPathSegments(metaData.PathSegments.ToArray());
            }
            fUrl = url;
            if (metaData.QueryParameters != null)
            {
                fUrl.SetQueryParams(metaData.QueryParameters);
            }

            if (metaData.Method == ServiceMethod.GET)
            {
                return(fUrl.GetJsonAsync <T>());
            }
            else if (metaData.Method == ServiceMethod.POST)
            {
                return(fUrl.PostJsonAsync(metaData.PayLoad).ReceiveJson <T>());
            }
            else
            {
                throw new NotImplementedException("The http method is not implemented");
            }
        }
Ejemplo n.º 2
0
        public void Start()
        {
            _isRunning = true;
            var foundJob = true;

            while (foundJob && _isRunning)
            {
                var jobFound = _storageRepository.GetInstancesForLinkSend().GetAwaiter().GetResult();
                if (jobFound.Count > 0)
                {
                    try
                    {
                        foreach (var instance in jobFound)
                        {
                            try
                            {
                                var filePath = this._pathFinder.GetStoragePathForEncrypted(instance);
                                var sendLink = this._routeFinder.LinkRoute(instance);
                                if (sendLink == null)
                                {
                                    throw new Exception("No Link Client Found");
                                }
                                this._serviceInvoker.ConfigContext(sendLink.UrlEndPoint, HUBURL);
                                var serviceMetadata = new ServiceMetaData()
                                {
                                    Method     = ServiceMethod.POST,
                                    PayLoad    = instance,
                                    PayLoadKey = "instance"
                                };
                                var response = this._serviceInvoker.InvokeUploadFile(serviceMetadata, filePath).GetAwaiter().GetResult();
                                instance.isLinkedTransmitted = true;
                                this._storageRepository.UpdateInstance(instance).GetAwaiter().GetResult();
                            }
                            catch (Exception dEncry)
                            {
                                instance.isLinkedTransmitted = false;
                                instance.isLinkTramitFail    = true;
                                instance.ErrorMessage        = dEncry.Message;
                                this._storageRepository.UpdateInstance(instance);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                }
                else
                {
                    foundJob = false;
                    System.Threading.Thread.Sleep(1000);
                    foundJob = true;
                }
            }
        }
Ejemplo n.º 3
0
        public Task <HttpResponseMessage> InvokeUploadFile(ServiceMetaData metaData, string filePath)
        {
            if (string.IsNullOrEmpty(this._baseurl))
            {
                throw new Exception("No Base Url Setup");
            }
            Url    fUrl = null;
            string url  = string.Empty;

            url = this._baseurl;

            if (metaData.PathSegment != null)
            {
                //fUrl = this._baseurl
                //.AppendPathSegment(metaData.PathSegment);
                url = string.Format("{0}/{1}", this._baseurl, metaData.PathSegment.ToString());
            }

            if (metaData.PathSegments != null)
            {
                foreach (var seg in metaData.PathSegments)
                {
                    var token = string.Empty;
                    if (seg is DateTime)
                    {
                        token = System.Net.WebUtility.UrlEncode(((DateTime)seg).ToString("yyyy-MM-dd"));
                    }
                    else
                    {
                        token = System.Net.WebUtility.UrlEncode(seg.ToString());
                    }
                    url = string.Format("{0}/{1}", url, token);
                }
                //.AppendPathSegments(metaData.PathSegments.ToArray());
            }
            fUrl = url;
            if (metaData.QueryParameters != null)
            {
                fUrl.SetQueryParams(metaData.QueryParameters);
            }

            if (metaData.Method == ServiceMethod.POST)
            {
                return(fUrl.PostMultipartAsync(mp => mp.AddJson(metaData.PayLoadKey, metaData.PayLoad)
                                               .AddFile("file1", filePath)
                                               ));
            }
            else
            {
                throw new NotImplementedException("The http method is not implemented");
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="svcParentDirectoryPath"></param>
        /// <returns></returns>
        private List <ServiceMetaData> GetMattimonServices()
        {
            String svcParentDirectoryPath = MattimonAgentLibrary.Tools.RegistryTools.GetInstallLocationByDisplayName(MattimonAgentLibrary.Static.MattimonRegistrySubkeyNames.DisplayName); //System.IO.Directory.GetParent(System.Reflection.Assembly.GetEntryAssembly().Location.ToString()).FullName;

            List <ServiceMetaData> foundControllers = null;

            try
            {
                ManagementObjectSearcher   searcher   = new ManagementObjectSearcher("SELECT * FROM Win32_Service");
                ManagementObjectCollection collection = searcher.Get();
                foundControllers = new List <ServiceMetaData>();

                foreach (ManagementObject obj in collection)
                {
                    string name     = obj["Name"] as string;
                    string pathName = obj["PathName"] as string;

                    if (!System.IO.File.Exists(pathName))
                    {
                        continue;
                    }

                    string parent = System.IO.Directory.GetParent(pathName).FullName;
                    if (parent != svcParentDirectoryPath)
                    {
                        continue;
                    }

                    try
                    {
                        ServiceController svcCtrl = ServiceController.GetServices(System.Environment.MachineName).
                                                    Where(svc => svc.ServiceName == name).FirstOrDefault();

                        if (svcCtrl != null)
                        {
                            ServiceMetaData data = new ServiceMetaData(svcCtrl, pathName);
                            foundControllers.Add(data);
                        }
                    }
                    catch { }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message + "\n\n" + ex.StackTrace,
                                Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            return(foundControllers);
        }
Ejemplo n.º 5
0
        public void NewIncomingFile(IncomingFileEventArgs evtArgs)
        {
            this._serviceInvoker.ConfigContext(HUBURL);
            var serviceMetadata = new ServiceMetaData()
            {
                Method       = ServiceMethod.GET,
                PathSegments = new List <object>()
                {
                    "addnewfile"
                }
            };

            this._serviceInvoker.Invoke <string>(serviceMetadata).GetAwaiter().GetResult();
            //this.IncomingFile.Invoke(this, evtArgs);
        }
        /// <summary>
        /// Handles the command.
        /// </summary>
        /// <param name="cmd">The cmd<see cref="Command"/></param>
        /// <param name="streamId">The streamId<see cref="string"/></param>
        /// <param name="cloudId">The cloud identifier.</param>
        /// <returns>a task</returns>
        private async Task <GrpcResponse> HandleCommand(NetworkUser user, Command cmd, string streamId, string cloudId)
        {
            GrpcResponse result = Ok();

            switch (cmd.Topic)
            {
            case StaticCommandKeys.Connect:
                user = cmd.Data.FirstOrDefault()?.CastToModel <NetworkUser>();
                if (user == null)
                {
                    throw new Exception("Invalid connection data.");
                }

                return(await this.tokenObserver.Authenticate(user.LoginName, user.PasswordHash));

            case StaticCommandKeys.ServiceMetaData:
                ServiceMetaData metaData = cmd.Data.FirstOrDefault()?.CastToModel <ServiceMetaData>();
                if (metaData == null)
                {
                    throw new Exception("Invalid data for service meta data information.");
                }

                if (serviceMetaData.ContainsKey(metaData.ServiceAddress))
                {
                    ServiceMetaData s;
                    while (!serviceMetaData.TryRemove(metaData.ServiceAddress, out s))
                    {
                        await Task.Delay(1);
                    }
                }

                while (!serviceMetaData.TryAdd(metaData.ServiceAddress, metaData))
                {
                    await Task.Delay(1);
                }

                Log("Added service metadata for service " + streamId, LogLevel.Debug);
                break;

            case StaticCommandKeys.Subscribe:
                try
                {
                    SubscriptionMessage msg = cmd.Data.FirstOrDefault()?.CastToModel <SubscriptionMessage>();
                    if (msg == null)
                    {
                        throw new Exception("Invalid data for the subscription message.");
                    }

                    Log("Client subscribed to Topic: " + msg.Topic, LogLevel.Debug);
                    CommonSubscriptionHandler.SubscribeToTopic(streamId, msg.Topic.ToString());
                }
                catch (Exception ex)
                {
                    return(BadRequest(ex));
                }

                break;

            case StaticCommandKeys.Unsubscribe:
                try
                {
                    SubscriptionMessage msg = cmd.Data.FirstOrDefault()?.CastToModel <SubscriptionMessage>();
                    if (msg == null)
                    {
                        throw new Exception("Invalid data for the subscription message.");
                    }

                    Log("Client unsubscribed from Topic: " + msg.Topic, LogLevel.Debug);
                    CommonSubscriptionHandler.UnsubscribeFromTopic(streamId, msg.Topic.ToString());
                }
                catch (Exception ex)
                {
                    return(BadRequest(ex));
                }

                break;

            default:

                string topic = cmd.Topic;
                if (!string.IsNullOrEmpty(cmd.TargetId))
                {
                    topic = topic + "/" + cmd.TargetId;
                }

                CommonSubscriptionHandler.ForwardByTopic(cmd, topic);
                break;
            }

            return(result);
        }