Skip to content

Commit

Permalink
make Key-Value RPC demo use persistent store
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobias Oberstein committed Aug 27, 2011
1 parent 7ba319b commit 54c2a20
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 39 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
*.pyc
*.pyo
*.dat
/lib/python/build/*
/lib/python/dist/*
/lib/python/autobahn.egg-info*/
Expand Down
32 changes: 4 additions & 28 deletions demo/rpc/keyvalue/keyvalue_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@
from autobahn.autobahn import AutobahnClientFactory, AutobahnClientProtocol


class SimpleClientProtocol(AutobahnClientProtocol):
"""
Demonstrates simple Remote Procedure Calls (RPC) with
Autobahn WebSockets and Twisted Deferreds.
"""
class KeyValueClientProtocol(AutobahnClientProtocol):

def show(self, result):
print "SUCCESS:", result
Expand All @@ -42,37 +38,17 @@ def done(self, *args):

def onOpen(self):

#d1 = self.call("square", 23).addCallback(self.show)

self.prefix("calc", "http://example.com/simple/calc#")
self.prefix("keyvalue", "http://example.com/simple/keyvalue#")

# prints 23
d2 = self.call("calc:square", 23).addCallback(lambda res: \
self.call("calc:sqrt", res)).addCallback(self.show)

# prints 23
d3 = self.call("calc:square", 23).addCallback(lambda res: \
self.call("calc:sqrt", res).addCallback(self.show))

d4 = self.call("calc:sqrt", -1).addCallbacks(self.show, self.alert)

d5 = self.call("keyvalue:get", "otto").addCallbacks(self.show, self.alert)

#d4 = self.call("sum", [1, 2, 3, 4, 5]).addCallback(self.show)

#d5 = self.call("asum", [1, 2, 3]).addCallback(self.show)

#d6 = self.call("sum", [4, 5, 6]).addCallback(self.show)
d1 = self.call("keyvalue:get", "otto").addCallbacks(self.show)

# DeferredList([d1, d2, d3, d4, d5, d6]).addCallback(self.done)
DeferredList([d2, d3, d4, d5]).addCallback(self.done)
DeferredList([d1]).addCallback(self.done)


if __name__ == '__main__':

log.startLogging(sys.stdout)
factory = AutobahnClientFactory(debug = False)
factory.protocol = SimpleClientProtocol
factory.protocol = KeyValueClientProtocol
reactor.connectTCP("localhost", 9000, factory)
reactor.run()
29 changes: 18 additions & 11 deletions demo/rpc/keyvalue/keyvalue_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,37 @@
##
###############################################################################

import sys, math
import sys, math, shelve
from twisted.internet import reactor, defer
from twisted.python import log
from autobahn.autobahn import exportRpc, AutobahnServerFactory, AutobahnServerProtocol


class KeyValue:
"""
Simple, non-persistent key-value store.
Simple, persistent key-value store.
"""

def __init__(self):
self.store = {}
def __init__(self, filename):
self.store = shelve.open(filename)

@exportRpc
def set(self, key, value):
self.store[key] = value
k = str(key)
if value:
self.store[k] = value
else:
if self.store.has_key(k):
del self.store[k]

@exportRpc
def get(self, key = None):
if key:
return self.store.get(key, None)
else:
return self.store
def get(self, key):
k = str(key)
return self.store.get(k, None)

@exportRpc
def keys(self):
return self.store.keys()


class KeyValueServerProtocol(AutobahnServerProtocol):
Expand All @@ -63,7 +70,7 @@ def __init__(self, debug = False):

## the key-value store resides on the factory object, since it is to
## be shared among all client connections
self.keyvalue = KeyValue()
self.keyvalue = KeyValue("keyvalue.dat")


if __name__ == '__main__':
Expand Down

0 comments on commit 54c2a20

Please sign in to comment.