예제 #1
0
        /// <summary>
        /// Unregisters a P2P application from the registered list of applications.
        /// </summary>
        /// <param name="appType"></param>
        public static bool UnregisterApplication(Type appType)
        {
            bool unregistered = false;

            if (appType == null)
            {
                throw new ArgumentNullException("appType");
            }

            if (!appType.IsSubclassOf(typeof(P2PApplication)))
            {
                Trace.WriteLineIf(Settings.TraceSwitch.TraceError,
                                  String.Format("Type {0} can't be unregistered! It must be derived from P2PApplication", appType.Name), "P2PApplication");

                return(false);
            }

            foreach (P2PApplicationAttribute att in appType.GetCustomAttributes(typeof(P2PApplicationAttribute), false))
            {
                lock (p2pAppCache)
                {
                    if (p2pAppCache.ContainsKey(att.EufGuid))
                    {
                        P2PApp app = new P2PApp(att.EufGuid, att.AppId, appType);

                        if (p2pAppCache[att.EufGuid].Contains(app))
                        {
                            while (p2pAppCache[att.EufGuid].Remove(app))
                            {
                                unregistered = true;

                                Trace.WriteLineIf(Settings.TraceSwitch.TraceWarning,
                                                  String.Format("Application has unregistered! EufGuid: {0}, AppId: {1}, AppType: {2}", att.EufGuid, att.AppId, appType), "P2PApplication");
                            }
                        }
                    }
                }
            }

            return(unregistered);
        }
예제 #2
0
        /// <summary>
        /// Unregisters a P2P application from the registered list of applications.
        /// </summary>
        /// <param name="appType"></param>
        public static bool UnregisterApplication(Type appType)
        {
            bool unregistered = false;

            if (appType == null)
                throw new ArgumentNullException("appType");

            if (!appType.IsSubclassOf(typeof(P2PApplication)))
            {
                Trace.WriteLineIf(Settings.TraceSwitch.TraceError,
                    String.Format("Type {0} can't be unregistered! It must be derived from P2PApplication", appType.Name), "P2PApplication");

                return false;
            }

            foreach (P2PApplicationAttribute att in appType.GetCustomAttributes(typeof(P2PApplicationAttribute), false))
            {
                lock (p2pAppCache)
                {
                    if (p2pAppCache.ContainsKey(att.EufGuid))
                    {
                        P2PApp app = new P2PApp(att.EufGuid, att.AppId, appType);

                        if (p2pAppCache[att.EufGuid].Contains(app))
                        {
                            while (p2pAppCache[att.EufGuid].Remove(app))
                            {
                                unregistered = true;

                                Trace.WriteLineIf(Settings.TraceSwitch.TraceWarning,
                                    String.Format("Application has unregistered! EufGuid: {0}, AppId: {1}, AppType: {2}", att.EufGuid, att.AppId, appType), "P2PApplication");
                            }
                        }
                    }
                }
            }

            return unregistered;
        }
예제 #3
0
        /// <summary>
        /// Registers a P2P application which inherits P2PApplication, with option to override the existing class.
        /// </summary>
        /// <param name="appType"></param>
        /// <param name="overrideExisting"></param>
        /// <returns></returns>
        public static bool RegisterApplication(Type appType, bool overrideExisting)
        {
            if (appType == null)
            {
                throw new ArgumentNullException("appType");
            }

            if (!appType.IsSubclassOf(typeof(P2PApplication)))
            {
                Trace.WriteLineIf(Settings.TraceSwitch.TraceError,
                                  String.Format("Type {0} can't be registered! It must be derived from P2PApplication", appType.Name), "P2PApplication");

                return(false);
            }

            bool added        = false;
            bool hasAttribute = false;

            foreach (P2PApplicationAttribute att in appType.GetCustomAttributes(typeof(P2PApplicationAttribute), false))
            {
                hasAttribute = true;

                lock (p2pAppCache)
                {
                    if (!p2pAppCache.ContainsKey(att.EufGuid))
                    {
                        p2pAppCache[att.EufGuid] = new List <P2PApp>(1);
                    }

                    P2PApp app = new P2PApp(att.EufGuid, att.AppId, appType);

                    if (p2pAppCache[att.EufGuid].Contains(app))
                    {
                        if (overrideExisting)
                        {
                            while (p2pAppCache[att.EufGuid].Remove(app))
                            {
                                Trace.WriteLineIf(Settings.TraceSwitch.TraceWarning,
                                                  String.Format("Application has been unregistered! EufGuid: {0}, AppId: {1}, AppType: {2}", att.EufGuid, att.AppId, appType), "P2PApplication");
                            }

                            p2pAppCache[att.EufGuid].Add(app);
                            added = true;

                            Trace.WriteLineIf(Settings.TraceSwitch.TraceInfo,
                                              String.Format("New application has been registered in force mode! EufGuid: {0}, AppId: {1}, AppType: {2}", att.EufGuid, att.AppId, appType), "P2PApplication");
                        }
                        else
                        {
                            Trace.WriteLineIf(Settings.TraceSwitch.TraceWarning,
                                              String.Format("Application has already registered! EufGuid: {0}, AppId: {1}, AppType: {2}", att.EufGuid, att.AppId, appType), "P2PApplication");
                        }
                    }
                    else
                    {
                        p2pAppCache[att.EufGuid].Add(app);
                        added = true;

                        Trace.WriteLineIf(Settings.TraceSwitch.TraceInfo,
                                          String.Format("New application registered! EufGuid: {0}, AppId: {1}, AppType: {2}", att.EufGuid, att.AppId, appType), "P2PApplication");
                    }
                }
            }

            if (hasAttribute == false)
            {
                Trace.WriteLineIf(Settings.TraceSwitch.TraceError,
                                  String.Format("Type {0} can't be registered! It must have [P2PApplicationAttribute(appID, eufGUID)].", appType.Name), "P2PApplication");
            }

            return(added);
        }
