00001 using System;
00002 using System.Threading;
00003 using System.IO;
00004 using Common;
00005
00006 namespace Client {
00010 public class ClientControl {
00011 internal ConnectionManager connectionManager;
00012 internal ClientCacheManager clientCacheManager;
00013 internal ServerStub serverStub;
00014 internal HTTPObjectQueue userRequests;
00015 internal ClientSettings settings;
00016
00017 public MessageLogger msgLog;
00018
00019 Thread processingThread;
00020
00021 public ClientControl() {
00022 try {
00023 Console.WriteLine("Initialising GRPSWeb Client Library (Version {0}, Build {1})", System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(2), System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.Build);
00024 this.settings = new ClientSettings(this);
00025 this.msgLog = new MessageLogger(Console.Out);
00026 this.connectionManager = new ConnectionManager(this);
00027 this.userRequests = new HTTPObjectQueue();
00028 this.clientCacheManager = new ClientCacheManager(this);
00029 this.serverStub = new ServerStub(this);
00030 } catch (ApplicationException appEx) {
00031 throw new ApplicationException("Unable to create Client Control object", appEx);
00032 }
00033 }
00034
00035 public void ResetConnection() {
00036 Console.Error.WriteLine("*** CONNECTION RESET ***");
00037 serverStub.ResetConnection();
00038 }
00039
00040 void run() {
00041 ClientHTTPRequestQueueObject currentReqQObj;
00042 Connection client;
00043 try {
00044 while(true) {
00045
00046 currentReqQObj = (ClientHTTPRequestQueueObject)userRequests.DequeueBlocking();
00047
00048
00049 client = currentReqQObj.connection;
00050
00051
00052 try {
00053
00054
00055
00056 if (currentReqQObj.HasPragmaNoCacheSet) {
00057 Console.WriteLine(" Pragma: no-cache set for {0}", currentReqQObj.URI);
00058 }
00059 serverStub.ServiceReq(currentReqQObj, client);
00060
00061 } catch (CacheNotFoundException ex) {
00062 Console.WriteLine("*** "+ ex.ToString() + " ***");
00063 Console.WriteLine(" Issuing regular request");
00064 serverStub.ServiceReq(currentReqQObj, client);
00065 }
00066 }
00067 } catch (ThreadAbortException) {
00068 Console.WriteLine("[Caught Thread Abort Exception: Shutting down Request Queue processor]");
00069 } catch (Exception ex) {
00070 Console.Error.WriteLine("*** Unhandled error in client library. Shutting Down ***\n{0}", ex);
00071 Stop();
00072 }
00073 }
00074
00075 public void Start() {
00076 this.processingThread = new Thread(new ThreadStart(run));
00077 serverStub.Start();
00078 processingThread.Start();
00079 connectionManager.Start();
00080
00081
00082
00083 }
00084
00085 public void Stop() {
00086 Console.WriteLine("+++ Stopping Client Control library +++");
00087 if (processingThread != null && processingThread.IsAlive) {
00088 processingThread.Abort();
00089 }
00090
00091 connectionManager.Stop();
00092 clientCacheManager.Stop();
00093 serverStub.Stop();
00094
00095 if (processingThread != null && processingThread.IsAlive) {
00096 processingThread.Join();
00097 }
00098 Console.WriteLine("+++ Client Control library Stopped +++");
00099 }
00100
00101 public void Join() {
00102 processingThread.Join();
00103 }
00104 }
00105
00106
00107
00108
00109 class ClientHTTPRequestQueueObject : HTTPRequestQueueObject {
00110 public Connection connection;
00111 public ClientHTTPRequestQueueObject(System.Net.Sockets.NetworkStream s) : base(s) {}
00112 public ClientHTTPRequestQueueObject(HTTPRequest req) : base(req) {}
00113 }
00114
00115
00119 public class Logger {
00120 StreamWriter textOut;
00121 bool started;
00122 ClientSettings settings;
00123
00128 public Logger(ClientSettings settings) {
00129 this.settings = settings;
00130 started = false;
00131 if (settings.LogRequests) {
00132 Start();
00133 }
00134 }
00135
00142 public void Log(Uri requestUri, DateTime startTime, DateTime endTime) {
00143 try {
00144 if (started) {
00145 TimeSpan delta = endTime - startTime;
00146 textOut.WriteLine("{0}\t{1}\t{2}\t{3}", requestUri.ToString(), delta.ToString(), startTime.ToString(), endTime.ToString());
00147 textOut.Flush();
00148 }
00149 } catch (Exception ex) {
00150 Console.Error.WriteLine("*** Error Writing to Client Log: ***\n{0}", ex);
00151 }
00152 }
00153
00157 public void Start() {
00158 try {
00159 if (!started) {
00160 textOut = new StreamWriter("usage.log", true);
00161 textOut.WriteLine("Logging started {0}", DateTime.Now.ToString());
00162 started = true;
00163 }
00164 } catch (Exception ex) {
00165 Console.Error.WriteLine("*** Error Starting Client Log: ***\n{0}", ex);
00166 }
00167 }
00168
00172 public void Stop() {
00173 try {
00174 if (started) {
00175 textOut.WriteLine("Logging stopped {0}", DateTime.Now.ToString());
00176 textOut.WriteLine();
00177 textOut.Close();
00178 }
00179 } catch (Exception ex) {
00180 Console.Error.WriteLine("*** Error Closing Client Log: ***\n{0}", ex);
00181 }
00182 }
00183 }
00184 }
00185