Click here to Skip to main content
12,451,093 members (42,397 online)
Click here to Skip to main content

Stats

51.7K views
606 downloads
24 bookmarked
Posted

The case for a channel-schema concept

, 17 May 2003 CPOL
An article about .NET remoting channel schemas
using System;
using System.Collections;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Proxies;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;

using InterfaceLibrary;



namespace RemotingClient {

	public class CallbackObject : MarshalByRefObject, ICallbackObject {
		public String Callback(String msg) {
			Console.WriteLine("Callback received!");
			return "Return from CallbackObject.Callback";
		}
		[OneWay] public void OneWayCallback(String msg) {
			Console.WriteLine("OneWayCallback received!");
			Console.WriteLine(msg);
		}
		public String AsyncCallback(String msg) {
			Console.WriteLine("AsyncCallback received!");
			Console.WriteLine(msg);
			return "Return from CallbackObject.AsyncCallback.";
		}
	}

	delegate String Delegate(String s);

	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	class Class1 {
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args)	{

			try {

				RemotingConfiguration.Configure(@"..\..\RemotingClient.exe.config");

	
				// create the callback object
				CallbackObject callback = new CallbackObject();

				// convert to ObjRef
				ObjRef objRef = RemotingServices.Marshal(callback);

				// get at the channel data
				Object[] channelData = objRef.ChannelInfo.ChannelData;

				foreach(Object data in channelData) {

					// look for a ChannelDataStore
					if(data is ChannelDataStore) {
						// get at the channel URIs
						String[] channelUris = ((ChannelDataStore)data).ChannelUris;

						// print each uri, the client�s server channel address
						foreach(String uri in channelUris)
							Console.WriteLine(uri); // 
					}
				}
				String url = "http-secure://localhost:6789/KnossosObject.rem";
				IKnossosObject objSecure = (IKnossosObject)RemotingServices.Connect(typeof(IKnossosObject), url);
				Console.WriteLine("Communicating secure with Knossos ");
				Test(objSecure);
				TestCallback(objSecure, callback);
				
				Console.WriteLine();

				url="http-binary://localhost:9090/KnossosObject.rem";
				IKnossosObject objKnossos = (IKnossosObject)RemotingServices.Connect(typeof(IKnossosObject), url);
				Console.WriteLine("Communicating with Knossos");
				Test(objKnossos);
				TestCallback(objKnossos, callback);

				Console.WriteLine();

				url="tcp-soap://localhost:9000/ZakrosObject.soap";
				IZakrosObject objZakros = (IZakrosObject)RemotingServices.Connect(typeof(IZakrosObject), url);
				Console.WriteLine("Communicating with Zakros");
				Test(objZakros);
				TestCallback(objZakros, callback);

			} catch(Exception e) {
				Console.WriteLine(e);
			}

			Console.WriteLine("Press ENTER to quit...");
			Console.ReadLine();
		}


		static void TestCallback(ITestObject obj, ICallbackObject callback) {
			int sync = 1;
			int oneway = 1;
			int async = 1;


			if(sync > 0) {
				Console.WriteLine("Method with Sync Callback from Server");
				while(sync-- > 0) 
					Console.WriteLine(obj.CallMe(callback));
			}
			if(async > 0) {
				Console.WriteLine("Method with Async Callback from Server");
				while(async-- > 0) 
					Console.WriteLine(obj.CallMeAsync(callback));
			}
			if(oneway > 0) {
				Console.WriteLine("Method with OneWay Callback from Server");
				while(oneway-- > 0) 
					Console.WriteLine(obj.CallMeOneWay(callback));
			}

		}
		static void Test(ITestObject obj) {

			int ping = 1;
			int regular = 1;
			int asyncway = 1;
			int oneway = 1;


			if(ping > 0) {
				Console.WriteLine("Calling Ping()");
				while(ping-- > 0)
					Console.WriteLine(obj.Ping());
			}

			if(regular > 0) {
				Console.WriteLine();
				Console.WriteLine("Calling RegularMethod()");
				while(regular-- > 0) {
					obj.RegularMethod(String.Format("param {0}", regular));
				}
			}

			if(oneway > 0) {
				Console.WriteLine();
				Console.WriteLine("Calling OneWayMethod()");
				while(oneway-- > 0)
					obj.OneWayMethod(String.Format("param {0}", oneway));
			}

			if(asyncway > 0) {
				Console.WriteLine();
				Console.WriteLine("Calling AsyncMethod()");
				while(asyncway-- > 0) {
					Delegate async = new Delegate(obj.AsyncMethod);
					IAsyncResult result = async.BeginInvoke(String.Format("param {0}", asyncway), null, null);
					//
					//Wait for the call to complete
					//
					result.AsyncWaitHandle.WaitOne();
					Console.WriteLine(async.EndInvoke(result));
				}
			}

			Console.WriteLine();
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Wytek Szymanski
Web Developer
United States United States
I am a consultant, trainer, software archtect/engineer, since the early 1980s, working in the greater area of Boston, MA, USA.

My work comprises the entire spectrum of software, shrink-wrapped applications, IT client-server, systems and protocol related work, compilers and operating systems, and more ....

I am currently focused on platform development for distributed computing in service oriented data centers.

You may also be interested in...

Pro
Pro
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160826.1 | Last Updated 18 May 2003
Article Copyright 2003 by Wytek Szymanski
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid