public async Task <IHttpActionResult> CreateFile()
        {
            var cookies = Request.Headers.GetCookies("session").FirstOrDefault();

            if (cookies == null)
            {
                return(JsonResponseEx.Create(HttpStatusCode.Unauthorized, new { message = "Session cookie is missing." }));
            }
            var sessionCookieValue = cookies["session"].Values;
            var account            = await AuthorizationController.AccountFromCookie(sessionCookieValue, false);

            if (null == account)
            {
                return(JsonResponseEx.Create(HttpStatusCode.Unauthorized, new { message = "Failed to locate an account for the auth cookie." }));
            }

            var client = await SharedConfig.GetOneDriveClientForAccountAsync(account);

            var item = await client.Drive.Special[account.SourceFolder].ItemWithPath("test_file.txt").Content.Request().PutAsync <Item>(this.TestFileStream());

            await AzureStorage.InsertActivityAsync(
                new Activity
            {
                UserId  = account.Id,
                Type    = ActivityEventCode.FileChanged,
                Message = string.Format("Creating test file test_file.txt with resource id: {0}", item.Id)
            });

            return(JsonResponseEx.Create(HttpStatusCode.OK, item));
        }
        public void Recursive()
        {
            var config = new Config()
            {
                Info = new PackageInfo("MyMod", "id", new SemVer.Version("0.1.0"))
            };
            var dep = new Dependency("id", new SemVer.Range("^0.1.0"));

            config.Dependencies.Add(dep);
            var depConfig = new SharedConfig {
                Config = new Config()
                {
                    Info = new PackageInfo("Cool Name", "id", new SemVer.Version("0.1.0"))
                }
            };

            var configProvider = Utils.GetConfigProvider(config);
            var uriHandler     = Utils.GetUriHandler(new Dictionary <Dependency, SharedConfig> {
                { dep, depConfig }
            });

            var restorer = new RestoreHandler(configProvider.Object, uriHandler.Object);

            // Should throw a recursive exception (id cannot include id)
            Assert.Throws <DependencyException>(() => restorer.CollectDependencies());
            // Should never have made any GetConfig calls
            uriHandler.Verify(mocks => mocks.GetSharedConfig(It.IsAny <RestoredDependencyPair>()), Times.Never);
        }
예제 #3
0
        /// <summary>
        /// Register next settings assembly.
        /// </summary>
        /// <param name="assemblyIndex"></param>
        public static void RegisterSettingsAssembly(uint assemblyIndex)
        {
            // Locate the settings assembly config.
            //
            var assemblyConfigKey = new Core.Internal.RegisteredSettingsAssemblyConfig.CodegenKey
            {
                AssemblyIndex = assemblyIndex,
            };

            SharedConfig <MlosProxyInternal.RegisteredSettingsAssemblyConfig> assemblySharedConfig = MlosContext.SharedConfigManager.Lookup(assemblyConfigKey);

            if (assemblySharedConfig.HasSharedConfig)
            {
                MlosProxyInternal.RegisteredSettingsAssemblyConfig assemblyConfig = assemblySharedConfig.Config;

                // Try to load assembly from the agent folder.
                //
                string applicationFolderPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                string assemblyFilePath      = Path.Combine(applicationFolderPath, assemblyConfig.AssemblyFileName.Value);

                if (!File.Exists(assemblyFilePath))
                {
                    // Try to load assembly from the target folder.
                    //
                    applicationFolderPath = Path.GetDirectoryName(assemblyConfig.ApplicationFilePath.Value);
                    assemblyFilePath      = Path.Combine(applicationFolderPath, assemblyConfig.AssemblyFileName.Value);
                }

                Assembly assembly = Assembly.LoadFrom(assemblyFilePath);

                RegisterAssembly(assembly, dispatchTableBaseIndex: assemblyConfig.DispatchTableBaseIndex);
            }
        }
        public void DoNotMatchConfig()
        {
            // Collect nested dependencies that are not collapsible, should cause a DependencyException
            // Collect nested dependencies that are collapsible, should result in 1
            var config = new Config()
            {
                Info = new PackageInfo("MyMod", "asdf", new SemVer.Version("0.1.0"))
            };
            var dep      = new Dependency("id", new SemVer.Range("^0.1.0"));
            var otherDep = new Dependency("needed", new SemVer.Range("^0.1.4"));

            config.Dependencies.Add(dep);
            config.Dependencies.Add(otherDep);
            var depConfig = new SharedConfig {
                Config = new Config()
                {
                    Info = new PackageInfo("Cool Name", "id", new SemVer.Version("0.1.0"))
                }
            };
            var innerDep = new Dependency("needed", new SemVer.Range("0.1.0"));

            depConfig.Config.Dependencies.Add(innerDep);
            var innerDepConfig = new SharedConfig {
                Config = new Config {
                    Info = new PackageInfo("Needed by both", "needed", new SemVer.Version("0.1.4"))
                }
            };

            var configProvider = Utils.GetConfigProvider(config);
            var uriHandler     = Utils.GetUriHandler(new Dictionary <Dependency, SharedConfig>
            {
                { dep, depConfig }, { otherDep, innerDepConfig }, { innerDep, innerDepConfig }
            });

            var innerRestorer = new RestoreHandler(Utils.GetConfigProvider(depConfig.Config).Object, uriHandler.Object);

            // Should throw
            Assert.Throws <DependencyException>(() => innerRestorer.CollectDependencies());
            // Assume it DID NOT restore

            var restorer = new RestoreHandler(configProvider.Object, uriHandler.Object);
            // This should NOT throw, since the version we test here actually exists and is satisfiable.
            // and our dependency config DOES NOT contain a restored dependency that causes any problems.
            // TODO: We could add a test here to test for when one matches but they other doesn't too.
            var deps = restorer.CollectDependencies();

            Assert.Collection(deps,
                              kvp =>
            {
                Assert.True(kvp.Key.Dependency.Id == dep.Id);
                Assert.True(kvp.Key.Dependency.VersionRange == dep.VersionRange);
                Assert.True(kvp.Value.Config.Info.Version == depConfig.Config.Info.Version);
            },
                              kvp =>
            {
                Assert.True(kvp.Key.Dependency.Id == otherDep.Id);
                Assert.True(kvp.Key.Dependency.VersionRange == otherDep.VersionRange);
                Assert.True(kvp.Value.Config.Info.Version == innerDepConfig.Config.Info.Version);
            });
        }
