Click here to Skip to main content
12,511,896 members (45,454 online)
Click here to Skip to main content

Stats

51.8K 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.Runtime.Remoting.Messaging;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Services;

namespace TestLibrary {

	using InterfaceLibrary;

    public class TestObject : MarshalByRefObject, ITestObject  {
		public delegate String Delegate(String s);

		public TestObject() : this("Server") {
		}
		public TestObject(String name) {
			Console.WriteLine("{0}: Object Constructor called.", name);
			_name = name;
			_counter = 0;
		}
		String _name = "Server";
        static int _counter = 0;
        public String Ping()
        {
            _counter++;
            Console.WriteLine("\nEntering Ping() on {0}, counter {1}", _name, _counter);
            return String.Format("Returning from Ping() on {0}, counter {1}", _name, _counter);            
        }

        [OneWay] public void OneWayMethod(String s) {
            Console.WriteLine("\nEntering OneWayMethod() on {0} with parameter {1}", _name, s);
        }
 
        public void RegularMethod(String s) {
             Console.WriteLine("\nEntering RegularMethod() on {0} with parameter {1}", _name, s);
        }

        public String AsyncMethod(String s) {
            Console.WriteLine("\nEntering AsyncMethod() on {0} with parameter {1}", _name, s);
            return String.Format("Returning from AsyncMethod() with {0} on {1}", s, _name);
        }

        
		public String CallMe(ICallbackObject cb) {
            Console.WriteLine("\nEntering CallMe() on {0}", _name);
            Console.WriteLine("{0}: Trying a callback...", _name);

			String URL = RemotingServices.GetObjectUri((MarshalByRefObject)cb);
			Console.WriteLine(URL);

			ObjRef objRef = RemotingServices.GetObjRefForProxy((MarshalByRefObject)cb);

			Object[] channelData = objRef.ChannelInfo.ChannelData;


            String result = cb.Callback(String.Format("Calling back from CallMe() on {0} ...", _name));
			Console.WriteLine("Callback result: {0}", result);

            return String.Format("Returning from CallMe() on {0}", _name);
        }

		public String CallMeOneWay(ICallbackObject cb) {
            Console.WriteLine("\nEntering CallMeOneWay() on {0}", _name);
			Console.WriteLine("{0}: Trying a one way callback ...", _name);

			cb.OneWayCallback(String.Format("Calling back from CallMeOneWay() on {0} ...", _name));

			return String.Format("Returning from CallMeOneWay() on {0}", _name);
		}

		public String CallMeAsync(ICallbackObject cb) {
            Console.WriteLine("\nEntering CallMeAsync() on {0}", _name);
			Console.WriteLine("{0}: Trying async callback ...", _name);

			Delegate async = new Delegate(cb.AsyncCallback);
			IAsyncResult asyncResult = async.BeginInvoke(String.Format("Calling back from CallMeAsync() on {0} ...", _name), null, null);
			//
			//Wait for the call to complete
			//
			asyncResult.AsyncWaitHandle.WaitOne();

			String result = async.EndInvoke(asyncResult);

			Console.WriteLine("Callback result: {0}", result);
			return String.Format("Returning from CallMeAsync() on {0}", _name);
		}

		public static String GetURLForObject(MarshalByRefObject obj) {
			// trying for CAOs
			ObjRef o = RemotingServices.GetObjRefForProxy(obj);
			if (o!=null) {
				foreach (object data in o.ChannelInfo.ChannelData) {
					ChannelDataStore ds = data as ChannelDataStore;
					if (ds != null) {
						return ds.ChannelUris[0] + o.URI;
					}
				}
			} 
			else {
				// either SAO or not remote!
				String URL = RemotingServices.GetObjectUri(obj);
				return URL;
			}
			return null;
		}


	}

	public class KnossosObject : TestObject, IKnossosObject {
		public KnossosObject() : base("knossos") { }
	}
	public class ZakrosObject : TestObject, IZakrosObject {
		public ZakrosObject() : base("zakros") { }
	}
}

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.160929.1 | Last Updated 18 May 2003
Article Copyright 2003 by Wytek Szymanski
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid