ICE 学习进阶3-注意事项

1. 窄化

 窄化就是向下转换,基类到子类
 The code does a checkedCast to narrow the Node proxy to a Directory proxy, as well as an uncheckedCast to narrow the Node proxy to a File
 proxy. Exactly one of those casts will succeed, so there is no need to call
 checkedCast twice: if the Node is-a Directory, the code uses the DirectoryPrx
 returned by the checkedCast; if the checkedCast fails, we
 know that the Node is-a File and, therefore, an uncheckedCast is sufficient
 to get a FilePrx.
 In general, if you know that a down-cast to a specific type will succeed, it is
 preferable to use an uncheckedCast instead of a checkedCast because
 an uncheckedCast does not incur any network traffic.

2. checkedCast和uncheckedCast


checkedCast函数说明
checkedCast is a static member function so, to do a down-cast,
you always use the syntax <interface-name>Prx::checkedCast.
A checkedCast typically results in a remote message to the server. The
message effectively asks the server “is the object denoted by this reference of
type Derived” The reply from the server is communicated to the application
code in form of a successful (non-null) or unsuccessful (null) result.
使用情况类似C++ dynamic_cast


An uncheckedCast provides a down-cast without consulting the server as
to the actual run-time type of the object,
You should use an uncheckedCast only if you are certain that the proxy
indeed supports the more derived type: an uncheckedCast, as the name
implies, is not checked in any way; it does not contact the object in the server
and, if it fails, it does not return null.危险但是有用,因省去了和服务的交互确认
使用情况类似C++ static_cast

 

 

3.


对象代理的函数>ice_toString()
cout<<"Prx:"<<APrintPrx->ice_toString()<<endl;
输出:Prx:SimplePrinter -t:tcp -p 10000


抛异常后,输出参数状态不确定了,需要返回值来确定是否有效。


当创建一个 NAME接口后,自动生成 NAMEPtr 智能指针类型,自动生成NAMEPrx 代理指针。
若对空智能指针执行操作,则抛(const IceUtil::NullHandleException&)

 

 

4. servant实现


By convention, servant classes have the name of their interface with an I-suffix,
so the servant class for the Node interface is called NodeI. (This is a convention
only: as far as the Ice run time is concerned, you can chose any name you prefer
for your servant classes.)
This makes the servant class a concrete class that can be instantiated.

Parameter passing on the server side follows the rules for the client side:
 in-parameters are passed by value or const reference.
 out-parameters are passed by reference.
 return values are passed by value

 

5. 抛异常方法

how to pass parameters and throw exceptions
To throw an exception from an operation implementation, you simply instantiate
the exception, initialize it, and throw it. For example:
void
Filesystem::FileI::write(const Filesystem::Lines& text,
const Ice::Current&)
{
// Try to write the file contents here...
// Assume we are out of space...
if (error) {
Filesystem::GenericError e;
e.reason = "file too large";
throw e;
}
};

 

 

 退出main函数时,一定要执行ic->destroy();否则有无法预测的异常

 

6. Ice::Application 总结

   Application::main
    完成ice运行环境的建立和消除,运行时异常捕获,
    通过communicator()函数,来访问运行环境
 解析argv Ice参数
 Ice::Application::appName来访问运行程序名称
 It creates an IceUtil::CtrlCHandler that properly destroys the communicator. 
   ICE运行环境退出方法
 destroyOnInterrupt
This function creates an IceUtil::CtrlCHandler that destroys the
communicator when one of the monitored signals is raised. This is the default
behavior.
 shutdownOnInterrupt
This function creates an IceUtil::CtrlCHandler that shuts down the
communicator when one of the monitored signals is raised.
 ignoreInterrupt
This function causes signals to be ignored.
 
Ice::Application和运行环境的关系
Ice::Application is a singleton class that creates a single communicator. If
you are using multiple communicators, you cannot use Ice::Application.

 

 


7  Ice::Service 总结

The Service::main member function performs the following sequence of
tasks:
1. Scans the argument vector for reserved options that indicate whether the
program should run as a system service and removes these options from the
argument vector (argc is adjusted accordingly). Additional reserved options
are supported for administrative tasks.
2. Configures the program for running as a system service (if necessary) by
invoking configureService or configureDaemon, as appropriate for
the platform.
3. Invokes the run member function and returns its result.
Note that, as for Application::main, Service::main

 

The Service::run member function executes the service in the steps
shown below:
1. Installs an IceUtil::CtrlCHandler (see Section 31.10) for proper
signal handling.
2. Invokes the initializeCommunicator member function to obtain a
communicator. The communicator instance can be accessed using the
communicator member function.
3. Invokes the start member function. If start returns false to indicate
failure, run destroys the communicator and returns immediately.


4. Invokes the waitForShutdown member function, which should block until
shutdown is invoked.
5. Invokes the stop member function. If stop returns true, run considers
the application to have terminated successfully.
6. Destroys the communicator.
7. Gracefully terminates the system service (if necessary).


增加--daemon选项  Ice::Service changes the current working directory of the child process
to the root directory, and closes all unnecessary file descriptors. 所以标准输入,输出,错误输出都不能用了


--nochdir 选项: Prevents Ice::Service from changing the current working directory.
--service NAME 选项   :Run as a Windows service named NAME, which must already be installed.

 

Each Ice object requires an identity. That identity must be unique for all servants
using the same object adapter.


To activate a servant, you invoke the add operation on the object
adapter. Assuming that we have access to the object adapter in the _adapter
variable, we can write:
_adapter->add(servant, id);


8.客户端通过代理使用远程服务,一个代理操作有如下步骤:
1. The proxy for an Ice object, apart from addressing information, contains the
identity of the Ice object. When a client invokes an operation, the object identity
is sent with the request to the server.
2. The object adapter receives the request, retrieves the identity, and uses the
identity as an index into the servant map.
3. If a servant with that identity is active, the object adapter retrieves the servant
pointer from the servant map and dispatches the incoming request into the
correct member function on the servant.
Assuming that the object adapter is in the active


9. AMD 服务器异步派发

The number of simultaneous synchronous requests a server is capable of
supporting is determined by the number of threads in the server’s thread pool
If all of the threads are busy dispatching long-running operations,
then no threads are available to process new requests and therefore clients may
experience an unacceptable lack of responsiveness.
Asynchronous Method Dispatch (AMD), the server-side equivalent of AMI


AMD is transparent to the client, that is, there is no way for a client to distinguish
a request that, in the server, is processed synchronously from a request that
is processed asynchronously.
In practical terms, an AMD operation typically queues the request data (i.e.,
the callback object and operation arguments) for later processing by an application
thread (or thread pool). In this way, the server minimizes the use of dispatch
threads and becomes capable of efficiently supporting thousands of simultaneous
clients.
An alternate use case for AMD is an operation that requires further processing
after completing the client’s request. In order to minimize the client’s delay, the
operation returns the results while still in the dispatch thread, and then continues
using the dispatch thread for additional work.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值