예제 #5
0
        protected void Run(Action <IContainer> action)
        {
            var builder = new ContainerBuilder();

            builder.RegisterAppulateModules();
            builder.RegisterModule <PersistenceRegistrationModule>();
            builder.RegisterModule <MessageQueueRegistrationModule>();
            builder.RegisterModule <ProgramSubsystemModule>();

            builder.Register(ctx => AutoMapperConfiguration.Create());
            builder.Register(ctx => ctx.Resolve <MapperConfiguration>().CreateMapper()).As <IMapper>().InstancePerLifetimeScope();

            builder.Register(c => {
                var sharedConfig = new SharedConfig(
                    connectionString: c.Resolve <IDatabaseSettings>().GetDatabaseConnectionString(),
                    storageFolder: c.Resolve <IStorageSettings>().StorageFolder,
                    rabbitMqOptions: new RabbitMqOptions());
                return(new Startup(sharedConfig));
            }).AsSelf().SingleInstance();

            builder.RegisterType <EmptySecurityContext>().As <ISecurityContext>();
            using (IContainer container = builder.Build()) {
                Factory.InitComponentsRegistry(new ComponentsRegistry(container));
                Settings.Initialize(new LocalSettings());
                container.Resolve <Startup>().Init();
                action(container);
            }
        }
예제 #6
0
        /// <summary>
        /// Tries to open a shared memory map.
        /// </summary>
        /// <param name="globalMemoryRegion"></param>
        /// <param name="memoryRegionId"></param>
        /// <param name="sharedMemoryMapView"></param>
        /// <returns></returns>
        public static bool TryOpenExisting(
            this GlobalMemoryRegion globalMemoryRegion,
            MlosInternal.MemoryRegionId memoryRegionId,
            out SharedMemoryMapView sharedMemoryMapView)
        {
            MlosInternal.RegisteredMemoryRegionConfig.CodegenKey registeredMemoryLookupKey = default;
            registeredMemoryLookupKey.MemoryRegionId = memoryRegionId;

            // Locate shared memory region config.
            //
            SharedConfig <MlosProxyInternal.RegisteredMemoryRegionConfig> registeredMemoryRegionSharedConfig =
                SharedConfigManager.Lookup(globalMemoryRegion.SharedConfigDictionary, registeredMemoryLookupKey);

            if (!registeredMemoryRegionSharedConfig.HasSharedConfig)
            {
                sharedMemoryMapView = null;
                return(false);
            }

            // Config exists, create a shared config memory region.
            //
            MlosProxyInternal.RegisteredMemoryRegionConfig registeredMemoryRegionConfig = registeredMemoryRegionSharedConfig.Config;

            sharedMemoryMapView = SharedMemoryMapView.OpenExisting(
                registeredMemoryRegionConfig.MemoryMapName.Value,
                registeredMemoryRegionConfig.MemoryRegionSize);

            return(true);
        }
