示例#1
0
        /// <summary>
        /// Subscribes to channel.
        /// </summary>
        /// <param name="channelTag">The channel tag.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException">channel_tag</exception>
        public Subscription SubscribeToChannel(string channelTag)
        {
            #region pre-processing

            if (string.IsNullOrWhiteSpace(channelTag))
            {
                throw new ArgumentNullException("channel_tag");
            }

            #endregion pre-processing


            #region processing

            ChannelSubscriptionRequest request = new ChannelSubscriptionRequest()
            {
                ChannelTag = channelTag
            };

            BasicSubscription basicResponse = PostRequest <BasicSubscription>(string.Concat(PushbulletConstants.BaseUrl, PushbulletConstants.SubscriptionUrls.Subscriptions), request);
            Subscription      result        = ConvertFromBasicSubscription(basicResponse);
            return(result);

            #endregion processing
        }
示例#2
0
        ChannelSubscriptionResponse IChannelSubscription.SubscribeChannel(ChannelSubscriptionRequest request)
        {
            IEnumerable <KeyValuePair <string, string> > out_params;
            Dictionary <string, string> in_params = new Dictionary <string, string>();

            foreach (KeyValuePair kvp in request.param)
            {
                in_params.Add(kvp.name, kvp.value);
            }
            string clientid;

            try
            {
                clientid = _target.SubscribeClient(request.channelid, request.transport, in_params, out out_params);
            }
            catch
            {
                throw;
            }

            ChannelSubscriptionResponse ret = new ChannelSubscriptionResponse();

            ret.clientid = clientid;

            List <KeyValuePair> lst = new List <KeyValuePair>();

            foreach (KeyValuePair <string, string> kvp in out_params)
            {
                lst.Add(new KeyValuePair {
                    name = kvp.Key, value = kvp.Value
                });
            }
            ret.param = lst.ToArray();

            return(ret);
        }
示例#3
0
 /// <remarks/>
 public ChannelSubscriptionResponse SubscribeChannel(ChannelSubscriptionRequest request)
 {
     return(target.SubscribeChannel(request));
 }
        public override void Start()
        {
            if (Disposed)
            {
                throw new ObjectDisposedException(GetType().ToString());
            }
            if (_running)
            {
                throw new NotSupportedException("Client is already running");
            }
            try
            {
                CancelEventArgs arg = new CancelEventArgs();
                OnStarting(arg);
                if (arg.Cancel)
                {
                    return;
                }

                bool supported = false;
                foreach (string transport in ChannelSubscriber.GetAvailableTransports())
                {
                    if (transport == "udp")
                    {
                        supported = true;
                        break;
                    }
                }
                if (!supported)
                {
                    throw new NotSupportedException(
                              "Remote Logbus-ng node does not support TLS protocol for delivery. You must use a different client");
                }

                int port;
                //Decide on which address to listen
                IPAddress localIp = GetIpAddress();

                /* Workaround to Mono bug 643475, https://bugzilla.novell.com/show_bug.cgi?id=643475
                 * fixed since Mono master 512d3f2 and probably available from release next to 2.6.7
                 * */
#if MONO
                IPEndPoint[] eps = IPGlobalProperties.GetIPGlobalProperties().GetActiveUdpListeners();
#endif
                for (int i = START_PORT; i <= END_PORT; i++)
                {
#if MONO
                    //Workaround to Mono bug 64375
                    bool found = false;
                    for (int c = 0; c < eps.Length; c++)
                    {
                        if (eps[c].Port == i && (eps[c].Address.Equals(IPAddress.Any) || eps[c].Address.Equals(localIp)))
                        {
                            found = true;
                            break;
                        }
                    }
                    if (!found)
                    {
                        Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                        socket.Bind(new IPEndPoint(localIp, i));
                        _client = new UdpClient {
                            Client = socket
                        };
                        break;
                    }
#else
                    try
                    {
                        Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
                        {
                            ExclusiveAddressUse = true
                        };
                        socket.Bind(new IPEndPoint(localIp, i));

                        _client = new UdpClient {
                            Client = socket
                        };
                        break;
                    }
                    catch (SocketException)
                    {
                    }
#endif
                }
                //Unable to bind to one of the default ports.
                //Now pray your firewall is open to all UDP ports
                if (_client == null)
                {
                    _client = new UdpClient(new IPEndPoint(localIp, 0));
                }

                EndPoint ep = _client.Client.LocalEndPoint;
                if (ep is IPEndPoint)
                {
                    IPEndPoint ipe = (IPEndPoint)ep;
                    port = ipe.Port;
                }
                else
                {
                    throw new NotSupportedException("Only IP networks are supported");
                }

                _runningThread = new Thread(RunnerLoop)
                {
                    IsBackground = true
                };
                _runningThread.Start();


                ChannelSubscriptionRequest req = new ChannelSubscriptionRequest
                {
                    channelid = ChannelId,
                    transport = "udp",
                    param     = new[]
                    {
                        new KeyValuePair
                        {
                            name  = "port",
                            value =
                                port.ToString(
                                    CultureInfo.InvariantCulture)
                        },
                        new KeyValuePair
                        {
                            name = "ip", value = localIp.ToString()
                        }
                    }
                };
                ChannelSubscriptionResponse res = ChannelSubscriber.SubscribeChannel(req);
                _clientId   = res.clientid;
                _channelTtl = MAX_REFRESH_TIME;
                foreach (KeyValuePair kvp in res.param)
                {
                    if (kvp.name == "ttl")
                    {
                        if (long.TryParse(kvp.value, out _channelTtl))
                        {
                            break;
                        }
                    }
                }
                long refreshTime = Math.Min(_channelTtl * 4 / 5, MAX_REFRESH_TIME);
                //80% of the max TTL, but not over max TTL

                _refreshTimer = new Timer(RefreshChannel, _clientId, refreshTime, refreshTime);

                _running = true;

                OnStarted(EventArgs.Empty);
            }
            catch (LogbusException ex)
            {
                OnError(new UnhandledExceptionEventArgs(ex, true));

                throw;
            }
            catch (Exception ex)
            {
                OnError(new UnhandledExceptionEventArgs(ex, true));

                throw new LogbusException("Unable to subscribe channel", ex);
            }
        }
