|
转自:http://weiht.blogchina.com/1750430.html
作者: 胡传魁 2005年06月2日, 星期四 10:57
类库Plouffe_Bellard:
此类库只有一个类PB.Plouffe_Bellard,用于计算Pi小数点后从给定位置开始的9位。此类就是分布式计算的核心工具,客户端从分布式服务器上运行此类并返回结果。
服务器程序ServerLoader:
此程序在主窗体构造函数中配置为可以远程计算:
try {
string ConfigFilePath =
Path.GetDirectoryName(Application.ExecutablePath) +
"\\ServerLoader.exe.config";
RemotingConfiguration.Configure(ConfigFilePath);
} catch (System.Runtime.Remoting.RemotingException e) {
MessageBox.Show(e.Message, "Remoting Exception",
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
}
而在指定的配置文件ServerLoader.exe.config中,配置了远程计算使用的要素(类名、调用方式、信道等)。
<configuration>
<system.runtime.remoting>
<application name = "ServerLoader">
<service>
<wellknown
mode="SingleCall"
type="PB.Plouffe_Bellard,Plouffe_Bellard"
objectUri="Plouffe_Bellard"/>
</service>
<channels>
<channel ref="tcp server" port="9000"/>
</channels>
</application>
</system.runtime.remoting>
</configuration>
system.runtime.remoting元素有application、channels、channelSinkProviders、 debug四个子元素,此文件只使用了applicaiton元素(包含有关应用程序使用和公开的远程对象的信息,可在 system.runtime.remoting元素中出现一次。)。此文件使用了applicaition元素的service和channels两个子元素,service的wellknown元素含义如下:
mode:可以是Singleton(对所有客户请求返回同一个对象)或者SingleCall(对每个客户请求返回一个新对象)。
type:指定对象的完整类型名(PB.Plouffe_Bellard)和此类型实现的程序集(assembly)名称(Plouffe_Bellard)。
objectUri:指定对象的Uri终结点。
另外,channels元素的channel子元素含义如下:
ref:指示客户端要注册以供使用的信道模板的id。
port:因为使用了TcpChannel,所以要指定端口;如果使用HttpChannel,则不必指定端口。
使用单个服务器计算的客户端SimpleClient:
此程序从服务器上获取一个远程对象,调用远程对象的方法进行计算。
Plouffe_Bellard PiCalculator = null;
string MachineName = MachineNameTextBox.Text;
try {
int port = 9000;
String URL = "tcp://127.0.0.1:" + port +
"/ServerLoader/Plouffe_Bellard";
PiCalculator = (Plouffe_Bellard)
Activator.GetObject(typeof(Plouffe_Bellard), URL);
ResultsTextBox.Text = "3." + PiCalculator.CalculatePiDigits(1);
} catch(Exception) {
MessageBox.Show("Need to run ServerLoader.exe on machine " +
MachineName,
"Simple Client",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
使用多个服务器计算的客户端,设计思路是:
·配置一个可用服务器列表(ArrayList)。
·设计一个管理器,为每一个服务器启动一个线程,为各个服务器线程分配计算的Pi的起始位(至于每次计算的长度,已经在服务端所使用的类库中指定,当然可以改写程序使之可指定)。
·计算完毕,管理器将计算结果拼接返回,并统计各服务器返回的位数。 |
|