예제 #4
0
        /// <summary>
        /// Registers a P2P application which inherits P2PApplication, with option to override the existing class.
        /// </summary>
        /// <param name="appType"></param>
        /// <param name="overrideExisting"></param>
        /// <returns></returns>
        public static bool RegisterApplication(Type appType, bool overrideExisting)
        {
            if (appType == null)
                throw new ArgumentNullException("appType");

            if (!appType.IsSubclassOf(typeof(P2PApplication)))
            {
                Trace.WriteLineIf(Settings.TraceSwitch.TraceError,
                    String.Format("Type {0} can't be registered! It must be derived from P2PApplication", appType.Name), "P2PApplication");

                return false;
            }

            bool added = false;
            bool hasAttribute = false;

            foreach (P2PApplicationAttribute att in appType.GetCustomAttributes(typeof(P2PApplicationAttribute), false))
            {
                hasAttribute = true;

                lock (p2pAppCache)
                {
                    if (!p2pAppCache.ContainsKey(att.EufGuid))
                        p2pAppCache[att.EufGuid] = new List<P2PApp>(1);

                    P2PApp app = new P2PApp(att.EufGuid, att.AppId, appType);

                    if (p2pAppCache[att.EufGuid].Contains(app))
                    {
                        if (overrideExisting)
                        {
                            while (p2pAppCache[att.EufGuid].Remove(app))
                            {
                                Trace.WriteLineIf(Settings.TraceSwitch.TraceWarning,
                                    String.Format("Application has been unregistered! EufGuid: {0}, AppId: {1}, AppType: {2}", att.EufGuid, att.AppId, appType), "P2PApplication");
                            }

                            p2pAppCache[att.EufGuid].Add(app);
                            added = true;

                            Trace.WriteLineIf(Settings.TraceSwitch.TraceInfo,
                                String.Format("New application has been registered in force mode! EufGuid: {0}, AppId: {1}, AppType: {2}", att.EufGuid, att.AppId, appType), "P2PApplication");
                        }
                        else
                        {
                            Trace.WriteLineIf(Settings.TraceSwitch.TraceWarning,
                                String.Format("Application has already registered! EufGuid: {0}, AppId: {1}, AppType: {2}", att.EufGuid, att.AppId, appType), "P2PApplication");
                        }
                    }
                    else
                    {
                        p2pAppCache[att.EufGuid].Add(app);
                        added = true;

                        Trace.WriteLineIf(Settings.TraceSwitch.TraceInfo,
                            String.Format("New application registered! EufGuid: {0}, AppId: {1}, AppType: {2}", att.EufGuid, att.AppId, appType), "P2PApplication");
                    }
                }
            }

            if (hasAttribute == false)
            {
                Trace.WriteLineIf(Settings.TraceSwitch.TraceError,
                    String.Format("Type {0} can't be registered! It must have [P2PApplicationAttribute(appID, eufGUID)].", appType.Name), "P2PApplication");
            }

            return added;
        }