summaryrefslogtreecommitdiff
path: root/demo2/conclusion.py
diff options
context:
space:
mode:
authorLorry Tar Creator <lorry-tar-importer@baserock.org>2014-02-21 04:46:49 +0000
committer <>2014-08-04 21:38:17 +0000
commitf3765db04b903b3671733e07cf1541a51966dd14 (patch)
treedefcc3c47d9b8bd78b97dcc04ee779a758d37b1c /demo2/conclusion.py
downloadposix-ipc-tarball-master.tar.gz
Imported from /home/lorry/working-area/delta_python-packages_posix-ipc-tarball/posix_ipc-0.9.8.tar.gz.HEADposix_ipc-0.9.8master
Diffstat (limited to 'demo2/conclusion.py')
-rw-r--r--demo2/conclusion.py65
1 files changed, 65 insertions, 0 deletions
diff --git a/demo2/conclusion.py b/demo2/conclusion.py
new file mode 100644
index 0000000..d857663
--- /dev/null
+++ b/demo2/conclusion.py
@@ -0,0 +1,65 @@
+# Python modules
+import time
+import sys
+PY_MAJOR_VERSION = sys.version_info[0]
+# hashlib is only available in Python >= 2.5. I still want to support
+# older Pythons so I import md5 if hashlib is not available. Fortunately
+# md5 can masquerade as hashlib for my purposes.
+try:
+ import hashlib
+except ImportError:
+ import md5 as hashlib
+
+# 3rd party modules
+import posix_ipc
+
+# Utils for this demo
+import utils
+
+
+utils.say("Oooo 'ello, I'm Mrs. Conclusion!")
+
+params = utils.read_params()
+
+# Mrs. Premise has already created the message queue. I just need a handle
+# to it.
+mq = posix_ipc.MessageQueue(params["MESSAGE_QUEUE_NAME"])
+
+what_i_sent = ""
+
+for i in range(0, params["ITERATIONS"]):
+ utils.say("iteration %d" % i)
+
+ s, _ = mq.receive()
+ s = s.decode()
+ utils.say("Received %s" % s)
+
+ while s == what_i_sent:
+ # Nothing new; give Mrs. Premise another chance to respond.
+ mq.send(s)
+
+ s, _ = mq.receive()
+ s = s.decode()
+ utils.say("Received %s" % s)
+
+ if what_i_sent:
+ if PY_MAJOR_VERSION > 2:
+ what_i_sent = what_i_sent.encode()
+ try:
+ assert(s == hashlib.md5(what_i_sent).hexdigest())
+ except AssertionError:
+ utils.raise_error(AssertionError,
+ "Message corruption after %d iterations." % i)
+ #else:
+ # When what_i_sent is blank, this is the first message which
+ # I always accept without question.
+
+ # MD5 the reply and write back to Mrs. Premise.
+ s = hashlib.md5(s.encode()).hexdigest()
+ utils.say("Sending %s" % s)
+ mq.send(s)
+ what_i_sent = s
+
+
+utils.say("")
+utils.say("%d iterations complete" % (i + 1))