📄 corbaserver.java
字号:
import ex5.*;
import org.omg.CORBA.*;
import org.omg.PortableServer.*;
import org.omg.PortableServer.POA;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import java.net.*;
public class CORBAServer implements Runnable{
/*构造函数*/
//orbd -ORBInitialPort 1050
public CORBAServer(String[] args)
{
initORB(args);
}
private void initORB(String[] args)
{
try
{
//根据参数初始化ORB
ORB orb = ORB.init(args, null);
//获得跟POA的引用,并激活POAManager
//org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
POA rootpoa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
rootpoa.the_POAManager().activate();
//新建一个ServerImpl实现对象,并把它与ORB关联
ServerImpl serverimpl = new ServerImpl();
//连接该服务实现对象
//orb.connect(operateRef);
serverimpl.setORB(orb);
//获得ServerImpl对象的CORBA类型的对象引用
org.omg.CORBA.Object ref = rootpoa.servant_to_reference(serverimpl);
Service href = ServiceHelper.narrow(ref);
//获得代表命名服务的Context
org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
//把Service对象与"Service"名字绑定
String name = "Service";
NameComponent path[] = ncRef.to_name(name);
ncRef.rebind(path, href);
System.out.println("ready and waiting...");
}
catch(Exception e)
{
e.printStackTrace();
}
}
/*实现Runnable接口*/
public void run()
{
try
{
//输出系统提示信息
System.out.println("CORBA Server Host: " +
InetAddress.getLocalHost().getHostAddress() + "," +
"Port: 1005\nCORBA Server Ready, Waiting Client Invoke Now...");
java.lang.Object object = new java.lang.Object();
//同步对象,等待客户调用
synchronized(object)
{
object.wait();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
CORBAServer cs = new CORBAServer(new String[]{ "-ORBInitialPort","1005","-ORBInitialHost","localhost"});
//运行CORBA服务对象
cs.run();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -