Main Page | Class Hierarchy | Class List | File List | Class Members

ClientCacheManager.cs

00001 using System;
00002 using System.Collections;
00003 using System.Collections.Specialized;
00004 using Common;
00005 
00006 namespace Client {
00007         /*************************************************************************/
00008         public class ClientCacheManager : CacheManager, IResponseHandler {
00009                 readonly long CLIENT_CACHE_SIZE;
00010                 ClientControl clientControl;
00011 
00012                 public ClientCacheManager(ClientControl clientControl) : base(clientControl.settings.CacheSize) {
00013                         this.clientControl = clientControl;
00014                         CLIENT_CACHE_SIZE = clientControl.settings.CacheSize;
00015                 }
00016 
00017                 public void ServiceReq(HTTPRequest req, Connection client) {
00018                         // we assume that req.uri is in the cache... by the time we're here.
00019                         if (!uriMapper.IsFresh(req.URI)) { // if URL <-> CHK mapping is not fresh
00020                                 // send 'freshen up' request
00021 //                              Console.WriteLine("- URI Mapping for {0} is not fresh. Spawning HTTP Request", req.URI);
00022                                 HTTPResponseHandler responseHandler = new HTTPResponseHandler(req, client, clientControl);
00023 //                              clientControl.serverStub.Freshen(((URIMappingEntry)uriMapper[req.URI]).CHK, req, responseHandler);
00024                         }
00025                         else { // mapping is fresh, so return it from the cache directly.
00026                                 //Console.WriteLine("- Servicing {0} from client cache.", req.URI);
00027                                 client.SendResponse(Get(req));
00028                         }
00029                 }
00030 
00031                 public CacheIndexMessage GetCacheIndexMessage() {
00032                         return new CacheIndexMessage(cache.GetCacheIndex());
00033                 }
00034 
00039                 public void Handle(Message msg) {
00040                         if (msg.Type == MessageType.CacheUpdateHTTPResponse) {
00041                                 CacheUpdateHTTPResponseMsg typedMsg = (CacheUpdateHTTPResponseMsg)msg;
00042                                 HTTPResponse response; 
00043                                 try {
00044                                         response = typedMsg.encodedResponse.Decode(this);
00045                                         Console.WriteLine("> Got (Pushed) Cache Update Message (HTTP Response) for {0}", typedMsg.RequestUri);
00046                                         UpdateCache(response, typedMsg.RequestUri);
00047                                 } catch (CacheNotFoundException ex) { 
00048                                         // TODO *** Extend this catch block to include the 'no change' message. Perhaps
00049                                         // didn't go so well, so thell the server what we do have
00050                                         Console.WriteLine("***" + ex.Message + "***");
00051                                         Console.WriteLine("Re-issuing index message");
00052                                         clientControl.serverStub.SendCacheIndexMessage(this.GetCacheIndexMessage());
00053                                 }
00054                         } else if (msg.Type == MessageType.CacheUpdateNoChange) {
00055                                 CacheUpdateNoChangeMsg typedMsg = (CacheUpdateNoChangeMsg)msg;
00056                                 Console.WriteLine("> Got (Pushed) Cache Update Message (No Change) for {0}", typedMsg.RequestUri);
00057                                 if (typedMsg.chk == null) {
00058                                         Console.WriteLine("### NEW CODE: Server has requested cache index. Sending... ###");
00059                                         clientControl.serverStub.SendCacheIndexMessage(this.GetCacheIndexMessage());
00060                                 } else {
00061                                         UpdateURIMapping(typedMsg.RequestUri, typedMsg.chk, typedMsg.responseHeaders.rawString);
00062                                 }
00063                         } else if (msg.Type == MessageType.CacheIndexRequest) {
00064                                 Console.WriteLine("### NEW CODE: Server has requested cache index. Sending... ###");
00065                                 clientControl.serverStub.SendCacheIndexMessage(this.GetCacheIndexMessage());
00066                         } else {
00067                                 throw new ApplicationException("Client Cache Manager asked to handle inappropriate message");
00068                         }
00069                 }
00070 
00071                 public void Stop() {
00072                         Console.WriteLine("+++ Stopping Client Cache Manager +++");
00073                         Console.WriteLine("### TODO: Code Client Cache Manager Start and Stop ###");
00074                         Console.WriteLine("+++ Client Cache Manager Stopped +++");
00075                 }
00076         }
00077 
00078 
00079 
00080 }

Generated on Mon May 8 22:07:27 2006 by  doxygen 1.3.9.1