[Mono-patches] mcs/class/System.Web/Test/TestMonoWeb AsyncHandler.cs,NONE,1.1 AsyncModule.cs,NONE,1.1 AsyncOperation.cs,NONE,1.1 README,NONE,1.1 SyncHandler.cs,NONE,1.1 SyncModule.cs,NONE,1.1 Test1.cs,NONE,1.1 TestMonoWeb.build,NONE,1.1

Patrik Torstensson totte@mono-cvs.ximian.com
Mon, 5 Aug 2002 10:31:24 -0400


Update of /cvs/public/mcs/class/System.Web/Test/TestMonoWeb
In directory mono-cvs.ximian.com:/tmp/cvs-serv32741

Added Files:
	AsyncHandler.cs AsyncModule.cs AsyncOperation.cs README 
	SyncHandler.cs SyncModule.cs Test1.cs TestMonoWeb.build 
Log Message:
2002-08-05  Patrik Torstensson <ptorsten@hotmail.com>

	This is a simple test program for the new HttpRuntime execution system.



--- NEW FILE: AsyncHandler.cs ---
using System;
using System.Threading;
using System.Web;

namespace TestMonoWeb {
	/// <summary>
	/// Summary description for AsyncHandler.
	/// </summary>
	public class AsyncHandler : IHttpAsyncHandler {
		private HttpContext _context;
		public bool IsReusable {
			get {
				//To enable pooling, return true here.
				//This keeps the handler in memory.
				return false;
			}
		}

		public IAsyncResult BeginProcessRequest(HttpContext context,    AsyncCallback cb, Object extraData) {
			AsynchOperation asynch = new AsynchOperation(cb, context, null);
			asynch.StartAsyncWork();

			context.Response.Write("AsyncHandler.BeginProcessRequest<br>\n");
			context.Response.Flush();

			//Signal the application that asynchronous 
			//processing is being performed. 
			SomeResult asynchForBegin = new SomeResult();

			//Processing is not synchronous.
			asynchForBegin.SetSynch(false);

			//Processing is not complete.
			asynchForBegin.SetCompleted(false);

			_context = context;

			return new SomeResult();
		}

		public void EndProcessRequest(IAsyncResult result) {
			_context.Response.Write("AsyncHandler.EndProcessRequest<br>\n");
		}

		//This method is required but is not called.
		public void ProcessRequest(HttpContext context) {
		}

	}//end class

	public class SomeResult : IAsyncResult {

		/*
		An instance of this class is returned to the application.
		This class lets the application know how the BeginEventHandler method has been handled. The application checks the CompletedSynchronously method.
		*/

		private bool _blnIsCompleted = false;
		private Mutex myMutex = null;
		private Object myAsynchStateObject = null;
		private bool _blnCompletedSynchronously = false;

		public void SetCompleted(bool blnTrueOrFalse) {
			_blnIsCompleted = blnTrueOrFalse;
		}

		public void SetSynch(bool blnTrueOrFalse) {
			_blnCompletedSynchronously = blnTrueOrFalse;
		}

		public bool IsCompleted {
			/*
			  This is not called by the application. However, set it to true. 
			*/
			get {
				return _blnIsCompleted;
			}
		}

		public WaitHandle AsyncWaitHandle {
			//The application does not call this method.         
			get {
				return myMutex;
			}
		}

		public Object AsyncState {
			//The application does not call this method because
			//null is passed in as the last parameter to BeginEventHandler.
			get {
				return myAsynchStateObject;
			}
		}

		public bool CompletedSynchronously {
			//The application wants to know if this is synchronous.
			//Return true if the Begin method was called synchronously.
			get { 
				return _blnCompletedSynchronously;
			}
		}
	}	
}

--- NEW FILE: AsyncModule.cs ---
using System;
using System.Web;

namespace TestMonoWeb
{
	/// <summary>
	/// Summary description for AsyncModule.
	/// </summary>
	public class AsyncModule : IHttpModule
	{
		HttpApplication _app;

		public void Init(HttpApplication app) {
			app.AddOnPreRequestHandlerExecuteAsync(
				new BeginEventHandler(this.BeginPreHandlerExecute), 
				new EndEventHandler(this.EndPreHandlerExecute));

			_app = app;
		}

		IAsyncResult BeginPreHandlerExecute(Object source, EventArgs e, AsyncCallback cb, Object extraData) {
			((HttpApplication) source).Context.Response.Write("AsyncModule.BeginPreHandlerExecute()<br>\n");

			AsynchOperation asynch = new AsynchOperation(cb, _app.Context, extraData);
			asynch.StartAsyncWork();
			return asynch;
		}
		
		void EndPreHandlerExecute(IAsyncResult ar) {
			((AsynchOperation) ar).Context.Response.Write("AsyncModule.EndPreHandlerExecute()<br>\n");
		}		

		public void Dispose() {
		}
	}
}

--- NEW FILE: AsyncOperation.cs ---
using System;
using System.Threading;
using System.Web;

namespace TestMonoWeb
{
	class AsynchOperation : IAsyncResult {
		private bool _completed;
		private Object _state;
		private AsyncCallback _callback;
		private HttpContext _context;

