/// <summary>
        /// Adds the specified resource to the cache
        /// </summary>
        /// <param name="applicationName">The application that owns the resource</param>
        /// <param name="binaryData"><see cref="BinaryData"/> to cache</param>
        public static void Add(string applicationName, BinaryData binaryData)
        {
            if (IsCacheConfigured)
            {
                string        path         = GetApplicationCacheDirectoryName(applicationName);
                DirectoryInfo appDirectory = CreateAppCacheDirectory(path); // do this every time, in case the underlying filesystem has changed
                if (!cache.ContainsKey(appDirectory.FullName))
                {
                    cache.Add(appDirectory.FullName, new Dictionary <string, string>());
                }

                Dictionary <string, string> appCache = cache[appDirectory.FullName];
                if (appCache.ContainsKey(binaryData.ID))
                {
                    appCache.Remove(binaryData.ID);
                }


                string filename = String.Format(@"{0}{1}", binaryData.ID, EXTENSION);
                string filepath = PathUtility.Combine(appDirectory.FullName, filename);
                if (WriteFile(filepath, binaryData.Data))
                {
                    appCache.Add(binaryData.ID, filepath);
                }
            }
        }
        /// <summary>
        /// Gets the application-specific cache folder name
        /// </summary>
        /// <param name="applicationName">The application that owns the resources</param>
        /// <returns>string - folder path</returns>
        private static string GetApplicationCacheDirectoryName(string applicationName)
        {
            string safeAppName = PathUtility.GetSafeFolderName(applicationName);
            string path        = PathUtility.Combine(resourceFolder, safeAppName);

            return(path);
        }
예제 #3
0
        /// <summary>
        /// Creates a new instance of the Growl server.
        /// </summary>
        /// <param name="port">The port to listen on. The standard GNTP port is <see cref="ConnectorBase.TCP_PORT"/>.</param>
        /// <param name="passwordManager">The <see cref="PasswordManager"/> containing the list of allowed passwords.</param>
        /// <param name="userFolder">The full path to the user folder where logs, resource cache, and other files will be stored.</param>
        public GrowlServer(int port, PasswordManager passwordManager, string userFolder)
        {
            // this will set the server name and version properly
            ServerName = serverName;

            this.port = (ushort)port;

            this.passwordManager = passwordManager;

            this.userFolder = userFolder;

            this.logFolder = PathUtility.Combine(userFolder, @"Log\");
            PathUtility.EnsureDirectoryExists(this.logFolder);

            this.resourceFolder = PathUtility.Combine(userFolder, @"Resources\");
            PathUtility.EnsureDirectoryExists(this.resourceFolder);

            this.listenSocket = new AsyncSocket();
            this.listenSocket.AllowMultithreadedCallbacks = true; // VERY IMPORTANT: if we dont set this, async socket events will silently be swallowed by the AsyncSocket class
            listenSocket.DidAccept += new AsyncSocket.SocketDidAccept(listenSocket_DidAccept);
            //listenSocket.DidClose += new AsyncSocket.SocketDidClose(listenSocket_DidClose);

            // Initialize list to hold connected sockets
            // We support multiple concurrent connections
            connectedSockets            = new ConnectedSocketCollection();
            connectedHandlers           = new Dictionary <AsyncSocket, MessageHandler>();
            socketCleanupTimer          = new System.Timers.Timer(30 * 1000);
            socketCleanupTimer.Elapsed += new System.Timers.ElapsedEventHandler(socketCleanupTimer_Elapsed);

            ResourceCache.ResourceFolder = this.resourceFolder;
            ResourceCache.Enabled        = true;

            this.bonjour = new BonjourService(BonjourServiceName, BONJOUR_SERVICE_TYPE);
        }