def callRemote(): thread = EventletThread() thread.start() transport = Net('127.0.0.1:6503') vat = RPCHandler('127.0.0.1:6503', '', {}, node=transport, t_model=thread) cb = vat.call('127.0.0.1:6502', 'OBJ', 'fun_b', [5]) print cb.wait()
def handler(transport): # transport is a ws_transport.WebSocketHandler thread = EventletThread() thread.callFromThread = thread.call handler = RPCHandler(transport, {}, t_model=thread, verbose=verbose, jc_opts=jc_opts) handler.client_address = (transport.client_ip, transport.client_address[1]) hproxy = weakref.proxy(handler) factory.initSession(hproxy) try: transport.handle() except: pass factory.closeSession(hproxy)
def testAutoSave(self): store = TestStore() data = Data({}) data.ref = store data['name'] = 'Fred' self.assertEqual(store.count, 0) # no save yet data.save() self.assertEqual(store.count, 1) self.assertEqual(store.value, {'name': 'Fred'}) sync = Synchronous() saver = AutoSave(data, sync) data.change_obs._add(saver) # with a synchronous save, every update is saved immediately data['age'] = 25 self.assertEqual(store.count, 2) self.assertEqual(store.value['age'], 25) data['age'] = 26 self.assertEqual(store.count, 3) self.assertEqual(store.value['age'], 26) # remove it, back to manual saving. data.change_obs._remove(saver) data['age'] = 27 self.assertEqual(store.count, 3) self.assertEqual(store.value['age'], 26) # green-threads are more efficient since a save will be done # only once per yield. green = EventletThread() green.start(thread=True) saver = AutoSave(data, green) data.change_obs._add(saver) cb = Callback() def makeUpdates(): data['age'] = 28 data['pet'] = 'Fido' data['address'] = 'Wiltshire' cb.success(None) green.callFromThread(makeUpdates) cb.wait() self.assertEqual(store.count, 4) self.assertEqual(store.value['address'], 'Wiltshire') green.stop()
def testWorker(self): net = MockNet() gta = EventletThread() va, ra = net.addRPCHandler('X', '', {}, t_model=gta) gta.start(True) worker = EventletThread() vb, rb = net.addRPCHandler('Y', '', {}, t_model=worker) worker.start() va['data'] = {'name': 'Tom'} pb = rb.makeProxy('data', 'X') self.assertEqual(pb['name'], 'Tom')
def testGreen(self): net = MockNet() gta = EventletThread() va, ra = net.addRPCHandler('X', '', {}, t_model=gta) gta.start(True) gtb = EventletThread() vb, rb = net.addRPCHandler('Y', '', {}, t_model=gtb) gtb.start(True) va['data'] = {'name': 'Tom'} pb = rb.makeProxy('data', 'X') th = TestHandler() def pr(fn, arg): th.handle('message', {'message': fn(arg)}) # The reason we have to call pa[] in gta (green thread a) # is because if the cb.wait() is called by this thread # cb.main_loop and cb.resume end up being in different threads. with th.expect(1): gtb.callFromThread(pr, pb.__getitem__, 'name') self.assertEqual(th.received, ['Tom']) gta.stop() gtb.stop()
def test(self): th = TestHandler() eth = EventletTestHandler() thread = EventletThread() thread.start() with eth.expect(1): thread.callFromThread(eth.handle, 'message', {'message': 'hi'}) with eth.expect(1): thread.callAfter(0, eth.msg, 'foo') cb = thread.makeCallback() thread.callAfter(0, cb.failure, Exception('bar')) self.assertRaises(Exception, cb.wait) thread.stop() thread.start(True) # now in a different thread with th.expect(1): thread.callFromThread(th.handle, 'message', {'message': 'lo'})
RUN_CONSOLE = (__name__ == '__main__') SERVER = '127.0.0.1:6502' CLIENT = '127.0.0.1:6503' SSL = { 'certfile': os.path.join(codeDir(), 'data/host.cert'), 'keyfile': os.path.join(codeDir(), 'data/host.key') } store = FSDict(os.path.join(dataRoot(), 'client')) net = Transport(CLIENT, ssl=SSL) thread = EventletThread() s0 = Storage(store) v0 = RPCHandler(net, s0, t_model=thread) def wrap(x): return REPLProxy(x, thread) class ClientConsole(InteractiveConsole): def raw_input(self, prompt): sys.stdout.write(prompt) sys.stdout.flush() select.select([sys.stdin],[],[]) s = sys.stdin.readline() if not s: raise EOFError() return s.strip()
import os import sys import time from serf.rpc_handler import RPCHandler from serf.transport import Transport from serf.eventlet_thread import EventletThread from serf.proxy import Proxy from serf.repl_proxy import REPLProxy from serf.po.printer import Printer from serf.tables.table import * from serf.tables.query import * SERVER = '127.0.0.1:6506' transport = Transport() thread = EventletThread() rpc = RPCHandler(transport, {}, t_model=thread) rpc.safe.append('serf.tables') def proxy(name): return REPLProxy(Proxy(SERVER, name, rpc), thread) # thread.start(True) means start a new thread. thread.start(True) thread.callFromThread(transport.start) table = proxy('table')
RUN_CONSOLE = (__name__ == '__main__') SERVER = '127.0.0.1:6502' CLIENT = '127.0.0.1:6503' SSL = { 'certfile': os.path.join(codeDir(), 'data/host.cert'), 'keyfile': os.path.join(codeDir(), 'data/host.key') } store = FSDict(os.path.join(dataRoot(), 'client')) net = Transport(CLIENT, ssl=SSL) thread = EventletThread() s0 = Storage(store) v0 = RPCHandler(net, s0, t_model=thread) def wrap(x): return REPLProxy(x, thread) class ClientConsole(InteractiveConsole): def raw_input(self, prompt): sys.stdout.write(prompt) sys.stdout.flush() select.select([sys.stdin], [], []) s = sys.stdin.readline() if not s:
The 'fred' value is a proxy to a persistent UserCaps object to which we can add 'capabilities', other persistent objects that are available to the 'fred' user for getting things done. """ from serf.rpc_handler import RPCHandler from serf.transport import Transport from serf.eventlet_thread import EventletThread from serf.proxy import Proxy from serf.repl_proxy import REPLProxy SERVER = '127.0.0.1:6508' net = Transport() thread = EventletThread() rpc = RPCHandler(net, {}, t_model=thread) rpc.safe.append('serf.tables') def remote(x): return REPLProxy(rpc.makeProxy(x, SERVER), thread) thread.start(True) thread.callFromThread(net.start) admin = remote('admin') login = remote('login') users = remote('users')
"""Demo of implementing a back-end using RPC over a web-socket.""" import eventlet from serf.eventlet_thread import EventletThread from serf.rpc_handler import RPCHandler from serf.transport import Transport from serf.ws_transport import WSTransport from serf.fs_dict import FSDict from serf.storage import Storage from serf.tables.table import * from serf.tables.query import * SERF_NODE = '127.0.0.1:6506' thread = EventletThread() store = FSDict('/var/lib/serf/tables') storage = Storage(store) if 'table' not in storage: storage['table'] = Table() TABLE = storage['table'] JC_OPTS = dict(hooks=JC_HOOKS, safe=['serf.tables'], auto_proxy=True) def handle(transport): thread = EventletThread() thread.callFromThread = thread.call handler = RPCHandler(transport, {}, t_model=thread, jc_opts=JC_OPTS) handler.provide('table', TABLE)
def handle(transport): thread = EventletThread() thread.callFromThread = thread.call handler = RPCHandler(transport, {}, t_model=thread, jc_opts=JC_OPTS) handler.provide('table', TABLE) transport.handle()
import sys import time from serf.rpc_handler import RPCHandler from serf.transport import Transport from serf.eventlet_thread import EventletThread from serf.proxy import Proxy from serf.repl_proxy import REPLProxy from serf.po.printer import Printer from serf.tables.table import * from serf.tables.query import * SERVER = '127.0.0.1:6506' transport = Transport() thread = EventletThread() rpc = RPCHandler(transport, {}, t_model=thread) rpc.safe.append('serf.tables') def proxy(name): return REPLProxy(Proxy(SERVER, name, rpc), thread) # thread.start(True) means start a new thread. thread.start(True) thread.callFromThread(transport.start) table = proxy('table')