예제 #7
0
        /// <summary>
        /// Tries to open a named event.
        /// </summary>
        /// <param name="globalMemoryRegion"></param>
        /// <param name="memoryRegionId"></param>
        /// <param name="namedEvent"></param>
        /// <returns></returns>
        public static bool TryOpenExisting(
            this GlobalMemoryRegion globalMemoryRegion,
            MlosInternal.MemoryRegionId memoryRegionId,
            out NamedEvent namedEvent)
        {
            MlosInternal.RegisteredNamedEventConfig.CodegenKey namedEventLookupKey = default;
            namedEventLookupKey.MemoryRegionId = memoryRegionId;

            // Locate named event config.
            //
            SharedConfig <MlosProxyInternal.RegisteredNamedEventConfig> registeredNamedEventSharedConfig =
                SharedConfigManager.Lookup(globalMemoryRegion.SharedConfigDictionary, namedEventLookupKey);

            if (!registeredNamedEventSharedConfig.HasSharedConfig)
            {
                namedEvent = null;
                return(false);
            }

            // Config exists, create a named event.
            //
            MlosProxyInternal.RegisteredNamedEventConfig registeredNamedEventConfig = registeredNamedEventSharedConfig.Config;

            namedEvent = NamedEvent.CreateOrOpen(registeredNamedEventConfig.EventName.Value);

            return(true);
        }
        /// <summary>
        /// Ensure that we have a valid subscription to receive webhooks for the target folder
        /// on this account.
        /// </summary>
        /// <param name="account"></param>
        /// <param name="log"></param>
        /// <returns></returns>
        public static async Task SubscribeToWebhooksForAccount(Account account, WebJobLogger log)
        {
            try
            {
                log.WriteLog(ActivityEventCode.CreatingSubscription, "Creating subscription on OneDrive service.");

                log.WriteLog("Connecting to OneDrive...");

                // Build a new OneDriveClient with the account information
                var client = await SharedConfig.GetOneDriveClientForAccountAsync(account);

                await CreateNewSubscriptionAsync(account, client, log);

                account.WebhooksReceived += 1;
                await AzureStorage.UpdateAccountAsync(account);

                log.WriteLog("Updated account {0} with hooks received: {1}", account.Id, account.WebhooksReceived);
            }
            catch (Exception ex)
            {
                log.WriteLog("Exception: {0}", ex);
            }
            finally
            {
                AccountLocker.ReleaseLock(account.Id);
            }
        }