示例#5
0
        public override void Start()
        {
            if (Disposed)
            {
                throw new ObjectDisposedException(GetType().ToString());
            }
            if (Running)
            {
                throw new NotSupportedException("Client is already running");
            }
            try
            {
                Log.Info("TLS client starting");
                CancelEventArgs arg = new CancelEventArgs();
                OnStarting(arg);
                if (arg.Cancel)
                {
                    return;
                }

                bool supported = false;
                foreach (string transport in ChannelSubscriber.GetAvailableTransports())
                {
                    if (transport == "tls")
                    {
                        supported = true;
                        break;
                    }
                }
                if (!supported)
                {
                    throw new NotSupportedException(
                              "Remote Logbus-ng node does not support TLS protocol for delivery. You must use a different client");
                }


                int port;
                //Decide on which address to listen
                IPAddress localIp = GetIpAddress();
                _localhost = (IPAddress.IsLoopback(localIp)) ? "localhost" : Dns.GetHostName();


                for (int i = START_PORT; i <= END_PORT; i++)
                {
                    try
                    {
                        _server = new TcpListener(new IPEndPoint(localIp, i));
                        _server.Start(1);
                        break;
                    }
                    catch (SocketException)
                    {
                    }
                }
                //Unable to bind to one of the default ports.
                //Now pray your firewall is open to all TCP ports
                if (_server == null)
                {
                    _server = new TcpListener(new IPEndPoint(localIp, 0));
                    _server.Start();
                }

                EndPoint ep = _server.Server.LocalEndPoint;
                if (ep is IPEndPoint)
                {
                    IPEndPoint ipe = (IPEndPoint)ep;
                    port = ipe.Port;
                }
                else
                {
                    throw new NotSupportedException("Only IP networks are supported");
                }


                _listenerThread = new Thread(ListenerLoop);
                _listenerThread.Start();
                _processingThread = new Thread(ProcessLoop);
                _processingThread.Start();


                ChannelSubscriptionRequest req = new ChannelSubscriptionRequest
                {
                    channelid = ChannelId,
                    transport = "tls",
                    param     = new[]
                    {
                        new KeyValuePair
                        {
                            name  = "port",
                            value =
                                port.ToString(
                                    CultureInfo.InvariantCulture)
                        },
                        new KeyValuePair
                        {
                            name  = "host",
                            value = _localhost
                        },
                        new KeyValuePair
                        {
                            name  = "ip",
                            value = GetIpAddress().ToString()
                        }
                    }
                };
                ChannelSubscriptionResponse res = ChannelSubscriber.SubscribeChannel(req);
                _clientId = res.clientid;

                Running = true;

                OnStarted(EventArgs.Empty);
                Log.Info("TLS client started");
            }
            catch (LogbusException ex)
            {
                OnError(new UnhandledExceptionEventArgs(ex, true));
                Log.Error("Error starting TLS client");
                Log.Debug("Error details: {0}", ex.Message);

                throw;
            }
            catch (Exception ex)
            {
                OnError(new UnhandledExceptionEventArgs(ex, true));
                Log.Error("Error starting TLS client");
                Log.Debug("Error details: {0}", ex.Message);

                throw new LogbusException("Unable to subscribe channel", ex);
            }
        }