public bool Equals(VModule otherModule) { int secret = this.Secret(); int otherModuleSecret = otherModule.Secret(); if (secret == otherModuleSecret) return true; else return false; //return (Secret().Equals(otherModule.Secret())); }
internal static IModule V2C(VModule view) { if (!System.Runtime.Remoting.RemotingServices.IsObjectOutOfAppDomain(view) && (view.GetType().Equals(typeof(ModuleC2V)))) { return ((ModuleC2V)(view)).GetSourceContract(); } else { return new ModuleV2C(view); } }
public bool Equals(VModule otherModule) { int secret = this.Secret(); int otherModuleSecret = otherModule.Secret(); if (secret == otherModuleSecret) { return(true); } else { return(false); } //return (Secret().Equals(otherModule.Secret())); }
/// <summary> /// Send a port deregisterd message to all active modules /// </summary> /// <param name="port"></param> /// <param name="owner"></param> public void BroadcastPortDeregistration(VPort port, VModule owner) { Dictionary<String, SafeThread> listOfThreads = new Dictionary<string, SafeThread>() ; lock (this) { foreach (VModule module in runningModules.Keys) { if (!module.Equals(owner)) { //***< Thread that monitors and timesout the port deregistered thread SafeThread mPortDeregThreadControl = new SafeThread(delegate() { //***< Thread that invokes port deregistered on the module SafeThread mPortDeregThread = new SafeThread(delegate() { InvokePortDeregistered(module, port); }, module + ".PortDeregistered(" + port + ")", logger); //***> mPortDeregThread.Start(); mPortDeregThread.Join(TimeSpan.FromMilliseconds(Settings.MaxPortDeregisteredExecutionTime)); try { if (mPortDeregThread.IsAlive()) mPortDeregThread.Abort(); } catch (Exception) { //The PortDeregistered() calls when aborted (if so) will raise an exception } } , module + ".PortDeregistered(" + port + ") - Control" , logger); //*** listOfThreads[mPortDeregThreadControl.Name()] = mPortDeregThreadControl; //store the list because we want to wait on these later mPortDeregThreadControl.Start(); } } } foreach (SafeThread t in listOfThreads.Values) { t.Join(); } }
public void CancelAllSubscriptions(VModule module , VPort controlPort, VCapability controlportcap) { _contract.CancelAllSubscriptions(ModuleAdapter.V2C(module), PortAdapter.V2C(controlPort), CapabilityAdapter.V2C(controlportcap)); }
/// <summary> /// Issues a capability /// </summary> /// <param name="module">The module that is asking for the capability</param> /// <param name="targetPort">The port for which the capability is being requested</param> /// <param name="userName">The name of the user on behalf of which the capability is being requested</param> /// <param name="password">The password of the user</param> /// <returns>The issued capability</returns> public VCapability GetCapability(VModule requestingModule, VPort targetPort, string username, string password) { // .... //check if the user is valid, the module exists, and the port is registered // .... if (!username.Equals(Constants.UserSystem.Name) && config.ValidUserUntil(username, password) < DateTime.Now) return null; if (!runningModules.ContainsKey(requestingModule)) return null; //if (!config.allPorts.ContainsKey(targetPort.GetInfo())) // return null; if (!registeredPorts.ContainsKey(targetPort)) return null; string portName = targetPort.GetInfo().GetFriendlyName(); DateTime allowedUntil = (Settings.EnforcePolicies) ? policyEngine.AllowAccess(portName, requestingModule.GetInfo().FriendlyName(), username) : DateTime.MaxValue; if (allowedUntil < DateTime.Now) { return null; } Capability capability = new Capability("platform", allowedUntil); if (RemoteInstallCapability(capability, targetPort)) { return capability; } else { return null; } }
private void InvokePortDeregistered(VModule module, VPort port) { bool done = false; while (!done) { if (this.CanEnterPortDeregistered(module)) //*** Check if the module is ready to deregister a port (e.g., it has finished its start() or its portregistered() ) { try { module.PortDeregistered(port); done = true; } catch (NullReferenceException) { logger.Log("NullReferenceException thrown in Bcast Port deregistration"); //Ratul TODO: not quite sure why this gets thrown, but for now this works System.Threading.Thread.Sleep(1000); } } } }
private void RemovePortFromConfig(VPort port , VModule module) { PortInfo targetPortInfo = new PortInfo(port.GetInfo().ModuleFacingName(), module.GetInfo()); PortInfo matchingPortInfo = config.GetMatchingPortInfo(targetPortInfo); if (matchingPortInfo == null) { } else { config.RemovePort(matchingPortInfo); } }
public void UpdateState(VModule module , VModuleState state ) { // Console.WriteLine("*** PLATFORM HAS UPDATED THE STATE of "+module.GetInfo().BinaryName()+" with secret "+module.Secret()+" to "+state.GetSimpleState()); ResultCode result = new ResultCode(); if (runningModules.ContainsKey(module)) // This will check if the module exists in runningModules AND the secrets match { lock (this) { if (!runningModulesStates.ContainsKey(module)) // updating the first time. must be an "EnterStart" state { runningModulesStates[module] = state; } else { runningModulesStates[module].Update(state); } result = ResultCode.Success; } } else { result = ResultCode.ModuleNotFound; } if (result == ResultCode.Success ) { // logger.Log(this + " updated state of module {0} to {1} ", module.ToString(), ((ModuleState.SimpleState)state.GetSimpleState()).ToString()); } else { logger.Log(this + " got invalid state update: {0} from module {1}", state.GetSimpleState().ToString(), module.ToString()); } }
public int ModuleFinished(VModule module) { return _contract.ModuleFinished(ModuleAdapter.V2C(module)); }
public VCapability GetCapability(VModule module, VPort targetPort, string username, string password) { return CapabilityAdapter.C2V(_contract.GetCapability(ModuleAdapter.V2C(module), PortAdapter.V2C(targetPort), username, password)); }
public void SetRoles(VPortInfo portInfo, IList<VRole> roles, VModule module) { _contract.SetRoles(PortInfoAdapter.V2C(portInfo), CollectionAdapters.ToIListContract<VRole, IRole>(roles, RoleAdapter.V2C, RoleAdapter.C2V), ModuleAdapter.V2C(module)); }
public VPortInfo GetPortInfo(string moduleFacingName, VModule module) { return PortInfoAdapter.C2V(_contract.GetPortInfo(moduleFacingName, ModuleAdapter.V2C(module))); }
//*** public int DeregisterPort(VPort port, VModule module) { return _contract.DeregisterPort(PortAdapter.V2C(port), ModuleAdapter.V2C(module)); }
private bool ModuleHasRegisteredPorts(VModule module) { lock (this) { foreach (VModule moduleWithActivePorts in registeredPorts.Values) { if (module.Equals(moduleWithActivePorts)) { return true; } } } return false; }
/// <summary> /// Signals to the platform that a particular module is terminating /// </summary> /// <param name="module"></param> /// <returns></returns> public int ModuleFinished(VModule module) { ResultCode result; bool activePorts = ModuleHasRegisteredPorts(module); lock (this) { if (runningModules.ContainsKey(module) && runningModulesStates.ContainsKey(module)) // take module off platform's data structures { //*** runningModules.Remove(module); runningModulesStates.Remove(module); //*** result = ResultCode.Success; } else { logger.Log(this + "got module finished for non-existent module: {0}", module.ToString()); result = ResultCode.Failure; } } if (activePorts) // if the module has active ports deregister them { logger.Log(this + " got module finished for a module with active ports: {0}", module.ToString()); List<VPort> portsToDeregister = new List<VPort>(); lock (this) { //*** Separating the parts of enumerating over registeredPorts and removing things (deregistering ports) from registeredPorts foreach (VPort port in registeredPorts.Keys) // enumeration cannot continue while dictionary is being modified (e.g., dereg ports) { if (registeredPorts[port].Equals(module)) { portsToDeregister.Add(port); } } } foreach (VPort port in portsToDeregister) { DeregisterPort(port, module); } // we should not delete this port/service from config. we want to remember it //foreach (VPort port in portsToDeregister) //{ // RemovePortFromConfig(port, module);// removing ports from platform config //} //*** } return (int)result; }
public bool StopModule(VModule moduleToStop) { //*** try { SafeThread t = new SafeThread(delegate() { moduleToStop.Stop(); }, moduleToStop.GetInfo().AppName() + "stop thread", logger); // invoke stop on the module t.Start(); t.Join(TimeSpan.FromMilliseconds(Settings.MaxStopExecutionTime)); if (t.IsAlive()) t.Abort(); ModuleFinished(moduleToStop); // dereg its ports broadcast ports' dereg; wipe module off data structures AddInCleanup(moduleToStop); // addin cleanup return true; } catch (Exception e) { logger.Log("Exception in stopping of module: " + e); return false; } //*** }
private bool CanEnterPortDeregistered(VModule module) { if (runningModulesStates.ContainsKey(module)) { if (runningModulesStates[module].GetSimpleState() == (int)ModuleState.SimpleState.ExitStart || runningModulesStates[module].GetSimpleState() == (int)ModuleState.SimpleState.ExitPortDeregistered || runningModulesStates[module].GetSimpleState() == (int)ModuleState.SimpleState.ExitPortRegistered) return true; else return false; } else { return false; } }
/// <summary> /// Issues a portinfo object /// </summary> /// <param name="moduleFacingName">The local name used by the owning module for this port</param> /// <param name="module">The owning module</param> /// <returns></returns> public VPortInfo GetPortInfo(string moduleFacingName, VModule module) { PortInfo targetPortInfo = new PortInfo(moduleFacingName, module.GetInfo()); //if matching portInfo exists, return that object //NB: we cannot return targetPortInfo itself because that is a different object (which does not have location and other things set) VPortInfo matchingPortInfo = config.GetMatchingPortInfo(targetPortInfo); if (matchingPortInfo != null) return matchingPortInfo; //this is not a port that we've seen before //make up a friendly name for this port as well as a location targetPortInfo.SetFriendlyName(moduleFacingName + " - " + module.GetInfo().FriendlyName()); targetPortInfo.SetLocation(config.RootLocation); config.AddUnconfiguredPort(targetPortInfo); return targetPortInfo; }
private void AddInCleanup(VModule moduleStopped) { //cleanup if (Constants.ModuleIsolationLevel == ModuleIsolationModes.AppDomain) { logger.Log("AppDomain cleanup for "+ moduleStopped.GetInfo().AppName()); bool done = false; AddInController aiController = AddInController.GetAddInController(moduleStopped); SafeThread t = new SafeThread(delegate() { while (!done) { try { aiController.Shutdown(); done = true; } catch (CannotUnloadAppDomainException) { logger.Log("AppDomain Unload did not succeed. Retrying."); System.Threading.Thread.Sleep(1000); // keep trying to unload until it gets unloaded } } }, moduleStopped.ToString()+"-UnloadingAppDomain", logger); t.Start(); t.Join(TimeSpan.FromMilliseconds(Settings.MaxFinallyBlockExecutionTime)); if(t.IsAlive()) t.Abort(); } else if (Constants.ModuleIsolationLevel == ModuleIsolationModes.Process) { //TODO: test this AddInController aiController = AddInController.GetAddInController(moduleStopped); aiController.Shutdown(); } else if (Constants.ModuleIsolationLevel == ModuleIsolationModes.NoAddInAppDomain) { // TODO handle cleanup here } else {// Globals.ModuleIsolationLevel == ModuleIsolationModes.None // TODO handle cleanup here } }
/// <summary> /// Set the roles that are being exported by a port /// </summary> /// <param name="portInfo">the portinfo object of the port</param> /// <param name="roles">the list of roles to bind</param> /// <param name="module">the module that own the port</param> public void SetRoles(VPortInfo vPortInfo, IList<VRole> roles, VModule module) { PortInfo storedPortInfo = config.GetMatchingPortInfo(vPortInfo); if (storedPortInfo == null) throw new Exception(vPortInfo + " not found!"); IList<VRole> oldList = storedPortInfo.GetRoles(); bool roleListChanged = (roles.Count != oldList.Count || roles.Count != roles.Intersect(oldList).Count()); storedPortInfo.SetRoles(roles); //update the configuration if (roleListChanged) config.UpdateRoleList(storedPortInfo); }
public void CancelAllSubscriptions(VModule module , VPort controlPort, VCapability controlportcap) { foreach (VPort p in registeredPorts.Keys) { if (!registeredPorts[p].Equals(module)) { foreach (VRole r in p.GetInfo().GetRoles()) { foreach (VOperation o in r.GetOperations()) { logger.Log("Unsubscribing " + controlPort + " from port: " + p + " operation: " + o); p.Unsubscribe(r.Name(), o.Name(), controlPort, controlportcap); } } } } }
/// <summary> /// Function call to register a port. Called by modules to activate new ports or to register changes in status. /// </summary> /// <param name="port"></param> /// <returns></returns> public int RegisterPort(VPort port, VModule module) { ResultCode result; lock (this) { if (registeredPorts.ContainsKey(port)) { logger.Log(this + " got registration for an existing port: {0}", port.ToString()); result = ResultCode.Failure; } else { if (runningModules.ContainsKey(module)) { registeredPorts[port] = module; logger.Log(this + " added {0} from {1}", port.ToString(), module.ToString()); result = ResultCode.Success; } else { logger.Log(this + " got port {0} registeration request from non-existent module {1}", port.ToString(), module.ToString()); result = ResultCode.ModuleNotFound; } } } if (result == ResultCode.Success) { SafeThread newThread = new SafeThread(delegate() { // System.Threading.Thread.Sleep(Settings.portRegisterDelay); BroadcastPortRegistration(port, module); }, "RegisterPort " + port , logger); newThread.Start(); } return (int) result; }
public ModuleV2C(VModule view) { _view = view; }
/// <summary> /// Send a port registration message to all active modules /// </summary> /// <param name="port"></param> /// <param name="owner"></param> public void BroadcastPortRegistration(VPort port, VModule owner) { lock (this) { foreach (VModule module in runningModules.Keys) { if (!module.Equals(owner)) { VModule tmp = module; //make a copy to avoid delegate weirdnesses //spawn this in a separate thread to avoid locking SafeThread mPortRegThread = new SafeThread(delegate() { bool done = false; while (!done) { //*** Check if the module is ready to receive a new port (e.g., it has finished its start() ) if (this.CanEnterPortRegistered(module)) { //*** try { tmp.PortRegistered(port); done = true; } catch (NullReferenceException e) { logger.Log("Exception " + e); //Ratul TODO: not quite sure why this gets thrown, but for now this works System.Threading.Thread.Sleep(1000); } } } } ,tmp + ".PortRegistered(" + port + ")" , logger ); mPortRegThread.Start(); } } } }
/// <summary> /// Deregister a port to declare it unavailable for use by other modules /// </summary> /// <param name="port">The port to deregisted</param> /// <param name="owner">The module to which the port belongs</param> /// <returns></returns> public int DeregisterPort(VPort port, VModule module) { ResultCode result; lock (this) { if (registeredPorts.ContainsKey(port)) { if (module.Equals(registeredPorts[port])) { logger.Log("deregistering port: {0}", port.ToString()); registeredPorts.Remove(port); result = ResultCode.Success; } else { logger.Log("got port deregisteration for {0} from a non-owner {1}", port.ToString(), module.ToString()); result = ResultCode.Failure; } } else { logger.Log("got deregisteration for unregistered port: {0}", port.ToString()); result = ResultCode.PortNotFound; } } if (result == ResultCode.Success) { BroadcastPortDeregistration(port, module); // broadcast needs to be asyncronous because the module's addin (and appdomain, ports, etc) will be wiped /* System.Threading.Thread newThread = new System.Threading.Thread(delegate() { // System.Threading.Thread.Sleep(Settings.portRegisterDelay); BroadcastPortDeregistration(port, module); }); newThread.Name = "Deregister Port " + port; newThread.Start(); */ } return (int)result; }
//*** public void UpdateState(VModule module, VModuleState state) { _contract.UpdateState(ModuleAdapter.V2C(module), ModuleStateAdapter.V2C(state)); }