예제 #9
0
        public void SimpleCollapse()
        {
            // Collect a dependency, collapse it, and nothing should change
            var config = new Config()
            {
                Info = new PackageInfo("MyMod", "asdf", new SemVer.Version("0.1.0"))
            };
            var dep = new Dependency("id", new SemVer.Range("^0.1.0"));

            config.Dependencies.Add(dep);
            var depConfig = new SharedConfig {
                Config = new Config()
                {
                    Info = new PackageInfo("Cool Name", "id", new SemVer.Version("0.1.1"))
                }
            };

            var configProvider = Utils.GetConfigProvider(config);
            var uriHandler     = Utils.GetUriHandler(new Dictionary <Dependency, SharedConfig> {
                { dep, depConfig }
            });

            var restorer = new RestoreHandler(configProvider.Object, uriHandler.Object);
            // Should not throw
            var deps   = restorer.CollectDependencies();
            var result = RestoreHandler.CollapseDependencies(deps);

            Assert.All(deps, kvp =>
            {
                Assert.True(result.TryGetValue(kvp.Key, out var conf));
                Assert.True(kvp.Value == conf);
            });
        }
        public async Task <IHttpActionResult> FetchStatisticsAsync()
        {
            var cookies = Request.Headers.GetCookies("session").FirstOrDefault();

            if (cookies == null)
            {
                return(JsonResponseEx.Create(HttpStatusCode.Unauthorized, new { message = "Session cookie is missing." }));
            }
            var sessionCookieValue = cookies["session"].Values;
            var account            = await AuthorizationController.AccountFromCookie(sessionCookieValue, false);

            if (null == account)
            {
                return(JsonResponseEx.Create(HttpStatusCode.Unauthorized, new { message = "Failed to locate an account for the auth cookie." }));
            }

            var client = await SharedConfig.GetOneDriveClientForAccountAsync(account);

            var cameraRollFolder = await client.Drive.Special["cameraroll"].Request().GetAsync();


            var responseObj = new
            {
                itemCount    = cameraRollFolder.Folder.ChildCount,
                totalSize    = cameraRollFolder.Size,
                lastModified = cameraRollFolder.LastModifiedDateTime
            };

            return(JsonResponseEx.Create(HttpStatusCode.OK, responseObj));
        }
        public void Simple()
        {
            var config = new Config()
            {
                Info = new PackageInfo("MyMod", "asdf", new SemVer.Version("0.1.0"))
            };
            var dep = new Dependency("id", new SemVer.Range("^0.1.0"));

            config.Dependencies.Add(dep);
            var depConfig = new SharedConfig {
                Config = new Config()
                {
                    Info = new PackageInfo("Cool Name", "id", new SemVer.Version("0.1.1"))
                }
            };

            var configProvider = Utils.GetConfigProvider(config);
            var uriHandler     = Utils.GetUriHandler(new Dictionary <Dependency, SharedConfig> {
                { dep, depConfig }
            });

            var restorer = new RestoreHandler(configProvider.Object, uriHandler.Object);
            // Should not throw
            var deps = restorer.CollectDependencies();

            // Ensure we still only have one dependency and nothing has changed
            Assert.Collection(deps,
                              kvp =>
            {
                Assert.True(kvp.Key.Dependency.Id == dep.Id);
                Assert.True(kvp.Key.Dependency.VersionRange == dep.VersionRange);
            });
        }
        public void NestedRecursive()
        {
            var config = new Config()
            {
                Info = new PackageInfo("MyMod", "id", new SemVer.Version("0.1.0"))
            };
            var dep = new Dependency("asdf", new SemVer.Range("^0.1.0"));

            config.Dependencies.Add(dep);
            var depConfig = new SharedConfig {
                Config = new Config()
                {
                    Info = new PackageInfo("Cool Name", "asdf", new SemVer.Version("0.1.0"))
                }
            };
            // It's undefined behavior to attempt to load a config that allows its dependencies to ask for itself
            // Therefore, we will test ourselves, and all other configs must follow this same principle
            var innerDep = new Dependency("id", new SemVer.Range("^0.1.0"));

            depConfig.Config.Dependencies.Add(innerDep);
            var innerDepConfig = new SharedConfig {
                Config = new Config {
                    Info = new PackageInfo("inner", "id", new SemVer.Version("0.1.0"))
                }
            };

            var configProvider = Utils.GetConfigProvider(config);
            var uriHandler     = Utils.GetUriHandler(new Dictionary <Dependency, SharedConfig> {
                { dep, depConfig }, { innerDep, innerDepConfig }
            });

            var innerRestorer = new RestoreHandler(Utils.GetConfigProvider(depConfig.Config).Object, uriHandler.Object);
            // Should not throw
            var innerDeps = innerRestorer.CollectDependencies();

            Assert.Collection(innerDeps,
                              kvp =>
            {
                Assert.True(kvp.Key.Dependency.Id == innerDep.Id);
                Assert.True(kvp.Key.Dependency.VersionRange == innerDep.VersionRange);
                Assert.True(kvp.Value.Config.Info.Version == innerDepConfig.Config.Info.Version);
            }
                              );
            uriHandler.Verify(mocks => mocks.GetSharedConfig(It.Is <RestoredDependencyPair>(p => p.Dependency == innerDep)), Times.Once);
            // Assume it restored
            depConfig.RestoredDependencies.Add(new RestoredDependencyPair {
                Dependency = innerDep, Version = innerDepConfig.Config.Info.Version
            });

            var restorer = new RestoreHandler(configProvider.Object, uriHandler.Object);

            // Should throw a recursive exception (id cannot include id)
            Assert.Throws <DependencyException>(() => restorer.CollectDependencies());
            // Should have tried to get asdf's config
            uriHandler.Verify(mocks => mocks.GetSharedConfig(It.Is <RestoredDependencyPair>(p => p.Dependency == dep)), Times.Once);
            // The inner dependency should have been attempted to have been gotten exactly once, from the innerRestorer collection
            // It should not have been collected again in our collection of dependencies
            uriHandler.Verify(mocks => mocks.GetSharedConfig(It.Is <RestoredDependencyPair>(p => p.Dependency == innerDep)), Times.Once);
        }
 private void PrintDependencies(string indent, SharedConfig config)
 {
     foreach (var p in config.RestoredDependencies)
     {
         Console.WriteLine(indent + PrintRestoredDependency(p));
         // TODO: Recurse down this properly
     }
 }
        /// <summary>
        /// Internal lookup. TProxy type is deduced by the caller.
        /// </summary>
        /// <typeparam name="TProbingPolicy">HashTable lookup policy.</typeparam>
        /// <typeparam name="TProxy">Codegen proxy type.</typeparam>
        /// <param name="sharedConfigMemoryRegion"></param>
        /// <param name="codegenKey"></param>
        /// <param name="slotIndex"></param>
        /// <returns></returns>
        public static SharedConfig <TProxy> Get <TProbingPolicy, TProxy>(
            this SharedConfigMemoryRegion sharedConfigMemoryRegion,
            ICodegenKey codegenKey,
            ref uint slotIndex)
            where TProbingPolicy : IProbingPolicy
            where TProxy : ICodegenProxy, new()
        {
            TProbingPolicy probingPolicy = default;

            uint probingCount = 0;

            slotIndex = 0;

            var configsArray = new UIntArray()
            {
                Buffer = sharedConfigMemoryRegion.Buffer + (int)sharedConfigMemoryRegion.ConfigsArrayOffset
            };

            uint elementCount = configsArray.Count;
            ProxyArray <uint> sharedConfigsOffsets = configsArray.Elements;

            SharedConfig <TProxy> sharedConfig = default;

            while (true)
            {
                slotIndex = probingPolicy.CalculateIndex(codegenKey, ref probingCount, elementCount);

                uint sharedConfigOffsets = sharedConfigsOffsets[(int)slotIndex];

                if (sharedConfigOffsets == 0)
                {
                    // Slot entry is empty.
                    //
                    sharedConfig.Buffer = IntPtr.Zero;
                    break;
                }

                // Compare the object keys.
                // Create a proxy to the shared config.
                //
                sharedConfig.Buffer = sharedConfigMemoryRegion.Buffer + (int)sharedConfigOffsets;

                // Compare key with the proxy.
                //
                bool foundEntry = codegenKey.CodegenTypeIndex() == sharedConfig.Header.CodegenTypeIndex &&
                                  codegenKey.CompareKey(sharedConfig.Config);
                if (foundEntry)
                {
                    break;
                }

                ++probingCount;
            }

            return(sharedConfig);
        }
