internal void ListenerCallbackAsync()

in src/Library/ClusterInfoListener.cs [86:135]


        internal void ListenerCallbackAsync(IAsyncResult result)
        {
            if (this.IsRunning)
            {
                HttpListener listener = (HttpListener)result.AsyncState;
                HttpListenerContext context = listener.EndGetContext(result);
                HttpListenerRequest request = context.Request;
                HttpListenerResponse response = context.Response;
                try
                {

                    if (!request.HttpMethod.Equals("Post", StringComparison.InvariantCultureIgnoreCase))
                    {
                        //we only support post method
                        Diagnostics.LogInfo(FormattableString.Invariant($"invalid http method : {request.HttpMethod}"));
                        response.StatusCode = (int)HttpStatusCode.MethodNotAllowed;
                    }
                    else if (!request.Headers["Content-Type"].Equals("application/json"))
                    {
                        // we only support json payloads
                        Diagnostics.LogInfo(FormattableString.Invariant($"invalid content type : {request.Headers["Content-Type"]}"));
                        //https://tools.ietf.org/html/rfc7231#section-6.5.13 
                        response.StatusCode = (int)HttpStatusCode.UnsupportedMediaType;
                    }
                    else
                    {
                        DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(ClusterIdPayloadObject));
                        ClusterIdPayloadObject payloadObject = (ClusterIdPayloadObject)serializer.ReadObject(request.InputStream);
                        Diagnostics.LogInfo(FormattableString.Invariant($"received payload with cluster id : {payloadObject.clusterId}"));

                        HeartbeatCustomizer heartbeat = new HeartbeatCustomizer();
                        heartbeat.UpdateClusterId(payloadObject.clusterId);
                        response.StatusCode = (int)HttpStatusCode.Accepted;
                    }

                }
                catch (Exception e)
                {
                    Diagnostics.LogError($"error processing request : {e.ToString()}");
                    response.StatusCode = (int)HttpStatusCode.BadRequest;
                }
                finally
                {
                    response.Close();
                }

                this.listener.BeginGetContext(new AsyncCallback(this.ListenerCallbackAsync), this.listener);
                Diagnostics.LogInfo("Restarting listening");
            }
        }