diff options
Diffstat (limited to 'py')
-rw-r--r-- | py/demo/Ice/README | 7 | ||||
-rw-r--r-- | py/demo/Ice/bidir/Callback.ice | 30 | ||||
-rw-r--r-- | py/demo/Ice/bidir/Client.py | 53 | ||||
-rw-r--r-- | py/demo/Ice/bidir/Server.py | 85 | ||||
-rw-r--r-- | py/demo/Ice/bidir/config | 7 |
5 files changed, 182 insertions, 0 deletions
diff --git a/py/demo/Ice/README b/py/demo/Ice/README index a50695de2e0..e2cdb1f9740 100644 --- a/py/demo/Ice/README +++ b/py/demo/Ice/README @@ -9,6 +9,13 @@ ICE_LICENSE file included in this distribution. Demos in this directory: +- bidir + + This demo shows how to use bi-directional connections for + callbacks. This is typically used if the server cannot open a + connection to the client to send callbacks, for example, because + firewalls block incoming connections to the client. + - callback A simple callback demo that illustrates how a client can pass a diff --git a/py/demo/Ice/bidir/Callback.ice b/py/demo/Ice/bidir/Callback.ice new file mode 100644 index 00000000000..92a252be2ca --- /dev/null +++ b/py/demo/Ice/bidir/Callback.ice @@ -0,0 +1,30 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2004 ZeroC, Inc. All rights reserved. +// +// This copy of Ice is licensed to you under the terms described in the +// ICE_LICENSE file included in this distribution. +// +// ********************************************************************** + +#ifndef CALLBACK_ICE +#define CALLBACK_ICE + +#include <Ice/Identity.ice> + +module Demo +{ + +interface CallbackReceiver +{ + void callback(int num); +}; + +interface CallbackSender +{ + void addClient(Ice::Identity ident); +}; + +}; + +#endif diff --git a/py/demo/Ice/bidir/Client.py b/py/demo/Ice/bidir/Client.py new file mode 100644 index 00000000000..59eea5b35c9 --- /dev/null +++ b/py/demo/Ice/bidir/Client.py @@ -0,0 +1,53 @@ +# ********************************************************************** +# +# Copyright (c) 2003-2004 ZeroC, Inc. All rights reserved. +# +# This copy of Ice is licensed to you under the terms described in the +# ICE_LICENSE file included in this distribution. +# +# ********************************************************************** + +import os, sys, Ice + +slice_dir = os.getenv('ICEPY_HOME', '') +if len(slice_dir) == 0 or not os.path.exists(os.path.join(slice_dir, "slice")): + slice_dir = os.getenv('ICE_HOME', '') +if len(slice_dir) == 0 or not os.path.exists(os.path.join(slice_dir, "slice")): + print sys.argv[0] + ': Slice directory not found. Define ICEPY_HOME or ICE_HOME.' + sys.exit(1) + +Ice.loadSlice('-I' + slice_dir + '/slice Callback.ice') +import Demo + +class CallbackReceiverI(Demo.CallbackReceiver): + def callback(self, num, current=None): + print "received callback #" + str(num) + +class CallbackClient(Ice.Application): + def run(self, args): + properties = self.communicator().getProperties() + proxyProperty = 'Callback.Client.CallbackServer' + proxy = properties.getProperty(proxyProperty) + if len(proxy) == 0: + print self.appName() + ": property `" + proxyProperty + "' not set" + return 1 + + server = Demo.CallbackSenderPrx.checkedCast(self.communicator().stringToProxy(proxy)) + if not server: + print self.appName() + ": invalid proxy" + return 1 + + adapter = self.communicator().createObjectAdapter("Callback.Client") + ident = Ice.Identity() + ident.name = Ice.generateUUID() + ident.category = "" + adapter.add(CallbackReceiverI(), ident) + adapter.activate() + server.ice_connection().setAdapter(adapter) + server.addClient(ident) + self.communicator().waitForShutdown() + + return 0 + +app = CallbackClient() +sys.exit(app.main(sys.argv, "config")) diff --git a/py/demo/Ice/bidir/Server.py b/py/demo/Ice/bidir/Server.py new file mode 100644 index 00000000000..e73f72d6632 --- /dev/null +++ b/py/demo/Ice/bidir/Server.py @@ -0,0 +1,85 @@ +# ********************************************************************** +# +# Copyright (c) 2003-2004 ZeroC, Inc. All rights reserved. +# +# This copy of Ice is licensed to you under the terms described in the +# ICE_LICENSE file included in this distribution. +# +# ********************************************************************** + +import os, sys, traceback, threading, Ice + +slice_dir = os.getenv('ICEPY_HOME', '') +if len(slice_dir) == 0 or not os.path.exists(os.path.join(slice_dir, "slice")): + slice_dir = os.getenv('ICE_HOME', '') +if len(slice_dir) == 0 or not os.path.exists(os.path.join(slice_dir, "slice")): + print sys.argv[0] + ': Slice directory not found. Define ICEPY_HOME or ICE_HOME.' + sys.exit(1) + +Ice.loadSlice('-I' + slice_dir + '/slice Callback.ice') +import Demo + +class CallbackSenderI(Demo.CallbackSender, threading.Thread): + def __init__(self): + threading.Thread.__init__(self) + self._destroy = False + self._num = 0 + self._clients = [] + self._cond = threading.Condition() + + def destroy(self): + self._cond.acquire() + + print "destroying callback sender" + self._destroy = True + + try: + self._cond.notify() + finally: + self._cond.release() + + self.join() + + def addClient(self, ident, current=None): + print "adding client `" + Ice.identityToString(ident) + "'" + + client = Demo.CallbackReceiverPrx.uncheckedCast(current.con.createProxy(ident)) + self._clients.append(client) + + def run(self): + self._cond.acquire() + + try: + while not self._destroy: + self._cond.wait(2) + + if not self._destroy and len(self._clients) > 0: + self._num = self._num + 1 + + for p in self._clients[:]: # Iterate over a copy so we can modify the original list. + try: + p.callback(self._num) + except: + print "removing client `" + Ice.identityToString(p.ice_getIdentity()) + "':" + traceback.print_exc() + self._clients.remove(p) + finally: + self._cond.release() + +class CallbackServer(Ice.Application): + def run(self, args): + adapter = self.communicator().createObjectAdapter("Callback.Server") + sender = CallbackSenderI() + adapter.add(sender, Ice.stringToIdentity("sender")) + adapter.activate() + + sender.start() + try: + self.communicator().waitForShutdown() + finally: + sender.destroy() + + return 0 + +app = CallbackServer() +sys.exit(app.main(sys.argv, "config")) diff --git a/py/demo/Ice/bidir/config b/py/demo/Ice/bidir/config new file mode 100644 index 00000000000..d194f5d3765 --- /dev/null +++ b/py/demo/Ice/bidir/config @@ -0,0 +1,7 @@ +Callback.Client.CallbackServer=sender:tcp -p 10000 +Callback.Client.Endpoints= +Callback.Server.Endpoints=tcp -p 10000 + +#Ice.Trace.Network=1 +#Ice.Trace.Protocol=1 +Ice.Warn.Connections=1 |