예제 #15
0
        private void GenerateSignInLink()
        {
            QueryStringBuilder qsb = new QueryStringBuilder();

            qsb.Add("client_id", SharedConfig.AppClientID);
            qsb.Add("scope", SharedConfig.OAuthScopes());
            qsb.Add("response_type", "code");
            qsb.Add("redirect_uri", SharedConfig.RedirectUri);
            linkToSignIn.NavigateUrl = SharedConfig.AuthorizationService + qsb.ToString();
        }
예제 #16
0
        public virtual void LoadConfiguration(string[] args)
        {
            Args = args;

            GlobalLogger = CreateLoggers();
            Log(Strings.LOADING_CONFIGURATION, ESeverity.DEBUG);
            var cfg = new SharedConfig(Configuration);

            Protocol = GenericProtocol.GetInstance(cfg);
            SetCulture(cfg.ServiceCulture);
        }
예제 #17
0
 public string ToString(SharedConfig config)
 {
     try
     {
         return(JsonSerializer.Serialize(config, options));
     }
     catch
     {
         return(null);
     }
 }
예제 #18
0
        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Chargement des données de configuration
        /// </summary>
        /// <param name="args">Arguments de la ligne de commande</param>
        /// -----------------------------------------------------------------------------
        public override void LoadConfiguration(string[] args)
        {
            Args = args;

            GlobalLogger = CreateLoggers();
            Log(Shared.Resources.Strings.LOADING_CONFIGURATION, ESeverity.DEBUG);
            var cfg = new SharedConfig(Configuration);

            // unneeded in IIs web hosting model, see web.config
            // m_protocol = GenericProtocol.GetInstance(cfg);
            SetCulture(cfg.ServiceCulture);
        }
        public static UserConfig LoadIni_UserCfg(string inifn)
        {
            //ファイル名
            //string fileIni = @"UnityInjector\Config\XtMsterSlave\User_SaveData\" + inifn;
            string fileIni = @"User_SaveData\" + inifn;

            //bool isExist = false;

            try
            {
                string xfn = extpath(fileIni);
                Console.WriteLine("ユーザーセーブini設定読み込み :" + xfn);

                UserConfig uc = new UserConfig();
                uc.cfg_h      = SharedConfig.ReadConfig <UserConfigHeader>("Config", xfn);
                uc.cfg_h.Memo = uc.cfg_h.Memo.Replace("<改行>", "\r\n");
                //tmpc.Memo = tmpc.Memo.Replace("<改行>", "\n");

                for (int i = 0; i < XtMasterSlave.MAX_PAGENUM; i++)
                {
                    uc.cfgs_p[i] = SharedConfig.ReadConfig <UserConfigPage>("Page-" + (i + 1).ToString(), xfn);

                    //debugPrintConsole(uc.cfgs_p[i].Master_Name);

                    //勝手に取ってくれるっぽい…
                    //uc.cfgs_p[i].Master_Name = uc.cfgs_p[i].Master_Name._dq();
                    //uc.cfgs_p[i].Slave_Name = uc.cfgs_p[i].Slave_Name._dq();
                }
                //isExist = true;

                for (int i = 0; i < uc.cfgs_ms.Length; i++)
                {
                    uc.cfgs_ms[i] = SharedConfig.ReadConfig <XtMasterSlave.MsLinkConfig>("Config-" + (i + 1).ToString(), xfn);
                }

                /*
                 * for (int i = 0; i < cfgs.Length; i++)
                 * {
                 *  v3ofs[i].v3StackOffset = faTov3(cfgs[i].v3StackOffsetFA);
                 *  v3ofs[i].v3StackOffsetRot = faTov3(cfgs[i].v3StackOffsetRotFA);
                 *  v3ofs[i].v3HandLOffset = faTov3(cfgs[i].v3HandLOffsetFA);
                 *  v3ofs[i].v3HandROffset = faTov3(cfgs[i].v3HandROffsetFA);
                 * }*/

                return(uc);
            }
            catch (Exception e)
            {
                UnityEngine.Debug.Log("XtMs+INI Error:" + e);
            }

            return(null);
        }
