public void SendEcho() { if (string.IsNullOrEmpty(_virtualHost)) { throw new Exception("virtualHost is not defined"); } var link = State.Links.First(restLink => restLink.Relation == "http://api.sportingsolutions.com/rels/stream/batchecho"); var echouri = new Uri(link.Href); var streamEcho = new StreamEcho { Host = _virtualHost, Message = Guid.NewGuid() + ";" + DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss.fffZ") }; var response = ConnectClient.Request(echouri, RestSharp.Method.POST, streamEcho, UDAPI.Configuration.ContentType, 3000); if (response.ErrorException != null || response.Content == null) { RestErrorHelper.LogRestError(Logger, response, "Error sending echo request"); throw new Exception(string.Format("Error calling {0}", echouri), response.ErrorException); } }
private void SendEcho(CancellationToken cancelToken) { var echoGuid = Guid.NewGuid().ToString(); while (_isStreaming) { var indexofSignal = WaitHandle.WaitAny(new[] { _echoTimerEvent, cancelToken.WaitHandle }, _echoSenderInterval); if (indexofSignal == 1) { return; } if (cancelToken.IsCancellationRequested) { return; } if (_isProcessingStreamEvent) { continue; } try { if (State != null) { var theLink = State.Links.First( restLink => restLink.Relation == "http://api.sportingsolutions.com/rels/stream/echo"); var theUrl = theLink.Href; var streamEcho = new StreamEcho { Host = _virtualHost, Queue = _queueName, Message = echoGuid + ";" + DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss.fffZ") }; var stringStreamEcho = streamEcho.ToJson(); RestHelper.GetResponse(new Uri(theUrl), stringStreamEcho, "POST", "application/json", Headers, 3000); } } catch (Exception ex) { _logger.Error(string.Format("Unable to post echo for fixtureName=\"{0}\" fixtureId={1}", Name, Id), ex); } var waitHandleResult = WaitHandle.WaitAny(new[] { _echoResetEvent, cancelToken.WaitHandle }, _echoMaxDelay); var echoArrived = false; if (waitHandleResult == 0) { echoArrived = true; } else if (waitHandleResult == 1) { return; } _echoResetEvent.Reset(); if (cancelToken.IsCancellationRequested) { return; } //signal was recieved if (echoArrived) { if (echoGuid.Equals(_lastRecievedEchoGuid)) { _logger.DebugFormat("Echo recieved for fixtureId={0} fixtureName=\"{1}\"", Id, Name); } else { _logger.Error("Recieved Echo Messages from differerent client"); } } else { if (!_isProcessingStreamEvent) { _logger.InfoFormat("No echo recieved for fixtureId={0} fixtureName=\"{1}\"", Id, Name); LastStreamDisconnect = DateTime.UtcNow; //reached timeout, no echo has arrived _isReconnecting = true; Reconnect(); _echoTimerEvent.Set(); _isReconnecting = false; if (cancelToken.IsCancellationRequested) { return; } } } } _logger.DebugFormat("Echo successfully cancelled for fixtureId={0} fixtureName=\"{1}\"", Id, Name); }