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

ResponseHandlers.cs

00001 using System;
00002 using Common;
00003 
00004 namespace Client {
00005         /*************************************************************************/
00010         public interface IResponseHandler {
00015                 void Handle(Message responseMsg);
00016         }
00017 
00018         /*************************************************************************/
00024         public class HTTPResponseHandler : IResponseHandler {
00026                 Connection client;
00027                 ClientControl clientControl;
00028                 HTTPRequest originalRequest;
00029 
00031 
00032 
00033 
00034 
00035 
00036 
00037 
00038 
00039 
00040                 public HTTPResponseHandler(HTTPRequest originalRequest, Connection client, ClientControl clientControl) {
00041                         this.originalRequest = originalRequest;
00042                         this.client = client;
00043                         this.clientControl = clientControl;
00044                 }
00045 
00052                 public void Handle(Message msg) {
00053                         HTTPResponse response;
00054                         if (msg.Type == MessageType.HTTPResponse) {
00055                                 HTTPResponseMessage rspMsg = (HTTPResponseMessage)msg;
00056                                 // decode the message
00057                                 try {
00058                                         response = rspMsg.encodedResponse.Decode(clientControl.clientCacheManager);
00059                                         // add it to the cache
00060                                         clientControl.clientCacheManager.UpdateCache(response, client.Request.URI); /*DISABLE CACHE*/
00061                                         if (clientControl.settings.PendingRequestsEnabled) {
00062                                                 if (response != null && response.ContentType != null && response.ContentType.StartsWith("text/html")) {
00063                                                         if (response.Body != null) {
00064                                                                 clientControl.serverStub.pendingRequestManager.AddPlaceholders(ImageUriParser.GetImageUris(clientControl.msgLog, client.Request.URI, response.Body.data));
00065                                                         }
00066                                                 }
00067                                         }
00068                                         client.SendResponse(response);
00069                                 } catch (CacheNotFoundException ex) {
00070                                         Console.WriteLine("***" + ex.Message + "***");
00071                                         Console.WriteLine("* Re-issuing index message and original request");
00072                                         clientControl.serverStub.SendCacheIndexMessage(clientControl.clientCacheManager.GetCacheIndexMessage()); /*DISABLE CACHE*/
00073                                         clientControl.serverStub.ServiceReq(originalRequest, client);
00074                                 }
00075                         } else if (msg.Type == MessageType.NoChange) {
00076                                 NoChangeMessage noChangeMsg = (NoChangeMessage)msg;
00077                                 // test to see if we still have it in the cache
00078                                 if (noChangeMsg.chk != null && clientControl.clientCacheManager.CacheContains(noChangeMsg.chk)) {
00079                                         // null check to see if we're not forcing an update. chk == null imples update request
00080                                         // we still have it in the cache, so update the UriMapper with latest headers.
00081                                         clientControl.clientCacheManager.UpdateURIMapping(originalRequest.URI, noChangeMsg.chk, noChangeMsg.responseHeaders.rawString);
00082                                         // obtain response from cache
00083                                         response = clientControl.clientCacheManager.Get(originalRequest);
00084                                         // send to client
00085                                         client.SendResponse(response);
00086                                 } else { // it's not in the cache. So the server has it wrong. So update the server with the cache situation.
00087                                         // null may have meant request update
00088                                         clientControl.serverStub.SendCacheIndexMessage(clientControl.clientCacheManager.GetCacheIndexMessage());
00089                                         if (noChangeMsg.chk != null) {
00090                                                 clientControl.serverStub.ServiceReq(originalRequest, client);
00091                                         }
00092                                 }
00093                         } else if (msg.Type == MessageType.CacheUpdateHTTPResponse || msg.Type == MessageType.CacheUpdateNoChange) {
00094                                 /* by the time it's arrived here, the response message will have been handled and the document *should* be in the cache
00095                                  * there's a slight chance that it mught have been removed, but we can probably service this request from the cache */
00096                                 clientControl.msgLog.Log("Handling previously pending request for {0}", originalRequest.URI);
00097                                 clientControl.clientCacheManager.ServiceReq(originalRequest, client);
00098                         } else { // unknown message
00099                                 throw new ApplicationException(String.Format("Unkown message type {0} in HTTPResponseHandler.Handle", msg.Type.ToString()));
00100                         }
00101                 }
00102 
00104                 public Uri RequestUri {
00105                         get { return originalRequest.URI; }
00106                 }
00107         }
00108         
00109 }

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