예제 #20
0
        public void Push(SharedConfig config)
        {
            if (config is null)
            {
                throw new ArgumentNullException(nameof(config));
            }
            // We don't perform any validity here, simply ship it away
            var s = configProvider.ToString(config);

            client.Headers.Add(HttpRequestHeader.Authorization, AuthorizationHeader);
            client.UploadString($"/{config.Config.Info.Id}/{config.Config.Info.Version}", s);
        }
예제 #21
0
        /// <summary>
        /// Internal lookup. TProxy type is deduced by the caller.
        /// </summary>
        /// <typeparam name="TProxy">Codegen proxy type.</typeparam>
        /// <param name="sharedConfigDictionary"></param>
        /// <param name="codegenKey"></param>
        /// <param name="slotIndex"></param>
        /// <returns></returns>
        internal static SharedConfig <TProxy> Get <TProxy>(
            SharedConfigDictionary sharedConfigDictionary,
            ICodegenKey codegenKey,
            ref uint slotIndex)
            where TProxy : ICodegenProxy, new()
        {
            TProbingPolicy probingPolicy = default;

            uint probingCount = 0;

            slotIndex = 0;

            UIntArray configsArray = sharedConfigDictionary.ConfigsOffsetArray;

            uint elementCount = configsArray.Count;
            ProxyArray <uint> sharedConfigsOffsets = configsArray.Elements;

            SharedConfig <TProxy> sharedConfig = default;

            while (true)
            {
                slotIndex = probingPolicy.CalculateIndex(codegenKey, ref probingCount, elementCount);

                uint offsetToSharedConfig = sharedConfigsOffsets[(int)slotIndex];

                if (offsetToSharedConfig == 0)
                {
                    // Slot entry is empty.
                    //
                    sharedConfig.Buffer = IntPtr.Zero;
                    break;
                }

                // Compare the object keys.
                // Create a proxy to the shared config.
                //
                sharedConfig.Buffer = sharedConfigDictionary.Buffer - sharedConfigDictionary.Allocator.OffsetToAllocator + (int)offsetToSharedConfig;

                // Compare key with the proxy.
                //
                bool foundEntry = codegenKey.CodegenTypeIndex() == sharedConfig.Header.CodegenTypeIndex &&
                                  codegenKey.CompareKey(sharedConfig.Config);
                if (foundEntry)
                {
                    break;
                }

                ++probingCount;
            }

            return(sharedConfig);
        }
