|
//--------------------------------------------
// Written by Roman Kiss (rkiss@pathcom.com)
// December 5, 2001
//
// History:
// 12-05-2001 RK Initial Release
//--------------------------------------------
using System;
using System.Threading;
using System.Diagnostics;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using RemoteInterface;
// using RKiss.MSMQChannelLib; // necessary for option 1
public class Client
{
private delegate string delegateSayHello(string text);
private delegate void delegateOneWay(string text);
private delegate void delegateDoIt(int session);
// worker for sync calls
public class WorkerSync
{
public int result;
private int numOfLoops;
public WorkerSync(int numOfLoops)
{
result = 0;
this.numOfLoops = numOfLoops;
}
public void doit(int session)
{
try
{
// activate a remote object
Type interfaceType = typeof(IRemInterface);
string objectUrl = @"msmq://./reqchannel/endpoint";
IRemInterface robj = (IRemInterface)Activator.GetObject(interfaceType, objectUrl);
for(int ii = 0; ii < numOfLoops; ii++)
{
robj.OneWay(string.Format("This is a Sync test #{0}.{1}", session, ii));
Console.WriteLine(robj.SayHello(string.Format("World from Sync #{0}.{1}", session, ii)));
lock(this) { result++; }
}
Trace.WriteLine(string.Format("obj.SayHello - Sync DONE #{0}", session));
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
// worker for Async calls
public class WorkerAsync
{
public int result;
private int numOfLoops;
public WorkerAsync(int numOfLoops)
{
result = 0;
this.numOfLoops = numOfLoops;
}
public void doit(int session)
{
try
{
// activate a remote object
Type interfaceType = typeof(IRemInterface);
string objectUrl = @"msmq://./reqchannel/endpoint";
IRemInterface robj = (IRemInterface)Activator.GetObject(interfaceType, objectUrl);
for(int ii = 0; ii < numOfLoops; ii++ )
{
// OneWay
delegateOneWay dow = new delegateOneWay(robj.OneWay);
string arg = string.Format("This is a Async test #{0}.{1}", session, ii);
IAsyncResult ar = dow.BeginInvoke(arg, null, null);
// SayHello
delegateSayHello dsh = new delegateSayHello(robj.SayHello);
AsyncCallback ac = new AsyncCallback(OnAsyncResult);
arg = string.Format("World from Async #{0}.{1}", session, ii);
ar = dsh.BeginInvoke(arg, ac, dsh);
}
Trace.WriteLine(string.Format("obj.SayHello - Async-BeginInvoke DONE #{0}", session));
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
public void OnAsyncResult(IAsyncResult ar)
{ int num;
delegateSayHello dsh = (delegateSayHello)ar.AsyncState;
string retval = dsh.EndInvoke(ar);
Console.WriteLine();
Console.WriteLine("AsyncResult = {0}", retval);
lock(this) { num = result++; }
Trace.WriteLine(string.Format("obj.SayHello - Async done {0}", num));
}
}
public static void Main()
{
int numOfLoops = 0;
WorkerSync wtSync = null;
WorkerAsync wtAsync = null;
Console.Write("Enter number of test loops: ");
try
{
numOfLoops = Convert.ToInt32(Console.ReadLine());
wtSync = new WorkerSync(numOfLoops);
wtAsync = new WorkerAsync(numOfLoops);
/*
// option 1
MSMQSender chan = new MSMQSender();
ChannelServices.RegisterChannel(chan);
MSMQSender chan1 = new MSMQSender("msmq_1");
ChannelServices.RegisterChannel(chan1);
*/
// option 2
RemotingConfiguration.Configure(@"..\..\Client.exe.config");
// create workers to perform this test
for(int ii = 0; ii < numOfLoops; ii++)
{
delegateDoIt ddtAsync = new delegateDoIt(wtAsync.doit);
ddtAsync.BeginInvoke(ii, null, null);
//
delegateDoIt ddtSync = new delegateDoIt(wtSync.doit);
ddtSync.BeginInvoke(ii, null, null);
}
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.Write("Hit <enter> to see a result of the test\n");
Console.ReadLine();
// result of the test
int totalLoops = numOfLoops * numOfLoops;
Console.WriteLine("**** Sync Test result: {0} = {1} ****", totalLoops, wtSync.result);
Console.WriteLine("**** Async Test result: {0} = {1} ****", totalLoops, wtAsync.result);
//
Console.Write("Hit <enter> to exit this test client\n");
Console.ReadLine();
}
}
|
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.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.