private static void SendToServer(string server, int port, IEnumerable <string> metrics, bool stream) { UdpMessenger messenger = null; SimpleObjectPool <UdpMessenger> serverPool = null; try { serverPool = _messengerPool.GetOrAdd(string.Format(CultureInfo.InvariantCulture, "{0}:{1}", server, port), new SimpleObjectPool <UdpMessenger>(3, pool => new UdpMessenger(server, port))); messenger = serverPool.Pop(); //all used up, sorry! if (null == messenger) { return; } if (stream) { messenger.StreamMetrics(metrics); } else { messenger.SendMetrics(metrics); } } finally { if (null != serverPool && null != messenger) { serverPool.Push(messenger); } } }
/// <summary> Initializes a new instance of the MetricClient class. </summary> /// <exception cref="ArgumentException"> Thrown when the hostNameOrAddress is null or empty OR the key contains invalid characters. </exception> /// <param name="hostNameOrAddress"> The DNS hostName or IPv4 or IPv6 address of the server. </param> /// <param name="port"> The port. </param> /// <param name="format"> Describes the metric format to use. </param> /// <param name="key"> The optional key to prefix metrics with. </param> public MetricClient(string hostNameOrAddress, int port, MetricFormat format, string key) { if (string.IsNullOrEmpty(hostNameOrAddress)) { throw new ArgumentException("cannot be null or empty", "hostNameOrAddress"); } if (!key.IsValidKey()) { throw new ArgumentException("contains invalid characters", "key"); } _messenger = new UdpMessenger(hostNameOrAddress, port); _key = key; _format = format; }
/// <summary> Initializes a new instance of the MetricClient class. </summary> /// <exception cref="ArgumentNullException"> Thrown when the configuration object is null. </exception> /// <param name="configuration"> The configuration. </param> public MetricClient(IMetricPublishingConfiguration configuration) { if (null == configuration) { throw new ArgumentNullException("configuration"); } if (string.IsNullOrEmpty(configuration.HostNameOrAddress)) { throw new ArgumentException("HostNameOrAddress cannot be null or empty", "configuration"); } if (!configuration.PrefixKey.IsValidKey()) { throw new ArgumentException("PrefixKey contains invalid characters", "configuration"); } _messenger = new UdpMessenger(configuration.HostNameOrAddress, configuration.Port); _key = configuration.PrefixKey; _format = configuration.Format; }
/// <summary> Initializes a new instance of the MetricClient class. </summary> /// <exception cref="ArgumentException"> Thrown when the hostNameOrAddress is null or empty OR the key contains invalid characters. </exception> /// <param name="hostNameOrAddress"> The DNS hostName or IPv4 or IPv6 address of the server. </param> /// <param name="port"> The port. </param> /// <param name="format"> Describes the metric format to use. </param> /// <param name="key"> The optional key to prefix metrics with. </param> public MetricClient(string hostNameOrAddress, int port, MetricFormat format, string key) { if (string.IsNullOrEmpty(hostNameOrAddress)) { throw new ArgumentException("cannot be null or empty", "hostNameOrAddress"); } if (!key.IsValidKey()) { throw new ArgumentException("contains invalid characters", "key"); } _messenger = new UdpMessenger(hostNameOrAddress, port); _key = key; _format = format; }
public Task CreateContinuousTask(CancellationToken cancellationToken) { return(new Task(() => { var reader = new PerfmonCounterReader(); using (var messenger = new UdpMessenger(_metricPublishingConfig.HostName, _metricPublishingConfig.Port)) { foreach (var metricBatch in reader.StreamCounterSamples(_counterPaths, _counterSamplingConfig.SampleInterval, cancellationToken) .SelectMany(set => set.CounterSamples.ToGraphiteString(_metricPublishingConfig.PrefixKey)) .Chunk(10)) { messenger.SendMetrics(metricBatch); } } }, cancellationToken)); }
/// <summary> Initializes a new instance of the MetricClient class. </summary> /// <exception cref="ArgumentNullException"> Thrown when the configuration object is null. </exception> /// <param name="configuration"> The configuration. </param> public MetricClient(IMetricPublishingConfiguration configuration) { if (null == configuration) { throw new ArgumentNullException("configuration"); } if (string.IsNullOrEmpty(configuration.HostNameOrAddress)) { throw new ArgumentException("HostNameOrAddress cannot be null or empty", "configuration"); } if (!configuration.PrefixKey.IsValidKey()) { throw new ArgumentException("PrefixKey contains invalid characters", "configuration"); } _messenger = new UdpMessenger(configuration.HostNameOrAddress, configuration.Port); _key = configuration.PrefixKey; _format = configuration.Format; }
public void init(AppInfo appInfo) { messenger = UdpMessenger.getInstance("127.0.0.1", 5556, appInfo); messenger.addMessageListener(messageHandler); }
/// <summary> /// Main Background worker thread entry /// </summary> /// <param name="bgwContext">The command and control struct</param> public void DoWork(object sender, BgwSwitchPanelContext bgwContext) { m_worker = sender as BackgroundWorker; m_worker.ReportProgress(0, "SwitchPanelSupport - About to start"); // first message.. m_bgwContext = bgwContext; m_pfspManager = new PFSwPanelManager( ); bool abort = false; bool eventsHooked = false; // Try to connect to the HID driver if (m_pfspManager.OpenDevice( )) { // attach to the events from the HID device m_pfspManager.DeviceAttached += PfspManager_DeviceAttached; m_pfspManager.DeviceRemoved += PfspManager_DeviceRemoved; m_pfspManager.ButtonUp += PfspManager_ButtonUp; m_pfspManager.ButtonDown += PfspManager_ButtonDown; m_pfspManager.RotaryChanged += PfspManager_RotaryChanged; eventsHooked = true; m_pfspManager.SetLed(PFSwPanelLeds.GEAR_L, PFSwPanelLedState.Led_Off); m_pfspManager.SetLed(PFSwPanelLeds.GEAR_R, PFSwPanelLedState.Led_Off); m_pfspManager.SetLed(PFSwPanelLeds.GEAR_N, PFSwPanelLedState.Led_Off); // Try to load the config file m_panelConfig = SwitchPanelConfig.FromJson(m_bgwContext.ConfigFile); if (m_panelConfig.Valid) { m_worker.ReportProgress(0, $" Config: {m_panelConfig.ConfigName}"); // first message.. } else { m_worker.ReportProgress(0, $" Invalid Config File - aborting"); // first message.. m_worker.ReportProgress(0, $" {SwitchPanelConfig.ErrorMsg}"); // first message.. abort = true; } } else { m_worker.ReportProgress(0, "SwitchPanelSupport - Device open failed"); // first message.. abort = true; } // Try to connect the JoystickServer if (int.TryParse(m_bgwContext.ServerPort, out int portNo)) { if (UdpMessenger.CheckIP(m_bgwContext.ServerIP)) { m_udp = new UdpMessenger(m_bgwContext.ServerIP, portNo); } else { m_worker.ReportProgress(0, $"SwitchPanelSupport - Invalid IP # {m_bgwContext.ServerIP}"); } } else { m_worker.ReportProgress(0, $"SwitchPanelSupport - Invalid Port number # {m_bgwContext.ServerPort}"); } // Report about incidents.. // Task loop - wait until killed - handling happens in the events while (!abort) { if (m_bgwContext.LedChanged) { m_pfspManager.SetLed(PFSwPanelLeds.GEAR_L, m_bgwContext.L_Led); m_pfspManager.SetLed(PFSwPanelLeds.GEAR_N, m_bgwContext.N_Led); m_pfspManager.SetLed(PFSwPanelLeds.GEAR_R, m_bgwContext.R_Led); m_bgwContext.LedChanged = false; // commit } Thread.Sleep(200); // check every 200 ms for abort or LED changes abort = m_worker.CancellationPending; // check once in a while } // clean UDP m_udp = null; // clean HID up // if the device is still attached if (m_devAttached) { // final message... m_pfspManager.SetLed(PFSwPanelLeds.GEAR_L, PFSwPanelLedState.Led_Red); m_pfspManager.SetLed(PFSwPanelLeds.GEAR_R, PFSwPanelLedState.Led_Green); m_pfspManager.SetLed(PFSwPanelLeds.GEAR_N, PFSwPanelLedState.Led_Amber); m_pfspManager.CloseDevice( ); } // events if (eventsHooked) { m_pfspManager.DeviceAttached -= PfspManager_DeviceAttached; m_pfspManager.DeviceRemoved -= PfspManager_DeviceRemoved; m_pfspManager.ButtonUp -= PfspManager_ButtonUp; m_pfspManager.ButtonDown -= PfspManager_ButtonDown; m_pfspManager.RotaryChanged -= PfspManager_RotaryChanged; } // the HID object m_pfspManager.Dispose( ); m_pfspManager = null; m_worker.ReportProgress(100, "SwitchPanelSupport - Ending now"); // final message.. }