		bool IAsyncResult.IsCompleted { get { return _completed; } }
		WaitHandle IAsyncResult.AsyncWaitHandle { get { return null; } }
		Object IAsyncResult.AsyncState { get { return _state; } }
		bool IAsyncResult.CompletedSynchronously { get { return false; } }

		public HttpContext Context {
			get {
				return _context;
			}
		}

		public AsynchOperation(AsyncCallback callback, HttpContext context, Object state) {
			_callback = callback;
			_context = context;
			_state = state;
			_completed = false;
		}

		public void StartAsyncWork() {
			ThreadPool.QueueUserWorkItem(new WaitCallback(DoSomething), null /*workItemState*/);
		}

		private void DoSomething(Object workItemState) {
			// Just for testing..
			Thread.Sleep(100);
			_completed = true;
			_callback(this);
		}
	}
}

--- NEW FILE: README ---
This small test program tests HttpModule and HttpHandler. The test program can both handle async and sync tests.

This program uses the temporary configuration for modules and handlers.

- Patrik Torstensson
--- NEW FILE: SyncHandler.cs ---
using System;
using System.Web;

namespace TestMonoWeb
{
	public class SyncHandler : IHttpHandler {
		
		public void ProcessRequest(HttpContext context) {
			context.Response.Write("SyncHandler.ProcessRequest<br>\n");
		}
		
		public bool IsReusable {
			get { return false; }  
		}
	}
}

--- NEW FILE: SyncModule.cs ---
using System;
using System.Collections;
using System.Web; 

namespace TestMonoWeb
{
	public class SyncModule : IHttpModule {
		public String ModuleName { 
			get { return "HelloWorldModule"; } 
		}    
		//In the Init function, register for HttpApplication 
		//events by adding your handlers.
		public void Init(HttpApplication application) { 
			application.BeginRequest += (new EventHandler(this.Application_BeginRequest));
			application.EndRequest += (new EventHandler(this.Application_EndRequest));
		}
    
		//Your BeginRequest event handler.
		private void Application_BeginRequest(Object source, EventArgs e) {
			HttpApplication application = (HttpApplication)source;
			HttpContext context = application.Context;
        
			context.Response.Write("SyncModule.Application_BeginRequest()<br>\n");
		}
    
		//Your EndRequest event handler.
		private void Application_EndRequest(Object source, EventArgs e) {
			HttpApplication application = (HttpApplication)source;
			HttpContext context = application.Context;
        
			context.Response.Write("SyncModule.Application_EndRequest()<br>\n");
		}  
      
		public void Dispose() {
		}
	}
}

--- NEW FILE: Test1.cs ---
using System;
using System.Web;
using System.Web.Hosting;
using System.Web.Configuration;

namespace TestMonoWeb
{
	public class MyHost : MarshalByRefObject {
		public MyHost() {
		}
	}
	/// <summary>
	/// Summary description for Test1.
	/// </summary>
	public class Test1
	{
		static void Main(string[] args) {
			// Create the application host
			object host = ApplicationHost.CreateApplicationHost(typeof(MyHost), "/", "c:\\");
			
			int request_count = 10;
			SimpleWorkerRequest [] requests = new SimpleWorkerRequest[request_count];

			int pos;
			for (pos = 0; pos != request_count; pos++) {
				requests[pos] = new SimpleWorkerRequest("test.aspx", "", Console.Out);
			}

			ModulesConfiguration.Add("syncmodule", typeof(SyncModule).AssemblyQualifiedName);
			ModulesConfiguration.Add("asyncmodule", typeof(AsyncModule).AssemblyQualifiedName);
			
			HandlerFactoryConfiguration.Add("get", "/", typeof(AsyncHandler).AssemblyQualifiedName);
			//HandlerFactoryConfiguration.Add("get", "/", typeof(SyncHandler).AssemblyQualifiedName);

			for (pos = 0; pos != request_count; pos++) 
				HttpRuntime.ProcessRequest(requests[pos]);

			HttpRuntime.Close();
/*
			Console.Write("Press Enter to quit.");
			Console.WriteLine();
			Console.ReadLine();
*/
		}	
	}
}

--- NEW FILE: TestMonoWeb.build ---
<?xml version="1.0" encoding="iso-8859-1"?>
<project name="System" default="build">
	<property name="debug" value="false"/>

	<target name="build">
		<csc target="exe" output="TestMonoWeb.exe" debug="${debug}">
			<arg value="/nowarn:1595"/>
			<arg value="/nowarn:0169"/>
			<arg value="/nowarn:0649"/> <!-- always default value -->
			<arg value="/nowarn:0067"/> <!-- event never used -->
			<arg value="/nowarn:0679"/> <!-- internal virual -->
			<arg value="/nowarn:0168"/> <!-- never used variable -->
			<arg value="/nowarn:0162"/> <!-- unreachable code -->
			<arg value="/unsafe"/>
			<arg value="/noconfig"/>
			<arg value="/debug"/>
			<arg value="/r:System.dll"/>
			<arg value="/r:System.Web.dll"/>
			<arg value="/r:System.Drawing.dll"/>
			<arg value="/r:System.Xml.dll"/>
			<sources>
				<includes name="**/*.cs"/> 
			</sources>
		</csc>
	</target>
</project>