/// <summary>
 /// Subscribes a client to a tile
 /// </summary>
 public static void SubscribeTile(DTSession session, DynamicTileTarget target, int token)
 {
     lock (subscriptions)
     {
         subscriptions.Add(new TileSubscription
         {
             session = session,
             target  = target,
             token   = token
         });
     }
 }
        /// <summary>
        /// Sends events to all subscribed clients
        /// </summary>
        /// <param name="target"></param>
        /// <param name="url"></param>
        public static void SendEvent(DynamicTileTarget target, string url)
        {
            //Find targets
            List <TileSubscription> targets;

            lock (subscriptions)
                targets = subscriptions.Where(x => x.target.Compare(target)).ToList();

            //Send targets
            foreach (var t in targets)
            {
                t.session.SendTileLoadMessage(t.target.map_id, t.token, url);
            }
        }
        /// <summary>
        /// Called when an updated tile is needed
        /// </summary>
        public bool RequestImage(DynamicTileTarget target, bool highPriority, DbServer sdata)
        {
            //Check if any servers exist
            if (servers.Count == 0)
            {
                return(false);
            }

            //Loop through servers and find server with the least number of requests pending
            int           min    = int.MaxValue;
            BuilderServer server = null;

            lock (servers)
            {
                foreach (var s in servers)
                {
                    if (s.pending.Count < min)
                    {
                        min    = s.pending.Count;
                        server = s;
                    }
                }
            }

            //If no servers were found, return false
            if (server == null)
            {
                return(false);
            }

            //Add this tile to the list
            server.ProcessTile(new DynamicTileBuilderRequest
            {
                highPriority          = highPriority,
                target                = target,
                structure_revision_id = sdata.revision_id_structures
            });
            return(true);
        }