예제 #22
0
        public static GenericProtocol GetInstance(SharedConfig config)
        {
            var protoObj = (GenericProtocol)typeof(GenericProtocol).Assembly.CreateInstance(config.ServiceProtocol);

            if (protoObj == null)
            {
                throw new NotSupportedException(config.ServiceProtocol);
            }

            protoObj.Name    = config.ServiceName;
            protoObj.Port    = config.ServicePort;
            protoObj.Address = config.ServiceAddress;
            return(protoObj);
        }
        /// <summary>
        /// Process the changes for an account's OneDrive and organize any new files returned.
        /// </summary>
        /// <returns></returns>
        public static async Task ProcessChangesInOneDriveAsync(Account account, WebJobLogger log)
        {
            #region Acquire lock for account or abort processing
            // Acquire a simple lock to ensure that only one thread is processing
            // an account at the same time to avoid concurrency issues.
            // NOTE: If the web job is running on multiple VMs, this will not be sufficent to
            // ensure errors don't occur from one account being processed multiple times.
            bool acquiredLock = AccountLocker.TryAcquireLock(account.Id);
            if (!acquiredLock || !account.Enabled)
            {
                log.WriteLog("Failed to acquire lock for account. Another thread is already processing updates for this account or the account is disabled.");
                return;
            }
            log.WriteLog("Account lock acquired. Connecting to OneDrive.");
            #endregion

            try
            {
                // Build a new OneDriveClient with the account information
                var client = await SharedConfig.GetOneDriveClientForAccountAsync(account);

                // Execute our organization code
                FolderOrganizer organizer = new FolderOrganizer(client, account, log);
                await organizer.OrganizeSourceFolderItemChangesAsync();

                // Record that we received another webhook and save the account back to table storage
                account.WebhooksReceived += 1;
                await AzureStorage.UpdateAccountAsync(account);

                log.WriteLog("Updated account {0} with hooks received: {1}", account.Id, account.WebhooksReceived);
            }
            #region Error Handling
            catch (Exception ex)
            {
                log.WriteLog("Exception: {0}", ex);
            }
            finally
            {
                AccountLocker.ReleaseLock(account.Id);
            }

            log.WriteLog("Processing completed for account: {0}", account.Id);
            #endregion
        }
        /// <summary>
        /// Add a new shared config.
        /// </summary>
        /// <typeparam name="TProbingPolicy">HashTable lookup policy.</typeparam>
        /// <typeparam name="TType">Codegen config type.</typeparam>
        /// <typeparam name="TProxy">Codegen proxy type.</typeparam>
        /// <param name="sharedConfigMemoryRegion"></param>
        /// <param name="componentConfig"></param>
        public static void Add <TProbingPolicy, TType, TProxy>(
            this SharedConfigMemoryRegion sharedConfigMemoryRegion,
            ComponentConfig <TType, TProxy> componentConfig)
            where TProbingPolicy : IProbingPolicy
            where TType : ICodegenType, new()
            where TProxy : ICodegenProxy <TType, TProxy>, new()
        {
            uint slotIndex = 0;

            SharedConfig <TProxy> sharedConfig = sharedConfigMemoryRegion.Get <TProbingPolicy, TProxy>(componentConfig.Config, ref slotIndex);

            if (sharedConfig.Buffer != IntPtr.Zero)
            {
                throw new ArgumentException("Config already present", nameof(componentConfig));
            }

            TType config = componentConfig.Config;

            // Calculate size to allocate.
            //
            sharedConfig = sharedConfigMemoryRegion.Allocate <SharedConfig <TProxy> >();

            // Update hash map
            //
            ProxyArray <uint> sharedConfigOffsets = sharedConfigMemoryRegion.ConfigsOffsetArray.Elements;

            sharedConfigOffsets[(int)slotIndex] = (uint)sharedConfig.Buffer.Offset(sharedConfigMemoryRegion.Buffer);

            // Copy header, copy config.
            //
            SharedConfigHeader sharedHeader = sharedConfig.Header;
            TProxy             configProxy  = sharedConfig.Config;

            // Initialize header.
            //
            sharedHeader.ConfigId.Store(1);
            sharedHeader.CodegenTypeIndex = config.CodegenTypeIndex();

            // Copy the config to proxy.
            //
            CodegenTypeExtensions.Serialize(componentConfig.Config, sharedConfig.Config.Buffer);
        }
        public async Task <IHttpActionResult> AuthRedirect(string code)
        {
            // Redeem authorization code for account information

            OAuthHelper helper = new OAuthHelper(SharedConfig.TokenService,
                                                 SharedConfig.AppClientID,
                                                 SharedConfig.AppClientSecret,
                                                 SharedConfig.RedirectUri);

            var token = await helper.RedeemAuthorizationCodeAsync(code);

            if (null == token)
            {
                return(JsonResponseEx.Create(HttpStatusCode.InternalServerError, new { message = "Invalid response from token service.", code = "tokenServiceNullResponse" }));
            }

            Account account = new Account(token);
            var     client  = await SharedConfig.GetOneDriveClientForAccountAsync(account);

            var rootDrive = await client.Drive.Request().GetAsync();

            account.Id          = rootDrive.Id;
            account.DisplayName = rootDrive.Owner.User.DisplayName;

            var existingAccount = await AzureStorage.LookupAccountAsync(rootDrive.Id);

            if (null == existingAccount)
            {
                await AzureStorage.InsertAccountAsync(account);

                existingAccount = account;
            }
            else
            {
                existingAccount.SetTokenResponse(token);
                await AzureStorage.UpdateAccountAsync(existingAccount);
            }

            var authCookie = CookieForAccount(existingAccount);

            return(RedirectResponse.Create("/default.aspx", authCookie));
        }
예제 #26
0
        /// <summary>
        /// Add a new shared config.
        /// </summary>
        /// <typeparam name="TType">Codegen config type.</typeparam>
        /// <typeparam name="TProxy">Codegen proxy type.</typeparam>
        /// <param name="sharedConfigDictionary"></param>
        /// <param name="componentConfig"></param>
        internal static void Add <TType, TProxy>(
            SharedConfigDictionary sharedConfigDictionary,
            ComponentConfig <TType, TProxy> componentConfig)
            where TType : ICodegenType, new()
            where TProxy : ICodegenProxy <TType, TProxy>, new()
        {
            uint slotIndex = 0;

            SharedConfig <TProxy> sharedConfig = Get <TProxy>(sharedConfigDictionary, componentConfig.Config, ref slotIndex);

            if (sharedConfig.Buffer != IntPtr.Zero)
            {
                throw new ArgumentException("Config already present", nameof(componentConfig));
            }

            TType config = componentConfig.Config;

            // Calculate size to allocate.
            //
            sharedConfig = sharedConfigDictionary.Allocator.Allocate <SharedConfig <TProxy> >();

            // Update hash map
            //
            ProxyArray <uint> sharedConfigOffsets = sharedConfigDictionary.ConfigsOffsetArray.Elements;

            // #TODO verify.
            sharedConfigOffsets[(int)slotIndex] = (uint)sharedConfig.Buffer.Offset(sharedConfigDictionary.Buffer - sharedConfigDictionary.Allocator.OffsetToAllocator);

            // Copy header, copy config.
            //
            SharedConfigHeader sharedHeader = sharedConfig.Header;
            TProxy             configProxy  = sharedConfig.Config;

            // Initialize header.
            //
            sharedHeader.ConfigId.Store(1);
            sharedHeader.CodegenTypeIndex = config.CodegenTypeIndex();

            // Copy the config to proxy.
            //
            CodegenTypeExtensions.Serialize(componentConfig.Config, sharedConfig.Config.Buffer);
        }
