/// <summary> /// Implements IOutboundTransport.UnsubscribeClient /// </summary> public void UnsubscribeClient(string clientId) { _listlock.AcquireReaderLock(DEFAULT_JOIN_TIMEOUT); try { if (!_clients.ContainsKey(clientId)) { throw new ClientNotSubscribedException(); } LockCookie ck = _listlock.UpgradeToWriterLock(DEFAULT_JOIN_TIMEOUT); try { UdpClientExpire client = _clients[clientId]; _clients.Remove(clientId); client.Client.Close(); } catch (Exception ex) { throw new TransportException("Unable to unsubscribe client", ex); } finally { _listlock.DowngradeFromWriterLock(ref ck); } } finally { _listlock.ReleaseReaderLock(); } }
/// <summary> /// /// </summary> /// <param name="inputInstructions">List of input parameters (required): /// <list> /// <item>ip: IP address of client, or of another machine that will receive logs</item> /// <item>port: UDP port on which the destination will be listening</item> /// </list> /// </param> /// <param name="outputInstructions">List of client instructions: /// <list> /// <item>ttl: Time To Live in milliseconds</item> /// </list> /// </param> /// <returns></returns> public string SubscribeClient(IEnumerable <KeyValuePair <string, string> > inputInstructions, out IEnumerable <KeyValuePair <string, string> > outputInstructions) { if (_disposed) { throw new ObjectDisposedException(GetType().FullName); } outputInstructions = new Dictionary <string, string>(); ((Dictionary <string, string>)outputInstructions).Add("ttl", SubscriptionTtl.ToString()); try { string ipstring = null, portstring = null; foreach (KeyValuePair <string, string> kvp in inputInstructions) { if (kvp.Key.Equals("ip")) { ipstring = kvp.Value; } if (kvp.Key.Equals("port")) { portstring = kvp.Value; } } if (string.IsNullOrEmpty(ipstring)) { throw new TransportException("Field \"ip\" is required for subscription"); } int port; if (!int.TryParse(portstring, NumberStyles.Integer, CultureInfo.InvariantCulture, out port)) { throw new TransportException("Invalid port number"); } if (port < 1 || port > 65535) { throw new TransportException("Invalid port number"); } IPAddress client; if (!IPAddress.TryParse(ipstring, out client)) { throw new TransportException("Invalid IP address"); } string clientid = ipstring + ":" + port; UdpClientExpire newClient = new UdpClientExpire { Client = new UdpClient(ipstring, port), LastRefresh = DateTime.Now }; _listlock.AcquireWriterLock(DEFAULT_JOIN_TIMEOUT); try { _clients.Add(clientid, newClient); } finally { _listlock.ReleaseWriterLock(); } return(clientid); } catch (TransportException ex) { ex.Data["input"] = inputInstructions; throw; } catch (Exception ex) { TransportException e = new TransportException("Unable to subscribe client", ex); if (inputInstructions != null) { e.Data["input"] = inputInstructions; } throw e; } }