summaryrefslogtreecommitdiff
path: root/py/demo
diff options
context:
space:
mode:
authorDwayne Boone <dwayne@zeroc.com>2007-08-30 13:37:21 -0230
committerDwayne Boone <dwayne@zeroc.com>2007-08-30 13:37:21 -0230
commit16a985e3a93121f17d8025492472d51a7906c46a (patch)
tree5b158e09637562b2fb1ec662d4afcd69e257a3a0 /py/demo
parentSquashed commit of the following: (diff)
downloadice-16a985e3a93121f17d8025492472d51a7906c46a.tar.bz2
ice-16a985e3a93121f17d8025492472d51a7906c46a.tar.xz
ice-16a985e3a93121f17d8025492472d51a7906c46a.zip
bug 2347 - added book demos to python
Diffstat (limited to 'py/demo')
-rw-r--r--py/demo/book/README11
-rw-r--r--py/demo/book/printer/Client.py38
-rw-r--r--py/demo/book/printer/Printer.ice23
-rw-r--r--py/demo/book/printer/README8
-rw-r--r--py/demo/book/printer/Server.py41
-rwxr-xr-xpy/demo/book/printer/expect.py40
-rw-r--r--py/demo/book/simple_filesystem/Client.py74
-rw-r--r--py/demo/book/simple_filesystem/Filesystem.ice31
-rw-r--r--py/demo/book/simple_filesystem/README10
-rw-r--r--py/demo/book/simple_filesystem/Server.py151
-rwxr-xr-xpy/demo/book/simple_filesystem/expect.py39
11 files changed, 466 insertions, 0 deletions
diff --git a/py/demo/book/README b/py/demo/book/README
new file mode 100644
index 00000000000..263a2495643
--- /dev/null
+++ b/py/demo/book/README
@@ -0,0 +1,11 @@
+Demos in this directory:
+
+- printer
+
+ An implementation of the simple printer example at the beginning of
+ the book.
+
+- simple_filesystem
+
+ An implementation of the simple (non-persistent, non-life-cycle)
+ version of the filesystem example.
diff --git a/py/demo/book/printer/Client.py b/py/demo/book/printer/Client.py
new file mode 100644
index 00000000000..17dec92ae88
--- /dev/null
+++ b/py/demo/book/printer/Client.py
@@ -0,0 +1,38 @@
+#!/usr/bin/env python
+# **********************************************************************
+#
+# Copyright (c) 2003-2007 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 sys, traceback, Ice
+
+Ice.loadSlice('Printer.ice')
+import Demo
+
+status = 0
+ice = None
+try:
+ ic = Ice.initialize(sys.argv)
+ base = ic.stringToProxy("SimplePrinter:default -p 10000")
+ printer = Demo.PrinterPrx.checkedCast(base)
+ if not printer:
+ raise RuntimeError("Invalid proxy")
+
+ printer.printString("Hello World!")
+except:
+ traceback.print_exc()
+ status = 1
+
+if ic:
+ # Clean up
+ try:
+ ic.destroy()
+ except:
+ traceback.print_exc()
+ status = 1
+
+sys.exit(status)
diff --git a/py/demo/book/printer/Printer.ice b/py/demo/book/printer/Printer.ice
new file mode 100644
index 00000000000..01a4d213aa0
--- /dev/null
+++ b/py/demo/book/printer/Printer.ice
@@ -0,0 +1,23 @@
+// **********************************************************************
+//
+// Copyright (c) 2003-2007 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 SIMPLE_ICE
+#define SIMPLE_ICE
+
+module Demo
+{
+
+ interface Printer
+ {
+ void printString(string s);
+ };
+
+};
+
+#endif
diff --git a/py/demo/book/printer/README b/py/demo/book/printer/README
new file mode 100644
index 00000000000..9c3ae7969af
--- /dev/null
+++ b/py/demo/book/printer/README
@@ -0,0 +1,8 @@
+This demo implements the printer example in chapter 3 of the
+documentation. To run it, start the server in a window:
+
+$ python Server.py
+
+In a separate window, run the client:
+
+$ python Client.py
diff --git a/py/demo/book/printer/Server.py b/py/demo/book/printer/Server.py
new file mode 100644
index 00000000000..39f3ae8025d
--- /dev/null
+++ b/py/demo/book/printer/Server.py
@@ -0,0 +1,41 @@
+#!/usr/bin/env python
+# **********************************************************************
+#
+# Copyright (c) 2003-2007 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 sys, traceback, Ice
+
+Ice.loadSlice('Printer.ice')
+import Demo
+
+class PrinterI(Demo.Printer):
+ def printString(self, s, current=None):
+ print s
+
+status = 0
+ice = None
+try:
+ ic = Ice.initialize(sys.argv)
+ adapter = ic.createObjectAdapterWithEndpoints("SimplePrinterAdapter", "default -p 10000")
+ object = PrinterI()
+ adapter.add(object, ic.stringToIdentity("SimplePrinter"))
+ adapter.activate()
+ ic.waitForShutdown()
+except:
+ traceback.print_exec()
+ status = 1
+
+if ic:
+ # Clean up
+ try:
+ ic.destroy()
+ except:
+ traceback.print_exec()
+ status = 1
+
+sys.exit(status)
diff --git a/py/demo/book/printer/expect.py b/py/demo/book/printer/expect.py
new file mode 100755
index 00000000000..8c0d9741af4
--- /dev/null
+++ b/py/demo/book/printer/expect.py
@@ -0,0 +1,40 @@
+#!/usr/bin/env python
+# **********************************************************************
+#
+# Copyright (c) 2003-2007 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 sys, os
+
+try:
+ import demoscript
+except ImportError:
+ for toplevel in [".", "..", "../..", "../../..", "../../../.."]:
+ toplevel = os.path.normpath(toplevel)
+ if os.path.exists(os.path.join(toplevel, "demoscript")):
+ break
+ else:
+ raise "can't find toplevel directory!"
+ sys.path.append(os.path.join(toplevel))
+ import demoscript
+
+import demoscript.Util
+demoscript.Util.defaultLanguage = "Python"
+import signal
+
+server = demoscript.Util.spawn('Server.py --Ice.PrintAdapterReady')
+server.expect('.* ready')
+
+print "testing...",
+sys.stdout.flush()
+client = demoscript.Util.spawn('Client.py')
+client.waitTestSuccess()
+server.expect('Hello World!')
+server.kill(signal.SIGTERM)
+server.wait()
+server.signalstatus == signal.SIGTERM
+print "ok"
diff --git a/py/demo/book/simple_filesystem/Client.py b/py/demo/book/simple_filesystem/Client.py
new file mode 100644
index 00000000000..16522d80c9e
--- /dev/null
+++ b/py/demo/book/simple_filesystem/Client.py
@@ -0,0 +1,74 @@
+#!/usr/bin/env python
+# **********************************************************************
+#
+# Copyright (c) 2003-2007 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 sys, traceback, Ice
+
+Ice.loadSlice('Filesystem.ice')
+import Filesystem
+
+# Recursively print the contents of directory "dir"
+# in tree fashion. For files, show the contents of
+# each file. The "depth" parameter is the current
+# nesting level (for indentation).
+
+def listRecursive(dir, depth):
+ indent = ''
+ depth = depth + 1
+ for i in range(depth):
+ indent = indent + '\t'
+
+ contents = dir.list()
+
+ for node in contents:
+ subdir = Filesystem.DirectoryPrx.checkedCast(node)
+ file = Filesystem.FilePrx.uncheckedCast(node)
+ print indent + node.name(),
+ if subdir:
+ print "(directory):"
+ listRecursive(subdir, depth)
+ else:
+ print "(file):"
+ text = file.read()
+ for line in text:
+ print indent + "\t" + line
+
+status = 0
+ic = None
+try:
+ # Create a communicator
+ #
+ ice = Ice.initialize(sys.argv)
+
+ # Create a proxy to the root directory
+ #
+ obj = ice.stringToProxy("RootDir:default -p 10000")
+
+ # Downcast the proxy to a Directory proxy
+ #
+ rootDir = Filesystem.DirectoryPrx.checkedCast(obj)
+
+ # Recursively list the contents of the root directory
+ #
+ print "Contents of root directory:"
+ listRecursive(rootDir, 0)
+except:
+ traceback.print_exc()
+ status = 1
+
+if ic:
+ # Clean up
+ #
+ try:
+ ic.destroy()
+ except:
+ traceback.print_exc()
+ status = 1
+
+sys.exit(status)
diff --git a/py/demo/book/simple_filesystem/Filesystem.ice b/py/demo/book/simple_filesystem/Filesystem.ice
new file mode 100644
index 00000000000..c2458a5e8a8
--- /dev/null
+++ b/py/demo/book/simple_filesystem/Filesystem.ice
@@ -0,0 +1,31 @@
+// **********************************************************************
+//
+// Copyright (c) 2003-2007 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.
+//
+// **********************************************************************
+
+module Filesystem {
+ exception GenericError {
+ string reason;
+ };
+
+ interface Node {
+ idempotent string name();
+ };
+
+ sequence<string> Lines;
+
+ interface File extends Node {
+ idempotent Lines read();
+ idempotent void write(Lines text) throws GenericError;
+ };
+
+ sequence<Node*> NodeSeq;
+
+ interface Directory extends Node {
+ idempotent NodeSeq list();
+ };
+};
diff --git a/py/demo/book/simple_filesystem/README b/py/demo/book/simple_filesystem/README
new file mode 100644
index 00000000000..45194f48b4f
--- /dev/null
+++ b/py/demo/book/simple_filesystem/README
@@ -0,0 +1,10 @@
+This demo implements the simple filesystem application shown at the
+end of the client and server Python mapping chapter.
+
+To run it, start the server in a window:
+
+$ python Server.py
+
+Then run the client in a separate window:
+
+$ python Client.py
diff --git a/py/demo/book/simple_filesystem/Server.py b/py/demo/book/simple_filesystem/Server.py
new file mode 100644
index 00000000000..e69265e8e17
--- /dev/null
+++ b/py/demo/book/simple_filesystem/Server.py
@@ -0,0 +1,151 @@
+#!/usr/bin/env python
+# **********************************************************************
+#
+# Copyright (c) 2003-2007 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 sys, threading, Ice
+
+Ice.loadSlice('Filesystem.ice')
+import Filesystem
+
+class DirectoryI(Filesystem.Directory):
+ def __init__(self, name, parent):
+ self._name = name
+ self._parent = parent
+ self._contents = []
+
+ # Create an identity. The parent has the fixed identity "RootDir"
+ #
+ if self._parent:
+ myID = Ice.stringToIdentity(Ice.generateUUID())
+ else:
+ myID = Ice.stringToIdentity("RootDir")
+
+ # Add the identity to the object adapter
+ #
+ self._adapter.add(self, myID)
+
+ # Create a proxy for the new node and add it as a child to the parent
+ thisNode = Filesystem.NodePrx.uncheckedCast(self._adapter.createProxy(myID))
+
+ if self._parent:
+ self._parent.addChild(thisNode)
+
+ # Slice Node::name() operation
+
+ def name(self, current=None):
+ return self._name
+
+ # Slice Directory::list() operation
+
+ def list(self, current=None):
+ return self._contents
+
+ # addChild is called by the child in order to add
+ # itself to the _contents member of the parent
+
+ def addChild(self, child):
+ self._contents.append(child)
+
+ _adpater = None
+
+class FileI(Filesystem.File):
+ def __init__(self, name, parent):
+ self._name = name
+ self._parent = parent
+ self.lines = []
+
+ assert(self._parent != None)
+
+ # Create an identity
+ #
+ myID = Ice.stringToIdentity(Ice.generateUUID())
+
+ # Add the identity to the object adapter
+ #
+ self._adapter.add(self, myID)
+
+ # Create a proxy for the new node and add it as a child to the parent
+ #
+ thisNode = Filesystem.NodePrx.uncheckedCast(self._adapter.createProxy(myID))
+ self._parent.addChild(thisNode)
+
+ # Slice Node::name() operation
+
+ def name(self, current=None):
+ return self._name
+
+ # Slice File::reas() operation
+
+ def read(self, current=None):
+ return self._lines
+
+ # Slice File::write() operation
+
+ def write(self, text, current=None):
+ self._lines = text
+
+ _adapter = None
+
+class Server(Ice.Application):
+ def run(self, args):
+ # Terminate cleanly on receipt of a signal
+ #
+ self.shutdownOnInterrupt()
+
+ # Create an object adapter (stored in the _adapter static members)
+ #
+ adapter = self.communicator().createObjectAdapterWithEndpoints("SimpleFileSystem", "default -p 10000")
+ DirectoryI._adapter = adapter
+ FileI._adapter = adapter
+
+ # Create the root directory (with name "/" and no parent)
+ #
+ root = DirectoryI("/", None)
+
+ # Create a file called "README" in the root directory
+ #
+ file = FileI("README", root)
+ text = [ "This file system contains a collection of poetry." ]
+ try:
+ file.write(text)
+ except Filesystem.GenericError, e:
+ print e.reason
+
+ # Create a directory called "Coleridge" in the root directory
+ #
+ coleridge = DirectoryI("Coleridge", root)
+
+ # Create a file called "Kubla_Khan" in the Coleridge directory
+ #
+ file = FileI("Kubla_Khan", coleridge)
+ text = [ "In Xanadu did Kubla Khan",
+ "A stately pleasure-dome decree:",
+ "Where Alph, the sacred river, ran",
+ "Through caverns measureless to man",
+ "Down to a sunless sea." ]
+ try:
+ file.write(text)
+ except Filesystem.GenericError, e:
+ print e.reason
+
+ # All objects are created, allow client requests now
+ #
+ adapter.activate()
+
+ # Wait until we are done
+ #
+ self.communicator().waitForShutdown()
+
+ if self.interrupted():
+ print self.appName() + ": terminating"
+
+ return 0
+
+app = Server()
+sys.exit(app.main(sys.argv))
diff --git a/py/demo/book/simple_filesystem/expect.py b/py/demo/book/simple_filesystem/expect.py
new file mode 100755
index 00000000000..9ca5b7b04de
--- /dev/null
+++ b/py/demo/book/simple_filesystem/expect.py
@@ -0,0 +1,39 @@
+#!/usr/bin/env python
+# **********************************************************************
+#
+# Copyright (c) 2003-2007 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 sys, os
+
+try:
+ import demoscript
+except ImportError:
+ for toplevel in [".", "..", "../..", "../../..", "../../../.."]:
+ toplevel = os.path.normpath(toplevel)
+ if os.path.exists(os.path.join(toplevel, "demoscript")):
+ break
+ else:
+ raise "can't find toplevel directory!"
+ sys.path.append(os.path.join(toplevel))
+ import demoscript
+
+import demoscript.Util
+demoscript.Util.defaultLanguage = "Python"
+import signal
+
+server = demoscript.Util.spawn('Server.py --Ice.PrintAdapterReady')
+server.expect('.* ready')
+
+print "testing...",
+sys.stdout.flush()
+client = demoscript.Util.spawn('Client.py')
+client.expect('Contents of root directory:\r{1,2}\n.*Down to a sunless sea.')
+client.waitTestSuccess()
+server.kill(signal.SIGINT)
+server.waitTestSuccess()
+print "ok"