예제 #1
0
        /// <summary>
        /// Starts a module given its token
        /// Has the side effect of updating the moduleInfo object's version to what was exactly ran
        /// </summary>
        /// <param name="moduleInfo"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        private VModule StartModule(ModuleInfo moduleInfo, AddInToken token)
        {
            VModule startedModule = null;

            string moduleVersion = GetHomeOSUpdateVersion(GetAddInConfigFilepath(moduleInfo.BinaryName()));
            if (!CompareModuleVersions(moduleInfo.GetVersion(), moduleVersion))
            {
                logger.Log("WARNING: Starting an inexact match for {0}", moduleInfo.FriendlyName());
                moduleInfo.SetVersion(moduleVersion);
            }

            switch (Constants.ModuleIsolationLevel)
            {
                case ModuleIsolationModes.AppDomain:
                    //     AppDomain addInAppDomain = AppDomain.CreateDomain(moduleInfo.BinaryName());
                    //    startedModule = token.Activate<VModule>(addInAppDomain);

                    // Adding upfront, to check if the module is already running.
                    // So that token is not re-activated if not needed - rayman
                    if (runningModules.Values.Contains(moduleInfo))
                        throw new Exception(string.Format("Attempted to run duplicate module. New = {0}. Old = {1}", moduleInfo, moduleInfo));

                    startedModule = token.Activate<VModule>(AddInSecurityLevel.FullTrust);

                    //AddInController ainController = AddInController.GetAddInController(startedModule);
                    //AppDomain domain = ainController.AppDomain;

                    break;
                case ModuleIsolationModes.Process:
                    AddInProcess addInProc = new AddInProcess();
                    startedModule = token.Activate<VModule>(addInProc, AddInSecurityLevel.FullTrust);
                    break;
                case ModuleIsolationModes.NoAddInAppDomain:
                    //this requires putting Views.dll in the directory with AppBenchmarker.dll
                    //the simplest way to do that is to just add AppBenchmarker as a reference
                    //AppDomainSetup ads = new AppDomainSetup();
                    //ads.ApplicationBase = Environment.CurrentDirectory;//AddInRoot + "\\AddIns\\" + "AppBenchmarker";
                    var ad = AppDomain.CreateDomain(moduleInfo.FriendlyName());//, null, ads);
                    startedModule = (VModule)ad.CreateInstanceAndUnwrap("AppBenchmarker", "AppBenchmarker.Benchmarker");
                    break;
                case ModuleIsolationModes.None:
                    //this requires adding AppBenchmarker and ModuleBase projects as References
                    //startedModule = (VModule)new AppBenchmarker.Benchmarker();
                    //if (moduleInfo.BinaryName().Equals("AppCamera"))
                    //    startedModule = (VModule)new AppCamera.CameraController();
                    //else if (moduleInfo.BinaryName().Equals("DriverFoscam"))
                    //    startedModule = (VModule)new DriverFoscam.DriverFoscam();
                    //else
                    //    logger.Log("Unknown module: {0}", moduleInfo.BinaryName());
                    break;
            }

            if (moduleInfo.WorkingDir() == null)
                moduleInfo.SetWorkingDir(Settings.ModuleWorkingDirBase + "\\" + moduleInfo.FriendlyName());

            if (String.IsNullOrEmpty(moduleInfo.BaseURL()))
                moduleInfo.SetBaseURL(GetBaseUrl() + "/" + moduleInfo.FriendlyName());

            if (string.IsNullOrEmpty(moduleInfo.BinaryDir()))
                moduleInfo.SetBinaryDir(Constants.AddInRoot + "\\AddIns\\" + moduleInfo.BinaryName());

            int secret = GetNewSecret();
            startedModule.Initialize(this, logger, moduleInfo, secret);

            lock (this)
            {
                //check if we already have this module running
                foreach (ModuleInfo runningModuleInfo in runningModules.Values)
                {
                    if (runningModuleInfo.Equals(moduleInfo))
                    {
                        //moduleThread.Abort();
                        throw new Exception(string.Format("Attempted to run duplicate module. New = {0}. Old = {1}", moduleInfo, moduleInfo));
                    }
                }

                runningModules[startedModule] = moduleInfo;
            }

            SafeThread moduleThread = new SafeThread(delegate() { startedModule.Start(); }, moduleInfo.FriendlyName(), logger);
            moduleThread.Start();

            return startedModule;
        }
