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
00019 if (!uriMapper.IsFresh(req.URI)) {
00020
00021
00022 HTTPResponseHandler responseHandler = new HTTPResponseHandler(req, client, clientControl);
00023
00024 }
00025 else {
00026
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
00049
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 }