distributed | execution
Visitor, Hello! Transfer to Netcom Station | Switch to Telecom station building block Home | More than 500 kinds of Web page effects finishing | Practical Query Function Manual | Block network bt Download Alliance | Classic Jokes | Radio Stations | High-definition classic picture material
Program development web design search engine special effects code operating system Protection virus hacker technology graphic image Computer hardware network technology Server database net article pristine
Your location: Home >> program Development channel >> C # >> Body: Title: Remote execution and distributed computing time in C #: 2006-8-6 Source: Unknown Views: 5 times
Summary
Remote execution is an infrastructure that enables developers to use remote objects in C #. A remote object is an object that is outside the caller's application domain. The examples in this article illustrate how to use two remote object access mechanisms (value delivery and address delivery), and it also illustrates the powerful capabilities of remote objects in distributed computing through a simple, powerful task server implementation.
The task server can accept all objects that implement the ITask interface and run them in its application domain. More importantly, it can accept tasks from multiple clients at a time.
After completing this article, the reader will be able to:
━━ establish a connection between server/client objects.
━━ passes objects by value.
━━ passes an object by address.
━━ understand the concept of remote task assignment.
Remote objects
A remote object passes an object through an address or through the value of an object.
In the first case, the address of an object is passed by application domain A to application domain B, but the object's method invocation is between application domain A and application domain B. The object exists and runs in application domain A, but it is also like a local object in application domain B.
In the second case, the entire object and its associated entities (called object graphs) are serialized into byte form and transferred from application domain A to application domain B. The object is then "drag" in application domain B and reverts to its original state. The object now exists and runs on application domain B.
Establish object host (also known as server)
The first step in setting up a server is to establish a channel between the two application domains in which the object communicates, which can be a TCP/IP channel or an HTTP channel. The TCP channel is fast, it is suitable for the network use that the packet transmission in the network is less limited, the HTTP channel is more flexible, it is suitable to use on the internet and so on WAN.
We will use the TCP/IP channel and will run both the server and client side on the same machine with two different application domains. Therefore, enter the following code to create the channel MyChannel on the 8065 port on the TCP/IP stack:
TcpChannel MyChannel = new TcpChannel (8065);
Here is the direction. NET Channel service registers the MyChannel channel, which allows the channel to be accessed outside the server application domain. We can do this by using the following code:
ChannelServices.RegisterChannel (MyChannel);
The last step is to tell. NET remote execution infrastructure about the objects we want to open, we need to advertise the type and location of the object, the name that the client uses to locate the object, and the. NET's remote infrastructure on how to handle this object invocation. We can get the type of the object through the following code:
Type ObjectType = new Mycoolobject (). GetType ()
You can use the following code to. NET remote and infrastructure registers the object:
RemotingConfiguration.RegisterWellKnownServiceType (
ObjectType, "Mycoolobject",
Wellknownobjectmode.singleton);
There are two ways to handle the invocation of an object: Singleton and SingleCall. In the Singleton method, the object is created at the first client method call, and the object exists until the client aborts the connection or the object dies naturally; In the SingleCall mode, each client's method call creates an object that exists only during a method call, once the method call has ended , the object will die. In SingleCall mode, the client connection is not aborted with the end of the method call, only the object is killed as the method call ends.
Establishing a client for a remote object
The first requirement for the client is that the class of the remote object be on the client's native. The net remote execution infrastructure agent uses it to interpret and assemble the information passed between the remote objects.
Need to build a channel again, and then to. NET remote infrastructure to make the channel available:
TcpChannel MyChannel = new TcpChannel ();
ChannelServices.RegisterChannel (MyChannel);
It should be noted that we did not specify a port address when creating the channel. We will specify the port address when the server is asked to give a reference to the remote object we are calling, as shown in the following code:
Mycoolobject mine = (mycoolobject) Activator.GetObject (
typeof (Mycoolobject),
"Tcp://localhost:8085/mycoolobject");
The first parameter gets the type of object we want to locate, and the second parameter specifies the URL of the remote object. Once we get the object, we need to convert its type from plain object to Mycoolobject type.
Now we have a reference to the remote object located on the server side, which we can consider as a local object.
A powerful task server
A task server is a way to leverage. NET is an example of a mechanism for passing a remote object by value. The task server has an object named Taskrunner, and the client can obtain its reference, Taskrunner can accept any object that implements the ITask interface from the client.
The ITask interface forces the client to create a Task object using Run () and identify () two methods, and the client can then create a complex, resource-sensitive task that implements the ITask interface, and then submits it to the task server and executes it in its own application domain. This means that clients without sufficient computational resources can take full advantage of the resources of the task server to perform the more complex tasks that they cannot accomplish.
Therefore, clients that need to calculate pi with 3,000 decimal digits can create an object that completes the related computation and implements the ITask interface, which is then submitted to the task server for execution using the Taskrunner object.
The ITask interface forces the client to return an object from their run () method, of course, the value of the object may be empty, but it may also contain meaningful values.
Simply put, remote execution by value is best suited to a situation where the server does not have a clear representation of objects that the client expects to execute in a remote environment.
Passing objects by value
When an object is passed by value, it needs to be converted into a format that can be transmitted. An object in an application domain occupies a system memory and is able to react to messages sent to it by other objects. To pass this object to another application domain, you must serialize it. This means that it must be able to be decomposed into byte streams, and then be able to be regroup at the destination as the original object.
To do this, you must use the characteristics of the compiler serializable, so that the compiler for some of the structure of the LU-row processing. We can mark the entire class as serializable, or you can mark part of a class that includes some of the selected methods as serializable. In a task server environment, the entire client task object must be able to be serialized. The code looks like this:
Using System;
? [Serializable ()]
Class Clienttask {
?
It is important to note that if an object has an attached part, such as an external library file, the part used by the object must also be serialized, which is necessary to re-establish the object in the remote application domain.
Running example Programs
This example was written in C # for. NET Beta 2 and passed the test in C # for. NET Beta 2.
In order to use the task server, we must compile the program in the following order:
Csc/target:library/out:itask.dll ITask.cs
Csc/target:library/out:taskrunner.dll TaskRunner.cs
Csc/r:itask.dll/r:taskrunner.dll TaskServer.cs
Csc/r:itask.dll/r:taskrunner.dll TaskClient.cs
The order above is important because the Taskserver and Taskclient classes use the Taskrunner and itask libraries.
To run this example program, start the server code first and wait for the following prompt to appear:
[i] Task Server started, press to exit?
Then you can run the client program.
There is a class in the Taskclient class that implements the ITask interface and can be serialized, and the task will find a product of two numbers and return an object of type int. I recommend that readers create their own tasks and try to run them on a task server, which will give you a hands-on experience of the power of this distributed computing. It enables client software developers to make full use of the rich resources on the task server to complete complex, resource-intensive tasks.
We can use the task server to complete such operations as digital processing, compression, encryption, sorting, and so on.
The positive is the source code involved in the example in this article, and its simple annotation for interested readers ' reference:
ITask.cs
Users can use this interface to create their own tasks, which completes the following two actions:
• The server can run the established task by invoking method run ().
• The client can ensure that the task starts from the method run ().
There is also a identify () method that the server uses to display information about the task.
The interface is compiled into a separate library file under the same namespace so that the administrator of the task server can distribute the interface as a contract for all tasks that can run on/on his task server.
The client inherits the class, creates its own task object, and submits it to the server to run.
Namespace Taskserver {
It must be defined as an interface
Public interface ITask {
Object Run ();
string identify ();
}
}
TaskRunner.cs
This object is used to run tasks submitted by the client, and the submitted tasks are executed in the application domain of the server.
The Taskrunner object is passed to the client as a reference without having to serialize it
Taskrunner accepts all tasks that implement the ITask interface, which requires two parameters: Run () and identify ().
Using System;
Using System.Runtime.Remoting;
Using System.Runtime.Remoting.Channels;
Using SYSTEM.RUNTIME.REMOTING.CHANNELS.TCP;
Namespace Taskserver {
public class Taskrunner:marshalbyrefobject {
ITask Remotetaskobject;
Public Taskrunner () {
Console.WriteLine ("\n[i] taskrunner activated");
}
public string Loadtask (ITask Task) {
Console.WriteLine ("\n[i] Loading New task ...");
if (task = null) {
Console.WriteLine ("[E] Task reference is NULL. Task not Loaded. ");
Return ' [E] Task not loaded. '
}
Remotetaskobject = task;
Console.WriteLine ("[I] Task has been loaded.");
Console.WriteLine ("[I] Task ID:" + remotetaskobject.identify () + "\ n");
Return "[i] Task loaded. Welcome to the all powerful taskserver. ";
}
public Object Runtask () {
Console.WriteLine ("\n[i] Running the task ...");
Object result = Remotetaskobject.run ();
Console.WriteLine ("[I] Task finished.");
return result;
}
}
}
TaskServerEngine.cs
This class is used to start the task server application. It establishes the back-end of C # remote execution, loads the channel, registers the Taskrunner object, and then lets//remote execution mechanism's backend monitor the connection request on the TCP channel
Using System;
Using System.IO;
The following libraries are used to register our objects with remote execution mechanisms
Using System.Runtime.Remoting;
The following libraries are used to register our TCP channel devices with the channel service
Using System.Runtime.Remoting.Channels;
This library provides the TCP channel needed to communicate with remote application domain (client)
Using SYSTEM.RUNTIME.REMOTING.CHANNELS.TCP;
Namespace Taskserver {
public class Taskserverengine {
All we need is a method, which can be static, because we do not need to create an instance of this class, and the purpose of this method is simply to create and initiate the taskserverengine.
public static void Main () {
Show the user that we are starting the server class
Console.WriteLine ("The All powerful Task server!");
Console.WriteLine ("... \" Your Task is my command\ "...");
Console.WriteLine ("\n[i] starting Task Server ...");
try {
Creating a TCP Channel
Console.WriteLine ("[I]--creating TCP channel");
TcpChannel chan = new TcpChannel (8085);
Registers a newly created TcpChannel with a. NET remote service to enable clients to use this service
Console.WriteLine ("[I]--registering TCP channel");
ChannelServices.RegisterChannel (Chan);
Register the Taskrunner object to the back end of the remote mechanism, the RegisterWellKnownServiceType method completes the//operation and receives the following parameters:
* content is the type object of Taskrunner object image
* The name of the service announced to the customer
* Object pattern: Singleton means that all clients request to share an object service; SingleCall means//per client request to use a new object service.
Console.WriteLine ("[I]--Registering the Taskrunner object");
Type Thetype = new Taskrunner (). GetType ();
RemotingConfiguration.RegisterWellKnownServiceType (Thetype, "Taskserver", Wellknownobjectmode.singleton);
Console.WriteLine ("[i] Task Server started, press to exit ...");
Console.ReadLine ();
catch (Exception e) {
Console.WriteLine ("[!] An error occured while initialising the Taskserverengine. ");
Console.WriteLine (e);
}
}
}
}
TaskClient.cs
This is a client application domain, and the client's task is to create a task object and submit it to the task server
Using System;
Set up the library files necessary to connect to the task server
Using System.Runtime.Remoting;
Using System.Runtime.Remoting.Channels;
Using SYSTEM.RUNTIME.REMOTING.CHANNELS.TCP;
Using Taskserver;
Namespace Happyclientwithtask {
[Serializable ()]
Here are the tasks we created
Class Clienttask:itask {
private int num1, num2;
private int result;
Public clienttask (int num1, int num2) {
THIS.NUM1 = NUM1;
this.num2 = num2;
}
public Object Run () {
result = Num1 * NUM2;
Return (object) result;
}
public string Identify () {
Return ("I am a multiplication task.");
}
}
public class Client {
public static void Main () {
Console.WriteLine ("\nwelcome to the humble, lowly client application domain.\n");
Clienttask clienttask = new Clienttask (100,100);
try {
Console.WriteLine ("[I] connecting to taskserver ...");
Console.WriteLine ("[i]-Opening TCP Channel");
TcpChannel chan = new TcpChannel ();
Console.WriteLine ("[I]-registering the channel");
ChannelServices.RegisterChannel (Chan);
Console.WriteLine ("[I] Connected to Taskserver");
Get a reference to the Taskrunner object from Taskserver
The GetObject method in the Activator class requires two parameters:
* Object Type
* URI location of the object
Console.WriteLine ("[i] getting a reference to the Taskrunner Object");
Taskrunner Taskrunner = (taskrunner) Activator.GetObject (typeof (Taskserver.taskrunner),
"Tcp://localhost:8085/taskserver");
if (Taskrunner = = null) {
Console.WriteLine ("[E] could not locate server.");
Console.WriteLine ("[I] exiting ...");
Return
}
Console.WriteLine ("[I] We have an object reference!");
Below we will pass the task object to the task server
Console.WriteLine ("\n[i] submitting we task to the server ...");
String response = Taskrunner.loadtask (Clienttask);
Console.WriteLine ("[I] Server says:" + response);
Console.WriteLine ("\n[i] Running the task and awaiting feedback ...");
Object result = Taskrunner.runtask ();
Console.WriteLine ("[Aaa-uuuum] the great and powerful taskserver Says:" + (int) result);
catch (Exception e) {
Console.WriteLine ("[E] An exception occurred.");
Console.WriteLine (e);
}
}
}
}