public void Notify(HttpRequest req, HttpResponse res)
        {
            var server = (SubscriberWebServer)req.Server;

            server.RaiseLogEvent("info", string.Format("received: {0}", req.RawBody));
            server.RaisePositionEvent(req.ParseJSON<PublishedResource<PositionEvent>>());

            res.SendJson(new { success = true });
        }
        public void Unsubscribe(HttpRequest req, HttpResponse res)
        {
            var data = req.ParseJSON<Subscription>();
            if (data == null || string.IsNullOrEmpty(data.ClientId) || string.IsNullOrEmpty(data.SubscribedEvent))
            {
                res.SendError(StatusCode.BadRequest, "Invalid data");
                return;
            }

            _subscriptions.RemoveAll(s => s.ClientId == data.ClientId && s.SubscribedEvent == data.SubscribedEvent);

            req.Server.RaiseLogEvent("info", string.Format("subsriber removed: {0}", req.RawBody));

            res.SendJson(new { success = true });
        }
        public void Publish(HttpRequest req, HttpResponse res)
        {
            var evt = req.ParseJSON<PublishedEvent>();
            var eventType = EventFactory.GetPublishedEventType(evt.Name);
            var evtData = req.ParseJSON(eventType) as PublishedEvent;

            if (evtData == null || eventType == null || evtData == null)
            {
                res.SendError(StatusCode.BadRequest, "Invalid data");
                return;
            }

            var subs = _subscriptions.Where(sub => sub.SubscribedEvent == evt.Name).ToList();
            req.Server.RaiseLogEvent("info", string.Format("event published: {0}", req.RawBody));

            // loop for all subscribers have registered for push notification
            foreach (var sub in subs)
            {
                var resource = EventFactory.CreatePublishedResource(evt.Name, sub.ClientId, evtData);

                if (!string.IsNullOrEmpty(sub.Endpoint))
                {
                    try
                    {
                        WebClient.PostJsonAsync(sub.Endpoint, resource, (responseText) =>
                        {
                            req.Server.RaiseLogEvent("info", string.Format("forwarded to subscriber {0}", sub.ClientId));
                        });
                    }
                    catch { }
                }
                else
                    _resources.Add(resource);
            }

            res.SendJson(new { success = true });
        }
 public void Ping(HttpRequest req, HttpResponse res)
 {
     req.Server.RaiseLogEvent("info", string.Format("ping success"));
     res.SendText("OK");
 }
        public void Acknowledge(HttpRequest req, HttpResponse res)
        {
            var data = req.QueryParameters;
            if (data.ContainsKey("id"))
            {
                res.SendError(StatusCode.BadRequest, "Invalid data");
                return;
            }

            var ids = data["id"].Split(',');

            _resources.RemoveAll(a => ids.Contains(a.Id));

            res.SendJson(new { success = true });
        }
        public void Pull(HttpRequest req, HttpResponse res)
        {
            var data = req.ParseJSON<Subscription>();
            if (data == null || string.IsNullOrEmpty(data.ClientId) || string.IsNullOrEmpty(data.SubscribedEvent))
            {
                res.SendError(StatusCode.BadRequest, "Invalid data");
                return;
            }

            var pulled = _resources.Where(r => r.ClientId == data.ClientId && r.EventName == data.SubscribedEvent).ToList();
            _resources.RemoveAll(r => r.ClientId == data.ClientId && r.EventName == data.SubscribedEvent);

            req.Server.RaiseLogEvent("info", string.Format("client {0} pulling...", data.ClientId));

            res.SendJson(new {items = pulled});
        }
Example #7
0
        private void HandleRequest(object _tcpClient)
        {
            TcpClient tcpClient = (TcpClient)_tcpClient;

            try
            {
                RaiseLogEvent("debug", string.Format("client {0} connected", tcpClient.Client.RemoteEndPoint.ToString()));

                HttpRequest request = null;

                try { request = new HttpRequest(this, tcpClient); }
                catch (Exception ex1) { RaiseLogEvent("error", ex1.ToString()); }

                HttpResponse response = new HttpResponse(this, tcpClient);

                if (request == null)
                    response.SendError(StatusCode.BadRequest, "Bad request");
                else if (!this.Configuration.Router.Contain(request.Method, request.Path))
                    response.SendError(StatusCode.NotFound, "Page not found");
                else
                {
                    MethodInfo webCtrl = this.Configuration.Router.Get(request.Method, request.Path);

                    ConstructorInfo ctor = webCtrl.DeclaringType.GetConstructor(new Type[]{});

                    object obj = ctor.Invoke(new Type[] { });

                    try { webCtrl.Invoke(obj, new object[] { request, response }); }
                    catch { response.SendError(StatusCode.InternalServerError, "Internal server error"); }
                }
            }
            catch (Exception ex)
            {
                RaiseLogEvent("error", ex.ToString());
                if (tcpClient != null)
                {
                    tcpClient.Client.Close();
                    tcpClient.Close();
                }
            }
        }