コード例 #1
0
ファイル: ClientEvents.cs プロジェクト: xiaguoli/IoTGateway
        private static void QueueCacheItem_Removed(object Sender, CacheItemEventArgs <string, TabQueue> e)
        {
            TabQueue Queue = e.Value;
            string   TabID = Queue.TabID;
            string   Location;

            Queue.Queue.Clear();

            lock (locationByTabID)
            {
                if (locationByTabID.TryGetValue(TabID, out Location))
                {
                    locationByTabID.Remove(TabID);
                }
                else
                {
                    Location = null;
                }
            }

            if (Location != null)
            {
                lock (tabIdsByLocation)
                {
                    if (tabIdsByLocation.TryGetValue(Location, out Dictionary <string, List <KeyValuePair <string, string> > > TabIDs))
                    {
                        if (TabIDs.Remove(TabID) && TabIDs.Count == 0)
                        {
                            tabIdsByLocation.Remove(Location);
                        }
                    }
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Executes the POST method on the resource.
        /// </summary>
        /// <param name="Request">HTTP Request</param>
        /// <param name="Response">HTTP Response</param>
        /// <exception cref="HttpException">If an error occurred when processing the method.</exception>
        public async Task POST(HttpRequest Request, HttpResponse Response)
        {
            if (!Request.HasData || Request.Session is null)
            {
                throw new BadRequestException();
            }

            // TODO: Check User authenticated

            object Obj   = Request.DecodeData();
            string TabID = Request.Header["X-TabID"];

            if (!(Obj is string Location) || string.IsNullOrEmpty(TabID))
            {
                throw new BadRequestException();
            }

            TabQueue      Queue = Register(Request, null, Location, TabID);
            StringBuilder Json  = null;

            Response.ContentType = "application/json";

            if (!await Queue.SyncObj.TryBeginWrite(10000))
            {
                throw new InternalServerErrorException("Unable to get access to queue.");
            }

            try
            {
                if (!(Queue.Queue.First is null))
                {
                    foreach (string Event in Queue.Queue)
                    {
                        if (Json is null)
                        {
                            Json = new StringBuilder("[");
                        }
                        else
                        {
                            Json.Append(',');
                        }

                        Json.Append(Event);
                    }

                    Queue.Queue.Clear();
                    Queue.Response = null;
                }
                else
                {
                    Queue.Response = Response;
                }
            }
コード例 #3
0
ファイル: ClientEvents.cs プロジェクト: live0717/IoTGateway
        /// <summary>
        /// Executes the POST method on the resource.
        /// </summary>
        /// <param name="Request">HTTP Request</param>
        /// <param name="Response">HTTP Response</param>
        /// <exception cref="HttpException">If an error occurred when processing the method.</exception>
        public void POST(HttpRequest Request, HttpResponse Response)
        {
            if (!Request.HasData || Request.Session is null)
            {
                throw new BadRequestException();
            }

            // TODO: Check User authenticated

            object Obj   = Request.DecodeData();
            string TabID = Request.Header["X-TabID"];

            if (!(Obj is string Location) || string.IsNullOrEmpty(TabID))
            {
                throw new BadRequestException();
            }

            TabQueue      Queue = Register(Request, null, Location, TabID);
            StringBuilder Json  = null;

            Response.ContentType = "application/json";

            lock (Queue)
            {
                if (!(Queue.Queue.First is null))
                {
                    foreach (string Event in Queue.Queue)
                    {
                        if (Json is null)
                        {
                            Json = new StringBuilder("[");
                        }
                        else
                        {
                            Json.Append(',');
                        }

                        Json.Append(Event);
                    }

                    Queue.Queue.Clear();
                    Queue.Response = null;
                }
コード例 #4
0
ファイル: ClientEvents.cs プロジェクト: orf53975/IoTGateway
        /// <summary>
        /// Executes the POST method on the resource.
        /// </summary>
        /// <param name="Request">HTTP Request</param>
        /// <param name="Response">HTTP Response</param>
        /// <exception cref="HttpException">If an error occurred when processing the method.</exception>
        public void POST(HttpRequest Request, HttpResponse Response)
        {
            if (!Request.HasData || Request.Session == null)
            {
                throw new BadRequestException();
            }

            // TODO: Check User authenticated

            object Obj   = Request.DecodeData();
            string TabID = Request.Header["X-TabID"];

            if (!(Obj is string Location) || string.IsNullOrEmpty(TabID))
            {
                throw new BadRequestException();
            }

            Uri    Uri      = new Uri(Location);
            string Resource = Uri.LocalPath;
            List <KeyValuePair <string, string> > Query = null;
            string s;

            if (!string.IsNullOrEmpty(Uri.Query))
            {
                Query = new List <KeyValuePair <string, string> >();
                int i;

                s = Uri.Query;
                if (s.StartsWith("?"))
                {
                    s = s.Substring(1);
                }

                foreach (string Part in s.Split('&'))
                {
                    i = Part.IndexOf('=');
                    if (i < 0)
                    {
                        Query.Add(new KeyValuePair <string, string>(Part, string.Empty));
                    }
                    else
                    {
                        Query.Add(new KeyValuePair <string, string>(Part.Substring(0, i), Part.Substring(i + 1)));
                    }
                }
            }

            Response.ContentType = "application/json";

            if (!eventsByTabID.TryGetValue(TabID, out TabQueue Queue))
            {
                HttpFieldCookie Cookie        = Request.Header.Cookie;
                string          HttpSessionID = Cookie == null ? string.Empty : Cookie["HttpSessionID"];

                Queue = new TabQueue(TabID, HttpSessionID, Request.Session);
                eventsByTabID[TabID] = Queue;
            }

            lock (locationByTabID)
            {
                if (!locationByTabID.TryGetValue(TabID, out s) || s != Resource)
                {
                    locationByTabID[TabID] = Resource;
                }
            }

            lock (tabIdsByLocation)
            {
                if (!tabIdsByLocation.TryGetValue(Resource, out Dictionary <string, List <KeyValuePair <string, string> > > TabIds))
                {
                    TabIds = new Dictionary <string, List <KeyValuePair <string, string> > >();
                    tabIdsByLocation[Resource] = TabIds;
                }

                TabIds[TabID] = Query;
            }

            StringBuilder Json = null;

            lock (Queue)
            {
                if (Queue.Queue.First != null)
                {
                    foreach (string Event in Queue.Queue)
                    {
                        if (Json == null)
                        {
                            Json = new StringBuilder("[");
                        }
                        else
                        {
                            Json.Append(',');
                        }

                        Json.Append(Event);
                    }

                    Queue.Queue.Clear();
                    Queue.Response = null;
                }
                else
                {
                    Queue.Response = Response;
                }
            }

            if (Json != null)
            {
                timeoutByTabID.Remove(TabID);

                Json.Append(']');
                Response.Write(Json.ToString());
                Response.SendResponse();
                Response.Dispose();
            }
            else
            {
                timeoutByTabID[TabID] = Queue;
            }
        }
コード例 #5
0
ファイル: ClientEvents.cs プロジェクト: orf53975/IoTGateway
        internal static void RegisterWebSocket(WebSocket Socket, string Location, string TabID)
        {
            Uri    Uri      = new Uri(Location);
            string Resource = Uri.LocalPath;
            List <KeyValuePair <string, string> > Query = null;
            string s;

            if (!string.IsNullOrEmpty(Uri.Query))
            {
                Query = new List <KeyValuePair <string, string> >();
                int i;

                s = Uri.Query;
                if (s.StartsWith("?"))
                {
                    s = s.Substring(1);
                }

                foreach (string Part in s.Split('&'))
                {
                    i = Part.IndexOf('=');
                    if (i < 0)
                    {
                        Query.Add(new KeyValuePair <string, string>(Part, string.Empty));
                    }
                    else
                    {
                        Query.Add(new KeyValuePair <string, string>(Part.Substring(0, i), Part.Substring(i + 1)));
                    }
                }
            }

            if (eventsByTabID.TryGetValue(TabID, out TabQueue Queue))
            {
                Queue.WebSocket = Socket;
            }
            else
            {
                HttpFieldCookie Cookie        = Socket.HttpRequest.Header.Cookie;
                string          HttpSessionID = Cookie == null ? string.Empty : Cookie["HttpSessionID"];

                Queue = new TabQueue(TabID, HttpSessionID, Socket.HttpRequest.Session)
                {
                    WebSocket = Socket
                };

                eventsByTabID[TabID] = Queue;
            }

            lock (locationByTabID)
            {
                if (!locationByTabID.TryGetValue(TabID, out s) || s != Resource)
                {
                    locationByTabID[TabID] = Resource;
                }
            }

            lock (tabIdsByLocation)
            {
                if (!tabIdsByLocation.TryGetValue(Resource, out Dictionary <string, List <KeyValuePair <string, string> > > TabIds))
                {
                    TabIds = new Dictionary <string, List <KeyValuePair <string, string> > >();
                    tabIdsByLocation[Resource] = TabIds;
                }

                TabIds[TabID] = Query;
            }

            LinkedList <string> ToSend = null;

            lock (Queue)
            {
                if (Queue.Queue.First != null)
                {
                    ToSend = new LinkedList <string>();

                    foreach (string s2 in Queue.Queue)
                    {
                        ToSend.AddLast(s2);
                    }

                    Queue.Queue.Clear();
                }
            }

            if (ToSend != null)
            {
                foreach (string s2 in ToSend)
                {
                    Socket.Send(s2, 4096);
                }
            }
        }
コード例 #6
0
ファイル: TabQueue.cs プロジェクト: garyhertel/Atlas
 public Instance(TabQueue tab)
 {
     Tab = tab;
 }