예제 #2
0
        public List<string> InstallAppWeb(string appName)
        {
            try
            {
                logger.Log("UICalled:InstallAppWeb " + appName);

                HomeStoreApp app = homeStoreInfo.GetHomeStoreAppByName(appName);

                if (app == null)
                {
                    logger.Log("HomeStore app {0} was not found", appName);
                    return new List<string>() { "HomeStore app not found" };
                }

                //by default, we make the app auto start
                ModuleInfo moduleInfo = new ModuleInfo(app.AppName, app.AppName, app.BinaryName, null, true);
                moduleInfo.SetManifest(app.Manifest);
                moduleInfo.SetVersion(app.Version);

                AccessRule accessRule = new AccessRule();
                accessRule.ModuleName = moduleInfo.FriendlyName();
                accessRule.RuleName = "Access for " + moduleInfo.FriendlyName();
                accessRule.UserGroup = "everyone";
                accessRule.AccessMode = AccessMode.Allow;
                accessRule.DeviceList = new List<string> { "*" };
                accessRule.TimeList = new List<TimeOfWeek> { new TimeOfWeek(-1, 0, 2400) };
                accessRule.Priority = 0;

                platform.AddAccessRule(accessRule);

                //we now call startmodule: if we don't already have the binaries, this will download them as well
                var startedModule = platform.StartModule(moduleInfo, true);

                if (startedModule != null)
                {
                    //add this to our configuration
                    config.AddModule(moduleInfo);

                    return new List<string>() { "" };
                }
                else
                {
                    return new List<string>() { "Could not start module. Perhaps because we didn't find the binaries (with the right version)" };
                }
            }
            catch (Exception e)
            {
                logger.Log("Exception in InstallAppWeb: " + e.ToString());

                return new List<string>() { "Got exception: " + e.Message };
            }
        }
예제 #3
0
        private void ReadModuleList()
        {
            string fileName = ModulesFile;

            XmlDocument xmlDoc = new XmlDocument();
            XmlReader xmlReader = XmlReader.Create(fileName, xmlReaderSettings);
            xmlDoc.Load(xmlReader);
            XmlElement root = xmlDoc.FirstChild as XmlElement;

            if (!root.Name.Equals("Modules"))
                throw new Exception(fileName + " doesn't start with Modules");

            foreach (XmlElement xmlModule in root.ChildNodes)
            {
                if (!xmlModule.Name.Equals("Module"))
                    throw new Exception("child is not a Module in " + fileName);

                string name = xmlModule.GetAttribute("FriendlyName");
                string appName = xmlModule.GetAttribute("AppName");
                string binaryName = xmlModule.GetAttribute("BinaryName");
                string workingDir = xmlModule.GetAttribute("WorkingDir");
                string autoStartStr = xmlModule.GetAttribute("AutoStart");
                string backgroundStr = xmlModule.GetAttribute("Background");

                string version = xmlModule.GetAttribute("Version");

                string argStr = xmlModule.GetAttribute("ModuleArgStr");

                if (!argStr.Equals(""))
                    throw new Exception("module arguments are being supplied in old-fashioned way");

                //string[] words = argStr.Split(" ");

                if (workingDir.Equals(""))
                    workingDir = null;

                bool autoStart = (autoStartStr.Equals("1")) ? true : false;

                bool background = (backgroundStr.Equals("1")) ? true : false;

                string[] words = ReadModuleArguments(xmlModule);

                ModuleInfo moduleInfo = new ModuleInfo(name, appName, binaryName, workingDir, autoStart, words);
                moduleInfo.Background = background;

                // now lets set the version. if the version is  missing in the xml file set it as ""
                if(version.Equals(""))
                    moduleInfo.SetVersion(null);
                else
                    moduleInfo.SetVersion(version);

                //now let's attach the manifest
                Manifest manifest = ReadManifest(xmlModule);
                moduleInfo.SetManifest(manifest);

                AddModule(moduleInfo, false);

            }

            xmlReader.Close();
        }