Example #1
0
        /// <summary>

        /// Handles the parsed message after it is received

        /// </summary>

        /// <param name="obj">The <see cref="MessageHandler"/> object that parsed the message</param>

        private void HandleParsedMessage(object obj)

        {
            MessageHandler mh = (MessageHandler)obj;

            GNTPRequest request = mh.Request;



            try

            {
                Response response = null;

                switch (request.Directive)

                {
                case RequestType.REGISTER:

                    Application application = Application.FromHeaders(request.Headers);

                    List <NotificationType> notificationTypes = new List <NotificationType>();

                    for (int i = 0; i < request.NotificationsToBeRegistered.Count; i++)

                    {
                        HeaderCollection headers = request.NotificationsToBeRegistered[i];

                        notificationTypes.Add(NotificationType.FromHeaders(headers));
                    }

                    response = this.OnRegisterReceived(application, notificationTypes, mh.RequestInfo);

                    break;

                case RequestType.NOTIFY:

                    Notification notification = Notification.FromHeaders(request.Headers);

                    mh.CallbackInfo.NotificationID = notification.ID;

                    response = this.OnNotifyReceived(notification, mh.CallbackInfo, mh.RequestInfo);

                    break;

                case RequestType.SUBSCRIBE:

                    Subscriber subscriber = Subscriber.FromHeaders(request.Headers);

                    subscriber.IPAddress = mh.Socket.RemoteAddress.ToString();

                    subscriber.Key = new SubscriberKey(request.Key, subscriber.ID, request.Key.HashAlgorithm, request.Key.EncryptionAlgorithm);

                    response = this.OnSubscribeReceived(subscriber, mh.RequestInfo);

                    break;
                }



                ResponseType responseType = ResponseType.ERROR;

                if (response != null && response.IsOK)

                {
                    responseType = ResponseType.OK;

                    response.InResponseTo = request.Directive.ToString();
                }



                // no response

                if (response == null)
                {
                    response = new Response(ErrorCode.INTERNAL_SERVER_ERROR, ErrorDescription.INTERNAL_SERVER_ERROR);
                }



                AddServerHeaders(response);

                MessageBuilder mb = new MessageBuilder(responseType);

                HeaderCollection responseHeaders = response.ToHeaders();

                foreach (Header header in responseHeaders)

                {
                    mb.AddHeader(header);
                }

                // return any application-specific data headers that were received

                RequestData rd = RequestData.FromHeaders(request.Headers);

                AddRequestData(mb, rd);



                bool requestComplete = !mh.CallbackInfo.ShouldKeepConnectionOpen();

                mh.WriteResponse(mb, requestComplete);
            }

            catch (GrowlException gEx)

            {
                mh.WriteError(gEx.ErrorCode, gEx.Message, gEx.AdditionalInfo);
            }

            catch (Exception ex)

            {
                mh.WriteError(ErrorCode.INTERNAL_SERVER_ERROR, ex.Message);
            }
        }