Using the ATL Server Data Source Cache


The data source cache is available to the ATL Server application as a service by the ATL Server ISAPI extension. Recall that the ATL Server ISAPI extension is the code that routes requests to your ATL Server application. In many cases, you ll build one ATL Server ISAPI extension that can be used by numerous ATL Server applications.

You can add the data source cache service to your ATL Server ISAPI extension by simply choosing it as an option in the ATL Server Project Wizard, as shown in Figure 16-7.

click to expand
Figure 16-7. Adding the data source cache service in the ATL Server Project Wizard

Alternately, you can incorporate the code that you ll walk through in this chapter into your existing ATL Server ISAPI extension.

When you choose the data source cache option, the ATL Server Project Wizard generates the code in Listing 16-2.

Listing 16.2: Using the ATL Server Data Source Cache
start example
 1. #pragma once  2.  3. #include <atlisapi.h>  4. #define _DATASOURCE_CACHE 1  5. // CDataAccessExtensionWorker - custom thread worker class  6. // for per-thread services  7.  8. class CDataAccessExtensionWorker : public CIsapiWorker  9. {  10.    // per thread data source cache  11.    typedef CDataSourceCache<>      ds_cache_type;  12.    CComObjectGlobal<ds_cache_type> m_dsCache;  13.  14. public:  15.  16.    CDataAccessExtensionWorker()  17.    {  18.    }  19.  20.    ~CDataAccessExtensionWorker()  21.    {  22.    }  23.  24.  25.    virtual BOOL GetWorkerData(DWORD dwParam, void **ppvData)  26.    {  27.        if (dwParam == _DATASOURCE_CACHE && ppvData)  28.        {  29.            *ppvData = (void *)&m_dsCache;  30.            m_dsCache.AddRef();  31.            return TRUE;  32.        }  33.        return FALSE;  34.    }  35. }; // class CDataAccessExtensionWorker  36.  37. // CDataAccessExtension - the ISAPI extension class  38. template <class ThreadPoolClass=CThreadPool<CDataAccessExtensionWorker>,  39.           class CStatClass=CNoRequestStats,  40.           class HttpUserErrorTextProvider=CDefaultErrorProvider,  41.           class WorkerThreadTraits=DefaultThreadTraits >  42.           class CDataAccessExtension :  43.    public  CIsapiExtension<ThreadPoolClass,  44.            CStatClass,  45.            HttpUserErrorTextProvider,  46.            WorkerThreadTraits>  47. {  48.  49. protected:  50.  51.    typedef CIsapiExtension<ThreadPoolClass,  52.                            CStatClass,  53.                            HttpUserErrorTextProvider,  54.                            WorkerThreadTraits> baseISAPI;  55.    typedef CWorkerThread<WorkerThreadTraits> WorkerThreadClass;  56.  57. public:  58.  59.     BOOL GetExtensionVersion(HSE_VERSION_INFO* pVer)  60.     {  61.         if (!baseISAPI::GetExtensionVersion(pVer))  62.         {  63.             return FALSE;  64          }  65.     }  66.  67.     if (GetCriticalIsapiError() != 0)  68.     {  69.       return TRUE;  70.     }  71.  72.     return TRUE;  73.    }  74.  75.    BOOL TerminateExtension(DWORD dwFlags)  76.    {  77.        BOOL bRet = baseISAPI::TerminateExtension(dwFlags);  78.        return bRet;  79.    }  80.  81.    HRESULT STDMETHODCALLTYPE QueryService(REFGUID guidService,  82.                                           REFIID  riid,  83.                                           void**  ppvObject)  84.    {  85.        if (InlineIsEqualGUID(guidService,  86.            __uuidof(IDataSourceCache)))  87.        {  88.            CIsapiWorker *pWorker = GetThreadWorker();  89.            if (pWorker)  90.            {  91.                CDataSourceCache<> *pCache = NULL;  92.                if (pWorker->GetWorkerData(_DATASOURCE_CACHE,  93                                            (void **)&pCache))  94.                {  95.                    *ppvObject = static_cast<IDataSourceCache *>(pCache);  96.                    return S_OK;  97.                }  98.            }  99.        }  100.       return baseISAPI::QueryService(guidService,  101.                                      riid,  102.                                      ppvObject);  103.   }  104.  105.   virtual void OnThreadTerminate(DWORD /*dwThreadId*/)  106.   {  107.   }  108. } 
end example
 

Let s take a look at this code line-by-line :

  • Line 11: The data source cache is implemented by the CDataSourceCache class. This class is actually a template; by default it doesn t do synchronization. If you want to use this class from multiple threads in the same request handler, then you should specify CComCriticalSection as a template parameter.

  • Line 12: You ll make your instance of CDataSourceCache global to the application.

  • Lines 25 through 33: The GetWorkerData method is called once for each worker thread. You want to increment the reference count so that it s in synch with the number of worker threads in the thread pool maintained by ATL Server.

  • Line 38: You re using the CDataAccessExtensionWorker class in the thread pool instead of the default thread worker class.

  • Lines 81 through 103: The QueryService method is how all services are exposed by an ATL Server ISAPI extension. Line 82 determines whether the service being queried is the data source cache service. If it is, you get the reference to the data source cache from the worker thread by calling GetWorkerData and returning that interface to the caller. This if statement contains a clause for each service your extension supports.

Now that you understand how a service is exposed by the ATL Server ISAPI extension, in the next section you ll look at how your ATL Server application can use this service.




ATL Server. High Performance C++ on. NET
Observing the User Experience: A Practitioners Guide to User Research
ISBN: B006Z372QQ
EAN: 2147483647
Year: 2002
Pages: 181

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net