/// <summary> /// Tool register /// </summary> /// <param name="info">Tool information</param> /// <returns>result</returns> public bool RegisterTool(HcToolInfo info) { var lockTakenScan = false; var lockTakenRegister = false; try { // lock Monitor.TryEnter(ScannedTools, Timeout, ref lockTakenScan); Monitor.TryEnter(RegisteredTools, Timeout, ref lockTakenRegister); // check lock taken if (!lockTakenScan || !lockTakenRegister) { return(false); } // get register tool if (FindToolInfo(RegisteredTools, info.Mac) != null) { return(false); } // remove scan tool ScannedTools.Remove(FindToolInfo(ScannedTools, info.Mac)); // add register tool RegisteredTools.Add(info); // tool added event ToolAdded?.Invoke(info); return(true); } finally { // unlock if (lockTakenScan) { Monitor.Exit(ScannedTools); } if (lockTakenRegister) { Monitor.Exit(RegisteredTools); } } }
/// <summary> /// Handles when a new type has been loaded into a <see cref="TypeFactory"/>. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="TypeFactoryLoadedEventArgs"/> instance containing the event data.</param> void typeFactory_TypeLoaded(TypeFactory sender, TypeFactoryLoadedEventArgs e) { try { // Instantiate the type Tool tool; try { tool = (Tool)TypeFactory.GetTypeInstance(e.LoadedType, this); } catch (Exception ex) { const string errmsg = "Failed to instantiate tool type `{0}`. Exception: {1}"; if (log.IsErrorEnabled) { log.ErrorFormat(errmsg, e.LoadedType, ex); } Debug.Fail(string.Format(errmsg, e.LoadedType, ex)); return; } Debug.Assert(tool.GetType() == e.LoadedType); // Add the event hooks SetToolListeners(tool, true); // Add to the collection try { _tools.Add(e.LoadedType, tool); } catch (Exception ex) { const string errmsg = "Failed to add tool `{0}` (type: {1}) to collection. Exception: {2}"; if (log.IsErrorEnabled) { log.ErrorFormat(errmsg, tool, tool.GetType(), ex); } Debug.Fail(string.Format(errmsg, tool, tool.GetType(), ex)); tool.Dispose(); return; } // Add to the settings manager _toolState.Add(tool); // Notify the Tool about all the containers already in this ToolManager foreach (var container in ToolTargetContainers) { try { tool.InvokeToolTargetContainerAdded(container); } catch (Exception ex) { // When there is an error when adding, log it but move on const string errmsg = "Error when notifying `{0}` about container `{1}`. Exception: {2}"; if (log.IsErrorEnabled) { log.ErrorFormat(errmsg, tool, container, ex); } Debug.Fail(string.Format(errmsg, tool, container, ex)); } } // Notify listeners if (ToolAdded != null) { ToolAdded.Raise(this, EventArgsHelper.Create(tool)); } } catch (Exception ex) { const string errmsg = "Unexpected error while trying to load tool from type `{0}`. Exception: {1}"; if (log.IsErrorEnabled) { log.ErrorFormat(errmsg, e.LoadedType, ex); } Debug.Fail(string.Format(errmsg, e.LoadedType, ex)); } }