/// <summary> /// Registers a custom filter that is decorated with CustomFilterAttribute /// </summary> /// <param name="t">Type of the filter to register</param> public void RegisterCustomFilter(Type t) { if (t == null) { throw new ArgumentNullException("t"); } object[] customAttrs = t.GetCustomAttributes(typeof(CustomFilterAttribute), false); if (customAttrs.Length < 1) { throw new ArgumentException("Given type is not decorated with CustomFilterAttribute"); } CustomFilterAttribute attr = customAttrs[0] as CustomFilterAttribute; if (attr != null) { string typeName = t.AssemblyQualifiedName; if (!typeof(IFilter).IsAssignableFrom(t)) { LogbusException ex = new LogbusException("Given type does not implement IFilter"); ex.Data.Add("typeName", t); throw ex; } if (_registeredTypes.ContainsKey(attr.Tag)) { _registeredTypes.Remove(attr.Tag); } _registeredTypes.Add(attr.Tag, typeName); _registeredDescriptions.Add(attr.Tag, attr.Description); } }
/// <summary> /// Registers a custom filter for the given tag /// </summary> /// <param name="tag">Unique ID of filter</param> /// <param name="typeName">Type for filter</param> /// <param name="description">Human-readable description for filter</param> public void RegisterCustomFilter(string tag, string typeName, string description) { if (string.IsNullOrEmpty(tag)) { throw new ArgumentNullException("tag"); } if (string.IsNullOrEmpty(typeName)) { throw new ArgumentNullException("typeName"); } //Examine type try { Type filterType = Type.GetType(typeName); if (!typeof(IFilter).IsAssignableFrom(filterType)) { LogbusException ex = new LogbusException("Given type does not implement IFilter"); ex.Data.Add("typeName", typeName); throw ex; } if (_registeredTypes.ContainsKey(tag)) { _registeredTypes.Remove(tag); } _registeredTypes.Add(tag, typeName); _registeredDescriptions.Add(tag, description); } catch (LogbusException) { throw; } catch (Exception ex) //Usually TypeLoadException { throw new LogbusException("Unable to load type for custom filter", ex); } }
public void UnsubscribeClient(string clientId) { if (Disposed) { throw new ObjectDisposedException(GetType().FullName); } if (string.IsNullOrEmpty(clientId)) { throw new ArgumentNullException("clientId", "Client ID must not be null"); } int indexof = clientId.IndexOf(':'); if (indexof < 0) { ArgumentException ex = new ArgumentException("Invalid client ID"); ex.Data.Add("clientId-channel", clientId); throw ex; } string transportId = clientId.Substring(0, indexof), transportClientId = clientId.Substring(indexof + 1); if (string.IsNullOrEmpty(transportId)) { ArgumentException ex = new ArgumentException("Invalid client ID"); ex.Data.Add("clientId-channel", clientId); throw ex; } //First find the transport IOutboundTransport trans; _transportLock.AcquireReaderLock(DEFAULT_JOIN_TIMEOUT); try { if (!_transports.ContainsKey(transportId)) { ArgumentException ex = new ArgumentException("Invalid client ID. Transport is not recognized or not active on this channel."); ex.Data.Add("clientId-channel", clientId); throw ex; } trans = _transports[transportId]; } finally { _transportLock.ReleaseReaderLock(); } try { try { trans.UnsubscribeClient(transportClientId); Log.Info("Client {0} unsubscribed from channel {1}", clientId, ID); if (trans.SubscribedClients == 0) { _transportLock.AcquireWriterLock(DEFAULT_JOIN_TIMEOUT); try { _transports.Remove(transportId); } finally { _transportLock.ReleaseWriterLock(); } trans.Dispose(); } } catch (ObjectDisposedException) { _transportLock.AcquireWriterLock(DEFAULT_JOIN_TIMEOUT); try { _transports.Remove(transportId); } finally { _transportLock.ReleaseWriterLock(); } throw new TransportException("Transport crashed"); } catch (LogbusException ex) { ex.Data.Add("client-channel", clientId); throw; } catch (Exception e) { LogbusException ex = new LogbusException("Unable to unsubscribe client", e); ex.Data.Add("client-channel", clientId); throw; } } catch (Exception ex) { Log.Error("Unable to unsubscribe client {0} from channel {1}", clientId, ID); Log.Debug("Error details: {0}", ex.Message); if (Error != null) { Error(this, new UnhandledExceptionEventArgs(ex, true)); } throw; } }
public void RefreshClient(string clientId) { if (Disposed) { throw new ObjectDisposedException(GetType().FullName); } if (string.IsNullOrEmpty(clientId)) { throw new ArgumentNullException("clientId", "Client ID must not be null"); } int indexof = clientId.IndexOf(':'); if (indexof < 0) { ArgumentException ex = new ArgumentException("Invalid client ID"); ex.Data.Add("clientId-channel", clientId); throw ex; } string transportId = clientId.Substring(0, indexof), transportClientId = clientId.Substring(indexof + 1); if (string.IsNullOrEmpty(transportId)) { ArgumentException ex = new ArgumentException("Invalid client ID"); ex.Data.Add("clientId-channel", clientId); throw ex; } //First find the transport _transportLock.AcquireReaderLock(DEFAULT_JOIN_TIMEOUT); IOutboundTransport trans; try { if (!_transports.ContainsKey(transportId)) { ArgumentException ex = new ArgumentException("Invalid client ID"); ex.Data.Add("clientId-channel", clientId); throw ex; } trans = _transports[transportId]; } finally { _transportLock.ReleaseReaderLock(); } try { try { trans.RefreshClient(transportClientId); } catch (ObjectDisposedException) { _transportLock.AcquireWriterLock(DEFAULT_JOIN_TIMEOUT); try { _transports.Remove(transportId); } finally { _transportLock.ReleaseWriterLock(); } throw new TransportException("Transport crashed"); } catch (NotSupportedException ex) { ex.Data.Add("client-channel", clientId); throw; } catch (LogbusException ex) { ex.Data.Add("client-channel", clientId); throw; } catch (Exception e) { LogbusException ex = new LogbusException("Unable to refresh client", e); ex.Data.Add("client-channel", clientId); throw; } } catch (Exception ex) { if (Error != null) { Error(this, new UnhandledExceptionEventArgs(ex, true)); } throw; } }
public string SubscribeClient(string transportId, IEnumerable <KeyValuePair <string, string> > inputInstructions, out IEnumerable <KeyValuePair <string, string> > outputInstructions) { if (Disposed) { throw new ObjectDisposedException(GetType().FullName); } if (string.IsNullOrEmpty(transportId)) { throw new ArgumentNullException("transportId", "Transport ID cannot be null"); } try { Log.Info("New client subscribing on channel {0}", ID); IOutboundTransport toSubscribe; _transportLock.AcquireReaderLock(DEFAULT_JOIN_TIMEOUT); try { if (_transports.ContainsKey(transportId)) { toSubscribe = _transports[transportId]; } else { try { toSubscribe = TransportFactoryHelper.GetFactory(transportId).CreateTransport(); Log.Debug("Created new instance of transport type {0} for channel {1}, ID {2}", transportId, Name, toSubscribe.GetHashCode()); } catch (NotSupportedException e) { throw new LogbusException("Given transport is not supported on this node", e); } LockCookie ck = _transportLock.UpgradeToWriterLock(DEFAULT_JOIN_TIMEOUT); try { _transports.Add(transportId, toSubscribe); } finally { _transportLock.DowngradeFromWriterLock(ref ck); } } } finally { _transportLock.ReleaseReaderLock(); } try { _block = false; string clientId = string.Format("{0}:{1}", transportId, toSubscribe.SubscribeClient(inputInstructions, out outputInstructions)); Log.Info("New client subscribed on channel {0} with ID {1}", ID, clientId); return(clientId); } catch (ObjectDisposedException) { _transportLock.AcquireWriterLock(DEFAULT_JOIN_TIMEOUT); try { _transports.Remove(transportId); } finally { _transportLock.ReleaseWriterLock(); } return(SubscribeClient(transportId, inputInstructions, out outputInstructions)); } catch (LogbusException ex) { ex.Data.Add("transportId", transportId); throw; } catch (Exception e) { LogbusException ex = new LogbusException("Could not subscribe transport", e); ex.Data.Add("transportId", transportId); throw ex; } } catch (Exception ex) { Log.Error("Unable to subscribe new client on channel {0}", ID); Log.Debug("Error details: {0}", ex.Message); if (Error != null) { Error(this, new UnhandledExceptionEventArgs(ex, true)); } throw; } }