예제 #27
0
        /// <summary>
        /// Register shared config memory region.
        /// </summary>
        /// <param name="sharedMemoryRegionIndex">Index of the shared memory region.</param>
        private void RegisterSharedConfigMemoryRegion(uint sharedMemoryRegionIndex)
        {
            MlosInternal.RegisteredMemoryRegionConfig.CodegenKey codegenKey = default;
            codegenKey.MemoryRegionIndex = sharedMemoryRegionIndex;

            // Locate shared memory region config.
            //
            SharedConfig <RegisteredMemoryRegionConfig> registeredMemoryRegionSharedConfig =
                SharedConfigManager.Lookup(mlosContext.GlobalMemoryRegion.SharedConfigDictionary, codegenKey);

            if (registeredMemoryRegionSharedConfig.HasSharedConfig)
            {
                // Config exists, register memory region with the shared config manager.
                //
                RegisteredMemoryRegionConfig registeredMemoryRegionConfig = registeredMemoryRegionSharedConfig.Config;

                mlosContext.SharedConfigManager.RegisterSharedConfigMemoryRegion(
                    memoryRegionId: registeredMemoryRegionConfig.MemoryRegionIndex,
                    sharedMemoryMapName: registeredMemoryRegionConfig.SharedMemoryMapName.Value,
                    memoryRegionSize: registeredMemoryRegionConfig.MemoryRegionSize);
            }
        }
예제 #28
0
        static void Main(string[] args)
        {
            Console.Title = "Case18221.Sender";
            var configuration = new BusConfiguration();

            configuration.EndpointName("Case18221.Sender");

            SharedConfig.ConfigureBus(configuration);

            using (var bus = Bus.CreateSendOnly(configuration))
            {
                do
                {
                    Console.WriteLine("Press Escape to quit or Enter to send");
                    var key = Console.ReadKey();

                    if (key.Key == ConsoleKey.Escape)
                    {
                        break;
                    }

                    if (key.Key != ConsoleKey.Enter)
                    {
                        continue;
                    }

                    var teamId = Guid.NewGuid();

                    bus.Send("Case18221.Host", new PublishChartProductMasterCommand
                    {
                        TeamId = teamId
                    });

                    Console.WriteLine($"Sent {teamId}");
                } while (true);
            }
        }
예제 #29
0
 public SharedConfig GetSharedConfig(bool createOnFail = false)
 {
     if (localConfigGotten)
     {
         return(localConfig);
     }
     // We only set this flag once we create it. This basically allows us to continue checking to see if it exists over multiple calls.
     localConfig = null;
     if (!File.Exists(localConfigPath))
     {
         if (createOnFail)
         {
             // localConfig has a reference to config
             var config = GetConfig();
             if (config is null)
             {
                 return(localConfig);
             }
             localConfig = new SharedConfig {
                 Config = config
             };
             Console.WriteLine("Creating new local config at: " + localConfigPath);
             // Commit the created config when we explicitly want to create on failure
             localConfigGotten = true;
             Commit();
         }
     }
     else
     {
         // These will throw as needed to the caller on failure
         // TODO: If we can solve the issue by recreating the JSON, we can try that here
         var json = File.ReadAllText(localConfigPath);
         localConfig       = JsonSerializer.Deserialize <SharedConfig>(json, options);
         localConfigGotten = true;
     }
     return(localConfig);
 }
예제 #30
0
        public async Task <string> AuthenticateAsync()
        {
            if (null != CachedAccessTokenExpiration && null != CachedAccessToken &&
                CachedAccessTokenExpiration < DateTimeOffset.Now.AddMinutes(5))
            {
                return(CachedAccessToken);
            }

            if (null == this.RefreshToken)
            {
                throw new InvalidOperationException("No refresh token available. Cannot authenticate.");
            }

            OAuthHelper oauth = SharedConfig.MicrosoftAccountOAuth();
            var         token = await oauth.RedeemRefreshTokenAsync(this.RefreshToken);

            if (null != token)
            {
                SetTokenResponse(token);
                return(token.AccessToken);
            }

            throw new InvalidOperationException("Unable to authenticate. Need to sign-in again.");
        }