private Dictionary<string,bool> AvailableOnRep(ModuleInfo moduleInfo) { string[] URIs = Settings.RepositoryURIs.Split('|'); String[] path = moduleInfo.BinaryName().Split('.'); Dictionary<string, bool> retval = new Dictionary<string,bool>(); foreach (string uri in URIs) { //logger.Log("Checking " + moduleInfo.BinaryName() + " v" + moduleInfo.GetVersion() + " availability on Rep: " + uri); //string zipuri = uri +'/' + path[0] + '/' + path[1] + '/' + path[2] + '/' + path[3] + '/' + moduleInfo.GetVersion() + '/' + moduleInfo.BinaryName() + ".zip"; string zipuri = uri; foreach (string pathElement in path) zipuri += "/" + pathElement; //by default platform should point to the Latest on the repository if a version of the binary isn't specified string binaryversion = "Latest"; if (moduleInfo.GetDesiredVersion() != null && moduleInfo.GetDesiredVersion() != "0.0.0.0") { binaryversion = moduleInfo.GetDesiredVersion(); } zipuri += '/' + binaryversion + '/' + moduleInfo.BinaryName() + ".zip"; if (UrlIsValid(zipuri)) { retval[zipuri]=true; return retval; } } retval[""] = false; return retval; }
/// <summary> /// Starts a module given its token /// We don't do this anymore: 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 = Utils.GetHomeOSUpdateVersion(Utils.GetAddInConfigFilepath(moduleInfo.BinaryName()), logger); if (!CompareModuleVersions(moduleInfo.GetDesiredVersion(), moduleVersion)) { logger.Log("WARNING: Starting an inexact match for {0}", moduleInfo.FriendlyName()); } moduleInfo.SetRunningVersion(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; }
private bool GetAddInFromRep(ModuleInfo moduleInfo , bool rebuild = true) { Dictionary<string,bool> repAvailability = AvailableOnRep(moduleInfo) ; if (!repAvailability.ContainsValue(true)) { logger.Log("Can't find "+moduleInfo.BinaryName() + " v "+ moduleInfo.GetDesiredVersion()+" on any rep."); return false; } // on fetching the binaries of a module existing older versions shall be overwritten // making sure older version module is not running. foreach (ModuleInfo runningModuleInfo in runningModules.Values) { if (runningModuleInfo.BinaryName().Equals(moduleInfo.BinaryName())) throw new Exception(String.Format("Attempted to fetch module, with same binary name module running. Running: ({0}, v {1}) Fetching: ({2}, v{3})", runningModuleInfo.BinaryName(), runningModuleInfo.GetRunningVersion() , moduleInfo.BinaryName() , moduleInfo.GetDesiredVersion())); } //unloading addin AddInToken removeToken = null; foreach (AddInToken t in allAddinTokens) { if (t.Name.Equals(moduleInfo.BinaryName())) { removeToken = t; break; } } allAddinTokens.Remove(removeToken); removeToken = null; GC.Collect(); try { CreateAddInDirectory(Constants.AddInRoot + "\\AddIns\\", moduleInfo.BinaryName()); DownloadFile(repAvailability.FirstOrDefault(x => x.Value == true).Key, Constants.AddInRoot + "\\AddIns\\" + moduleInfo.BinaryName(), moduleInfo.BinaryName() + ".zip"); UnpackZip(Constants.AddInRoot + "\\AddIns\\" + moduleInfo.BinaryName() + "\\" + moduleInfo.BinaryName() + ".zip", Constants.AddInRoot + "\\AddIns\\" + moduleInfo.BinaryName()); if(rebuild) rebuildAddInTokens(); File.Delete(Constants.AddInRoot + "\\AddIns\\" + moduleInfo.BinaryName() + "\\" + moduleInfo.BinaryName() + ".zip"); } catch (Exception e) { logger.Log("Exception : " + e); return false; } return true; }
/// <summary> /// Starts a module by searching for a matching token /// </summary> /// <param name="moduleInfo">The ModuleInfo for the module to start</param> public VModule StartModule(ModuleInfo moduleInfo, bool exactlyMatchVersions = false) { VModule startedModule = null; foreach (AddInToken token in allAddinTokens) { if (token.Name.Equals(moduleInfo.BinaryName()) && (!exactlyMatchVersions || CompareModuleVersions(moduleInfo.GetDesiredVersion(), Utils.GetHomeOSUpdateVersion(Utils.GetAddInConfigFilepath(moduleInfo.BinaryName()), logger)))) { if (startedModule != null) { logger.Log("WARNING: Found multiple matching tokens for " + moduleInfo.ToString()); continue; } try { startedModule = StartModule(moduleInfo, token); } catch (Exception exception) { logger.Log("Could not start module {0}: {1}", moduleInfo.ToString(), exception.ToString()); return null; } } } //we ran something, lets return it if (startedModule != null) return startedModule; //we didn't run anything. //if we were doing exact match on versions, this could be because we didn't find an exact match if (exactlyMatchVersions) { logger.Log("No exact-match-version token found for Module: Binary name: " + moduleInfo.BinaryName() + ", App Name: " + moduleInfo.AppName() + ", Version: " + moduleInfo.GetDesiredVersion()); Version versionRep = new Version(GetVersionFromRep(Settings.RepositoryURIs, moduleInfo.BinaryName())); Version versionLocal = new Version(Utils.GetHomeOSUpdateVersion((Utils.GetAddInConfigFilepath(moduleInfo.BinaryName())), logger)); logger.Log("The latest version for {0} on the repository: {1}", moduleInfo.BinaryName(), versionRep.ToString()); logger.Log("The version for {0} in the local AddIn dir: {1}", moduleInfo.BinaryName(), versionLocal.ToString()); if (versionRep.CompareTo(versionLocal) > 0) { logger.Log("The latest version on the repository ({0}) > local version ({1}) in AddIn for {2} - the latest from the rep will be downloaded!", versionRep.ToString(), versionLocal.ToString(), moduleInfo.BinaryName()); //try to get an exact match from the homestore GetAddInFromRep(moduleInfo); } //maybe, we got the right version, maybe we didn't; in any case, lets now run what we can find, without being strict about version numbers return StartModule(moduleInfo, false); } else { logger.Log("No matching token at all found for Module: Binary name: " + moduleInfo.BinaryName() + ", App Name: " + moduleInfo.AppName() + ", Version: " + moduleInfo.GetDesiredVersion()); return null; } }