Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 925a314

Browse files
committed
context pattern, first prototype
This is a prototype: - Each context add its own layer of heartbeat on top of the previous one... - There is no way to "close" the server context explicitly from the Client. - There is no way to define a "destructor" for the Server context. - Nothing about client/server loss was tested properly.
1 parent 5547843 commit 925a314

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

zerorpc/decorators.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,7 @@ class rep(DecoratorBase):
6363

6464
class stream(DecoratorBase):
6565
pattern = ReqStream()
66+
67+
68+
class context(DecoratorBase):
69+
pattern = ReqContext()

zerorpc/patterns.py

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2323
# SOFTWARE.
2424

25+
import core
26+
from .events import WrappedEvents
2527

2628

2729
class ReqRep():
@@ -68,4 +70,48 @@ def iterator(event):
6870
bufchan.channel.channel.close()
6971
return iterator(event)
7072

71-
patterns_list = [ReqStream(), ReqRep()]
73+
74+
class ReqContext():
75+
76+
def process_call(self, context, bufchan, event, functor):
77+
context_gen =context.middleware_call_procedure(functor, *event.args)
78+
methods = context_gen.next()
79+
wchannel = WrappedEvents(bufchan)
80+
server = core.ServerBase(wchannel, methods, heartbeat=None)
81+
bufchan.emit('CTX', (None,))
82+
try:
83+
server.run()
84+
except Exception as e:
85+
try:
86+
context_gen.throw(e)
87+
except StopIteration:
88+
pass
89+
else:
90+
try:
91+
context_gen.next()
92+
except StopIteration:
93+
pass
94+
finally:
95+
server.close()
96+
wchannel.close()
97+
98+
def accept_answer(self, event):
99+
return event.name == 'CTX'
100+
101+
def process_answer(self, context, bufchan, event, method,
102+
raise_remote_error):
103+
if event.name == 'ERR':
104+
raise_remote_error(event)
105+
wchannel = WrappedEvents(bufchan)
106+
107+
class ContextClient(core.ClientBase):
108+
def close(self):
109+
self._channel.emit('CTX_CLOSE', (None,))
110+
super(ContextClient, self).close()
111+
bufchan.close()
112+
bufchan.channel.close()
113+
bufchan.channel.channel.close()
114+
115+
return ContextClient(wchannel, heartbeat=None)
116+
117+
patterns_list = [ReqContext(), ReqStream(), ReqRep()]

0 commit comments

Comments
 (0)