首页 > 代码库 > ICE提纲之demo/Glacier2/callback
ICE提纲之demo/Glacier2/callback
本例子展示了Glacier2 router的用法(没有使用Glacier2 session manager)。
如果clients与router不在同一台主机上,需要将以下地方修改为glacier2router所在机器的外部地址:
config.glacier2的Glacier2.Client.Endpoints
config.client的Ice.Default.Router和Callback.Client.Router
Glacier2router服务
启动命令
$ glacier2router --Ice.Config=config.glacier2
config.glacier2
#
# Set the instance name
#
Glacier2.InstanceName=DemoGlacier2
#
# The client-visible endpoint of Glacier2. This should be an endpoint
# visible from the public Internet, and it should be secure.
#
Glacier2.Client.Endpoints=ssl -p 4064 -h localhost
#
# The server-visible endpoint of Glacier2. This endpoint is only
# required if callbacks are needed (leave empty otherwise). This
# should be an endpoint on an internal network (like 192.168.x.x), or
# on the loopback, so that the server is not directly accessible from
# the Internet.
#
Glacier2.Server.Endpoints=tcp -h localhost
#
# For this demo, we use a null permissions verifier.
#
Glacier2.PermissionsVerifier=DemoGlacier2/NullPermissionsVerifier
#
# The timeout for inactive sessions. If any client session is inactive
# for longer than this value, the session expires and is removed. The
# unit is seconds.
#
Glacier2.SessionTimeout=30
#
# Glacier can forward requests buffered or unbuffered. Unbuffered
# means a lower resource consumption, as buffering requires one
# additional thread per connected client or server. However, without
# buffering, messages cannot be batched and message overriding doesn‘t
# work either. Also, with unbuffered request forwarding, the caller
# thread blocks for twoway requests.
# The default is to use buffering (=1), in both directions.
#Glacier2.Client.Buffered=0
#Glacier2.Server.Buffered=0
#
# These two lines instruct Glacier2 to forward contexts both for
# regular routing, as well as for callbacks (reverse routing).
#
Glacier2.Client.ForwardContext=1
Glacier2.Server.ForwardContext=1
#
# To prevent Glacier2 from being flooded with requests from or to one
# particular client, Glacier2 can be configured to sleep for a certain
# period after all current requests for this client have been
# forwarded. During this sleep period, new requests for the client are
# queued. These requests are then all sent once the sleep period is
# over. The unit is milliseconds.
#
Glacier2.Client.SleepTime=500
Glacier2.Server.SleepTime=500
#
# With the two settings below, Glacier2 can be instructed to always
# batch oneways, even if they are sent with a _fwd/o instead of a
# _fwd/O context.
# The default value for Glacier2.Client.AlwaysBatch and
# Glacier2.Server.AlwaysBatch is 0.
#Glacier2.Client.AlwaysBatch=1
#Glacier2.Server.AlwaysBatch=1
#
# Glacier2 always disables active connection management so there is no
# need to configure this manually. Connection retry does not need to
# be disabled, as it‘s safe for Glacier2 to retry outgoing connections
# to servers. Retry for incoming connections from clients must be
# disabled in the clients.
#
Slice
Callback.ice
module Demo
{
interface CallbackReceiver
{
void callback();
};
interface Callback
{
void initiateCallback(CallbackReceiver* proxy);
void shutdown();
};
};
服务器
CallbackI
voidCallbackI::initiateCallback(const CallbackReceiverPrx& proxy, const Ice::Current& current){ proxy->callback();}voidCallbackI::shutdown(const Ice::Current& c){ c.adapter->getCommunicator()->shutdown();}
config.server
#
# The endpoint of the server‘s object adapter. This should be an
# endpoint on an internal network (like 192.168.x.x), or on the
# loopback, so that the server is not directly accessible from the
# Internet.
#
Callback.Server.Endpoints=tcp -h localhost -p 10000
Server.cpp
intCallbackServer::run(int argc, char*[]){ Ice::ObjectAdapterPtr adapter = communicator()->createObjectAdapter("Callback.Server"); CallbackPtr cb = new CallbackI; adapter->add(cb, communicator()->stringToIdentity("callback")); adapter->activate(); communicator()->waitForShutdown();}
客户端
CallbackReceiverI
voidCallbackReceiverI::callback(const Ice::Current&){ cout << "received callback" << endl;}
config.client
#
# The proxy to the Glacier2 router for all outgoing connections. This
# must match the value of Glacier2.Client.Endpoints in config.glacier2.
#
Ice.Default.Router=DemoGlacier2/router:ssl -p 4064 -h localhost
#
# We don‘t need any endpoints for the client if we use a
# router. Incoming requests are received through connections
# established from the client to the router.
#
Callback.Client.Endpoints=
#
# This must match the value of Callback.Server.Endpoints in
# config.server.
#
Callback.Proxy=callback:tcp -h localhost -p 10000
Client.cpp
Glacier2::SessionPrxCallbackClient::createSession(){ Glacier2::SessionPrx session; cout << "This demo accepts any user-id / password combination.\n"; string id; string pw; session = router()->createSession(id, pw); return session;}intCallbackClient::runWithSession(int argc, char*[]){ Ice::Identity callbackReceiverIdent = createCallbackIdentity("callbackReceiver"); Ice::ObjectPrx base = communicator()->propertyToProxy("Callback.Proxy"); CallbackPrx twoway = CallbackPrx::checkedCast(base); objectAdapter()->add(new CallbackReceiverI, callbackReceiverIdent); CallbackReceiverPrx twowayR = CallbackReceiverPrx::uncheckedCast(objectAdapter()->createProxy(callbackReceiverIdent)); string override; char c; do { cin >> c; if(c == ‘t‘) { Ice::Context context; context["_fwd"] = "t"; if(!override.empty()) { context["_ovrd"] = override; } twoway->initiateCallback(twowayR, context); } else if(c == ‘v‘) { if(override.empty()) { override = "some_value"; } else { override.clear(); } } else if(c == ‘s‘) { twoway->shutdown(); } } while(1);}