利用Corba中间件方法开发一元二次方程计算分布式系统

it2023-10-03  79

1、题目要求

1、服务器端每次提供服务时,输出制作者姓名+IP+端口号; 2、客户端运行时可以输入方程系数; 3、请把运行截图(包括服务器端和客户端)和服务器端2个程序的截图贴在答案处,并把源程序和编译结果打包以附件形式上传。

2、操作步骤

2.1、新建BA.idl

module BAPkg{ typedef struct Equation{ double x1; double x2; }_Equation; interface EquationInterface{ void equationCalculation(in long a,in long b,in long c,out _Equation result); }; };

2.2、对BA.idl进行编译

然后同级目录下多出一个BAPkg文件夹。

2.3、编写接口实现类、Server、Client

BAPkg文件夹下新建EquationImpl.java,填入以下代码:

package BAPkg; public class EquationImpl extends EquationInterfacePOA { @Override public void equationCalculation(int a, int b, int c, EquationHolder result) { double dtr=b*b-4*a*c; Equation equation=new Equation(); if(dtr>=0) { double m=Math.sqrt(dtr); equation.x1=(-b+m)/(2*a); equation.x2=(-b-m)/(2*a); } result.value=equation; } }

BAPkg文件夹下新建Server.java,填入以下代码:("PWN"记得替换为自己的名字,建议为英文,不然后面编译可能会乱码)

package BAPkg; import org.omg.CORBA.ORB; import org.omg.CosNaming.NamingContextExt; import org.omg.CosNaming.NamingContextExtHelper; import org.omg.CosNaming.NameComponent; import org.omg.PortableServer.POA; import org.omg.PortableServer.POAHelper; import java.util.Properties; public class Server { public static void main(String[] args) { try { Properties env = new Properties(); env.setProperty("org.omg.CORBA.ORBInitialHost", "127.0.0.1"); env.setProperty("org.omg.CORBA.ORBInitialPort", "1050"); ORB orb = ORB.init(args, env); org.omg.CORBA.Object temp; temp = orb.resolve_initial_references("RootPOA"); POA poa = POAHelper.narrow(temp); poa.the_POAManager().activate(); EquationImpl servant=new EquationImpl(); temp = poa.servant_to_reference(servant); EquationInterface ro=EquationInterfaceHelper.narrow(temp); temp = orb.resolve_initial_references("NameService"); NamingContextExt node = NamingContextExtHelper.narrow(temp); NameComponent[] path = node.to_name("Basic Arithmetic"); node.rebind(path, ro); System.out.println("PWN +127.0.0.1+1050 : Server is running......."); orb.run(); } catch (Exception ex) { ex.printStackTrace(); } } }

BAPkg文件夹下新建Client.java,填入以下代码:

package BAPkg; import org.omg.CORBA.ORB; import org.omg.CosNaming.NamingContextExt; import org.omg.CosNaming.NamingContextExtHelper; import java.util.Properties; import java.util.Scanner; public class Client { public static void main(String[] args) { try { Properties env = new Properties(); env.setProperty("org.omg.CORBA.ORBInitialHost", "127.0.0.1"); env.setProperty("org.omg.CORBA.ORBInitialPort", "1050"); ORB orb = ORB.init(args, env); org.omg.CORBA.Object temp; temp = orb.resolve_initial_references("NameService"); NamingContextExt node = NamingContextExtHelper.narrow(temp); temp = node.resolve_str("Basic Arithmetic"); EquationInterface ro=EquationInterfaceHelper.narrow(temp); Scanner scanner=new Scanner(System.in); System.out.print("Please input the coefficient of quadratic term:"); int a=scanner.nextInt(); System.out.print("Please input the coefficient of primary term:"); int b=scanner.nextInt(); System.out.print("Please input the constant term coefficient:"); int c=scanner.nextInt(); EquationHolder result=new EquationHolder(); ro.equationCalculation(a,b,c,result); Equation res=result.value; if(res==null) { System.out.println("Subequation has no solution!"); }else { System.out.println("Solution [x1="+res.x1+",x2="+res.x2+"]"); } } catch (Exception ex) { ex.printStackTrace(); } } }

2.4、编译和运行

进入BAPkg所在目录打开cmd,输入命令orbd -ORBInitialPort 1050,完成后先不要关闭窗口 重新打开BAPkg所在目录的cmd窗口,输入命令java BAPkg.Server,完成后同样先不要关闭窗口 重新打开BAPkg所在目录的cmd窗口,输入命令java BAPkg.Client,输入一元二次方程的二次项 一次项、常数项,得出结果

最新回复(0)